Lolly 1.4.28
Loading...
Searching...
No Matches
analyze_test.cpp
Go to the documentation of this file.
1#include "a_lolly_test.hpp"
2#include "analyze.hpp"
3
4TEST_CASE ("is_alpha") {
5 for (unsigned char c= 0; c < 255; c++) {
6 if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
7 CHECK (is_alpha (c));
8 }
9 else {
10 CHECK (!is_alpha (c));
11 }
12 }
13}
14
15TEST_CASE ("is_digit") {
16 for (unsigned char c= 0; c < 255; c++) {
17 if (c >= 48 && c <= 57) {
18 CHECK (is_digit (c));
19 }
20 else {
21 CHECK (!is_digit (c));
22 }
23 }
24}
25
26TEST_CASE ("is_space") {
27 for (unsigned char c= 0; c < 255; c++) {
28 if ((c == 9) || (c == 10) || (c == 13) || (c == 32)) {
29 CHECK (is_space (c));
30 }
31 else {
32 CHECK (!is_space (c));
33 }
34 }
35}
36
37TEST_CASE ("is_binary_digit") {
38 for (unsigned char c= 0; c < 255; c++) {
39 if ((c == '0') || (c == '1')) {
41 }
42 else {
44 }
45 }
46}
47
48TEST_CASE ("is_alpha") {
49 CHECK (is_alpha ("a"));
50 CHECK (is_alpha ("abc"));
51 CHECK (is_alpha ("Hello"));
52 CHECK (!is_alpha ("!"));
53 CHECK (!is_alpha ("abc123"));
54 CHECK (!is_alpha (""));
55}
56
57TEST_CASE ("is_alphanum") {
58 CHECK (is_alphanum ("s3"));
59 CHECK (is_alphanum ("ipv6"));
60 CHECK (is_alphanum ("abc"));
61 CHECK (is_alphanum ("123"));
62 CHECK (!is_alphanum (""));
63 CHECK (!is_alphanum ("!"));
64}
65
66TEST_CASE ("test locase all") {
67 CHECK_EQ (locase_all (string ("true")) == string ("true"), true);
68 CHECK_EQ (locase_all (string ("TRue")) == string ("true"), true);
69 CHECK_EQ (locase_all (string ("TRUE")) == string ("true"), true);
70 CHECK_EQ (locase_all (string ("123TRUE")) == string ("123true"), true);
71}
72
73TEST_CASE ("test upcase all") {
74 CHECK_EQ (upcase_all (string ("true")) == string ("TRUE"), true);
75 CHECK_EQ (upcase_all (string ("TRue")) == string ("TRUE"), true);
76 CHECK_EQ (upcase_all (string ("TRUE")) == string ("TRUE"), true);
77 CHECK_EQ (upcase_all (string ("123true")) == string ("123TRUE"), true);
78}
79
80TEST_CASE ("test string minus") {
81 CHECK_EQ (string_minus ("Hello World", "eo") == string ("Hll Wrld"), true);
82 CHECK_EQ (string_minus ("", "abc") == string (""), true);
83 CHECK_EQ (string_minus ("abc", "") == string ("abc"), true);
84}
85
86TEST_CASE ("test string union") {
87 CHECK_EQ (string_union ("abc", "") == string ("abc"), true);
88 CHECK_EQ (string_union ("", "abc") == string ("abc"), true);
89 CHECK_EQ (string_union ("Hello World", "eo") == string ("Hll Wrldeo"), true);
90}
91
92TEST_CASE ("remove_prefix") {
93 string_eq (remove_prefix ("abc", "a"), "bc");
94 string_eq (remove_prefix ("abc", ""), "abc");
95 string_eq (remove_prefix ("", ""), "");
96 string_eq (remove_prefix ("abc", ""), "abc");
97 string_eq (remove_prefix ("a1a", "a"), "1a");
98}
99
100TEST_CASE ("remove_suffix") {
101 string_eq (remove_suffix ("abc", "c"), "ab");
102 string_eq (remove_suffix ("abc", ""), "abc");
103 string_eq (remove_suffix ("", ""), "");
104 string_eq (remove_suffix ("abc", ""), "abc");
105 string_eq (remove_suffix ("a1a", "a"), "a1");
106}
107
108TEST_CASE ("test_raw_quote") {
109 CHECK_EQ (raw_quote ("a") == "\"a\"", true);
110 CHECK_EQ (raw_quote ("") == "\"\"", true);
111}
112
113TEST_CASE ("test_raw_unquote") {
114 CHECK_EQ (raw_unquote ("\"a\"") == "a", true);
115 CHECK_EQ (raw_unquote ("\"a") == "\"a", true);
116 CHECK_EQ (raw_unquote ("a\"") == "a\"", true);
117 CHECK_EQ (raw_unquote ("") == "", true);
118 CHECK_EQ (raw_unquote ("a") == "a", true);
119}
120
121TEST_CASE ("test_unescape_guile") {
122 CHECK_EQ (unescape_guile ("\\\\") == "\\\\\\\\", true);
123}
124
125TEST_CASE ("test_starts") {
126 CHECK (starts ("abc_def", "abc"));
127 CHECK (!starts ("abc_def", "def"));
128 CHECK (starts ("abc", ""));
129 CHECK (starts ("", ""));
130}
131
132TEST_CASE ("test_ends") {
133 CHECK (ends ("abc_def", "def"));
134 CHECK (ends ("abc_def", ""));
135 CHECK (!ends ("abc_def", "de"));
136}
137
138TEST_CASE ("test_read_word") {
139 string word;
140 int i= 0;
141 CHECK (read_word ("hello123", i, word));
142 CHECK_EQ (word == "hello", true);
143 CHECK_EQ (i, 5);
144
145 i = 0;
146 word= "";
147 CHECK (!read_word ("123", i, word));
148 CHECK (is_empty (word));
149 CHECK_EQ (i, 0);
150}
151
152TEST_CASE ("contains/occurs") {
153 CHECK (contains ("abc", "a"));
154 CHECK (contains ("abc", "ab"));
155 CHECK (contains ("abc", "bc"));
156 CHECK (!contains ("abc", "B"));
157 CHECK (contains ("", ""));
158 CHECK (contains ("abc", ""));
159 CHECK (!contains ("abc", " "));
160 CHECK (contains ("hello world", " "));
161}
162
163TEST_CASE ("replace") {
164 CHECK_EQ (replace ("a-b", "-", "_") == "a_b", true);
165 CHECK_EQ (replace ("a-b-c", "-", "_") == "a_b_c", true);
166}
167
168TEST_CASE ("tokenize") {
169 CHECK_EQ (tokenize ("hello world", " "), array<string> ("hello", "world"));
170 CHECK_EQ (tokenize ("zotero://select/library/items/2AIFJFS7", "://"),
171 array<string> ("zotero", "select/library/items/2AIFJFS7"));
172}
173
174TEST_CASE ("recompose") {
175 string_eq (recompose (array<string> ("hello", "world"), " "), "hello world");
176 string_eq (
177 recompose (array<string> ("zotero", "select/library/items/2AIFJFS7"),
178 "://"),
179 "zotero://select/library/items/2AIFJFS7");
180}
bool starts(string s, const char *what)
Definition analyze.cpp:566
bool is_alphanum(string s)
Definition analyze.cpp:55
string locase_all(string s)
Converts all uppercase characters in a string to lowercase.
Definition analyze.cpp:137
string unescape_guile(string s)
Unescape a Guile-syntax string.
Definition analyze.cpp:515
array< string > tokenize(string s, string sep)
Definition analyze.cpp:948
bool ends(string s, const char *what)
Definition analyze.cpp:576
string string_union(string s1, string s2)
Union of two strings.
Definition analyze.cpp:151
string remove_prefix(string s, string prefix)
Remove the prefix from s if matches.
Definition analyze.cpp:168
string raw_quote(string s)
Add quotes around a string to indicate it's a string, not a symbol.
Definition analyze.cpp:420
bool is_alpha(string s)
Checks if a string contains only alphabetic characters.
Definition analyze.cpp:46
string recompose(array< string > a, string sep)
Definition analyze.cpp:963
string string_minus(string s1, string s2)
Remove characters from one string that are in another string.
Definition analyze.cpp:156
string replace(string s, string what, string by)
Definition analyze.cpp:899
bool read_word(string s, int &i, string &result)
Definition analyze.cpp:681
string upcase_all(string s)
Converts all lowercase characters in a string to uppercase.
Definition analyze.cpp:127
string raw_unquote(string s)
Remove quotes from a string label.
Definition analyze.cpp:426
string remove_suffix(string s, string suffix)
Remove the suffix from s if matches.
Definition analyze.cpp:175
bool is_digit(char c)
Definition analyze.hpp:37
bool is_space(char c)
Definition analyze.hpp:63
bool is_binary_digit(char c)
Definition analyze.hpp:41
bool contains(T a, array< T > b)
Check if an array contains a specified element.
The list class represents a linked list.
Definition list.hpp:48
void string_eq(string left, string right)
TEST_CASE("test for operator+= and advance()")
bool is_empty(string s)
Definition string.cpp:316