]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
2 | * This file is open source software, licensed to you under the terms | |
3 | * of the Apache License, Version 2.0 (the "License"). See the NOTICE file | |
4 | * distributed with this work for additional information regarding copyright | |
5 | * ownership. You may not use this file except in compliance with the License. | |
6 | * | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, | |
12 | * software distributed under the License is distributed on an | |
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | * KIND, either express or implied. See the License for the | |
15 | * specific language governing permissions and limitations | |
16 | * under the License. | |
17 | */ | |
18 | /* | |
19 | * Copyright 2014 Cloudius Systems | |
20 | */ | |
21 | ||
22 | #define BOOST_TEST_MODULE core | |
23 | ||
24 | #include <boost/test/included/unit_test.hpp> | |
25 | #include <seastar/core/sstring.hh> | |
26 | #include <list> | |
27 | ||
28 | using namespace seastar; | |
29 | ||
f67539c2 TL |
30 | BOOST_AUTO_TEST_CASE(test_make_sstring) { |
31 | std::string_view foo = "foo"; | |
32 | std::string bar = "bar"; | |
33 | sstring zed = "zed"; | |
34 | const char* baz = "baz"; | |
35 | BOOST_REQUIRE_EQUAL(make_sstring(foo, bar, zed, baz, "bah"), sstring("foobarzedbazbah")); | |
36 | } | |
37 | ||
11fdf7f2 | 38 | BOOST_AUTO_TEST_CASE(test_construction) { |
f67539c2 | 39 | BOOST_REQUIRE_EQUAL(sstring(std::string_view("abc")), sstring("abc")); |
11fdf7f2 TL |
40 | } |
41 | ||
42 | BOOST_AUTO_TEST_CASE(test_equality) { | |
43 | BOOST_REQUIRE_EQUAL(sstring("aaa"), sstring("aaa")); | |
44 | } | |
45 | ||
46 | BOOST_AUTO_TEST_CASE(test_to_sstring) { | |
47 | BOOST_REQUIRE_EQUAL(to_sstring(1234567), sstring("1234567")); | |
48 | } | |
49 | ||
50 | BOOST_AUTO_TEST_CASE(test_add_literal_to_sstring) { | |
51 | BOOST_REQUIRE_EQUAL("x" + sstring("y"), sstring("xy")); | |
52 | } | |
53 | ||
54 | BOOST_AUTO_TEST_CASE(test_find_sstring) { | |
55 | BOOST_REQUIRE_EQUAL(sstring("abcde").find('b'), 1u); | |
56 | BOOST_REQUIRE_EQUAL(sstring("babcde").find('b',1), 2u); | |
57 | } | |
58 | ||
1e59de90 TL |
59 | BOOST_AUTO_TEST_CASE(test_find_sstring_compatible) { |
60 | auto check_find = [](const char* s1, const char* s2, size_t pos) { | |
61 | const auto xpos_ss = sstring(s1).find(s2, pos); | |
62 | const auto xpos_std = std::string(s1).find(s2, pos); | |
63 | ||
64 | // verify that std::string really has the same behavior as we just tested for sstring | |
65 | if (xpos_ss == sstring::npos) { // sstring::npos may not equal std::string::npos ? | |
66 | BOOST_REQUIRE_EQUAL(xpos_std, std::string::npos); | |
67 | } else { | |
68 | BOOST_REQUIRE_EQUAL(xpos_ss, xpos_std); | |
69 | } | |
70 | }; | |
71 | ||
72 | check_find("", "", 0); | |
73 | check_find("", "", 1); | |
74 | check_find("abcde", "", 0); | |
75 | check_find("abcde", "", 1); | |
76 | check_find("abcde", "", 5); | |
77 | check_find("abcde", "", 6); | |
78 | } | |
79 | ||
11fdf7f2 TL |
80 | BOOST_AUTO_TEST_CASE(test_not_find_sstring) { |
81 | BOOST_REQUIRE_EQUAL(sstring("abcde").find('x'), sstring::npos); | |
82 | } | |
83 | ||
84 | BOOST_AUTO_TEST_CASE(test_str_find_sstring) { | |
85 | BOOST_REQUIRE_EQUAL(sstring("abcde").find("bc"), 1u); | |
86 | BOOST_REQUIRE_EQUAL(sstring("abcbcde").find("bc", 2), 3u); | |
1e59de90 TL |
87 | BOOST_REQUIRE_EQUAL(sstring("abcde").find("abcde"), 0u); |
88 | BOOST_REQUIRE_EQUAL(sstring("abcde").find("", 5), 5u); | |
89 | BOOST_REQUIRE_EQUAL(sstring("ababcbdbef").find("bef"), 7u); | |
90 | BOOST_REQUIRE_EQUAL(sstring("").find("", 0), 0u); | |
11fdf7f2 TL |
91 | } |
92 | ||
93 | BOOST_AUTO_TEST_CASE(test_str_not_find_sstring) { | |
94 | BOOST_REQUIRE_EQUAL(sstring("abcde").find("x"), sstring::npos); | |
1e59de90 TL |
95 | BOOST_REQUIRE_EQUAL(sstring("abcdefg").find("cde", 6), sstring::npos); |
96 | BOOST_REQUIRE_EQUAL(sstring("abcdefg").find("cde", 4), sstring::npos); | |
97 | BOOST_REQUIRE_EQUAL(sstring("ababcbdbe").find("bcd"), sstring::npos); | |
98 | BOOST_REQUIRE_EQUAL(sstring("").find("", 1), sstring::npos); | |
99 | BOOST_REQUIRE_EQUAL(sstring("abc").find("abcde"), sstring::npos); | |
11fdf7f2 TL |
100 | } |
101 | ||
102 | BOOST_AUTO_TEST_CASE(test_substr_sstring) { | |
103 | BOOST_REQUIRE_EQUAL(sstring("abcde").substr(1,2), "bc"); | |
104 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(1,2), "bc"); | |
105 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(1,3), "bc"); | |
106 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(0, 2), "ab"); | |
107 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(3, 2), ""); | |
108 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(1), "bc"); | |
109 | } | |
110 | ||
111 | BOOST_AUTO_TEST_CASE(test_substr_eor_sstring) { | |
112 | BOOST_REQUIRE_THROW(sstring("abcde").substr(6,1), std::out_of_range); | |
113 | } | |
114 | ||
115 | BOOST_AUTO_TEST_CASE(test_at_sstring) { | |
116 | BOOST_REQUIRE_EQUAL(sstring("abcde").at(1), 'b'); | |
117 | BOOST_REQUIRE_THROW(sstring("abcde").at(6), std::out_of_range); | |
118 | sstring s("abcde"); | |
119 | s.at(1) = 'd'; | |
120 | BOOST_REQUIRE_EQUAL(s, "adcde"); | |
121 | } | |
122 | ||
123 | BOOST_AUTO_TEST_CASE(test_find_last_sstring) { | |
124 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a'), 4u); | |
125 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a',5), 4u); | |
126 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a',4), 4u); | |
127 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a',3), 2u); | |
128 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('x'), sstring::npos); | |
129 | BOOST_REQUIRE_EQUAL(sstring("").find_last_of('a'), sstring::npos); | |
130 | } | |
131 | ||
132 | ||
133 | BOOST_AUTO_TEST_CASE(test_append) { | |
134 | BOOST_REQUIRE_EQUAL(sstring("aba").append("1234", 3), "aba123"); | |
135 | BOOST_REQUIRE_EQUAL(sstring("aba").append("1234", 4), "aba1234"); | |
136 | BOOST_REQUIRE_EQUAL(sstring("aba").append("1234", 0), "aba"); | |
137 | } | |
138 | ||
139 | BOOST_AUTO_TEST_CASE(test_replace) { | |
140 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(1,1, "xyz", 1), "axc"); | |
141 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(3,2, "xyz", 2), "abcxy"); | |
142 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(2,2, "xyz", 2), "abxy"); | |
143 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(0,2, "", 0), "c"); | |
144 | BOOST_REQUIRE_THROW(sstring("abc").replace(4,1, "xyz", 1), std::out_of_range); | |
145 | const char* s = "xyz"; | |
146 | sstring str("abcdef"); | |
147 | BOOST_REQUIRE_EQUAL(str.replace(str.begin() + 1 , str.begin() + 3, s + 1, s + 3), "ayzdef"); | |
148 | BOOST_REQUIRE_THROW(sstring("abc").replace(4,1, "xyz", 1), std::out_of_range); | |
149 | ||
150 | } | |
151 | ||
152 | BOOST_AUTO_TEST_CASE(test_insert) { | |
153 | sstring str("abc"); | |
154 | const char* s = "xyz"; | |
155 | str.insert(str.begin() +1, s + 1, s + 2); | |
156 | BOOST_REQUIRE_EQUAL(str, "aybc"); | |
157 | str = "abc"; | |
158 | BOOST_REQUIRE_THROW(str.insert(str.begin() + 5, s + 1, s + 2), std::out_of_range); | |
159 | } | |
160 | ||
161 | BOOST_AUTO_TEST_CASE(test_erase) { | |
162 | sstring str("abcdef"); | |
163 | auto i = str.erase(str.begin() + 1, str.begin() + 3); | |
164 | BOOST_REQUIRE_EQUAL(*i, 'd'); | |
165 | BOOST_REQUIRE_EQUAL(str, "adef"); | |
11fdf7f2 TL |
166 | } |
167 | ||
168 | BOOST_AUTO_TEST_CASE(test_ctor_iterator) { | |
169 | std::list<char> data{{'a', 'b', 'c'}}; | |
170 | sstring s(data.begin(), data.end()); | |
171 | BOOST_REQUIRE_EQUAL(s, "abc"); | |
172 | } | |
9f95a23c TL |
173 | |
174 | BOOST_AUTO_TEST_CASE(test_nul_termination) { | |
175 | using stype = basic_sstring<char, uint32_t, 15, true>; | |
176 | ||
177 | for (int size = 1; size <= 32; size *= 2) { | |
f67539c2 | 178 | auto s1 = uninitialized_string<stype>(size - 1); |
9f95a23c | 179 | BOOST_REQUIRE_EQUAL(s1.c_str()[size - 1], '\0'); |
f67539c2 | 180 | auto s2 = uninitialized_string<stype>(size); |
9f95a23c TL |
181 | BOOST_REQUIRE_EQUAL(s2.c_str()[size], '\0'); |
182 | ||
183 | s1 = stype("01234567890123456789012345678901", size - 1); | |
184 | BOOST_REQUIRE_EQUAL(s1.c_str()[size - 1], '\0'); | |
185 | s2 = stype("01234567890123456789012345678901", size); | |
186 | BOOST_REQUIRE_EQUAL(s2.c_str()[size], '\0'); | |
187 | ||
188 | s1 = stype(size - 1, ' '); | |
189 | BOOST_REQUIRE_EQUAL(s1.c_str()[size - 1], '\0'); | |
190 | s2 = stype(size, ' '); | |
191 | BOOST_REQUIRE_EQUAL(s2.c_str()[size], '\0'); | |
192 | ||
193 | s2 = s1; | |
194 | BOOST_REQUIRE_EQUAL(s2.c_str()[s1.size()], '\0'); | |
195 | s2.resize(s1.size()); | |
196 | BOOST_REQUIRE_EQUAL(s2.c_str()[s1.size()], '\0'); | |
197 | BOOST_REQUIRE_EQUAL(s1, s2); | |
198 | ||
199 | auto new_size = size / 2; | |
200 | s2 = s1; | |
201 | s2.resize(new_size); | |
202 | BOOST_REQUIRE_EQUAL(s2.c_str()[new_size], '\0'); | |
203 | BOOST_REQUIRE(!strncmp(s1.c_str(), s2.c_str(), new_size)); | |
204 | ||
205 | new_size = size * 2; | |
206 | s2 = s1; | |
207 | s2.resize(new_size); | |
208 | BOOST_REQUIRE_EQUAL(s2.c_str()[new_size], '\0'); | |
209 | BOOST_REQUIRE(!strncmp(s1.c_str(), s2.c_str(), std::min(s1.size(), s2.size()))); | |
210 | ||
211 | new_size = size * 2; | |
212 | s2 = s1 + s1; | |
213 | BOOST_REQUIRE_EQUAL(s2.c_str()[s2.size()], '\0'); | |
214 | BOOST_REQUIRE(!strncmp(s1.c_str(), s2.c_str(), std::min(s1.size(), s2.size()))); | |
215 | } | |
216 | } | |
1e59de90 TL |
217 | |
218 | BOOST_AUTO_TEST_CASE(test_resize_and_overwrite) { | |
219 | static constexpr size_t new_size = 42; | |
220 | static constexpr char pattern = 's'; | |
221 | { | |
222 | // the size of new content is identical to the specified count | |
223 | sstring s; | |
224 | s.resize_and_overwrite(new_size, [](char* buf, size_t n) { | |
225 | memset(buf, pattern, n); | |
226 | return n; | |
227 | }); | |
228 | BOOST_CHECK_EQUAL(s, sstring(new_size, pattern)); | |
229 | } | |
230 | { | |
231 | // the size of new content is smaller than the specified count | |
232 | static constexpr size_t smaller_size = new_size / 2; | |
233 | sstring s; | |
234 | s.resize_and_overwrite(new_size, [](char* buf, size_t n) { | |
235 | memset(buf, pattern, smaller_size); | |
236 | return smaller_size; | |
237 | }); | |
238 | BOOST_CHECK_EQUAL(s, sstring(smaller_size, pattern)); | |
239 | } | |
240 | } |