]>
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 | ||
30 | BOOST_AUTO_TEST_CASE(test_construction) { | |
31 | BOOST_REQUIRE_EQUAL(sstring(compat::string_view("abc")), sstring("abc")); | |
32 | } | |
33 | ||
34 | BOOST_AUTO_TEST_CASE(test_equality) { | |
35 | BOOST_REQUIRE_EQUAL(sstring("aaa"), sstring("aaa")); | |
36 | } | |
37 | ||
38 | BOOST_AUTO_TEST_CASE(test_to_sstring) { | |
39 | BOOST_REQUIRE_EQUAL(to_sstring(1234567), sstring("1234567")); | |
40 | } | |
41 | ||
42 | BOOST_AUTO_TEST_CASE(test_add_literal_to_sstring) { | |
43 | BOOST_REQUIRE_EQUAL("x" + sstring("y"), sstring("xy")); | |
44 | } | |
45 | ||
46 | BOOST_AUTO_TEST_CASE(test_find_sstring) { | |
47 | BOOST_REQUIRE_EQUAL(sstring("abcde").find('b'), 1u); | |
48 | BOOST_REQUIRE_EQUAL(sstring("babcde").find('b',1), 2u); | |
49 | } | |
50 | ||
51 | BOOST_AUTO_TEST_CASE(test_not_find_sstring) { | |
52 | BOOST_REQUIRE_EQUAL(sstring("abcde").find('x'), sstring::npos); | |
53 | } | |
54 | ||
55 | BOOST_AUTO_TEST_CASE(test_str_find_sstring) { | |
56 | BOOST_REQUIRE_EQUAL(sstring("abcde").find("bc"), 1u); | |
57 | BOOST_REQUIRE_EQUAL(sstring("abcbcde").find("bc", 2), 3u); | |
58 | } | |
59 | ||
60 | BOOST_AUTO_TEST_CASE(test_str_not_find_sstring) { | |
61 | BOOST_REQUIRE_EQUAL(sstring("abcde").find("x"), sstring::npos); | |
62 | } | |
63 | ||
64 | BOOST_AUTO_TEST_CASE(test_substr_sstring) { | |
65 | BOOST_REQUIRE_EQUAL(sstring("abcde").substr(1,2), "bc"); | |
66 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(1,2), "bc"); | |
67 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(1,3), "bc"); | |
68 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(0, 2), "ab"); | |
69 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(3, 2), ""); | |
70 | BOOST_REQUIRE_EQUAL(sstring("abc").substr(1), "bc"); | |
71 | } | |
72 | ||
73 | BOOST_AUTO_TEST_CASE(test_substr_eor_sstring) { | |
74 | BOOST_REQUIRE_THROW(sstring("abcde").substr(6,1), std::out_of_range); | |
75 | } | |
76 | ||
77 | BOOST_AUTO_TEST_CASE(test_at_sstring) { | |
78 | BOOST_REQUIRE_EQUAL(sstring("abcde").at(1), 'b'); | |
79 | BOOST_REQUIRE_THROW(sstring("abcde").at(6), std::out_of_range); | |
80 | sstring s("abcde"); | |
81 | s.at(1) = 'd'; | |
82 | BOOST_REQUIRE_EQUAL(s, "adcde"); | |
83 | } | |
84 | ||
85 | BOOST_AUTO_TEST_CASE(test_find_last_sstring) { | |
86 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a'), 4u); | |
87 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a',5), 4u); | |
88 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a',4), 4u); | |
89 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('a',3), 2u); | |
90 | BOOST_REQUIRE_EQUAL(sstring("ababa").find_last_of('x'), sstring::npos); | |
91 | BOOST_REQUIRE_EQUAL(sstring("").find_last_of('a'), sstring::npos); | |
92 | } | |
93 | ||
94 | ||
95 | BOOST_AUTO_TEST_CASE(test_append) { | |
96 | BOOST_REQUIRE_EQUAL(sstring("aba").append("1234", 3), "aba123"); | |
97 | BOOST_REQUIRE_EQUAL(sstring("aba").append("1234", 4), "aba1234"); | |
98 | BOOST_REQUIRE_EQUAL(sstring("aba").append("1234", 0), "aba"); | |
99 | } | |
100 | ||
101 | BOOST_AUTO_TEST_CASE(test_replace) { | |
102 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(1,1, "xyz", 1), "axc"); | |
103 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(3,2, "xyz", 2), "abcxy"); | |
104 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(2,2, "xyz", 2), "abxy"); | |
105 | BOOST_REQUIRE_EQUAL(sstring("abc").replace(0,2, "", 0), "c"); | |
106 | BOOST_REQUIRE_THROW(sstring("abc").replace(4,1, "xyz", 1), std::out_of_range); | |
107 | const char* s = "xyz"; | |
108 | sstring str("abcdef"); | |
109 | BOOST_REQUIRE_EQUAL(str.replace(str.begin() + 1 , str.begin() + 3, s + 1, s + 3), "ayzdef"); | |
110 | BOOST_REQUIRE_THROW(sstring("abc").replace(4,1, "xyz", 1), std::out_of_range); | |
111 | ||
112 | } | |
113 | ||
114 | BOOST_AUTO_TEST_CASE(test_insert) { | |
115 | sstring str("abc"); | |
116 | const char* s = "xyz"; | |
117 | str.insert(str.begin() +1, s + 1, s + 2); | |
118 | BOOST_REQUIRE_EQUAL(str, "aybc"); | |
119 | str = "abc"; | |
120 | BOOST_REQUIRE_THROW(str.insert(str.begin() + 5, s + 1, s + 2), std::out_of_range); | |
121 | } | |
122 | ||
123 | BOOST_AUTO_TEST_CASE(test_erase) { | |
124 | sstring str("abcdef"); | |
125 | auto i = str.erase(str.begin() + 1, str.begin() + 3); | |
126 | BOOST_REQUIRE_EQUAL(*i, 'd'); | |
127 | BOOST_REQUIRE_EQUAL(str, "adef"); | |
128 | BOOST_REQUIRE_THROW(str.erase(str.begin() + 5, str.begin() + 6), std::out_of_range); | |
129 | } | |
130 | ||
131 | BOOST_AUTO_TEST_CASE(test_ctor_iterator) { | |
132 | std::list<char> data{{'a', 'b', 'c'}}; | |
133 | sstring s(data.begin(), data.end()); | |
134 | BOOST_REQUIRE_EQUAL(s, "abc"); | |
135 | } | |
9f95a23c TL |
136 | |
137 | BOOST_AUTO_TEST_CASE(test_nul_termination) { | |
138 | using stype = basic_sstring<char, uint32_t, 15, true>; | |
139 | ||
140 | for (int size = 1; size <= 32; size *= 2) { | |
141 | auto s1 = stype(stype::initialized_later{}, size - 1); | |
142 | BOOST_REQUIRE_EQUAL(s1.c_str()[size - 1], '\0'); | |
143 | auto s2 = stype(stype::initialized_later{}, size); | |
144 | BOOST_REQUIRE_EQUAL(s2.c_str()[size], '\0'); | |
145 | ||
146 | s1 = stype("01234567890123456789012345678901", size - 1); | |
147 | BOOST_REQUIRE_EQUAL(s1.c_str()[size - 1], '\0'); | |
148 | s2 = stype("01234567890123456789012345678901", size); | |
149 | BOOST_REQUIRE_EQUAL(s2.c_str()[size], '\0'); | |
150 | ||
151 | s1 = stype(size - 1, ' '); | |
152 | BOOST_REQUIRE_EQUAL(s1.c_str()[size - 1], '\0'); | |
153 | s2 = stype(size, ' '); | |
154 | BOOST_REQUIRE_EQUAL(s2.c_str()[size], '\0'); | |
155 | ||
156 | s2 = s1; | |
157 | BOOST_REQUIRE_EQUAL(s2.c_str()[s1.size()], '\0'); | |
158 | s2.resize(s1.size()); | |
159 | BOOST_REQUIRE_EQUAL(s2.c_str()[s1.size()], '\0'); | |
160 | BOOST_REQUIRE_EQUAL(s1, s2); | |
161 | ||
162 | auto new_size = size / 2; | |
163 | s2 = s1; | |
164 | s2.resize(new_size); | |
165 | BOOST_REQUIRE_EQUAL(s2.c_str()[new_size], '\0'); | |
166 | BOOST_REQUIRE(!strncmp(s1.c_str(), s2.c_str(), new_size)); | |
167 | ||
168 | new_size = size * 2; | |
169 | s2 = s1; | |
170 | s2.resize(new_size); | |
171 | BOOST_REQUIRE_EQUAL(s2.c_str()[new_size], '\0'); | |
172 | BOOST_REQUIRE(!strncmp(s1.c_str(), s2.c_str(), std::min(s1.size(), s2.size()))); | |
173 | ||
174 | new_size = size * 2; | |
175 | s2 = s1 + s1; | |
176 | BOOST_REQUIRE_EQUAL(s2.c_str()[s2.size()], '\0'); | |
177 | BOOST_REQUIRE(!strncmp(s1.c_str(), s2.c_str(), std::min(s1.size(), s2.size()))); | |
178 | } | |
179 | } |