Lolly 1.4.28
Loading...
Searching...
No Matches
string_test.cpp
Go to the documentation of this file.
1#include "a_lolly_test.hpp"
2#include "string.hpp"
3
4TEST_CASE ("equality of string") {
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}
18
19TEST_CASE ("compare string") {
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}
27
28TEST_CASE ("test slice") {
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}
38
39TEST_CASE ("test concat") {
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}
44
45TEST_CASE ("test iteration") {
46 string str1 = "s\0tr1";
47 int expectedIndex= 0;
48 for (const auto element : str1) {
51 }
53}
54
55/******************************************************************************
56 * Modifications
57 ******************************************************************************/
58
59TEST_CASE ("test append") {
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}
66
67/******************************************************************************
68 * Conversions
69 ******************************************************************************/
70
71TEST_CASE ("test as bool") {
72 CHECK_EQ (as_bool (string ("true")), true);
73 CHECK_EQ (as_bool (string ("#t")), true);
74 CHECK_EQ (as_bool (string ("false")), false);
75
76 CHECK_EQ (as_bool (string ("true")), true);
77 CHECK_EQ (as_bool (string ("#t")), true);
78 CHECK_EQ (!as_bool (string ("false")), true);
79 // implicit conversion from char*
80}
81
82TEST_CASE ("as_int") {
83 SUBCASE ("normal conversion") {
84 CHECK_EQ (as_int (string ("1")), 1);
85 CHECK_EQ (as_int (string ("-1")), -1);
86 CHECK_EQ (as_int (string ("0")), 0);
87 CHECK_EQ (as_int (string ("+1")), 1);
88 }
89
90 // assuming int is 32bit
91 SUBCASE ("min and max") {
92 CHECK_EQ (as_int (string ("-2147483648")), -2147483648);
93 CHECK_EQ (as_int (string ("2147483647")), 2147483647);
94 }
95
96 SUBCASE ("int overflow") {
97 CHECK_EQ (as_int (string ("-2147483649")), 2147483647);
98 CHECK_EQ (as_int (string ("2147483648")), -2147483648);
99 }
100
101 SUBCASE ("invalid int") {
102 CHECK_EQ (as_int (string ("not a int")), 0);
103 CHECK_EQ (as_int (string ("++1")), 0);
104 CHECK_EQ (as_int (string ("1pt")), 1);
105 CHECK_EQ (as_int (string ("1.23")), 1);
106 }
107}
108
109TEST_CASE ("is_int") {
110 SUBCASE ("normal conversion") {
111 CHECK (is_int (string ("1")));
112 CHECK (is_int (string ("-1")));
113 CHECK (is_int (string ("0")));
114 CHECK (is_int (string ("+1")));
115 }
116
117 // assuming int is 32bit
118 SUBCASE ("min and max") {
119 CHECK (is_int (string ("-2147483648")));
120 CHECK (is_int (string ("2147483647")));
121 }
122
123 SUBCASE ("int overflow") {
124 CHECK (is_int (string ("-2147483649")));
125 CHECK (is_int (string ("2147483648")));
126 }
127
128 SUBCASE ("invalid int") {
129 CHECK_FALSE (is_int (string ("not a int")));
130 CHECK_FALSE (is_int (string ("++1")));
131 CHECK_FALSE (is_int (string ("1pt")));
132 CHECK_FALSE (is_int (string ("1.23")));
133 }
134}
135
136TEST_CASE ("test as strig bool") {
137 CHECK_EQ (as_string_bool (true) == string ("true"), true);
138 CHECK_EQ (as_string_bool (false) == string ("false"), true);
139}
140
141/******************************************************************************
142 * Predicates
143 ******************************************************************************/
144
145TEST_CASE ("test is empty") {
146 CHECK_EQ (is_empty (""), true);
147 CHECK_EQ (!is_empty (" "), true);
148 CHECK_EQ (!is_empty ("nonempty"), true);
149}
150
151TEST_CASE ("test is bool") {
152 CHECK_EQ (is_bool (string ("true")), true);
153 CHECK_EQ (is_bool (string ("false")), true);
154 CHECK_EQ (is_bool (string ("true")), true);
155 CHECK_EQ (is_bool (string ("false")), true);
156
157 CHECK_EQ (!is_bool (string ("100")), true);
158 CHECK_EQ (!is_bool (string ("nil")), true);
159}
160
161TEST_CASE ("test is int") {
162 // Empty string is not an int
163 CHECK_EQ (!is_int (string ("")), true);
164
165 // Only 0-9 in chars are int
166 for (auto i= 0; i < 256; i++) {
167 char iter= (char) i;
168 if (iter >= '0' && iter <= '9') CHECK_EQ (is_int (string (iter)), true);
169 else CHECK_EQ (!is_int (string (iter)), true);
170 }
171
172 // Random tests
173 CHECK_EQ (is_int (string ("-100")), true);
174 CHECK_EQ (is_int (string ("+100")), true);
175 CHECK_EQ (is_int (string ("100")), true);
176 CHECK_EQ (!is_int (string (".0")), true);
177 CHECK_EQ (!is_int (string ("0x09")), true);
178}
179
180TEST_CASE ("test is quoted") {
181 CHECK_EQ (is_quoted ("\"\""), true);
182 CHECK_EQ (is_quoted ("\"Hello TeXmacs\""), true);
183 CHECK_EQ (is_quoted ("\"Hello\"TeXmacs\""), true);
184
185 CHECK_EQ (!is_quoted ("\""), true);
186 CHECK_EQ (!is_quoted ("A"), true);
187 CHECK_EQ (!is_quoted ("9"), true);
188 CHECK_EQ (!is_quoted ("Hello TeXmacs"), true);
189 CHECK_EQ (!is_quoted ("\"Hello TeXmac\"s"), true);
190 CHECK_EQ (!is_quoted ("H\"ello TeXmacs\""), true);
191 // is_quoted only checks if a string starts with a double quote
192 // and ends with another double quote, regardless the validity
193 // of the raw string
194}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
SI as_int(double x)
Converts a double to a signed integer, rounding to the nearest integer.
Definition basic.hpp:261
The list class represents a linked list.
Definition list.hpp:48
TEST_CASE("test for operator+= and advance()")
string as_string_bool(bool f)
Definition string.cpp:266
bool as_bool(string s)
Definition string.cpp:201
bool is_int(string s)
Definition string.cpp:326
bool is_bool(string s)
Definition string.cpp:321
bool is_quoted(string s)
Definition string.cpp:370
bool is_empty(string s)
Definition string.cpp:316