unit_tests.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "command_line.h"
  9. #include "dynamic_string.h"
  10. #include "json.h"
  11. #include "macros.h"
  12. #include "math_utils.h"
  13. #include "memory.h"
  14. #include "murmur.h"
  15. #include "path.h"
  16. #include "sjson.h"
  17. #include "string_id.h"
  18. #include "string_utils.h"
  19. #include "temp_allocator.h"
  20. #include "vector.h"
  21. namespace crown
  22. {
  23. static void test_memory()
  24. {
  25. memory_globals::init();
  26. Allocator& a = default_allocator();
  27. void* p = a.allocate(32);
  28. CE_ENSURE(a.allocated_size(p) >= 32);
  29. a.deallocate(p);
  30. memory_globals::shutdown();
  31. }
  32. static void test_array()
  33. {
  34. memory_globals::init();
  35. Allocator& a = default_allocator();
  36. {
  37. Array<int> v(a);
  38. CE_ENSURE(array::size(v) == 0);
  39. array::push_back(v, 1);
  40. CE_ENSURE(array::size(v) == 1);
  41. CE_ENSURE(v[0] == 1);
  42. }
  43. memory_globals::shutdown();
  44. }
  45. static void test_vector()
  46. {
  47. memory_globals::init();
  48. Allocator& a = default_allocator();
  49. {
  50. Vector<int> v(a);
  51. CE_ENSURE(vector::size(v) == 0);
  52. vector::push_back(v, 1);
  53. CE_ENSURE(vector::size(v) == 1);
  54. CE_ENSURE(v[0] == 1);
  55. }
  56. memory_globals::shutdown();
  57. }
  58. static void test_murmur()
  59. {
  60. const u32 m = murmur32("murmur32", 8, 0);
  61. CE_ENSURE(m == 0x7c2365dbu);
  62. const u64 n = murmur64("murmur64", 8, 0);
  63. CE_ENSURE(n == 0x90631502d1a3432bu);
  64. }
  65. static void test_string_id()
  66. {
  67. {
  68. StringId32 a("murmur32");
  69. CE_ENSURE(a._id == 0x7c2365dbu);
  70. StringId32 b("murmur32", 8);
  71. CE_ENSURE(a._id == 0x7c2365dbu);
  72. char buf[128];
  73. a.to_string(buf);
  74. CE_ENSURE(strcmp(buf, "7c2365db") == 0);
  75. }
  76. {
  77. StringId64 a("murmur64");
  78. CE_ENSURE(a._id == 0x90631502d1a3432bu);
  79. StringId64 b("murmur64", 8);
  80. CE_ENSURE(a._id == 0x90631502d1a3432bu);
  81. char buf[128];
  82. a.to_string(buf);
  83. CE_ENSURE(strcmp(buf, "90631502d1a3432b") == 0);
  84. }
  85. }
  86. static void test_json()
  87. {
  88. memory_globals::init();
  89. {
  90. JsonValueType::Enum type = json::type("null");
  91. CE_ENSURE(type == JsonValueType::NIL);
  92. type = json::type("true");
  93. CE_ENSURE(type == JsonValueType::BOOL);
  94. type = json::type("false");
  95. CE_ENSURE(type == JsonValueType::BOOL);
  96. type = json::type("3.14");
  97. CE_ENSURE(type == JsonValueType::NUMBER);
  98. type = json::type("\"foo\"");
  99. CE_ENSURE(type == JsonValueType::STRING);
  100. type = json::type("[]");
  101. CE_ENSURE(type == JsonValueType::ARRAY);
  102. type = json::type("{}");
  103. CE_ENSURE(type == JsonValueType::OBJECT);
  104. const s32 i = json::parse_int("3.14");
  105. CE_ENSURE(i == 3);
  106. const f32 f = json::parse_float("3.14");
  107. CE_ENSURE(fequal(f, 3.14f));
  108. const bool b = json::parse_bool("true");
  109. CE_ENSURE(b == true);
  110. const bool c = json::parse_bool("false");
  111. CE_ENSURE(c == false);
  112. TempAllocator1024 ta;
  113. DynamicString str(ta);
  114. json::parse_string("\"This is JSON\"", str);
  115. CE_ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
  116. }
  117. memory_globals::shutdown();
  118. }
  119. static void test_sjson()
  120. {
  121. memory_globals::init();
  122. {
  123. JsonValueType::Enum type = sjson::type("null");
  124. CE_ENSURE(type == JsonValueType::NIL);
  125. type = sjson::type("true");
  126. CE_ENSURE(type == JsonValueType::BOOL);
  127. type = sjson::type("false");
  128. CE_ENSURE(type == JsonValueType::BOOL);
  129. type = sjson::type("3.14");
  130. CE_ENSURE(type == JsonValueType::NUMBER);
  131. type = sjson::type("\"foo\"");
  132. CE_ENSURE(type == JsonValueType::STRING);
  133. type = sjson::type("[]");
  134. CE_ENSURE(type == JsonValueType::ARRAY);
  135. type = sjson::type("{}");
  136. CE_ENSURE(type == JsonValueType::OBJECT);
  137. const s32 i = sjson::parse_int("3.14");
  138. CE_ENSURE(i == 3);
  139. const f32 f = sjson::parse_float("3.14");
  140. CE_ENSURE(fequal(f, 3.14f));
  141. const bool b = sjson::parse_bool("true");
  142. CE_ENSURE(b == true);
  143. const bool c = sjson::parse_bool("false");
  144. CE_ENSURE(c == false);
  145. TempAllocator1024 ta;
  146. DynamicString str(ta);
  147. sjson::parse_string("\"This is SJSON\"", str);
  148. CE_ENSURE(strcmp(str.c_str(), "This is SJSON") == 0);
  149. }
  150. {
  151. const Vector2 a = sjson::parse_vector2("[ 1.2 -2.5 ]");
  152. CE_ENSURE(fequal(a.x, 1.2f));
  153. CE_ENSURE(fequal(a.y, -2.5f));
  154. const Vector3 b = sjson::parse_vector3("[ 3.1 0.5 -5.7]");
  155. CE_ENSURE(fequal(b.x, 3.1f));
  156. CE_ENSURE(fequal(b.y, 0.5f));
  157. CE_ENSURE(fequal(b.z, -5.7f));
  158. const Vector4 c = sjson::parse_vector4("[ 6.7 -1.3 2.9 -0.4 ]");
  159. CE_ENSURE(fequal(c.x, 6.7f));
  160. CE_ENSURE(fequal(c.y, -1.3f));
  161. CE_ENSURE(fequal(c.z, 2.9f));
  162. CE_ENSURE(fequal(c.w, -0.4f));
  163. const Quaternion d = sjson::parse_quaternion("[ -1.5 -3.4 9.1 -3.5 ]");
  164. CE_ENSURE(fequal(d.x, -1.5f));
  165. CE_ENSURE(fequal(d.y, -3.4f));
  166. CE_ENSURE(fequal(d.z, 9.1f));
  167. CE_ENSURE(fequal(d.w, -3.5f));
  168. const Matrix4x4 e = sjson::parse_matrix4x4(
  169. "["
  170. "-3.2 5.3 -0.7 4.1 "
  171. " 5.6 7.0 -3.2 -1.2 "
  172. "-6.3 9.0 3.9 1.1 "
  173. " 0.4 -7.3 8.9 -0.1 "
  174. "]"
  175. );
  176. CE_ENSURE(fequal(e.x.x, -3.2f));
  177. CE_ENSURE(fequal(e.x.y, 5.3f));
  178. CE_ENSURE(fequal(e.x.z, -0.7f));
  179. CE_ENSURE(fequal(e.x.w, 4.1f));
  180. CE_ENSURE(fequal(e.y.x, 5.6f));
  181. CE_ENSURE(fequal(e.y.y, 7.0f));
  182. CE_ENSURE(fequal(e.y.z, -3.2f));
  183. CE_ENSURE(fequal(e.y.w, -1.2f));
  184. CE_ENSURE(fequal(e.z.x, -6.3f));
  185. CE_ENSURE(fequal(e.z.y, 9.0f));
  186. CE_ENSURE(fequal(e.z.z, 3.9f));
  187. CE_ENSURE(fequal(e.z.w, 1.1f));
  188. CE_ENSURE(fequal(e.t.x, 0.4f));
  189. CE_ENSURE(fequal(e.t.y, -7.3f));
  190. CE_ENSURE(fequal(e.t.z, 8.9f));
  191. CE_ENSURE(fequal(e.t.w, -0.1f));
  192. const StringId32 f = sjson::parse_string_id("\"murmur32\"");
  193. CE_ENSURE(f._id == 0x7c2365dbu);
  194. const ResourceId g = sjson::parse_resource_id("\"murmur64\"");
  195. CE_ENSURE(g._id == 0x90631502d1a3432bu);
  196. }
  197. memory_globals::shutdown();
  198. }
  199. static void test_path()
  200. {
  201. #if CROWN_PLATFORM_POSIX
  202. {
  203. const bool a = path::is_absolute("/home/foo");
  204. CE_ENSURE(a == true);
  205. const bool b = path::is_absolute("home/foo");
  206. CE_ENSURE(b == false);
  207. }
  208. {
  209. const bool a = path::is_relative("/home/foo");
  210. CE_ENSURE(a == false);
  211. const bool b = path::is_relative("home/foo");
  212. CE_ENSURE(b == true);
  213. }
  214. {
  215. const bool a = path::is_root("/");
  216. CE_ENSURE(a == true);
  217. const bool b = path::is_root("/home");
  218. CE_ENSURE(b == false);
  219. }
  220. #else
  221. {
  222. const bool a = path::is_absolute("C:\\Users\\foo");
  223. CE_ENSURE(a == true);
  224. const bool b = path::is_absolute("Users\\foo");
  225. CE_ENSURE(b == false);
  226. }
  227. {
  228. const bool a = path::is_relative("D:\\Users\\foo");
  229. CE_ENSURE(a == false);
  230. const bool b = path::is_relative("Users\\foo");
  231. CE_ENSURE(b == true);
  232. }
  233. {
  234. const bool a = path::is_root("E:\\");
  235. CE_ENSURE(a == true);
  236. const bool b = path::is_root("E:\\Users");
  237. CE_ENSURE(b == false);
  238. }
  239. #endif // CROWN_PLATFORM_POSIX
  240. {
  241. const char* p = path::basename("");
  242. CE_ENSURE(strcmp(p, "") == 0);
  243. const char* q = path::basename("/");
  244. CE_ENSURE(strcmp(q, "") == 0);
  245. const char* r = path::basename("boot.config");
  246. CE_ENSURE(strcmp(r, "boot.config") == 0);
  247. const char* s = path::basename("foo/boot.config");
  248. CE_ENSURE(strcmp(s, "boot.config") == 0);
  249. const char* t = path::basename("/foo/boot.config");
  250. CE_ENSURE(strcmp(t, "boot.config") == 0);
  251. }
  252. {
  253. const char* p = path::extension("");
  254. CE_ENSURE(p == NULL);
  255. const char* q = path::extension("boot");
  256. CE_ENSURE(q == NULL);
  257. const char* r = path::extension("boot.bar.config");
  258. CE_ENSURE(strcmp(r, "config") == 0);
  259. }
  260. }
  261. static void test_command_line()
  262. {
  263. const char* argv[] =
  264. {
  265. "args",
  266. "-s",
  267. "--switch",
  268. "--argument",
  269. "orange"
  270. };
  271. CommandLine cl(CE_COUNTOF(argv), argv);
  272. CE_ENSURE(cl.has_argument("switch", 's'));
  273. const char* orange = cl.get_parameter("argument");
  274. CE_ENSURE(orange != NULL && strcmp(orange, "orange") == 0);
  275. }
  276. static void run_unit_tests()
  277. {
  278. test_memory();
  279. test_array();
  280. test_vector();
  281. test_murmur();
  282. test_string_id();
  283. test_json();
  284. test_sjson();
  285. test_path();
  286. test_command_line();
  287. }
  288. } // namespace crown
  289. #endif // CROWN_BUILD_UNIT_TESTS