1 /* 2 * Copyright (C) 2014 The Android Open Source Project 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 17 #include "utils/int_array_view.h" 18 19 #include <gtest/gtest.h> 20 21 #include <array> 22 #include <vector> 23 24 namespace latinime { 25 namespace { 26 27 TEST(IntArrayViewTest, TestAccess) { 28 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 29 IntArrayView intArrayView(intVector); 30 EXPECT_EQ(intVector.size(), intArrayView.size()); 31 for (int i = 0; i < static_cast<int>(intVector.size()); ++i) { 32 EXPECT_EQ(intVector[i], intArrayView[i]); 33 } 34 } 35 36 TEST(IntArrayViewTest, TestIteration) { 37 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 38 IntArrayView intArrayView(intVector); 39 size_t expectedIndex = 0; 40 for (const int element : intArrayView) { 41 EXPECT_EQ(intVector[expectedIndex], element); 42 ++expectedIndex; 43 } 44 EXPECT_EQ(expectedIndex, intArrayView.size()); 45 } 46 47 TEST(IntArrayViewTest, TestConstructFromArray) { 48 const size_t ARRAY_SIZE = 100; 49 std::array<int, ARRAY_SIZE> intArray; 50 const auto intArrayView = IntArrayView::fromArray(intArray); 51 EXPECT_EQ(ARRAY_SIZE, intArrayView.size()); 52 } 53 54 TEST(IntArrayViewTest, TestConstructFromObject) { 55 const int object = 10; 56 const auto intArrayView = IntArrayView::singleElementView(&object); 57 EXPECT_EQ(1u, intArrayView.size()); 58 EXPECT_EQ(object, intArrayView[0]); 59 } 60 61 TEST(IntArrayViewTest, TestContains) { 62 EXPECT_FALSE(IntArrayView().contains(0)); 63 EXPECT_FALSE(IntArrayView().contains(1)); 64 65 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 66 IntArrayView intArrayView(intVector); 67 EXPECT_TRUE(intArrayView.contains(0)); 68 EXPECT_TRUE(intArrayView.contains(3)); 69 EXPECT_TRUE(intArrayView.contains(-2)); 70 EXPECT_FALSE(intArrayView.contains(-3)); 71 EXPECT_FALSE(intArrayView.limit(0).contains(3)); 72 } 73 74 TEST(IntArrayViewTest, TestLimit) { 75 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 76 IntArrayView intArrayView(intVector); 77 78 EXPECT_TRUE(intArrayView.limit(0).empty()); 79 EXPECT_EQ(intArrayView.size(), intArrayView.limit(intArrayView.size()).size()); 80 EXPECT_EQ(intArrayView.size(), intArrayView.limit(1000).size()); 81 82 IntArrayView subView = intArrayView.limit(4); 83 EXPECT_EQ(4u, subView.size()); 84 for (size_t i = 0; i < subView.size(); ++i) { 85 EXPECT_EQ(intVector[i], subView[i]); 86 } 87 } 88 89 TEST(IntArrayViewTest, TestSkip) { 90 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 91 IntArrayView intArrayView(intVector); 92 93 EXPECT_TRUE(intArrayView.skip(intVector.size()).empty()); 94 EXPECT_TRUE(intArrayView.skip(intVector.size() + 1).empty()); 95 EXPECT_EQ(intArrayView.size(), intArrayView.skip(0).size()); 96 EXPECT_EQ(intArrayView.size(), intArrayView.limit(1000).size()); 97 98 static const size_t SKIP_COUNT = 2; 99 IntArrayView subView = intArrayView.skip(SKIP_COUNT); 100 EXPECT_EQ(intVector.size() - SKIP_COUNT, subView.size()); 101 for (size_t i = 0; i < subView.size(); ++i) { 102 EXPECT_EQ(intVector[i + SKIP_COUNT], subView[i]); 103 } 104 } 105 106 TEST(IntArrayViewTest, TestCopyToArray) { 107 // "{{" to suppress warning. 108 std::array<int, 7> buffer = {{10, 20, 30, 40, 50, 60, 70}}; 109 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 110 IntArrayView intArrayView(intVector); 111 intArrayView.limit(0).copyToArray(&buffer, 0); 112 EXPECT_EQ(10, buffer[0]); 113 EXPECT_EQ(20, buffer[1]); 114 intArrayView.limit(1).copyToArray(&buffer, 0); 115 EXPECT_EQ(intVector[0], buffer[0]); 116 EXPECT_EQ(20, buffer[1]); 117 intArrayView.limit(1).copyToArray(&buffer, 1); 118 EXPECT_EQ(intVector[0], buffer[0]); 119 EXPECT_EQ(intVector[0], buffer[1]); 120 intArrayView.copyToArray(&buffer, 0); 121 for (size_t i = 0; i < intArrayView.size(); ++i) { 122 EXPECT_EQ(intVector[i], buffer[i]); 123 } 124 EXPECT_EQ(70, buffer[6]); 125 } 126 127 TEST(IntArrayViewTest, TestFirstOrDefault) { 128 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 129 IntArrayView intArrayView(intVector); 130 131 EXPECT_EQ(3, intArrayView.firstOrDefault(10)); 132 EXPECT_EQ(10, intArrayView.limit(0).firstOrDefault(10)); 133 EXPECT_EQ(-10, intArrayView.limit(0).firstOrDefault(-10)); 134 EXPECT_EQ(10, intArrayView.skip(6).firstOrDefault(10)); 135 } 136 137 TEST(IntArrayViewTest, TestLastOrDefault) { 138 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 139 IntArrayView intArrayView(intVector); 140 141 EXPECT_EQ(-2, intArrayView.lastOrDefault(10)); 142 EXPECT_EQ(10, intArrayView.limit(0).lastOrDefault(10)); 143 EXPECT_EQ(-10, intArrayView.limit(0).lastOrDefault(-10)); 144 EXPECT_EQ(10, intArrayView.skip(6).lastOrDefault(10)); 145 } 146 147 TEST(IntArrayViewTest, TestToVector) { 148 const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; 149 IntArrayView intArrayView(intVector); 150 EXPECT_EQ(intVector, intArrayView.toVector()); 151 EXPECT_EQ(std::vector<int>(), CodePointArrayView().toVector()); 152 } 153 154 TEST(IntArrayViewTest, TestSplit) { 155 EXPECT_TRUE(IntArrayView().split(0, 0).empty()); 156 { 157 const auto intArrayViews = IntArrayView().split(0, 1); 158 EXPECT_EQ(1u, intArrayViews.size()); 159 EXPECT_TRUE(intArrayViews[0].empty()); 160 } 161 { 162 const auto intArrayViews = IntArrayView().split(0, 100); 163 EXPECT_EQ(1u, intArrayViews.size()); 164 EXPECT_TRUE(intArrayViews[0].empty()); 165 } 166 167 const std::vector<int> intVector = {1, 2, 3, 3, 2, 3}; 168 const IntArrayView intArrayView(intVector); 169 { 170 const auto intArrayViews = intArrayView.split(2); 171 EXPECT_EQ(3u, intArrayViews.size()); 172 EXPECT_EQ(std::vector<int>({1}), intArrayViews[0].toVector()); 173 EXPECT_EQ(std::vector<int>({3, 3}), intArrayViews[1].toVector()); 174 EXPECT_EQ(std::vector<int>({3}), intArrayViews[2].toVector()); 175 } 176 { 177 const auto intArrayViews = intArrayView.split(2, 2); 178 EXPECT_EQ(2u, intArrayViews.size()); 179 EXPECT_EQ(std::vector<int>({1}), intArrayViews[0].toVector()); 180 EXPECT_EQ(std::vector<int>({3, 3, 2, 3}), intArrayViews[1].toVector()); 181 } 182 { 183 const auto intArrayViews = intArrayView.split(2, 1); 184 EXPECT_EQ(1u, intArrayViews.size()); 185 EXPECT_EQ(intVector, intArrayViews[0].toVector()); 186 } 187 { 188 const auto intArrayViews = intArrayView.split(2, 0); 189 EXPECT_EQ(0u, intArrayViews.size()); 190 } 191 { 192 const auto intArrayViews = intArrayView.split(3); 193 EXPECT_EQ(4u, intArrayViews.size()); 194 EXPECT_EQ(std::vector<int>({1, 2}), intArrayViews[0].toVector()); 195 EXPECT_EQ(std::vector<int>(), intArrayViews[1].toVector()); 196 EXPECT_EQ(std::vector<int>({2}), intArrayViews[2].toVector()); 197 EXPECT_EQ(std::vector<int>(), intArrayViews[3].toVector()); 198 } 199 } 200 201 } // namespace 202 } // namespace latinime 203