Lolly 1.4.27
Loading...
Searching...
No Matches
Functions
string_test.cpp File Reference
#include "a_lolly_test.hpp"
#include "string.hpp"
Include dependency graph for string_test.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("equality of string")
 
 TEST_CASE ("compare string")
 
 TEST_CASE ("test slice")
 
 TEST_CASE ("test concat")
 
 TEST_CASE ("test iteration")
 
 TEST_CASE ("test append")
 
 TEST_CASE ("test reserve along with append")
 
 TEST_CASE ("test expand_or_shrink_by along with sub index")
 
 TEST_CASE ("test resize")
 
 TEST_CASE ("test as bool")
 
 TEST_CASE ("as_int")
 
 TEST_CASE ("is_int")
 
 TEST_CASE ("test as strig bool")
 
 TEST_CASE ("test is empty")
 
 TEST_CASE ("test is bool")
 
 TEST_CASE ("test is int")
 
 TEST_CASE ("test is quoted")
 

Function Documentation

◆ TEST_CASE() [1/17]

TEST_CASE ( "equality of string" )

Definition at line 4 of file string_test.cpp.

4 {
5 CHECK_EQ (string ("abc") == "abc", true);
6 CHECK_EQ (string ("abc") == "", false);
7
8 CHECK_EQ (string ("abc") != "abc", false);
9 CHECK_EQ (string ("abc") != "", true);
10
11 CHECK_EQ (string ("abc") == string ("abc"), true);
12 CHECK_EQ (string ("abc") == string (), false);
13 CHECK_EQ (string ("abc") != string ("abc"), false);
14 CHECK_EQ (string ("abc") != string (), true);
15
16 CHECK_EQ (string () == string (), true);
17}
The list class represents a linked list.
Definition list.hpp:48

◆ TEST_CASE() [2/17]

TEST_CASE ( "compare string" )

Definition at line 19 of file string_test.cpp.

19 {
20 CHECK (string ("ab") < string ("b"));
21 CHECK (string () < string ("0"));
22 CHECK (string ("a") <= string ("a"));
23 CHECK (string ("ab") <= string ("b"));
24 CHECK (string () <= string ());
25 CHECK (string () <= string ("0"));
26}

◆ TEST_CASE() [3/17]

TEST_CASE ( "test slice" )

Definition at line 28 of file string_test.cpp.

28 {
29 CHECK_EQ (string ("abcde") (0, 0) == string (), true);
30 CHECK_EQ (string ("abcde") (0, 1) == string ("a"), true);
31 CHECK_EQ (string ("abcde") (0, 10) == string ("abcde"), true);
32 CHECK_EQ (string ("abcde") (-1, 1) == string ("a"), true);
33 CHECK_EQ (string ("abcde") (3, 2) == string (), true);
34 CHECK_EQ (string ("abcde") (3, -2) == string (), true);
35 CHECK_EQ (string ("abcde") (10, 11) == string (), true);
36 CHECK_EQ (string ("abcde") (-3, -2) == string (), true);
37}

◆ TEST_CASE() [4/17]

TEST_CASE ( "test concat" )

Definition at line 39 of file string_test.cpp.

39 {
40 CHECK_EQ (string ("abc") * "de" == string ("abcde"), true);
41 CHECK_EQ (string ("abc") * string ("de") == string ("abcde"), true);
42 CHECK_EQ ("abc" * string ("de") == string ("abcde"), true);
43}

◆ TEST_CASE() [5/17]

TEST_CASE ( "test iteration" )

Definition at line 45 of file string_test.cpp.

45 {
46 string str1 = "s\0tr1";
47 int expectedIndex= 0;
48 for (const auto element : str1) {
51 }
53}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170

◆ TEST_CASE() [6/17]

TEST_CASE ( "test append" )

Definition at line 59 of file string_test.cpp.

59 {
60 auto str= string ();
61 str << 'x';
62 CHECK_EQ (str == string ("x"), true);
63 str << string ("yz");
64 CHECK_EQ (str == string ("xyz"), true);
65}

◆ TEST_CASE() [7/17]

TEST_CASE ( "test reserve along with append" )

Definition at line 75 of file string_test.cpp.

75 {
76
77 SUBCASE ("reserved more space") {
78 auto str= string ();
79 str->reserve (6);
80 str << 'x';
81 CHECK_EQ (str == "x", true);
82 str << string ("yz");
83 CHECK_EQ (str == "xyz", true);
84 str << string (": larger than reserved space");
85 CHECK_EQ (str == "xyz: larger than reserved space", true);
86 }
87 SUBCASE ("reserved the same space") {
88 auto str= string ("abc");
89 str->reserve (3);
90 CHECK_EQ (str == "abc", true);
91 }
92 SUBCASE ("reserved less space should take no effect") {
93 auto str= string ("abc");
94 str->reserve (2);
95 CHECK_EQ (str == "abc", true);
96 }
97}

◆ TEST_CASE() [8/17]

TEST_CASE ( "test expand_or_shrink_by along with sub index" )

Definition at line 99 of file string_test.cpp.

99 {
100
101 SUBCASE ("expand") {
102 auto str = string ("abc");
103 int previous_n= str->expand_or_shrink_by (1);
104 CHECK_EQ (previous_n, 3);
105 str[3]= 'd';
106 CHECK_EQ (str == "abcd", true);
107 }
108 SUBCASE ("shrink") {
109 auto str = string ("abc");
110 int previous_n= str->expand_or_shrink_by (-1);
111 CHECK_EQ (previous_n, 3);
112 CHECK_EQ (str == "ab", true);
113 }
114 SUBCASE ("delta 0 takes no effect") {
115 auto str = string ("abc");
116 int previous_n= str->expand_or_shrink_by (0);
117 CHECK_EQ (previous_n, 3);
118 CHECK_EQ (str == "abc", true);
119 }
120}

◆ TEST_CASE() [9/17]

TEST_CASE ( "test resize" )

Definition at line 122 of file string_test.cpp.

122 {
123
124 SUBCASE ("expand") {
125 auto str= string ("abc");
126 str->resize (4);
127 str[3]= 'd';
128 CHECK_EQ (str == "abcd", true);
129 }
130 SUBCASE ("shrink") {
131 auto str= string ("abc");
132 str->resize (2);
133 CHECK_EQ (str == "ab", true);
134 }
135 SUBCASE ("delta 0 takes no effect") {
136 auto str= string ("abc");
137 str->resize (3);
138 CHECK_EQ (str == "abc", true);
139 }
140}

◆ TEST_CASE() [10/17]

TEST_CASE ( "test as bool" )

Definition at line 146 of file string_test.cpp.

146 {
147 CHECK_EQ (as_bool (string ("true")), true);
148 CHECK_EQ (as_bool (string ("#t")), true);
149 CHECK_EQ (as_bool (string ("false")), false);
150
151 CHECK_EQ (as_bool (string ("true")), true);
152 CHECK_EQ (as_bool (string ("#t")), true);
153 CHECK_EQ (!as_bool (string ("false")), true);
154 // implicit conversion from char*
155}
bool as_bool(string s)
Definition string.cpp:239

◆ TEST_CASE() [11/17]

TEST_CASE ( "as_int" )

Definition at line 157 of file string_test.cpp.

157 {
158 SUBCASE ("normal conversion") {
159 CHECK_EQ (as_int (string ("1")), 1);
160 CHECK_EQ (as_int (string ("-1")), -1);
161 CHECK_EQ (as_int (string ("0")), 0);
162 CHECK_EQ (as_int (string ("+1")), 1);
163 }
164
165 // assuming int is 32bit
166 SUBCASE ("min and max") {
167 CHECK_EQ (as_int (string ("-2147483648")), -2147483648);
168 CHECK_EQ (as_int (string ("2147483647")), 2147483647);
169 }
170
171 SUBCASE ("int overflow") {
172 CHECK_EQ (as_int (string ("-2147483649")), 2147483647);
173 CHECK_EQ (as_int (string ("2147483648")), -2147483648);
174 }
175
176 SUBCASE ("invalid int") {
177 CHECK_EQ (as_int (string ("not a int")), 0);
178 CHECK_EQ (as_int (string ("++1")), 0);
179 CHECK_EQ (as_int (string ("1pt")), 1);
180 CHECK_EQ (as_int (string ("1.23")), 1);
181 }
182}
SI as_int(double x)
Converts a double to a signed integer, rounding to the nearest integer.
Definition basic.hpp:261

◆ TEST_CASE() [12/17]

TEST_CASE ( "is_int" )

Definition at line 184 of file string_test.cpp.

184 {
185 SUBCASE ("normal conversion") {
186 CHECK (is_int (string ("1")));
187 CHECK (is_int (string ("-1")));
188 CHECK (is_int (string ("0")));
189 CHECK (is_int (string ("+1")));
190 }
191
192 // assuming int is 32bit
193 SUBCASE ("min and max") {
194 CHECK (is_int (string ("-2147483648")));
195 CHECK (is_int (string ("2147483647")));
196 }
197
198 SUBCASE ("int overflow") {
199 CHECK (is_int (string ("-2147483649")));
200 CHECK (is_int (string ("2147483648")));
201 }
202
203 SUBCASE ("invalid int") {
204 CHECK_FALSE (is_int (string ("not a int")));
205 CHECK_FALSE (is_int (string ("++1")));
206 CHECK_FALSE (is_int (string ("1pt")));
207 CHECK_FALSE (is_int (string ("1.23")));
208 }
209}
bool is_int(string s)
Definition string.cpp:364

◆ TEST_CASE() [13/17]

TEST_CASE ( "test as strig bool" )

Definition at line 211 of file string_test.cpp.

211 {
212 CHECK_EQ (as_string_bool (true) == string ("true"), true);
213 CHECK_EQ (as_string_bool (false) == string ("false"), true);
214}
string as_string_bool(bool f)
Definition string.cpp:304

◆ TEST_CASE() [14/17]

TEST_CASE ( "test is empty" )

Definition at line 220 of file string_test.cpp.

220 {
221 CHECK_EQ (is_empty (""), true);
222 CHECK_EQ (!is_empty (" "), true);
223 CHECK_EQ (!is_empty ("nonempty"), true);
224}
bool is_empty(string s)
Definition string.cpp:354

◆ TEST_CASE() [15/17]

TEST_CASE ( "test is bool" )

Definition at line 226 of file string_test.cpp.

226 {
227 CHECK_EQ (is_bool (string ("true")), true);
228 CHECK_EQ (is_bool (string ("false")), true);
229 CHECK_EQ (is_bool (string ("true")), true);
230 CHECK_EQ (is_bool (string ("false")), true);
231
232 CHECK_EQ (!is_bool (string ("100")), true);
233 CHECK_EQ (!is_bool (string ("nil")), true);
234}
bool is_bool(string s)
Definition string.cpp:359

◆ TEST_CASE() [16/17]

TEST_CASE ( "test is int" )

Definition at line 236 of file string_test.cpp.

236 {
237 // Empty string is not an int
238 CHECK_EQ (!is_int (string ("")), true);
239
240 // Only 0-9 in chars are int
241 for (auto i= 0; i < 256; i++) {
242 char iter= (char) i;
243 if (iter >= '0' && iter <= '9') CHECK_EQ (is_int (string (iter)), true);
244 else CHECK_EQ (!is_int (string (iter)), true);
245 }
246
247 // Random tests
248 CHECK_EQ (is_int (string ("-100")), true);
249 CHECK_EQ (is_int (string ("+100")), true);
250 CHECK_EQ (is_int (string ("100")), true);
251 CHECK_EQ (!is_int (string (".0")), true);
252 CHECK_EQ (!is_int (string ("0x09")), true);
253}

◆ TEST_CASE() [17/17]

TEST_CASE ( "test is quoted" )

Definition at line 255 of file string_test.cpp.

255 {
256 CHECK_EQ (is_quoted ("\"\""), true);
257 CHECK_EQ (is_quoted ("\"Hello TeXmacs\""), true);
258 CHECK_EQ (is_quoted ("\"Hello\"TeXmacs\""), true);
259
260 CHECK_EQ (!is_quoted ("\""), true);
261 CHECK_EQ (!is_quoted ("A"), true);
262 CHECK_EQ (!is_quoted ("9"), true);
263 CHECK_EQ (!is_quoted ("Hello TeXmacs"), true);
264 CHECK_EQ (!is_quoted ("\"Hello TeXmac\"s"), true);
265 CHECK_EQ (!is_quoted ("H\"ello TeXmacs\""), true);
266 // is_quoted only checks if a string starts with a double quote
267 // and ends with another double quote, regardless the validity
268 // of the raw string
269}
bool is_quoted(string s)
Definition string.cpp:408