-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.cpp
242 lines (192 loc) · 5.4 KB
/
tests.cpp
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
#include <FastString.h>
#include <array>
#include <gtest/gtest.h>
using namespace std::string_literals;
/* define types */
using string8 = fss::fixed_size_str<7>;
using string64 = fss::fixed_size_str<63>;
TEST(TestFixture, default_construction)
{
constexpr string8 a{};
ASSERT_EQ(0, a.length());
ASSERT_TRUE(a.empty());
ASSERT_EQ(7, a.max_size());
}
TEST(TestFixture, construction_from_constexpr)
{
constexpr const char *str = "1234";
constexpr string8 a = str;
ASSERT_EQ(4, a.length());
ASSERT_FALSE(a.empty());
ASSERT_EQ(7, a.max_size());
}
TEST(TestFixture, construction_from_const_buffer)
{
using string24 = fss::fixed_size_str<23>;
constexpr std::array<char, 24> buffer{'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
constexpr size_t buffer_str_size = 12;
constexpr string24 a(buffer.data(), buffer_str_size);
ASSERT_EQ("Hello World!"s, a.c_str());
ASSERT_EQ(12, a.length());
ASSERT_FALSE(a.empty());
ASSERT_EQ(23, a.max_size());
}
TEST(TestFixture, construction_from_buffer)
{
using string24 = fss::fixed_size_str<23>;
std::array<char, 24> buffer = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
size_t buffer_str_size = 12;
string24 a(buffer.data(), buffer_str_size);
ASSERT_EQ("Hello World!"s, a.c_str());
ASSERT_EQ(12, a.length());
ASSERT_FALSE(a.empty());
ASSERT_EQ(23, a.max_size());
}
TEST(TestFixture, copy_construction)
{
constexpr string8 a{"abc"};
auto a_copy = a;
ASSERT_EQ(a_copy.length(), a.length());
ASSERT_FALSE(a.empty());
ASSERT_FALSE(a_copy.empty());
ASSERT_EQ(a_copy.max_size(), a.max_size());
}
TEST(TestFixture, move_construction)
{
constexpr string8 a{"abc"};
auto a_copy = a;
auto a_move = std::move(a_copy);
ASSERT_EQ(a_move.length(), a.length());
ASSERT_FALSE(a.empty());
ASSERT_FALSE(a_move.empty());
ASSERT_EQ(a_move.max_size(), a.max_size());
}
TEST(TestFixture, copy_assignment)
{
constexpr string8 b{"1234", 4};
string8 c{"lmnopqrstuvxyz"};
ASSERT_EQ("1234"s, b.c_str());
ASSERT_EQ("lmnopqr"s, c.c_str()); // c is "lmnopqr"
c = b;
ASSERT_EQ("1234"s, c.c_str());
}
TEST(TestFixture, move_assignment)
{
constexpr string8 a{"1234", 4};
string8 b{"lmnopqrstuvxyz"};
b = std::move(a);
ASSERT_EQ("1234"s, b.c_str());
}
TEST(TestFixture, using_with_string_view)
{
constexpr string8 e{"abcdefghij", 10};
//constexpr auto e_sub_str = e.str().substr(0, 2);
//constexpr auto e_str = e.str();
auto e_sub_str = e.str().substr(0, 2);
auto e_str = e.str();
constexpr auto e_length = e.length();
ASSERT_EQ("abcdefg"s, e.c_str()); // truncated. e is "abcdefg";
ASSERT_EQ("ab"s, e_sub_str);
ASSERT_EQ("abcdefg"s, e_str);
ASSERT_EQ(7, e.length());
}
TEST(TestFixture, comparison)
{
constexpr string8 f{"abcd"};
constexpr string8 g{"abcd"};
constexpr string8 h{"abcf"};
ASSERT_TRUE((f == g));
ASSERT_FALSE((g == h));
}
TEST(TestFixture, append)
{
string8 k{"abc"}; // k is "abc"
ASSERT_EQ("abc"s, k.c_str());
k.append("d");
ASSERT_EQ("abcd"s, k.c_str());
k.append("efghi", 5);
ASSERT_EQ("abcdefg"s, k.c_str()); // k is "abcdefg". rest is truncated
}
TEST(TestFixture, clear)
{
string8 k{"abcdefg"};
ASSERT_EQ("abcdefg"s, k.c_str());
ASSERT_FALSE(k.empty());
k.clear();
ASSERT_EQ(""s, k.c_str());
ASSERT_TRUE(k.empty());
}
TEST(TestFixture, reset)
{
string8 k{ "abcdefg" };
ASSERT_EQ("abcdefg"s, k.c_str());
ASSERT_EQ(7, k.length());
k.reset("1234");
ASSERT_EQ("1234"s, k.c_str());
ASSERT_EQ(4, k.length());
k.reset("xyz", 3);
ASSERT_EQ("xyz"s, k.c_str());
ASSERT_EQ(3, k.length());
}
TEST(TestFixture, remove_suffix)
{
string8 l{ "1234567" };
ASSERT_EQ("1234567"s, l.c_str());
l.remove_suffix(3);
ASSERT_EQ("1234"s, l.c_str());
}
TEST(TestFixture, remove_prefix)
{
string8 l{ "1234" };
ASSERT_EQ("1234"s, l.c_str());
l.remove_prefix(2);
ASSERT_EQ("34"s, l.c_str());
}
TEST(TestFixture, stream_operator)
{
string8 l{ "1234" };
std::cout << l << std::endl;
ASSERT_TRUE(1);
}
TEST(TestFixture, swap)
{
string8 k{ "xyz" };
string8 l{ "34" };
l.swap(k);
// l is "xyz" and k is "34"
ASSERT_EQ("xyz"s, l.c_str());
ASSERT_EQ("34"s, k.c_str());
std::swap(l, k);
// l is "34" and k is "xyz"
ASSERT_EQ("34"s, l.c_str());
ASSERT_EQ("xyz"s, k.c_str());
}
struct test_struct
{
std::uint32_t a_{};
std::uint64_t b_{};
string8 c_ = "abcd";
constexpr auto get_c() const noexcept { return c_.c_str(); }
constexpr auto get_c_str() const noexcept { return c_.str(); }
constexpr void set_c(const char* str) { c_.reset(str); }
};
TEST(TestFixture, using_member_variables)
{
constexpr auto test_struct_size = sizeof(test_struct);
// uses only 8 + 4 bytes in stack
ASSERT_EQ(32, sizeof(test_struct));
constexpr test_struct t{};
// constexpr auto t_c = t.get_c();
// constexpr auto t_c_str = t.get_c_str();
auto t_c = t.get_c();
auto t_c_str = t.get_c_str();
ASSERT_EQ("abcd"s, t_c);
ASSERT_EQ("abcd"s, t_c_str);
test_struct t2{};
t2.set_c("123456");
const auto t2_c = t2.get_c();
const auto t2_c_str = t2.get_c();
ASSERT_EQ(32, sizeof(t2));
ASSERT_EQ("123456"s, t2_c);
ASSERT_EQ("123456"s, t2_c_str);
}