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 BaseTestWithScope<false> {
24 };
25
26 /**
27 * @tc.name: Push
28 * @tc.desc: Define a fixed length container for storing int type data, call the push function to assign a value
29 * to each position of the container, and when the maximum length is reached, overwrite the original
30 * value from scratch.
31 * @tc.type: FUNC
32 * @tc.require:
33 */
HWTEST_F_L0(GCRingBufferTest, Push)34 HWTEST_F_L0(GCRingBufferTest, Push)
35 {
36 constexpr int LENGTH = 10;
37 GCRingBuffer<int, LENGTH> gcBuffer;
38 EXPECT_EQ(gcBuffer.Count(), 0);
39 for (int i = 0; i < 2 * LENGTH; i++) {
40 gcBuffer.Push(i);
41 }
42 EXPECT_EQ(gcBuffer.Count(), LENGTH);
43 }
44
SumCallback(const int initial, int elements)45 static int SumCallback(const int initial, int elements)
46 {
47 return initial + elements;
48 }
49
50 /**
51 * @tc.name: Sum
52 * @tc.desc: The "Sum" function calculates the sum of stored data by calling the callback function.
53 * @tc.type: FUNC
54 * @tc.require:
55 */
HWTEST_F_L0(GCRingBufferTest, Sum)56 HWTEST_F_L0(GCRingBufferTest, Sum)
57 {
58 constexpr int LENGTH = 10;
59 GCRingBuffer<int, LENGTH> gcBuffer;
60
61 for (int i = 0; i < LENGTH; i++) {
62 gcBuffer.Push(i);
63 }
64 EXPECT_EQ(gcBuffer.Sum(SumCallback, 0), 45);
65
66 for (int i = 0; i < LENGTH; i++) {
67 gcBuffer.Push(1);
68 }
69 EXPECT_EQ(gcBuffer.Count(), LENGTH);
70 EXPECT_EQ(gcBuffer.Sum(SumCallback, 0), 10);
71 }
72
73 /**
74 * @tc.name: Reset
75 * @tc.desc: Set the subscript of the start position and the subscript of the end position of the container to zero.
76 * The next time you store data, store it from the first position. then call the "Count" function to check
77 * whether the count of the container is zero.
78 * @tc.type: FUNC
79 * @tc.require:
80 */
HWTEST_F_L0(GCRingBufferTest, Reset)81 HWTEST_F_L0(GCRingBufferTest, Reset)
82 {
83 constexpr int LENGTH = 10;
84 GCRingBuffer<int, LENGTH> gcBuffer;
85
86 for (int i = 0; i < LENGTH; i++) {
87 gcBuffer.Reset();
88 gcBuffer.Push(i);
89 }
90 EXPECT_EQ(gcBuffer.Count(), 1);
91 // reset count to zero
92 gcBuffer.Reset();
93 EXPECT_EQ(gcBuffer.Count(), 0);
94 }
95 } // namespace panda::ecmascript
96