1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "crazy_linker_util.h"
6
7#include <minitest/minitest.h>
8
9namespace crazy {
10
11TEST(GetBaseNamePtr, Test) {
12  const char kString[] = "/tmp/foo";
13  EXPECT_EQ(kString + 5, GetBaseNamePtr(kString));
14}
15
16TEST(String, Empty) {
17  String s;
18  EXPECT_TRUE(s.IsEmpty());
19  EXPECT_EQ(0u, s.size());
20  EXPECT_EQ('\0', s.c_str()[0]);
21}
22
23TEST(String, CStringConstructor) {
24  String s("Simple");
25  EXPECT_STREQ("Simple", s.c_str());
26  EXPECT_EQ(6U, s.size());
27}
28
29TEST(String, CStringConstructorWithLength) {
30  String s2("Simple", 3);
31  EXPECT_STREQ("Sim", s2.c_str());
32  EXPECT_EQ(3U, s2.size());
33}
34
35TEST(String, CopyConstructor) {
36  String s1("Simple");
37  String s2(s1);
38
39  EXPECT_EQ(6U, s2.size());
40  EXPECT_STREQ(s2.c_str(), s1.c_str());
41}
42
43TEST(String, CharConstructor) {
44  String s('H');
45  EXPECT_EQ(1U, s.size());
46  EXPECT_STREQ("H", s.c_str());
47}
48
49TEST(String, AppendCString) {
50  String s("Foo");
51  s += "Bar";
52  EXPECT_STREQ("FooBar", s.c_str());
53  s += "Zoo";
54  EXPECT_STREQ("FooBarZoo", s.c_str());
55}
56
57TEST(String, AppendOther) {
58  String s("Foo");
59  String s2("Bar");
60  s += s2;
61  EXPECT_STREQ("FooBar", s.c_str());
62}
63
64TEST(String, ArrayAccess) {
65  String s("FooBar");
66  EXPECT_EQ('F', s[0]);
67  EXPECT_EQ('o', s[1]);
68  EXPECT_EQ('o', s[2]);
69  EXPECT_EQ('B', s[3]);
70  EXPECT_EQ('a', s[4]);
71  EXPECT_EQ('r', s[5]);
72  EXPECT_EQ('\0', s[6]);
73}
74
75TEST(String, Resize) {
76  String s("A very long string to have fun");
77  s.Resize(10);
78  EXPECT_EQ(10U, s.size());
79  EXPECT_STREQ("A very lon", s.c_str());
80}
81
82TEST(String, ResizeToZero) {
83  String s("Long string to force a dynamic allocation");
84  s.Resize(0);
85  EXPECT_EQ(0U, s.size());
86  EXPECT_STREQ("", s.c_str());
87}
88
89TEST(Vector, IsEmpty) {
90  Vector<void*> v;
91  EXPECT_TRUE(v.IsEmpty());
92}
93
94TEST(Vector, PushBack) {
95  Vector<int> v;
96  v.PushBack(12345);
97  EXPECT_FALSE(v.IsEmpty());
98  EXPECT_EQ(1U, v.GetCount());
99  EXPECT_EQ(12345, v[0]);
100}
101
102TEST(Vector, PushBack2) {
103  const int kMaxCount = 500;
104  Vector<int> v;
105  for (int n = 0; n < kMaxCount; ++n)
106    v.PushBack(n * 100);
107
108  EXPECT_FALSE(v.IsEmpty());
109  EXPECT_EQ(static_cast<size_t>(kMaxCount), v.GetCount());
110}
111
112TEST(Vector, At) {
113  const int kMaxCount = 500;
114  Vector<int> v;
115  for (int n = 0; n < kMaxCount; ++n)
116    v.PushBack(n * 100);
117
118  for (int n = 0; n < kMaxCount; ++n) {
119    TEST_TEXT << "Checking v[" << n << "]";
120    EXPECT_EQ(n * 100, v[n]);
121  }
122}
123
124TEST(Vector, IndexOf) {
125  const int kMaxCount = 500;
126  Vector<int> v;
127  for (int n = 0; n < kMaxCount; ++n)
128    v.PushBack(n * 100);
129
130  for (int n = 0; n < kMaxCount; ++n) {
131    TEST_TEXT << "Checking v.IndexOf(" << n * 100 << ")";
132    EXPECT_EQ(n, v.IndexOf(n * 100));
133  }
134}
135
136TEST(Vector, InsertAt) {
137  const int kMaxCount = 500;
138
139  for (size_t k = 0; k < kMaxCount; ++k) {
140    Vector<int> v;
141    for (int n = 0; n < kMaxCount; ++n)
142      v.PushBack(n * 100);
143
144    v.InsertAt(k, -1000);
145
146    EXPECT_EQ(kMaxCount + 1, v.GetCount());
147    for (int n = 0; n < v.GetCount(); ++n) {
148      TEST_TEXT << "Checking v[" << n << "]";
149      int expected;
150      if (n < k)
151        expected = n * 100;
152      else if (n == k)
153        expected = -1000;
154      else
155        expected = (n - 1) * 100;
156      EXPECT_EQ(expected, v[n]);
157    }
158  }
159}
160
161TEST(Vector, RemoveAt) {
162  const int kMaxCount = 500;
163
164  for (size_t k = 0; k < kMaxCount; ++k) {
165    Vector<int> v;
166    for (int n = 0; n < kMaxCount; ++n)
167      v.PushBack(n * 100);
168
169    v.RemoveAt(k);
170
171    EXPECT_EQ(kMaxCount - 1, v.GetCount());
172    for (int n = 0; n < kMaxCount - 1; ++n) {
173      TEST_TEXT << "Checking v[" << n << "]";
174      int expected = (n < k) ? (n * 100) : ((n + 1) * 100);
175      EXPECT_EQ(expected, v[n]);
176    }
177  }
178}
179
180TEST(Vector, PopFirst) {
181  const int kMaxCount = 500;
182  Vector<int> v;
183  for (int n = 0; n < kMaxCount; ++n)
184    v.PushBack(n * 100);
185
186  for (int n = 0; n < kMaxCount; ++n) {
187    int first = v.PopFirst();
188    TEST_TEXT << "Checking " << n << "-th PopFirst()";
189    EXPECT_EQ(n * 100, first);
190    EXPECT_EQ(kMaxCount - 1 - n, v.GetCount());
191  }
192  EXPECT_EQ(0u, v.GetCount());
193  EXPECT_TRUE(v.IsEmpty());
194}
195
196TEST(Set, Empty) {
197  Set<int> s;
198  EXPECT_TRUE(s.IsEmpty());
199  EXPECT_EQ(0U, s.GetCount());
200}
201
202TEST(Set, OneItem) {
203  Set<int> s;
204
205  EXPECT_FALSE(s.Has(0));
206
207  EXPECT_TRUE(s.Add(0));
208  EXPECT_TRUE(s.Has(0));
209  EXPECT_FALSE(s.IsEmpty());
210  EXPECT_EQ(1U, s.GetCount());
211}
212
213TEST(Set, ThreeItems) {
214  Set<int> s;
215
216  EXPECT_TRUE(s.Add(0));
217  EXPECT_TRUE(s.Add(1));
218  EXPECT_TRUE(s.Add(2));
219
220  EXPECT_FALSE(s.Has(-1));
221  EXPECT_TRUE(s.Has(0));
222  EXPECT_TRUE(s.Has(1));
223  EXPECT_TRUE(s.Has(2));
224  EXPECT_FALSE(s.Has(3));
225
226  EXPECT_EQ(3U, s.GetCount());
227}
228
229}  // namespace crazy
230