unit_tests.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_BUILD_UNIT_TESTS
  7. #include "array.h"
  8. #include "dynamic_string.h"
  9. #include "json.h"
  10. #include "math_utils.h"
  11. #include "memory.h"
  12. #include "murmur.h"
  13. #include "path.h"
  14. #include "sjson.h"
  15. #include "string_id.h"
  16. #include "string_utils.h"
  17. #include "temp_allocator.h"
  18. #include "vector.h"
  19. namespace crown
  20. {
  21. static void test_memory()
  22. {
  23. memory_globals::init();
  24. Allocator& a = default_allocator();
  25. void* p = a.allocate(32);
  26. CE_ENSURE(a.allocated_size(p) >= 32);
  27. a.deallocate(p);
  28. memory_globals::shutdown();
  29. }
  30. static void test_array()
  31. {
  32. memory_globals::init();
  33. Allocator& a = default_allocator();
  34. {
  35. Array<int> v(a);
  36. CE_ENSURE(array::size(v) == 0);
  37. array::push_back(v, 1);
  38. CE_ENSURE(array::size(v) == 1);
  39. CE_ENSURE(v[0] == 1);
  40. }
  41. memory_globals::shutdown();
  42. }
  43. static void test_vector()
  44. {
  45. memory_globals::init();
  46. Allocator& a = default_allocator();
  47. {
  48. Vector<int> v(a);
  49. CE_ENSURE(vector::size(v) == 0);
  50. vector::push_back(v, 1);
  51. CE_ENSURE(vector::size(v) == 1);
  52. CE_ENSURE(v[0] == 1);
  53. }
  54. memory_globals::shutdown();
  55. }
  56. static void test_murmur()
  57. {
  58. const u32 m = murmur32("murmur32", 8, 0);
  59. CE_ENSURE(m == 0x7c2365dbu);
  60. const u64 n = murmur64("murmur64", 8, 0);
  61. CE_ENSURE(n == 0x90631502d1a3432bu);
  62. }
  63. static void test_string_id()
  64. {
  65. {
  66. StringId32 a("murmur32");
  67. CE_ENSURE(a._id == 0x7c2365dbu);
  68. StringId32 b("murmur32", 8);
  69. CE_ENSURE(a._id == 0x7c2365dbu);
  70. char buf[128];
  71. a.to_string(buf);
  72. CE_ENSURE(strcmp(buf, "7c2365db") == 0);
  73. }
  74. {
  75. StringId64 a("murmur64");
  76. CE_ENSURE(a._id == 0x90631502d1a3432bu);
  77. StringId64 b("murmur64", 8);
  78. CE_ENSURE(a._id == 0x90631502d1a3432bu);
  79. char buf[128];
  80. a.to_string(buf);
  81. CE_ENSURE(strcmp(buf, "90631502d1a3432b") == 0);
  82. }
  83. }
  84. static void test_json()
  85. {
  86. memory_globals::init();
  87. {
  88. JsonValueType::Enum type = json::type("null");
  89. CE_ENSURE(type == JsonValueType::NIL);
  90. type = json::type("true");
  91. CE_ENSURE(type == JsonValueType::BOOL);
  92. type = json::type("false");
  93. CE_ENSURE(type == JsonValueType::BOOL);
  94. type = json::type("3.14");
  95. CE_ENSURE(type == JsonValueType::NUMBER);
  96. type = json::type("\"foo\"");
  97. CE_ENSURE(type == JsonValueType::STRING);
  98. type = json::type("[]");
  99. CE_ENSURE(type == JsonValueType::ARRAY);
  100. type = json::type("{}");
  101. CE_ENSURE(type == JsonValueType::OBJECT);
  102. const s32 i = json::parse_int("3.14");
  103. CE_ENSURE(i == 3);
  104. const f32 f = json::parse_float("3.14");
  105. CE_ENSURE(fequal(f, 3.14f));
  106. const bool b = json::parse_bool("true");
  107. CE_ENSURE(b == true);
  108. const bool c = json::parse_bool("false");
  109. CE_ENSURE(c == false);
  110. TempAllocator1024 ta;
  111. DynamicString str(ta);
  112. json::parse_string("\"This is JSON\"", str);
  113. CE_ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
  114. }
  115. memory_globals::shutdown();
  116. }
  117. static void test_sjson()
  118. {
  119. memory_globals::init();
  120. {
  121. JsonValueType::Enum type = sjson::type("null");
  122. CE_ENSURE(type == JsonValueType::NIL);
  123. type = sjson::type("true");
  124. CE_ENSURE(type == JsonValueType::BOOL);
  125. type = sjson::type("false");
  126. CE_ENSURE(type == JsonValueType::BOOL);
  127. type = sjson::type("3.14");
  128. CE_ENSURE(type == JsonValueType::NUMBER);
  129. type = sjson::type("\"foo\"");
  130. CE_ENSURE(type == JsonValueType::STRING);
  131. type = sjson::type("[]");
  132. CE_ENSURE(type == JsonValueType::ARRAY);
  133. type = sjson::type("{}");
  134. CE_ENSURE(type == JsonValueType::OBJECT);
  135. const s32 i = sjson::parse_int("3.14");
  136. CE_ENSURE(i == 3);
  137. const f32 f = sjson::parse_float("3.14");
  138. CE_ENSURE(fequal(f, 3.14f));
  139. const bool b = sjson::parse_bool("true");
  140. CE_ENSURE(b == true);
  141. const bool c = sjson::parse_bool("false");
  142. CE_ENSURE(c == false);
  143. TempAllocator1024 ta;
  144. DynamicString str(ta);
  145. sjson::parse_string("\"This is SJSON\"", str);
  146. CE_ENSURE(strcmp(str.c_str(), "This is SJSON") == 0);
  147. }
  148. memory_globals::shutdown();
  149. }
  150. static void test_path()
  151. {
  152. #if CROWN_PLATFORM_POSIX
  153. {
  154. const bool a = path::is_absolute("/home/foo");
  155. CE_ENSURE(a == true);
  156. const bool b = path::is_absolute("home/foo");
  157. CE_ENSURE(b == false);
  158. }
  159. {
  160. const bool a = path::is_relative("/home/foo");
  161. CE_ENSURE(a == false);
  162. const bool b = path::is_relative("home/foo");
  163. CE_ENSURE(b == true);
  164. }
  165. {
  166. const bool a = path::is_root("/");
  167. CE_ENSURE(a == true);
  168. const bool b = path::is_root("/home");
  169. CE_ENSURE(b == false);
  170. }
  171. #else
  172. {
  173. const bool a = path::is_absolute("C:\\Users\\foo");
  174. CE_ENSURE(a == true);
  175. const bool b = path::is_absolute("Users\\foo");
  176. CE_ENSURE(b == false);
  177. }
  178. {
  179. const bool a = path::is_relative("D:\\Users\\foo");
  180. CE_ENSURE(a == false);
  181. const bool b = path::is_relative("Users\\foo");
  182. CE_ENSURE(b == true);
  183. }
  184. {
  185. const bool a = path::is_root("E:\\");
  186. CE_ENSURE(a == true);
  187. const bool b = path::is_root("E:\\Users");
  188. CE_ENSURE(b == false);
  189. }
  190. #endif // CROWN_PLATFORM_POSIX
  191. {
  192. const char* p = path::basename("");
  193. CE_ENSURE(strcmp(p, "") == 0);
  194. const char* q = path::basename("/");
  195. CE_ENSURE(strcmp(q, "") == 0);
  196. const char* r = path::basename("boot.config");
  197. CE_ENSURE(strcmp(r, "boot.config") == 0);
  198. const char* s = path::basename("foo/boot.config");
  199. CE_ENSURE(strcmp(s, "boot.config") == 0);
  200. const char* t = path::basename("/foo/boot.config");
  201. CE_ENSURE(strcmp(t, "boot.config") == 0);
  202. }
  203. {
  204. const char* p = path::extension("");
  205. CE_ENSURE(p == NULL);
  206. const char* q = path::extension("boot");
  207. CE_ENSURE(q == NULL);
  208. const char* r = path::extension("boot.bar.config");
  209. CE_ENSURE(strcmp(r, "config") == 0);
  210. }
  211. }
  212. static void run_unit_tests()
  213. {
  214. test_memory();
  215. test_array();
  216. test_vector();
  217. test_murmur();
  218. test_string_id();
  219. test_json();
  220. test_sjson();
  221. test_path();
  222. }
  223. } // namespace crown
  224. #endif // CROWN_BUILD_UNIT_TESTS