• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/base/gc_ring_buffer.h"
17 #include "ecmascript/tests/test_helper.h"
18 
19 using namespace panda::ecmascript;
20 using namespace panda::ecmascript::base;
21 
22 namespace panda::test {
23 class GCRingBufferTest : public testing::Test {
24 public:
SetUpTestCase()25     static void SetUpTestCase()
26     {
27         GTEST_LOG_(INFO) << "SetUpTestCase";
28     }
29 
TearDownTestCase()30     static void TearDownTestCase()
31     {
32         GTEST_LOG_(INFO) << "TearDownCase";
33     }
34 
SetUp()35     void SetUp() override
36     {
37         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
38     }
39 
TearDown()40     void TearDown() override
41     {
42         TestHelper::DestroyEcmaVMWithScope(instance, scope);
43     }
44 
45     EcmaVM *instance {nullptr};
46     EcmaHandleScope *scope {nullptr};
47     JSThread *thread {nullptr};
48 };
49 
50 /**
51  * @tc.name: Push
52  * @tc.desc: Define a fixed length container for storing int type data, call the push function to assign a value
53  *           to each position of the container, and when the maximum length is reached, overwrite the original
54  *           value from scratch.
55  * @tc.type: FUNC
56  * @tc.require:
57  */
HWTEST_F_L0(GCRingBufferTest,Push)58 HWTEST_F_L0(GCRingBufferTest, Push)
59 {
60     constexpr int LENGTH = 10;
61     GCRingBuffer<int, LENGTH> gcBuffer;
62     EXPECT_EQ(gcBuffer.Count(), 0);
63     for (int i = 0 ; i < 2 * LENGTH ; i++) {
64         gcBuffer.Push(i);
65     }
66     EXPECT_EQ(gcBuffer.Count(), LENGTH);
67 }
68 
SumCallback(const int initial,int elements)69 static int SumCallback(const int initial, int elements)
70 {
71     return initial + elements;
72 }
73 
74 /**
75  * @tc.name: Sum
76  * @tc.desc: The "Sum" function calculates the sum of stored data by calling the callback function.
77  * @tc.type: FUNC
78  * @tc.require:
79  */
HWTEST_F_L0(GCRingBufferTest,Sum)80 HWTEST_F_L0(GCRingBufferTest, Sum)
81 {
82     constexpr int LENGTH = 10;
83     GCRingBuffer<int, LENGTH> gcBuffer;
84 
85     for (int i = 0 ; i < LENGTH ; i++) {
86         gcBuffer.Push(i);
87     }
88     EXPECT_EQ(gcBuffer.Sum(SumCallback, 0), 45);
89 
90     for (int i = 0 ; i < LENGTH ; i++) {
91         gcBuffer.Push(1);
92     }
93     EXPECT_EQ(gcBuffer.Count(), LENGTH);
94     EXPECT_EQ(gcBuffer.Sum(SumCallback, 0), 10);
95 }
96 
97 /**
98  * @tc.name: Reset
99  * @tc.desc: Set the subscript of the start position and the subscript of the end position of the container to zero.
100  *           The next time you store data, store it from the first position. then call the "Count" function to check
101  *           whether the count of the container is zero.
102  * @tc.type: FUNC
103  * @tc.require:
104  */
HWTEST_F_L0(GCRingBufferTest,Reset)105 HWTEST_F_L0(GCRingBufferTest, Reset)
106 {
107     constexpr int LENGTH = 10;
108     GCRingBuffer<int, LENGTH> gcBuffer;
109 
110     for (int i = 0 ; i < LENGTH ; i++) {
111         gcBuffer.Reset();
112         gcBuffer.Push(i);
113     }
114     EXPECT_EQ(gcBuffer.Count(), 1);
115     // reset count to zero
116     gcBuffer.Reset();
117     EXPECT_EQ(gcBuffer.Count(), 0);
118 }
119 } // namespace panda::ecmascript