Fix last commit
[carla.git] / source / tests.old / CarlaString.cpp
blob878226e73098e654c723f65100a808894a52deb3
1 /*
2 * CarlaString Tests
3 * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaString.hpp"
20 int main()
22 CarlaString str;
24 // empty
25 assert(str.length() == 0);
26 assert(str.length() == std::strlen(""));
27 assert(str.isEmpty());
28 assert(str.contains(""));
29 assert(str.contains("\0"));
30 assert(str.isEmpty() == !str.isNotEmpty());
31 assert(! str.isNotEmpty());
32 assert(! str.contains(" "));
33 assert(! str.isDigit(0));
35 // single number
36 str = "5";
37 assert(str.length() == 1);
38 assert(str.length() == std::strlen("5"));
39 assert(str.isEmpty() == !str.isNotEmpty());
40 assert(str.isNotEmpty());
41 assert(str.contains(""));
42 assert(str.contains("5"));
43 assert(str.isDigit(0));
44 assert(! str.isEmpty());
45 assert(! str.contains("6"));
46 assert(! str.isDigit(1));
48 // single number, using constructor
49 str = CarlaString(5);
50 assert(str.length() == 1);
51 assert(str.length() == std::strlen("5"));
52 assert(str.isNotEmpty());
53 assert(str.contains(""));
54 assert(str.contains("5"));
55 assert(str.isDigit(0));
56 assert(! str.isEmpty());
57 assert(! str.contains("6"));
58 assert(! str.isDigit(1));
60 // decimal number
61 str = CarlaString(51);
62 assert(str.length() == 2);
63 assert(str.length() == std::strlen("51"));
64 assert(str.isNotEmpty());
65 assert(str.contains(""));
66 assert(str.contains("1"));
67 assert(str.contains("51"));
68 assert(str.isDigit(0));
69 assert(str.isDigit(1));
70 assert(! str.isEmpty());
71 assert(! str.contains("6"));
72 assert(! str.isDigit(2));
73 assert(! str.isDigit((size_t)-1)); // test out of bounds
75 // negative number
76 str = CarlaString(-51);
77 assert(str.length() == 3);
78 assert(str.length() == std::strlen("-51"));
79 assert(str.isNotEmpty());
80 assert(str.contains(""));
81 assert(str.contains("-5"));
82 assert(str.contains("51"));
83 assert(str.isDigit(1));
84 assert(str.isDigit(2));
85 assert(! str.isEmpty());
86 assert(! str.contains("6"));
87 assert(! str.isDigit(0));
88 assert(! str.isDigit((size_t)-1)); // test out of bounds
90 // small operations
91 str += "ah";
92 assert(str.length() == 5);
93 assert(str.length() == std::strlen("-51ah"));
94 assert(str.contains("-51ah"));
95 assert(! str.isDigit(3));
97 // hexacimal number
98 unsigned int n = 0x91;
99 str += CarlaString(n, true);
100 assert(str.length() == 9);
101 assert(str.length() == std::strlen("-51ah0x91"));
102 assert(str.contains("-51ah0x91"));
103 assert(! str.isDigit(6));
105 // float number
106 str += CarlaString(0.0102f);
107 assert(str.length() == 17);
108 assert(str.length() == std::strlen("-51ah0x910.010200"));
109 assert(str.contains("-51ah0x91"));
110 assert(! str.isDigit(6));
112 // double number
113 str += CarlaString(7.9642);
114 assert(str.length() == 23);
115 assert(str.length() == std::strlen("-51ah0x910.0102007.9642"));
116 assert(str.contains("7.9642"));
118 // replace
119 str.replace('0', 'k');
120 str.replace('k', 'O');
121 str.replace('O', '0');
122 str.replace('0', '\0'); // shouldn't do anything
124 // truncate
125 str.truncate(11);
126 assert(str.length() == 11);
127 assert(str.length() == std::strlen("-51ah0x910."));
129 // basic
130 str.toBasic();
131 assert(str.length() == 11);
132 assert(str == "_51ah0x910_");
134 // upper
135 str.toUpper();
136 assert(str.length() == 11);
137 assert(str == "_51AH0X910_");
139 // lower
140 str.toLower();
141 assert(str.length() == 11);
142 assert(str == "_51ah0x910_");
144 // random stuff
145 CarlaString str1(1.23);
146 str1 += "_ ?";
148 CarlaString str2("test1");
149 str2 = "test2";
150 str2 += ".0";
152 // startsWith, contains and endsWith
153 CarlaString str3("1.23_ ?test2.0 final");
154 assert(str3.startsWith('1'));
155 assert(str3.startsWith("1"));
156 assert(str3.startsWith("1.23_ "));
157 assert(str3.contains("1"));
158 assert(str3.contains("?test"));
159 assert(str3.contains("final"));
160 assert(! str3.contains("\n"));
161 assert(! str3.contains("\t"));
162 assert(str3.endsWith('l'));
163 assert(str3.endsWith("l"));
164 assert(str3.endsWith(" final"));
166 CarlaString str4 = "" + str1 + str2 + " final";
167 assert(str4.contains(str1));
168 assert(str4.contains(str2));
169 assert(str4.startsWith(str1));
170 assert(! str4.startsWith(str2));
172 // length and content
173 assert(str3 == "1.23_ ?test2.0 final");
174 assert(str3 == str4);
175 assert(str3.length() == str4.length());
176 assert(str3.length() == std::strlen("1.23_ ?test2.0 final"));
178 CarlaString str5 = "ola " + str + " " + CarlaString(6);
179 assert(str5 == "ola _51ah0x910_ 6");
180 assert(str5.length() == std::strlen("ola _51ah0x910_ 6"));
182 // find, rfind
183 bool found;
184 assert(str5.find('o', &found) == 0);
185 assert(found);
186 assert(str5.find('l', &found) == 1);
187 assert(found);
188 assert(str5.find('5', &found) == 5);
189 assert(found);
190 assert(str5.find('6', &found) == str5.length()-1);
191 assert(found);
193 assert(str5.rfind('6', &found) == str5.length()-1);
194 assert(found);
195 assert(str5.rfind(' ', &found) == str5.length()-2);
196 assert(found);
197 assert(str5.rfind('x', &found) == 10);
198 assert(found);
200 assert(str5.find('\0', &found) == str5.length());
201 assert(! found);
202 assert(str5.find('Z', &found) == str5.length());
203 assert(! found);
204 assert(str5.rfind('A', &found) == str5.length());
205 assert(! found);
207 assert(str5.find("o", &found) == 0);
208 assert(found);
209 assert(str5.find("ola", &found) == 0);
210 assert(found);
211 assert(str5.find("la", &found) == 1);
212 assert(found);
213 assert(str5.find(" ", &found) == 3);
214 assert(found);
215 assert(str5.find("6", &found) == str5.length()-1);
216 assert(found);
217 assert(str5.find(" 6", &found) == str5.length()-2);
218 assert(found);
220 assert(str5.rfind("6", &found) == str5.length()-1);
221 assert(found);
222 assert(str5.rfind(" ", &found) == str5.length()-2);
223 assert(found);
224 assert(str5.rfind(" 6", &found) == str5.length()-2);
225 assert(found);
226 assert(str5.rfind("ola", &found) == 0);
227 assert(found);
228 assert(str5.rfind("la ", &found) == 1);
229 assert(found);
231 assert(str5.find("", &found) == str5.length());
232 assert(! found);
233 assert(str5.find("Zoom", &found) == str5.length());
234 assert(! found);
235 assert(str5.rfind("", &found) == str5.length());
236 assert(! found);
237 assert(str5.rfind("haha!", &found) == str5.length());
238 assert(! found);
240 carla_stdout("FINAL: \"%s\"", str5.buffer());
242 // clear
243 str.clear();
244 assert(str.length() == 0);
245 assert(str.length() == std::strlen(""));
246 assert(str == "");
248 return 0;