Lolly 1.4.28
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 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/14]

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/14]

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/14]

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/14]

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/14]

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/14]

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/14]

TEST_CASE ( "test as bool" )

Definition at line 71 of file string_test.cpp.

71 {
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}
bool as_bool(string s)
Definition string.cpp:201

◆ TEST_CASE() [8/14]

TEST_CASE ( "as_int" )

Definition at line 82 of file string_test.cpp.

82 {
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}
SI as_int(double x)
Converts a double to a signed integer, rounding to the nearest integer.
Definition basic.hpp:261

◆ TEST_CASE() [9/14]

TEST_CASE ( "is_int" )

Definition at line 109 of file string_test.cpp.

109 {
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}
bool is_int(string s)
Definition string.cpp:326

◆ TEST_CASE() [10/14]

TEST_CASE ( "test as strig bool" )

Definition at line 136 of file string_test.cpp.

136 {
137 CHECK_EQ (as_string_bool (true) == string ("true"), true);
138 CHECK_EQ (as_string_bool (false) == string ("false"), true);
139}
string as_string_bool(bool f)
Definition string.cpp:266

◆ TEST_CASE() [11/14]

TEST_CASE ( "test is empty" )

Definition at line 145 of file string_test.cpp.

145 {
146 CHECK_EQ (is_empty (""), true);
147 CHECK_EQ (!is_empty (" "), true);
148 CHECK_EQ (!is_empty ("nonempty"), true);
149}
bool is_empty(string s)
Definition string.cpp:316

◆ TEST_CASE() [12/14]

TEST_CASE ( "test is bool" )

Definition at line 151 of file string_test.cpp.

151 {
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}
bool is_bool(string s)
Definition string.cpp:321

◆ TEST_CASE() [13/14]

TEST_CASE ( "test is int" )

Definition at line 161 of file string_test.cpp.

161 {
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}

◆ TEST_CASE() [14/14]

TEST_CASE ( "test is quoted" )

Definition at line 180 of file string_test.cpp.

180 {
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}
bool is_quoted(string s)
Definition string.cpp:370