summaryrefslogtreecommitdiff
path: root/utils/small_vector_test.cc
blob: d1d8dcabe03e55f06fec4b9c0698f7250180492b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "small_vector.h"

#include <gtest/gtest.h>
#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

class SVTest : public testing::Test {
 protected:
  virtual void SetUp() { }
  virtual void TearDown() { }
};

TEST_F(SVTest, LargerThan2) {
  SmallVectorInt v;
  SmallVectorInt v2;
  v.push_back(0);
  v.push_back(1);
  v.push_back(2);
  assert(v.size() == 3);
  assert(v[2] == 2);
  assert(v[1] == 1);
  assert(v[0] == 0);
  v2 = v;
  SmallVectorInt copy(v);
  assert(copy.size() == 3);
  assert(copy[0] == 0);
  assert(copy[1] == 1);
  assert(copy[2] == 2);
  assert(copy == v2);
  copy[1] = 99;
  assert(copy != v2);
  assert(v2.size() == 3);
  assert(v2[2] == 2);
  assert(v2[1] == 1);
  assert(v2[0] == 0);
  v2[0] = -2;
  v2[1] = -1;
  v2[2] = 0;
  assert(v2[2] == 0);
  assert(v2[1] == -1);
  assert(v2[0] == -2);
  SmallVectorInt v3(1,1);
  assert(v3[0] == 1);
  v2 = v3;
  assert(v2.size() == 1);
  assert(v2[0] == 1);
  SmallVectorInt v4(10, 1);
  assert(v4.size() == 10);
  assert(v4[5] == 1);
  assert(v4[9] == 1);
  v4 = v;
  assert(v4.size() == 3);
  assert(v4[2] == 2);
  assert(v4[1] == 1);
  assert(v4[0] == 0);
  SmallVectorInt v5(10, 2);
  assert(v5.size() == 10);
  assert(v5[7] == 2);
  assert(v5[0] == 2);
  assert(v.size() == 3);
  v = v5;
  assert(v.size() == 10);
  assert(v[2] == 2);
  assert(v[9] == 2);
  SmallVectorInt cc;
  for (int i = 0; i < 33; ++i)
    cc.push_back(i);
  for (int i = 0; i < 33; ++i)
    assert(cc[i] == i);
  cc.resize(20);
  assert(cc.size() == 20);
  for (int i = 0; i < 20; ++i)
    assert(cc[i] == i);
  cc[0]=-1;
  cc.resize(1, 999);
  assert(cc.size() == 1);
  assert(cc[0] == -1);
  cc.resize(99, 99);
  for (int i = 1; i < 99; ++i) {
    cerr << i << " " << cc[i] << endl;
    assert(cc[i] == 99);
  }
  cc.clear();
  assert(cc.size() == 0);
}

TEST_F(SVTest, Small) {
  SmallVectorInt v;
  SmallVectorInt v1(1,0);
  SmallVectorInt v2(2,10);
  SmallVectorInt v1a(2,0);
  EXPECT_TRUE(v1 != v1a);
  EXPECT_TRUE(v1 == v1);
  EXPECT_EQ(v1[0], 0);
  EXPECT_EQ(v2[1], 10);
  EXPECT_EQ(v2[0], 10);
  ++v2[1];
  --v2[0];
  EXPECT_EQ(v2[0], 9);
  EXPECT_EQ(v2[1], 11);
  SmallVectorInt v3(v2);
  assert(v3[0] == 9);
  assert(v3[1] == 11);
  assert(!v3.empty());
  assert(v3.size() == 2);
  v3.clear();
  assert(v3.empty());
  assert(v3.size() == 0);
  assert(v3 != v2);
  assert(v2 != v3);
  v3 = v2;
  assert(v3 == v2);
  assert(v2 == v3);
  assert(v3[0] == 9);
  assert(v3[1] == 11);
  assert(!v3.empty());
  assert(v3.size() == 2);
  cerr << sizeof(SmallVectorInt) << endl;
  cerr << sizeof(vector<int>) << endl;
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}