14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/base/gc_ring_buffer.h" 174514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 204514f5e3Sopenharmony_ciusing namespace panda::ecmascript::base; 214514f5e3Sopenharmony_ci 224514f5e3Sopenharmony_cinamespace panda::test { 234514f5e3Sopenharmony_ciclass GCRingBufferTest : public BaseTestWithScope<false> { 244514f5e3Sopenharmony_ci}; 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_ci/** 274514f5e3Sopenharmony_ci * @tc.name: Push 284514f5e3Sopenharmony_ci * @tc.desc: Define a fixed length container for storing int type data, call the push function to assign a value 294514f5e3Sopenharmony_ci * to each position of the container, and when the maximum length is reached, overwrite the original 304514f5e3Sopenharmony_ci * value from scratch. 314514f5e3Sopenharmony_ci * @tc.type: FUNC 324514f5e3Sopenharmony_ci * @tc.require: 334514f5e3Sopenharmony_ci */ 344514f5e3Sopenharmony_ciHWTEST_F_L0(GCRingBufferTest, Push) 354514f5e3Sopenharmony_ci{ 364514f5e3Sopenharmony_ci constexpr int LENGTH = 10; 374514f5e3Sopenharmony_ci GCRingBuffer<int, LENGTH> gcBuffer; 384514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Count(), 0); 394514f5e3Sopenharmony_ci for (int i = 0; i < 2 * LENGTH; i++) { 404514f5e3Sopenharmony_ci gcBuffer.Push(i); 414514f5e3Sopenharmony_ci } 424514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Count(), LENGTH); 434514f5e3Sopenharmony_ci} 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_cistatic int SumCallback(const int initial, int elements) 464514f5e3Sopenharmony_ci{ 474514f5e3Sopenharmony_ci return initial + elements; 484514f5e3Sopenharmony_ci} 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ci/** 514514f5e3Sopenharmony_ci * @tc.name: Sum 524514f5e3Sopenharmony_ci * @tc.desc: The "Sum" function calculates the sum of stored data by calling the callback function. 534514f5e3Sopenharmony_ci * @tc.type: FUNC 544514f5e3Sopenharmony_ci * @tc.require: 554514f5e3Sopenharmony_ci */ 564514f5e3Sopenharmony_ciHWTEST_F_L0(GCRingBufferTest, Sum) 574514f5e3Sopenharmony_ci{ 584514f5e3Sopenharmony_ci constexpr int LENGTH = 10; 594514f5e3Sopenharmony_ci GCRingBuffer<int, LENGTH> gcBuffer; 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci for (int i = 0; i < LENGTH; i++) { 624514f5e3Sopenharmony_ci gcBuffer.Push(i); 634514f5e3Sopenharmony_ci } 644514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Sum(SumCallback, 0), 45); 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ci for (int i = 0; i < LENGTH; i++) { 674514f5e3Sopenharmony_ci gcBuffer.Push(1); 684514f5e3Sopenharmony_ci } 694514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Count(), LENGTH); 704514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Sum(SumCallback, 0), 10); 714514f5e3Sopenharmony_ci} 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci/** 744514f5e3Sopenharmony_ci * @tc.name: Reset 754514f5e3Sopenharmony_ci * @tc.desc: Set the subscript of the start position and the subscript of the end position of the container to zero. 764514f5e3Sopenharmony_ci * The next time you store data, store it from the first position. then call the "Count" function to check 774514f5e3Sopenharmony_ci * whether the count of the container is zero. 784514f5e3Sopenharmony_ci * @tc.type: FUNC 794514f5e3Sopenharmony_ci * @tc.require: 804514f5e3Sopenharmony_ci */ 814514f5e3Sopenharmony_ciHWTEST_F_L0(GCRingBufferTest, Reset) 824514f5e3Sopenharmony_ci{ 834514f5e3Sopenharmony_ci constexpr int LENGTH = 10; 844514f5e3Sopenharmony_ci GCRingBuffer<int, LENGTH> gcBuffer; 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci for (int i = 0; i < LENGTH; i++) { 874514f5e3Sopenharmony_ci gcBuffer.Reset(); 884514f5e3Sopenharmony_ci gcBuffer.Push(i); 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Count(), 1); 914514f5e3Sopenharmony_ci // reset count to zero 924514f5e3Sopenharmony_ci gcBuffer.Reset(); 934514f5e3Sopenharmony_ci EXPECT_EQ(gcBuffer.Count(), 0); 944514f5e3Sopenharmony_ci} 954514f5e3Sopenharmony_ci} // namespace panda::ecmascript 96