summaryrefslogtreecommitdiff
path: root/src/strings/strings_test.cpp
blob: a95499d53028bb62af7f69b8fb91ec9fb7718b1a (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//    strings_test.cpp - Testsuite part 1 for strings.
//
//    Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <algorithm>

#include <gtest/gtest.h>

#include "all.hpp"

#include "../poison.hpp"


namespace tmwa
{
template<typename T>
class StringTest : public ::testing::Test
{
};
TYPED_TEST_CASE_P(StringTest);

TYPED_TEST_P(StringTest, basic)
{
    TypeParam hi("Hello"_s);
    EXPECT_EQ(5, hi.size());
    EXPECT_EQ(hi, hi);
    LString hi2 = "Hello\0random garbage"_s;
    EXPECT_EQ(hi, hi2);
    TypeParam hi0;
    EXPECT_EQ(0, hi0.size());

    __attribute__((unused))
    const RString *base = hi.base();
}

TYPED_TEST_P(StringTest, order)
{
    TypeParam a;
    TypeParam b("Hello"_s);
    TypeParam c("Hello,"_s);
    TypeParam d("World!"_s);

    // not using EXPECT_LT, etc. for better visibility

    EXPECT_FALSE(a < a);
    EXPECT_TRUE(a < b);
    EXPECT_TRUE(a < c);
    EXPECT_TRUE(a < d);
    EXPECT_FALSE(b < a);
    EXPECT_FALSE(b < b);
    EXPECT_TRUE(b < c);
    EXPECT_TRUE(b < d);
    EXPECT_FALSE(c < a);
    EXPECT_FALSE(c < b);
    EXPECT_FALSE(c < c);
    EXPECT_TRUE(c < d);
    EXPECT_FALSE(d < a);
    EXPECT_FALSE(d < b);
    EXPECT_FALSE(d < c);
    EXPECT_FALSE(d < d);

    EXPECT_TRUE(a <= a);
    EXPECT_TRUE(a <= b);
    EXPECT_TRUE(a <= c);
    EXPECT_TRUE(a <= d);
    EXPECT_FALSE(b <= a);
    EXPECT_TRUE(b <= b);
    EXPECT_TRUE(b <= c);
    EXPECT_TRUE(b <= d);
    EXPECT_FALSE(c <= a);
    EXPECT_FALSE(c <= b);
    EXPECT_TRUE(c <= c);
    EXPECT_TRUE(c <= d);
    EXPECT_FALSE(d <= a);
    EXPECT_FALSE(d <= b);
    EXPECT_FALSE(d <= c);
    EXPECT_TRUE(d <= d);

    EXPECT_TRUE(a >= a);
    EXPECT_FALSE(a >= b);
    EXPECT_FALSE(a >= c);
    EXPECT_FALSE(a >= d);
    EXPECT_TRUE(b >= a);
    EXPECT_TRUE(b >= b);
    EXPECT_FALSE(b >= c);
    EXPECT_FALSE(b >= d);
    EXPECT_TRUE(c >= a);
    EXPECT_TRUE(c >= b);
    EXPECT_TRUE(c >= c);
    EXPECT_FALSE(c >= d);
    EXPECT_TRUE(d >= a);
    EXPECT_TRUE(d >= b);
    EXPECT_TRUE(d >= c);
    EXPECT_TRUE(d >= d);

    EXPECT_FALSE(a > a);
    EXPECT_FALSE(a > b);
    EXPECT_FALSE(a > c);
    EXPECT_FALSE(a > d);
    EXPECT_TRUE(b > a);
    EXPECT_FALSE(b > b);
    EXPECT_FALSE(b > c);
    EXPECT_FALSE(b > d);
    EXPECT_TRUE(c > a);
    EXPECT_TRUE(c > b);
    EXPECT_FALSE(c > c);
    EXPECT_FALSE(c > d);
    EXPECT_TRUE(d > a);
    EXPECT_TRUE(d > b);
    EXPECT_TRUE(d > c);
    EXPECT_FALSE(d > d);

    EXPECT_TRUE(a == a);
    EXPECT_FALSE(a == b);
    EXPECT_FALSE(a == c);
    EXPECT_FALSE(a == d);
    EXPECT_FALSE(b == a);
    EXPECT_TRUE(b == b);
    EXPECT_FALSE(b == c);
    EXPECT_FALSE(b == d);
    EXPECT_FALSE(c == a);
    EXPECT_FALSE(c == b);
    EXPECT_TRUE(c == c);
    EXPECT_FALSE(c == d);
    EXPECT_FALSE(d == a);
    EXPECT_FALSE(d == b);
    EXPECT_FALSE(d == c);
    EXPECT_TRUE(d == d);

    EXPECT_FALSE(a != a);
    EXPECT_TRUE(a != b);
    EXPECT_TRUE(a != c);
    EXPECT_TRUE(a != d);
    EXPECT_TRUE(b != a);
    EXPECT_FALSE(b != b);
    EXPECT_TRUE(b != c);
    EXPECT_TRUE(b != d);
    EXPECT_TRUE(c != a);
    EXPECT_TRUE(c != b);
    EXPECT_FALSE(c != c);
    EXPECT_TRUE(c != d);
    EXPECT_TRUE(d != a);
    EXPECT_TRUE(d != b);
    EXPECT_TRUE(d != c);
    EXPECT_FALSE(d != d);
}

TYPED_TEST_P(StringTest, iterators)
{
    TypeParam hi("Hello"_s);
    EXPECT_EQ(hi.begin(), hi.begin());
    EXPECT_NE(hi.begin(), hi.end());
    EXPECT_EQ(5, std::distance(hi.begin(), hi.end()));
    const char *hi2 = "Hello";
    EXPECT_TRUE(std::equal(hi.begin(), hi.end(), hi2));
}

TYPED_TEST_P(StringTest, xslice)
{
    TypeParam hi("Hello, World!"_s);
    EXPECT_EQ(" World!"_s, hi.xslice_t(6));
    EXPECT_EQ("Hello,"_s, hi.xslice_h(6));
    EXPECT_EQ("World!"_s, hi.xrslice_t(6));
    EXPECT_EQ("Hello, "_s, hi.xrslice_h(6));
    typename TypeParam::iterator it = std::find(hi.begin(), hi.end(), ' ');
    EXPECT_EQ(" World!"_s, hi.xislice_t(it));
    EXPECT_EQ("Hello,"_s, hi.xislice_h(it));
    EXPECT_EQ("World"_s, hi.xlslice(7, 5));
    EXPECT_EQ("World"_s, hi.xpslice(7, 12));
    EXPECT_EQ("World"_s, hi.xislice(hi.begin() + 7, hi.begin() + 12));
    EXPECT_TRUE(hi.startswith("Hello"_s));
    EXPECT_TRUE(hi.endswith("World!"_s));
}

TYPED_TEST_P(StringTest, convert)
{
    constexpr bool is_zstring = std::is_same<TypeParam, ZString>::value;
    typedef typename std::conditional<is_zstring, TString, SString>::type Sstring;
    typedef typename std::conditional<is_zstring, ZString, XString>::type Xstring;
    RString r = "r"_s;
    AString a = "a"_s;
    TString t = "t"_s;
    Sstring s = "s"_s;
    ZString z = "z"_s;
    Xstring x = "x"_s;
    VString<255> v = "v"_s;
    LString l = "l"_s;
    VString<5> hi = "hello"_s;

    TypeParam r2 = r;
    TypeParam a2 = a;
    TypeParam t2 = t;
    TypeParam s2 = s;
    TypeParam z2 = z;
    TypeParam x2 = x;
    TypeParam v2 = v;
    TypeParam l2 = l;
    TypeParam hi2 = hi;

    EXPECT_EQ(r, r2);
    EXPECT_EQ(a, a2);
    EXPECT_EQ(t, t2);
    EXPECT_EQ(s, s2);
    EXPECT_EQ(z, z2);
    EXPECT_EQ(x, x2);
    EXPECT_EQ(v, v2);
    EXPECT_EQ(l, l2);
    EXPECT_EQ(hi, hi2);

    TypeParam r3, a3, t3, s3, z3, x3, v3, l3, hi3;
    r3 = r;
    a3 = a;
    t3 = t;
    s3 = s;
    z3 = z;
    x3 = x;
    v3 = v;
    l3 = l;
    hi3 = hi;

    EXPECT_EQ(r, r3);
    EXPECT_EQ(a, a3);
    EXPECT_EQ(t, t3);
    EXPECT_EQ(s, s3);
    EXPECT_EQ(z, z3);
    EXPECT_EQ(x, x3);
    EXPECT_EQ(v, v3);
    EXPECT_EQ(l, l3);
    EXPECT_EQ(hi, hi3);

    TypeParam r4(r);
    TypeParam a4(a);
    TypeParam t4(t);
    TypeParam s4(s);
    TypeParam z4(z);
    TypeParam x4(x);
    TypeParam v4(v);
    TypeParam l4(l);
    TypeParam hi4(hi);

    EXPECT_EQ(r, r4);
    EXPECT_EQ(a, a4);
    EXPECT_EQ(t, t4);
    EXPECT_EQ(s, s4);
    EXPECT_EQ(z, z4);
    EXPECT_EQ(x, x4);
    EXPECT_EQ(v, v4);
    EXPECT_EQ(l, l4);
    EXPECT_EQ(hi, hi4);
}

REGISTER_TYPED_TEST_CASE_P(StringTest,
        basic, order, iterators, xslice, convert);

typedef ::testing::Types<
    RString, AString, TString, SString, ZString, XString, VString<255>
> MostStringTypes;
INSTANTIATE_TYPED_TEST_CASE_P(StringStuff, StringTest, MostStringTypes);

TEST(VStringTest, basic)
{
    VString<5> hi = "Hello"_s;
    EXPECT_EQ(5, hi.size());
    EXPECT_EQ(hi, hi);
    // truncation
    VString<5> hi2(strings::really_construct_from_a_pointer, "Hello, world!");
    EXPECT_EQ(5, hi2.size());
    EXPECT_EQ(hi, hi2);
    // short
    hi = "hi"_s;
    EXPECT_EQ(2, hi.size());
    VString<5> hi0;
    EXPECT_EQ(0, hi0.size());
}

template<typename T>
class NulStringTest : public ::testing::Test
{
};
TYPED_TEST_CASE_P(NulStringTest);

TYPED_TEST_P(NulStringTest, basic)
{
    TypeParam hi("hello"_s);
    EXPECT_EQ(hi.size(), strlen(hi.c_str()));
    EXPECT_STREQ("hello", hi.c_str());
}

REGISTER_TYPED_TEST_CASE_P(NulStringTest,
        basic);

typedef ::testing::Types<
    RString, AString, TString, ZString, VString<255>
> NulStringTypes;
INSTANTIATE_TYPED_TEST_CASE_P(NulStringStuff, NulStringTest, NulStringTypes);
} // namespace tmwa