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"
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));
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
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));
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
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
92 assert(str
.length() == 5);
93 assert(str
.length() == std::strlen("-51ah"));
94 assert(str
.contains("-51ah"));
95 assert(! str
.isDigit(3));
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));
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));
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"));
119 str
.replace('0', 'k');
120 str
.replace('k', 'O');
121 str
.replace('O', '0');
122 str
.replace('0', '\0'); // shouldn't do anything
126 assert(str
.length() == 11);
127 assert(str
.length() == std::strlen("-51ah0x910."));
131 assert(str
.length() == 11);
132 assert(str
== "_51ah0x910_");
136 assert(str
.length() == 11);
137 assert(str
== "_51AH0X910_");
141 assert(str
.length() == 11);
142 assert(str
== "_51ah0x910_");
145 CarlaString
str1(1.23);
148 CarlaString
str2("test1");
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"));
184 assert(str5
.find('o', &found
) == 0);
186 assert(str5
.find('l', &found
) == 1);
188 assert(str5
.find('5', &found
) == 5);
190 assert(str5
.find('6', &found
) == str5
.length()-1);
193 assert(str5
.rfind('6', &found
) == str5
.length()-1);
195 assert(str5
.rfind(' ', &found
) == str5
.length()-2);
197 assert(str5
.rfind('x', &found
) == 10);
200 assert(str5
.find('\0', &found
) == str5
.length());
202 assert(str5
.find('Z', &found
) == str5
.length());
204 assert(str5
.rfind('A', &found
) == str5
.length());
207 assert(str5
.find("o", &found
) == 0);
209 assert(str5
.find("ola", &found
) == 0);
211 assert(str5
.find("la", &found
) == 1);
213 assert(str5
.find(" ", &found
) == 3);
215 assert(str5
.find("6", &found
) == str5
.length()-1);
217 assert(str5
.find(" 6", &found
) == str5
.length()-2);
220 assert(str5
.rfind("6", &found
) == str5
.length()-1);
222 assert(str5
.rfind(" ", &found
) == str5
.length()-2);
224 assert(str5
.rfind(" 6", &found
) == str5
.length()-2);
226 assert(str5
.rfind("ola", &found
) == 0);
228 assert(str5
.rfind("la ", &found
) == 1);
231 assert(str5
.find("", &found
) == str5
.length());
233 assert(str5
.find("Zoom", &found
) == str5
.length());
235 assert(str5
.rfind("", &found
) == str5
.length());
237 assert(str5
.rfind("haha!", &found
) == str5
.length());
240 carla_stdout("FINAL: \"%s\"", str5
.buffer());
244 assert(str
.length() == 0);
245 assert(str
.length() == std::strlen(""));