this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "vector.h"
3
4#include "gtest/gtest.h"
5
6namespace py {
7
8TEST(VectorTest, BasicTest) {
9 Vector<int> int_v;
10 for (int i = 0; i < 10; i++) {
11 int_v.push_back(i);
12 }
13
14 for (int i = 0; i < 10; i++) {
15 EXPECT_EQ(int_v[i], i);
16 }
17
18 int count = 0;
19 for (auto const& el : int_v) {
20 EXPECT_EQ(el, count);
21 count++;
22 }
23
24 int_v[3] = 42;
25 EXPECT_EQ(int_v[3], 42);
26
27 auto& ref = int_v[4];
28 ref = 123;
29 EXPECT_EQ(123, int_v[4]) << "vector should return references";
30
31 int_v.pop_back();
32 EXPECT_EQ(9, int_v.size());
33}
34
35TEST(VectorTest, CopyTest) {
36 Vector<int> int_v;
37 for (int i = 0; i < 10; i++) {
38 int_v.push_back(i);
39 }
40
41 Vector<int> copy_v(int_v);
42 EXPECT_NE(copy_v.begin(), int_v.begin()) << "copy_v needs its own storage";
43 EXPECT_EQ(copy_v.size(), int_v.size());
44 for (int i = 0; i < 10; i++) {
45 EXPECT_EQ(int_v[i], copy_v[i]);
46 }
47 auto old_copy_v_begin = copy_v.begin();
48 auto old_copy_v_capacity = copy_v.capacity();
49
50 Vector<int> new_v;
51 for (int i = 0; i < 4; i++) {
52 new_v.push_back(i);
53 }
54 // has capacity 10 already, should be able to hold a size 4 vector without
55 // reallocation.
56 copy_v = new_v;
57 EXPECT_EQ(old_copy_v_begin, copy_v.begin())
58 << "should not require reallocation";
59 EXPECT_EQ(new_v.size(), copy_v.size());
60 EXPECT_EQ(old_copy_v_capacity, copy_v.capacity())
61 << "capacity should not shrink";
62 for (int i = 0; i < 4; i++) {
63 EXPECT_EQ(i, copy_v[i]);
64 }
65}
66
67TEST(VectorTest, MoveTest) {
68 Vector<int> int_v;
69 for (int i = 0; i < 10; i++) {
70 int_v.push_back(i);
71 }
72 auto const old_begin = int_v.begin();
73
74 Vector<int> copy_v(std::move(int_v));
75 EXPECT_EQ(old_begin, copy_v.begin()) << "Storage should be stolen";
76 EXPECT_EQ(nullptr, int_v.begin());
77 for (int i = 0; i < 10; i++) {
78 EXPECT_EQ(i, copy_v[i]);
79 }
80
81 Vector<int> int_v2;
82 int_v2 = std::move(copy_v);
83 EXPECT_EQ(nullptr, copy_v.begin());
84 EXPECT_EQ(old_begin, int_v2.begin()) << "Storage should be stolen";
85}
86
87TEST(VectorTest, Reserve) {
88 Vector<int> int_v;
89 int_v.reserve(10);
90 int_v.push_back(0);
91 auto begin = int_v.begin();
92 for (int i = 1; i < 10; i++) {
93 int_v.push_back(i);
94 }
95
96 for (int i = 0; i < 10; i++) {
97 EXPECT_EQ(int_v[i], i);
98 }
99
100 EXPECT_EQ(begin, int_v.begin()) << "vector should not have been reallocated";
101
102 int_v.push_back(10);
103 EXPECT_NE(begin, int_v.begin()) << "vector should be reallocated";
104 for (int i = 0; i < 11; i++) {
105 EXPECT_EQ(int_v[i], i);
106 }
107}
108
109} // namespace py