xref: /aosp_15_r20/external/libchrome-gestures/src/vector_unittest.cc (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1*aed3e508SAndroid Build Coastguard Worker // Copyright 2011 The ChromiumOS Authors
2*aed3e508SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*aed3e508SAndroid Build Coastguard Worker // found in the LICENSE file.
4*aed3e508SAndroid Build Coastguard Worker 
5*aed3e508SAndroid Build Coastguard Worker #include <gtest/gtest.h>
6*aed3e508SAndroid Build Coastguard Worker 
7*aed3e508SAndroid Build Coastguard Worker #include "include/vector.h"
8*aed3e508SAndroid Build Coastguard Worker 
9*aed3e508SAndroid Build Coastguard Worker namespace gestures {
10*aed3e508SAndroid Build Coastguard Worker 
11*aed3e508SAndroid Build Coastguard Worker typedef vector<int, 10> test_vector;
12*aed3e508SAndroid Build Coastguard Worker 
13*aed3e508SAndroid Build Coastguard Worker class VectorTest : public ::testing::Test {};
14*aed3e508SAndroid Build Coastguard Worker 
ExpectGrowingVectorOfSize(const test_vector & vector,int expected_size)15*aed3e508SAndroid Build Coastguard Worker void ExpectGrowingVectorOfSize(const test_vector& vector, int expected_size) {
16*aed3e508SAndroid Build Coastguard Worker   int count = 0;
17*aed3e508SAndroid Build Coastguard Worker   for (const int& v : vector) {
18*aed3e508SAndroid Build Coastguard Worker     ++count;
19*aed3e508SAndroid Build Coastguard Worker     EXPECT_EQ(count, v);
20*aed3e508SAndroid Build Coastguard Worker   }
21*aed3e508SAndroid Build Coastguard Worker   EXPECT_EQ(expected_size, count);
22*aed3e508SAndroid Build Coastguard Worker }
23*aed3e508SAndroid Build Coastguard Worker 
TEST(VectorTest,PushFrontBackTest)24*aed3e508SAndroid Build Coastguard Worker TEST(VectorTest, PushFrontBackTest) {
25*aed3e508SAndroid Build Coastguard Worker   test_vector vector;
26*aed3e508SAndroid Build Coastguard Worker   vector.push_back(3);
27*aed3e508SAndroid Build Coastguard Worker   vector.insert(vector.begin(), 2);
28*aed3e508SAndroid Build Coastguard Worker   vector.push_back(4);
29*aed3e508SAndroid Build Coastguard Worker   vector.insert(vector.begin(), 1);
30*aed3e508SAndroid Build Coastguard Worker   vector.push_back(5);
31*aed3e508SAndroid Build Coastguard Worker   ExpectGrowingVectorOfSize(vector, 5);
32*aed3e508SAndroid Build Coastguard Worker }
33*aed3e508SAndroid Build Coastguard Worker 
TEST(VectorTest,InsertTest)34*aed3e508SAndroid Build Coastguard Worker TEST(VectorTest, InsertTest) {
35*aed3e508SAndroid Build Coastguard Worker   test_vector vector;
36*aed3e508SAndroid Build Coastguard Worker   vector.push_back(1);
37*aed3e508SAndroid Build Coastguard Worker   vector.push_back(2);
38*aed3e508SAndroid Build Coastguard Worker   vector.push_back(4);
39*aed3e508SAndroid Build Coastguard Worker   vector.push_back(5);
40*aed3e508SAndroid Build Coastguard Worker   vector.insert(vector.find(4), 3);
41*aed3e508SAndroid Build Coastguard Worker   ExpectGrowingVectorOfSize(vector, 5);
42*aed3e508SAndroid Build Coastguard Worker }
43*aed3e508SAndroid Build Coastguard Worker 
EvenFilter(const int & v)44*aed3e508SAndroid Build Coastguard Worker bool EvenFilter(const int& v) { return (v % 2) == 0; }
45*aed3e508SAndroid Build Coastguard Worker 
TEST(VectorTest,FilterTest)46*aed3e508SAndroid Build Coastguard Worker TEST(VectorTest, FilterTest) {
47*aed3e508SAndroid Build Coastguard Worker   test_vector vector;
48*aed3e508SAndroid Build Coastguard Worker   vector.push_back(1);
49*aed3e508SAndroid Build Coastguard Worker   vector.push_back(2);
50*aed3e508SAndroid Build Coastguard Worker   vector.push_back(3);
51*aed3e508SAndroid Build Coastguard Worker   vector.push_back(4);
52*aed3e508SAndroid Build Coastguard Worker   vector.push_back(5);
53*aed3e508SAndroid Build Coastguard Worker   vector.push_back(6);
54*aed3e508SAndroid Build Coastguard Worker 
55*aed3e508SAndroid Build Coastguard Worker   int count = 0;
56*aed3e508SAndroid Build Coastguard Worker   FilteredRange<int> range(vector.begin(), vector.end(), EvenFilter);
57*aed3e508SAndroid Build Coastguard Worker   for (const int& v : range) {
58*aed3e508SAndroid Build Coastguard Worker     EXPECT_TRUE((v % 2) == 0);
59*aed3e508SAndroid Build Coastguard Worker     ++count;
60*aed3e508SAndroid Build Coastguard Worker   }
61*aed3e508SAndroid Build Coastguard Worker   EXPECT_EQ(count, 3);
62*aed3e508SAndroid Build Coastguard Worker }
63*aed3e508SAndroid Build Coastguard Worker 
TEST(VectorTest,FilterEditTest)64*aed3e508SAndroid Build Coastguard Worker TEST(VectorTest, FilterEditTest) {
65*aed3e508SAndroid Build Coastguard Worker   test_vector vector;
66*aed3e508SAndroid Build Coastguard Worker   vector.push_back(1);
67*aed3e508SAndroid Build Coastguard Worker   vector.push_back(2);
68*aed3e508SAndroid Build Coastguard Worker   vector.push_back(4);
69*aed3e508SAndroid Build Coastguard Worker   vector.push_back(7);
70*aed3e508SAndroid Build Coastguard Worker   FilteredRange<int> range(vector.begin(), vector.end(), EvenFilter);
71*aed3e508SAndroid Build Coastguard Worker   for (int& v : range)
72*aed3e508SAndroid Build Coastguard Worker     v = v + 1;
73*aed3e508SAndroid Build Coastguard Worker   // vector = [1, 3, 5, 7]
74*aed3e508SAndroid Build Coastguard Worker   vector.insert(vector.find(3), 2);
75*aed3e508SAndroid Build Coastguard Worker   vector.insert(vector.find(5), 4);
76*aed3e508SAndroid Build Coastguard Worker   vector.insert(vector.find(7), 6);
77*aed3e508SAndroid Build Coastguard Worker   ExpectGrowingVectorOfSize(vector, 7);
78*aed3e508SAndroid Build Coastguard Worker }
79*aed3e508SAndroid Build Coastguard Worker 
80*aed3e508SAndroid Build Coastguard Worker }  // namespace gestures
81