1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <iterator>
17 #include <algorithm>
18 #include "common/common.h"
19 #include "common/cvop_common.h"
20 #include "gtest/gtest.h"
21 #include "securec.h"
22 #include "minddata/dataset/engine/perf/cyclic_array.h"
23 #include <chrono>
24
25 using namespace mindspore::dataset;
26
27 class MindDataTestCyclicArray : public UT::Common {
28 public:
MindDataTestCyclicArray()29 MindDataTestCyclicArray() {}
30 };
31
TEST_F(MindDataTestCyclicArray,Test1)32 TEST_F(MindDataTestCyclicArray, Test1) {
33 CyclicArray<int> arr(5);
34 EXPECT_EQ(5, arr.capacity());
35 EXPECT_EQ(0, arr.size());
36 arr.push_back(0);
37 EXPECT_EQ(5, arr.capacity());
38 EXPECT_EQ(1, arr.size());
39 EXPECT_EQ(arr[0], 0);
40 arr.push_back(1);
41 EXPECT_EQ(arr[1], 1);
42 for (auto i = 2; i < 5; i++) {
43 arr.push_back(i);
44 }
45 EXPECT_EQ(arr.capacity(), arr.size());
46 EXPECT_EQ(1, arr[1]);
47 EXPECT_EQ(4, arr[4]);
48 arr[4] = 42;
49 EXPECT_EQ(arr[4], 42);
50 auto a = arr[4];
51 EXPECT_EQ(a, 42);
52 arr.push_back(5);
53 EXPECT_EQ(arr[0], 1);
54 EXPECT_EQ(arr[4], 5);
55
56 CyclicArray<int> arr2 = arr;
57 EXPECT_EQ(arr2.capacity(), arr.capacity());
58 EXPECT_EQ(arr2.size(), arr.size());
59 auto last = arr2.end();
60 auto first = arr2.begin();
61 for (auto i = 0; i < arr.size(); i++) {
62 EXPECT_EQ(arr2[i], arr[i]);
63 }
64
65 arr.clear();
66 EXPECT_EQ(arr.size(), 0);
67 arr.push_back(42);
68 arr.push_back(43);
69 EXPECT_EQ(arr.size(), 2);
70 EXPECT_EQ(arr.capacity(), 5);
71 EXPECT_EQ(arr[0], 42);
72 EXPECT_EQ(arr[1], 43);
73 auto arr3 = arr;
74 EXPECT_EQ(arr3.size(), 2);
75 EXPECT_EQ(arr3.capacity(), 5);
76 EXPECT_EQ(arr.size(), 2);
77 EXPECT_EQ(arr.capacity(), 5);
78 EXPECT_EQ(arr[0], arr3[0]);
79 EXPECT_EQ(arr[1], arr3[1]);
80
81 arr.clear();
82 arr.push_back(21);
83 arr.push_back(22);
84 EXPECT_EQ(arr[arr.size() - 1], 22);
85 for (auto i = 23; i < 27; i++) {
86 arr.push_back(i);
87 }
88 EXPECT_EQ(arr[0], 22);
89 EXPECT_EQ(arr[arr.size() - 1], 26);
90 }
91
TEST_F(MindDataTestCyclicArray,TestIterator)92 TEST_F(MindDataTestCyclicArray, TestIterator) {
93 CyclicArray<int> arr(5);
94 for (auto i = 0; i < arr.capacity(); i++) {
95 arr.push_back(i);
96 }
97 arr.push_back(6);
98 arr.push_back(7);
99 auto i = 0;
100 for (auto it = arr.begin(); it != arr.end(); ++it) {
101 EXPECT_EQ(*it, arr[i++]);
102 }
103
104 std::iota(arr.begin(), arr.end(), -4);
105 EXPECT_EQ(arr[0], -4);
106 EXPECT_EQ(arr[4], 0);
107 const auto sz = 1000000;
108 CyclicArray<int> arr2(sz);
109 for (auto i = 0; i < sz - 1; i++) {
110 arr.push_back(0);
111 }
112 const auto val = -500000;
113 std::iota(arr2.begin(), arr2.end() + sz, val);
114 EXPECT_EQ(*arr2.begin(), val);
115 std::random_device rd;
116 std::mt19937 g(rd());
117 std::shuffle(arr2.begin(), arr2.end(), g);
118 std::sort(arr2.begin(), arr2.end(), [](const auto a, const auto b) { return a > b; });
119 EXPECT_EQ(*arr2.begin(), val);
120 const auto new_val = -600000;
121 for (auto i = 0; i < 100; i++) {
122 arr2.push_back(new_val);
123 }
124 EXPECT_EQ(*(--arr2.end()), new_val);
125 std::sort(arr2.begin(), arr2.end(), [](const auto a, const auto b) { return a > b; });
126 EXPECT_EQ(*arr2.begin(), new_val);
127 }
128