Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaString.cpp 7.1KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * CarlaString Tests
  3. * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  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.
  9. *
  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.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaString.hpp"
  18. int main()
  19. {
  20. CarlaString str;
  21. // empty
  22. assert(str.length() == 0);
  23. assert(str.length() == std::strlen(""));
  24. assert(str.isEmpty());
  25. assert(str.contains(""));
  26. assert(str.contains("\0"));
  27. assert(str.isEmpty() == !str.isNotEmpty());
  28. assert(! str.isNotEmpty());
  29. assert(! str.contains(" "));
  30. assert(! str.isDigit(0));
  31. // single number
  32. str = "5";
  33. assert(str.length() == 1);
  34. assert(str.length() == std::strlen("5"));
  35. assert(str.isEmpty() == !str.isNotEmpty());
  36. assert(str.isNotEmpty());
  37. assert(str.contains(""));
  38. assert(str.contains("5"));
  39. assert(str.isDigit(0));
  40. assert(! str.isEmpty());
  41. assert(! str.contains("6"));
  42. assert(! str.isDigit(1));
  43. // single number, using constructor
  44. str = CarlaString(5);
  45. assert(str.length() == 1);
  46. assert(str.length() == std::strlen("5"));
  47. assert(str.isNotEmpty());
  48. assert(str.contains(""));
  49. assert(str.contains("5"));
  50. assert(str.isDigit(0));
  51. assert(! str.isEmpty());
  52. assert(! str.contains("6"));
  53. assert(! str.isDigit(1));
  54. // decimal number
  55. str = CarlaString(51);
  56. assert(str.length() == 2);
  57. assert(str.length() == std::strlen("51"));
  58. assert(str.isNotEmpty());
  59. assert(str.contains(""));
  60. assert(str.contains("1"));
  61. assert(str.contains("51"));
  62. assert(str.isDigit(0));
  63. assert(str.isDigit(1));
  64. assert(! str.isEmpty());
  65. assert(! str.contains("6"));
  66. assert(! str.isDigit(2));
  67. assert(! str.isDigit((size_t)-1)); // test out of bounds
  68. // negative number
  69. str = CarlaString(-51);
  70. assert(str.length() == 3);
  71. assert(str.length() == std::strlen("-51"));
  72. assert(str.isNotEmpty());
  73. assert(str.contains(""));
  74. assert(str.contains("-5"));
  75. assert(str.contains("51"));
  76. assert(str.isDigit(1));
  77. assert(str.isDigit(2));
  78. assert(! str.isEmpty());
  79. assert(! str.contains("6"));
  80. assert(! str.isDigit(0));
  81. assert(! str.isDigit((size_t)-1)); // test out of bounds
  82. // small operations
  83. str += "ah";
  84. assert(str.length() == 5);
  85. assert(str.length() == std::strlen("-51ah"));
  86. assert(str.contains("-51ah"));
  87. assert(! str.isDigit(3));
  88. // hexacimal number
  89. unsigned int n = 0x91;
  90. str += CarlaString(n, true);
  91. assert(str.length() == 9);
  92. assert(str.length() == std::strlen("-51ah0x91"));
  93. assert(str.contains("-51ah0x91"));
  94. assert(! str.isDigit(6));
  95. // float number
  96. str += CarlaString(0.0102f);
  97. assert(str.length() == 17);
  98. assert(str.length() == std::strlen("-51ah0x910.010200"));
  99. assert(str.contains("-51ah0x91"));
  100. assert(! str.isDigit(6));
  101. // double number
  102. str += CarlaString(7.9642);
  103. assert(str.length() == 23);
  104. assert(str.length() == std::strlen("-51ah0x910.0102007.9642"));
  105. assert(str.contains("7.9642"));
  106. // replace
  107. str.replace('0', 'k');
  108. str.replace('k', 'O');
  109. str.replace('O', '0');
  110. str.replace('0', '\0'); // shouldn't do anything
  111. // truncate
  112. str.truncate(11);
  113. assert(str.length() == 11);
  114. assert(str.length() == std::strlen("-51ah0x910."));
  115. // basic
  116. str.toBasic();
  117. assert(str.length() == 11);
  118. assert(str == "_51ah0x910_");
  119. // upper
  120. str.toUpper();
  121. assert(str.length() == 11);
  122. assert(str == "_51AH0X910_");
  123. // lower
  124. str.toLower();
  125. assert(str.length() == 11);
  126. assert(str == "_51ah0x910_");
  127. // random stuff
  128. CarlaString str1(1.23);
  129. str1 += "_ ?";
  130. CarlaString str2("test1");
  131. str2 = "test2";
  132. str2 += ".0";
  133. // startsWith, contains and endsWith
  134. CarlaString str3("1.23_ ?test2.0 final");
  135. assert(str3.startsWith('1'));
  136. assert(str3.startsWith("1"));
  137. assert(str3.startsWith("1.23_ "));
  138. assert(str3.contains("1"));
  139. assert(str3.contains("?test"));
  140. assert(str3.contains("final"));
  141. assert(! str3.contains("\n"));
  142. assert(! str3.contains("\t"));
  143. assert(str3.endsWith('l'));
  144. assert(str3.endsWith("l"));
  145. assert(str3.endsWith(" final"));
  146. CarlaString str4 = "" + str1 + str2 + " final";
  147. assert(str4.contains(str1));
  148. assert(str4.contains(str2));
  149. assert(str4.startsWith(str1));
  150. assert(! str4.startsWith(str2));
  151. // length and content
  152. assert(str3 == "1.23_ ?test2.0 final");
  153. assert(str3 == str4);
  154. assert(str3.length() == str4.length());
  155. assert(str3.length() == std::strlen("1.23_ ?test2.0 final"));
  156. CarlaString str5 = "ola " + str + " " + CarlaString(6);
  157. assert(str5 == "ola _51ah0x910_ 6");
  158. assert(str5.length() == std::strlen("ola _51ah0x910_ 6"));
  159. // find, rfind
  160. bool found;
  161. assert(str5.find('o', &found) == 0);
  162. assert(found);
  163. assert(str5.find('l', &found) == 1);
  164. assert(found);
  165. assert(str5.find('5', &found) == 5);
  166. assert(found);
  167. assert(str5.find('6', &found) == str5.length()-1);
  168. assert(found);
  169. assert(str5.rfind('6', &found) == str5.length()-1);
  170. assert(found);
  171. assert(str5.rfind(' ', &found) == str5.length()-2);
  172. assert(found);
  173. assert(str5.rfind('x', &found) == 10);
  174. assert(found);
  175. assert(str5.find('\0', &found) == str5.length());
  176. assert(! found);
  177. assert(str5.find('Z', &found) == str5.length());
  178. assert(! found);
  179. assert(str5.rfind('A', &found) == str5.length());
  180. assert(! found);
  181. assert(str5.find("o", &found) == 0);
  182. assert(found);
  183. assert(str5.find("ola", &found) == 0);
  184. assert(found);
  185. assert(str5.find("la", &found) == 1);
  186. assert(found);
  187. assert(str5.find(" ", &found) == 3);
  188. assert(found);
  189. assert(str5.find("6", &found) == str5.length()-1);
  190. assert(found);
  191. assert(str5.find(" 6", &found) == str5.length()-2);
  192. 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(" 6", &found) == str5.length()-2);
  198. assert(found);
  199. assert(str5.rfind("ola", &found) == 0);
  200. assert(found);
  201. assert(str5.rfind("la ", &found) == 1);
  202. assert(found);
  203. assert(str5.find("", &found) == str5.length());
  204. assert(! found);
  205. assert(str5.find("Zoom", &found) == str5.length());
  206. assert(! found);
  207. assert(str5.rfind("", &found) == str5.length());
  208. assert(! found);
  209. assert(str5.rfind("haha!", &found) == str5.length());
  210. assert(! found);
  211. carla_stdout("FINAL: \"%s\"", str5.buffer());
  212. // clear
  213. str.clear();
  214. assert(str.length() == 0);
  215. assert(str.length() == std::strlen(""));
  216. assert(str == "");
  217. return 0;
  218. }