| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444 |
- /*
- * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
- * License: https://github.com/dbartolini/crown/blob/master/LICENSE
- */
- #include "config.h"
- #if CROWN_BUILD_UNIT_TESTS
- #include "core/command_line.h"
- #include "core/containers/array.inl"
- #include "core/containers/hash_map.inl"
- #include "core/containers/hash_set.inl"
- #include "core/containers/vector.inl"
- #include "core/filesystem/path.h"
- #include "core/guid.h"
- #include "core/json/json.h"
- #include "core/json/sjson.h"
- #include "core/math/aabb.inl"
- #include "core/math/color4.inl"
- #include "core/math/constants.h"
- #include "core/math/math.h"
- #include "core/math/matrix3x3.inl"
- #include "core/math/matrix4x4.inl"
- #include "core/math/quaternion.inl"
- #include "core/math/sphere.inl"
- #include "core/math/vector2.inl"
- #include "core/math/vector3.inl"
- #include "core/math/vector4.inl"
- #include "core/memory/memory.inl"
- #include "core/memory/temp_allocator.inl"
- #include "core/murmur.h"
- #include "core/os.h"
- #include "core/process.h"
- #include "core/strings/dynamic_string.inl"
- #include "core/strings/string.inl"
- #include "core/strings/string_id.inl"
- #include "core/thread/thread.h"
- #include "core/time.h"
- #include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
- #define ENSURE(condition) \
- do \
- { \
- if (!(condition)) \
- { \
- printf("Assertion failed: '%s' in %s:%d\n\n" \
- , #condition \
- , __FILE__ \
- , __LINE__ \
- ); \
- exit(EXIT_FAILURE); \
- } \
- } \
- while (0)
- namespace crown
- {
- static void test_memory()
- {
- memory_globals::init();
- Allocator& a = default_allocator();
- void* p = a.allocate(32);
- ENSURE(a.allocated_size(p) >= 32);
- a.deallocate(p);
- memory_globals::shutdown();
- }
- static void test_array()
- {
- memory_globals::init();
- Allocator& a = default_allocator();
- {
- Array<int> v(a);
- ENSURE(array::size(v) == 0);
- array::push_back(v, 1);
- ENSURE(array::size(v) == 1);
- ENSURE(v[0] == 1);
- }
- memory_globals::shutdown();
- }
- static void test_vector()
- {
- memory_globals::init();
- Allocator& a = default_allocator();
- {
- Vector<int> v(a);
- ENSURE(vector::size(v) == 0);
- vector::push_back(v, 1);
- ENSURE(vector::size(v) == 1);
- ENSURE(v[0] == 1);
- }
- memory_globals::shutdown();
- }
- static void test_hash_map()
- {
- memory_globals::init();
- Allocator& a = default_allocator();
- {
- HashMap<s32, s32> m(a);
- ENSURE(hash_map::size(m) == 0);
- ENSURE(hash_map::get(m, 0, 42) == 42);
- ENSURE(!hash_map::has(m, 10));
- for (s32 i = 0; i < 100; ++i)
- hash_map::set(m, i, i*i);
- for (s32 i = 0; i < 100; ++i)
- ENSURE(hash_map::get(m, i, 0) == i*i);
- hash_map::remove(m, 20);
- ENSURE(!hash_map::has(m, 20));
- hash_map::remove(m, 2000);
- ENSURE(!hash_map::has(m, 2000));
- hash_map::remove(m, 50);
- ENSURE(!hash_map::has(m, 50));
- hash_map::clear(m);
- for (s32 i = 0; i < 100; ++i)
- ENSURE(!hash_map::has(m, i));
- }
- {
- HashMap<s32, s32> m(a);
- hash_map_internal::grow(m);
- ENSURE(hash_map::capacity(m) == 16);
- hash_map::set(m, 0, 7);
- hash_map::set(m, 1, 1);
- for (s32 i = 2; i < 150; ++i)
- {
- hash_map::set(m, i, 2);
- ENSURE(hash_map::has(m, 0));
- ENSURE(hash_map::has(m, 1));
- ENSURE(hash_map::has(m, i));
- hash_map::remove(m, i);
- }
- }
- memory_globals::shutdown();
- }
- static void test_hash_set()
- {
- memory_globals::init();
- Allocator& a = default_allocator();
- {
- HashSet<s32> m(a);
- ENSURE(hash_set::size(m) == 0);
- ENSURE(!hash_set::has(m, 10));
- for (s32 i = 0; i < 100; ++i)
- hash_set::insert(m, i*i);
- for (s32 i = 0; i < 100; ++i)
- ENSURE(hash_set::has(m, i*i));
- hash_set::remove(m, 5*5);
- ENSURE(!hash_set::has(m, 5*5));
- hash_set::remove(m, 80*80);
- ENSURE(!hash_set::has(m, 80*80));
- hash_set::remove(m, 40*40);
- ENSURE(!hash_set::has(m, 40*40));
- hash_set::clear(m);
- for (s32 i = 0; i < 100; ++i)
- ENSURE(!hash_set::has(m, i*i));
- }
- memory_globals::shutdown();
- }
- static void test_vector2()
- {
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const Vector2 c = a - b;
- ENSURE(fequal(c.x, -1.5f, 0.0001f));
- ENSURE(fequal(c.y, 6.1f, 0.0001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const Vector2 c = a + b;
- ENSURE(fequal(c.x, 3.9f, 0.0001f));
- ENSURE(fequal(c.y, 2.3f, 0.0001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = a * 2.0f;
- ENSURE(fequal(b.x, 2.4f, 0.0001f));
- ENSURE(fequal(b.y, 8.4f, 0.0001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const f32 c = dot(a, b);
- ENSURE(fequal(c, -4.74f, 0.0001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const f32 c = length_squared(a);
- ENSURE(fequal(c, 19.08f, 0.0001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const f32 c = length(a);
- ENSURE(fequal(c, 4.36806f, 0.0001f));
- }
- {
- Vector2 a = vector2(1.2f, 4.2f);
- normalize(a);
- ENSURE(fequal(length(a), 1.0f, 0.00001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const float c = distance_squared(a, b);
- ENSURE(fequal(c, 39.46f, 0.00001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const float c = distance(a, b);
- ENSURE(fequal(c, 6.28171f, 0.00001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const Vector2 c = max(a, b);
- ENSURE(fequal(c.x, 2.7f, 0.00001f));
- ENSURE(fequal(c.y, 4.2f, 0.00001f));
- }
- {
- const Vector2 a = vector2(1.2f, 4.2f);
- const Vector2 b = vector2(2.7f, -1.9f);
- const Vector2 c = min(a, b);
- ENSURE(fequal(c.x, 1.2f, 0.00001f));
- ENSURE(fequal(c.y, -1.9f, 0.00001f));
- }
- }
- static void test_vector3()
- {
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const Vector3 c = a - b;
- ENSURE(fequal(c.x, -1.5f, 0.0001f));
- ENSURE(fequal(c.y, 6.1f, 0.0001f));
- ENSURE(fequal(c.z, 1.8f, 0.0001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const Vector3 c = a + b;
- ENSURE(fequal(c.x, 3.9f, 0.0001f));
- ENSURE(fequal(c.y, 2.3f, 0.0001f));
- ENSURE(fequal(c.z, -6.4f, 0.0001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = a * 2.0f;
- ENSURE(fequal(b.x, 2.4f, 0.0001f));
- ENSURE(fequal(b.y, 8.4f, 0.0001f));
- ENSURE(fequal(b.z, -4.6f, 0.0001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const f32 c = dot(a, b);
- ENSURE(fequal(c, 4.69f, 0.0001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const Vector3 c = cross(a, b);
- ENSURE(fequal(c.x, -21.59f, 0.0001f));
- ENSURE(fequal(c.y, -1.29f, 0.0001f));
- ENSURE(fequal(c.z, -13.62f, 0.0001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const f32 c = length_squared(a);
- ENSURE(fequal(c, 24.37f, 0.0001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const f32 c = length(a);
- ENSURE(fequal(c, 4.93659f, 0.0001f));
- }
- {
- Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- normalize(a);
- ENSURE(fequal(length(a), 1.0f, 0.00001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const float c = distance_squared(a, b);
- ENSURE(fequal(c, 42.70f, 0.00001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const float c = distance(a, b);
- ENSURE(fequal(c, 6.53452f, 0.00001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const Vector3 c = max(a, b);
- ENSURE(fequal(c.x, 2.7f, 0.00001f));
- ENSURE(fequal(c.y, 4.2f, 0.00001f));
- ENSURE(fequal(c.z, -2.3f, 0.00001f));
- }
- {
- const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
- const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
- const Vector3 c = min(a, b);
- ENSURE(fequal(c.x, 1.2f, 0.00001f));
- ENSURE(fequal(c.y, -1.9f, 0.00001f));
- ENSURE(fequal(c.z, -4.1f, 0.00001f));
- }
- }
- static void test_vector4()
- {
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const Vector4 c = a - b;
- ENSURE(fequal(c.x, -1.5f, 0.0001f));
- ENSURE(fequal(c.y, 6.1f, 0.0001f));
- ENSURE(fequal(c.z, 1.8f, 0.0001f));
- ENSURE(fequal(c.w, 4.5f, 0.0001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const Vector4 c = a + b;
- ENSURE(fequal(c.x, 3.9f, 0.0001f));
- ENSURE(fequal(c.y, 2.3f, 0.0001f));
- ENSURE(fequal(c.z, -6.4f, 0.0001f));
- ENSURE(fequal(c.w, 6.5f, 0.0001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 1.5f);
- const Vector4 b = a * 2.0f;
- ENSURE(fequal(b.x, 2.4f, 0.0001f));
- ENSURE(fequal(b.y, 8.4f, 0.0001f));
- ENSURE(fequal(b.z, -4.6f, 0.0001f));
- ENSURE(fequal(b.w, 3.0f, 0.0001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const f32 c = dot(a, b);
- ENSURE(fequal(c, 10.19f, 0.0001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const f32 c = length_squared(a);
- ENSURE(fequal(c, 54.62f, 0.0001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const f32 c = length(a);
- ENSURE(fequal(c, 7.39053f, 0.0001f));
- }
- {
- Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- normalize(a);
- ENSURE(fequal(length(a), 1.0f, 0.00001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const float c = distance_squared(a, b);
- ENSURE(fequal(c, 62.95f, 0.00001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const float c = distance(a, b);
- ENSURE(fequal(c, 7.93410f, 0.00001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const Vector4 c = max(a, b);
- ENSURE(fequal(c.x, 2.7f, 0.00001f));
- ENSURE(fequal(c.y, 4.2f, 0.00001f));
- ENSURE(fequal(c.z, -2.3f, 0.00001f));
- ENSURE(fequal(c.w, 5.5f, 0.00001f));
- }
- {
- const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
- const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
- const Vector4 c = min(a, b);
- ENSURE(fequal(c.x, 1.2f, 0.00001f));
- ENSURE(fequal(c.y, -1.9f, 0.00001f));
- ENSURE(fequal(c.z, -4.1f, 0.00001f));
- ENSURE(fequal(c.w, 1.0f, 0.00001f));
- }
- }
- static void test_quaternion()
- {
- {
- const Quaternion a = from_elements(0.0f, 0.0f, 0.0f, 1.0f);
- ENSURE(fequal(a.x, 0.0f, 0.00001f));
- ENSURE(fequal(a.y, 0.0f, 0.00001f));
- ENSURE(fequal(a.z, 0.0f, 0.00001f));
- ENSURE(fequal(a.w, 1.0f, 0.00001f));
- }
- }
- static void test_color4()
- {
- {
- const Color4 a = color4(1.3f, 2.6f, 0.2f, 0.6f);
- ENSURE(fequal(a.x, 1.3f, 0.00001f));
- ENSURE(fequal(a.y, 2.6f, 0.00001f));
- ENSURE(fequal(a.z, 0.2f, 0.00001f));
- ENSURE(fequal(a.w, 0.6f, 0.00001f));
- }
- {
- const Color4 a = from_rgba(63, 231, 12, 98);
- ENSURE(fequal(a.x, 0.24705f, 0.00001f));
- ENSURE(fequal(a.y, 0.90588f, 0.00001f));
- ENSURE(fequal(a.z, 0.04705f, 0.00001f));
- ENSURE(fequal(a.w, 0.38431f, 0.00001f));
- }
- {
- const Color4 a = from_rgb(63, 231, 12);
- ENSURE(fequal(a.x, 0.24705f, 0.00001f));
- ENSURE(fequal(a.y, 0.90588f, 0.00001f));
- ENSURE(fequal(a.z, 0.04705f, 0.00001f));
- ENSURE(fequal(a.w, 1.0f , 0.00001f));
- }
- {
- const Color4 a = from_rgba(0x3fe70c62);
- ENSURE(fequal(a.x, 0.24705f, 0.00001f));
- ENSURE(fequal(a.y, 0.90588f, 0.00001f));
- ENSURE(fequal(a.z, 0.04705f, 0.00001f));
- ENSURE(fequal(a.w, 0.38431f, 0.00001f));
- }
- {
- const Color4 a = from_rgba(63, 231, 12, 98);
- const u32 rgba = to_rgba(a);
- ENSURE(rgba == 0x3fe70c62);
- const u32 rgb = to_rgb(a);
- ENSURE(rgb == 0x3fe70cff);
- const u32 bgr = to_bgr(a);
- ENSURE(bgr == 0xff0ce73f);
- const u32 abgr = to_abgr(a);
- ENSURE(abgr == 0x620ce73f);
- }
- }
- static void test_matrix3x3()
- {
- {
- const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
- , 2.2f, -5.1f, 1.1f
- , 3.2f, 3.3f, -3.8f
- );
- const Matrix3x3 b = from_elements(3.2f, 4.8f, 6.0f
- , -1.6f, -7.1f, -2.4f
- , -3.1f, -2.2f, 8.9f
- );
- const Matrix3x3 c = a + b;
- ENSURE(fequal(c.x.x, 4.4f, 0.00001f));
- ENSURE(fequal(c.x.y, 2.5f, 0.00001f));
- ENSURE(fequal(c.x.z, 11.1f, 0.00001f));
- ENSURE(fequal(c.y.x, 0.6f, 0.00001f));
- ENSURE(fequal(c.y.y, -12.2f, 0.00001f));
- ENSURE(fequal(c.y.z, -1.3f, 0.00001f));
- ENSURE(fequal(c.z.x, 0.1f, 0.00001f));
- ENSURE(fequal(c.z.y, 1.1f, 0.00001f));
- ENSURE(fequal(c.z.z, 5.1f, 0.00001f));
- }
- {
- const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
- , 2.2f, -5.1f, 1.1f
- , 3.2f, 3.3f, -3.8f
- );
- const Matrix3x3 b = from_elements(3.2f, 4.8f, 6.0f
- , -1.6f, -7.1f, -2.4f
- , -3.1f, -2.2f, 8.9f
- );
- const Matrix3x3 c = a - b;
- ENSURE(fequal(c.x.x, -2.0f, 0.00001f));
- ENSURE(fequal(c.x.y, -7.1f, 0.00001f));
- ENSURE(fequal(c.x.z, -0.9f, 0.00001f));
- ENSURE(fequal(c.y.x, 3.8f, 0.00001f));
- ENSURE(fequal(c.y.y, 2.0f, 0.00001f));
- ENSURE(fequal(c.y.z, 3.5f, 0.00001f));
- ENSURE(fequal(c.z.x, 6.3f, 0.00001f));
- ENSURE(fequal(c.z.y, 5.5f, 0.00001f));
- ENSURE(fequal(c.z.z, -12.7f, 0.00001f));
- }
- {
- const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
- , 2.2f, -5.1f, 1.1f
- , 3.2f, 3.3f, -3.8f
- );
- const Matrix3x3 b = from_elements(3.2f, 4.8f, 6.0f
- , -1.6f, -7.1f, -2.4f
- , -3.1f, -2.2f, 8.9f
- );
- const Matrix3x3 c = a * b;
- ENSURE(fequal(c.x.x, -8.29f, 0.00001f));
- ENSURE(fequal(c.x.y, 10.87f, 0.00001f));
- ENSURE(fequal(c.x.z, 58.11f, 0.00001f));
- ENSURE(fequal(c.y.x, 11.79f, 0.00001f));
- ENSURE(fequal(c.y.y, 44.35f, 0.00001f));
- ENSURE(fequal(c.y.z, 35.23f, 0.00001f));
- ENSURE(fequal(c.z.x, 16.74f, 0.00001f));
- ENSURE(fequal(c.z.y, 0.29f, 0.00001f));
- ENSURE(fequal(c.z.z, -22.54f, 0.00001f));
- }
- {
- const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
- , 2.2f, -5.1f, 1.1f
- , 3.2f, 3.3f, -3.8f
- );
- const Matrix3x3 b = get_inverted(a);
- ENSURE(fequal(b.x.x, 0.140833f, 0.00001f));
- ENSURE(fequal(b.x.y, 0.072339f, 0.00001f));
- ENSURE(fequal(b.x.z, 0.209954f, 0.00001f));
- ENSURE(fequal(b.y.x, 0.106228f, 0.00001f));
- ENSURE(fequal(b.y.y, -0.186705f, 0.00001f));
- ENSURE(fequal(b.y.z, 0.088524f, 0.00001f));
- ENSURE(fequal(b.z.x, 0.210848f, 0.00001f));
- ENSURE(fequal(b.z.y, -0.101221f, 0.00001f));
- ENSURE(fequal(b.z.z, -0.009478f, 0.00001f));
- }
- {
- const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
- , 2.2f, -5.1f, 1.1f
- , 3.2f, 3.3f, -3.8f
- );
- const Matrix3x3 b = get_transposed(a);
- ENSURE(fequal(b.x.x, 1.2f, 0.00001f));
- ENSURE(fequal(b.x.y, 2.2f, 0.00001f));
- ENSURE(fequal(b.x.z, 3.2f, 0.00001f));
- ENSURE(fequal(b.y.x, -2.3f, 0.00001f));
- ENSURE(fequal(b.y.y, -5.1f, 0.00001f));
- ENSURE(fequal(b.y.z, 3.3f, 0.00001f));
- ENSURE(fequal(b.z.x, 5.1f, 0.00001f));
- ENSURE(fequal(b.z.y, 1.1f, 0.00001f));
- ENSURE(fequal(b.z.z, -3.8f, 0.00001f));
- }
- }
- static void test_matrix4x4()
- {
- {
- const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
- , 2.2f, -5.1f, 1.1f, -7.4f
- , 3.2f, 3.3f, -3.8f, -9.2f
- , -6.8f, -2.9f, 1.0f, 4.9f
- );
- const Matrix4x4 b = from_elements(3.2f, 4.8f, 6.0f, 5.3f
- , -1.6f, -7.1f, -2.4f, -6.2f
- , -3.1f, -2.2f, 8.9f, 8.3f
- , 3.8f, 9.1f, -3.1f, -7.1f
- );
- const Matrix4x4 c = a + b;
- ENSURE(fequal(c.x.x, 4.4f, 0.00001f));
- ENSURE(fequal(c.x.y, 2.5f, 0.00001f));
- ENSURE(fequal(c.x.z, 11.1f, 0.00001f));
- ENSURE(fequal(c.x.w, 4.1f, 0.00001f));
- ENSURE(fequal(c.y.x, 0.6f, 0.00001f));
- ENSURE(fequal(c.y.y, -12.2f, 0.00001f));
- ENSURE(fequal(c.y.z, -1.3f, 0.00001f));
- ENSURE(fequal(c.y.w, -13.6f, 0.00001f));
- ENSURE(fequal(c.z.x, 0.1f, 0.00001f));
- ENSURE(fequal(c.z.y, 1.1f, 0.00001f));
- ENSURE(fequal(c.z.z, 5.1f, 0.00001f));
- ENSURE(fequal(c.z.w, -0.9f, 0.00001f));
- ENSURE(fequal(c.t.x, -3.0f, 0.00001f));
- ENSURE(fequal(c.t.y, 6.2f, 0.00001f));
- ENSURE(fequal(c.t.z, -2.1f, 0.00001f));
- ENSURE(fequal(c.t.w, -2.2f, 0.00001f));
- }
- {
- const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
- , 2.2f, -5.1f, 1.1f, -7.4f
- , 3.2f, 3.3f, -3.8f, -9.2f
- , -6.8f, -2.9f, 1.0f, 4.9f
- );
- const Matrix4x4 b = from_elements(3.2f, 4.8f, 6.0f, 5.3f
- , -1.6f, -7.1f, -2.4f, -6.2f
- , -3.1f, -2.2f, 8.9f, 8.3f
- , 3.8f, 9.1f, -3.1f, -7.1f
- );
- const Matrix4x4 c = a - b;
- ENSURE(fequal(c.x.x, -2.0f, 0.00001f));
- ENSURE(fequal(c.x.y, -7.1f, 0.00001f));
- ENSURE(fequal(c.x.z, -0.9f, 0.00001f));
- ENSURE(fequal(c.x.w, -6.5f, 0.00001f));
- ENSURE(fequal(c.y.x, 3.8f, 0.00001f));
- ENSURE(fequal(c.y.y, 2.0f, 0.00001f));
- ENSURE(fequal(c.y.z, 3.5f, 0.00001f));
- ENSURE(fequal(c.y.w, -1.2f, 0.00001f));
- ENSURE(fequal(c.z.x, 6.3f, 0.00001f));
- ENSURE(fequal(c.z.y, 5.5f, 0.00001f));
- ENSURE(fequal(c.z.z, -12.7f, 0.00001f));
- ENSURE(fequal(c.z.w, -17.5f, 0.00001f));
- ENSURE(fequal(c.t.x, -10.6f, 0.00001f));
- ENSURE(fequal(c.t.y, -12.0f, 0.00001f));
- ENSURE(fequal(c.t.z, 4.1f, 0.00001f));
- ENSURE(fequal(c.t.w, 12.0f, 0.00001f));
- }
- {
- const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
- , 2.2f, -5.1f, 1.1f, -7.4f
- , 3.2f, 3.3f, -3.8f, -9.2f
- , -6.8f, -2.9f, 1.0f, 4.9f
- );
- const Matrix4x4 b = from_elements(3.2f, 4.8f, 6.0f, 5.3f
- , -1.6f, -7.1f, -2.4f, -6.2f
- , -3.1f, -2.2f, 8.9f, 8.3f
- , 3.8f, 9.1f, -3.1f, -7.1f
- );
- const Matrix4x4 c = a * b;
- ENSURE(fequal(c.x.x, -12.85f, 0.00001f));
- ENSURE(fequal(c.x.y, -0.05f, 0.00001f));
- ENSURE(fequal(c.x.z, 61.83f, 0.00001f));
- ENSURE(fequal(c.x.w, 71.47f, 0.00001f));
- ENSURE(fequal(c.y.x, -16.33f, 0.00001f));
- ENSURE(fequal(c.y.y, -22.99f, 0.00001f));
- ENSURE(fequal(c.y.z, 58.17f, 0.00001f));
- ENSURE(fequal(c.y.w, 104.95f, 0.00001f));
- ENSURE(fequal(c.z.x, -18.22f, 0.00001f));
- ENSURE(fequal(c.z.y, -83.43f, 0.00001f));
- ENSURE(fequal(c.z.z, 5.98f, 0.00001f));
- ENSURE(fequal(c.z.w, 30.28f, 0.00001f));
- ENSURE(fequal(c.t.x, -1.60f, 0.00001f));
- ENSURE(fequal(c.t.y, 30.34f, 0.00001f));
- ENSURE(fequal(c.t.z, -40.13f, 0.00001f));
- ENSURE(fequal(c.t.w, -44.55f, 0.00001f));
- }
- {
- const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
- , 2.2f, -5.1f, 1.1f, -7.4f
- , 3.2f, 3.3f, -3.8f, -9.2f
- , -6.8f, -2.9f, 1.0f, 4.9f
- );
- const Matrix4x4 b = get_inverted(a);
- ENSURE(fequal(b.x.x, -0.08464f, 0.00001f));
- ENSURE(fequal(b.x.y, 0.06129f, 0.00001f));
- ENSURE(fequal(b.x.z, -0.15210f, 0.00001f));
- ENSURE(fequal(b.x.w, -0.21374f, 0.00001f));
- ENSURE(fequal(b.y.x, 0.14384f, 0.00001f));
- ENSURE(fequal(b.y.y, -0.18486f, 0.00001f));
- ENSURE(fequal(b.y.z, 0.14892f, 0.00001f));
- ENSURE(fequal(b.y.w, 0.03565f, 0.00001f));
- ENSURE(fequal(b.z.x, 0.26073f, 0.00001f));
- ENSURE(fequal(b.z.y, -0.09877f, 0.00001f));
- ENSURE(fequal(b.z.z, 0.07063f, 0.00001f));
- ENSURE(fequal(b.z.w, 0.04729f, 0.00001f));
- ENSURE(fequal(b.t.x, -0.08553f, 0.00001f));
- ENSURE(fequal(b.t.y, -0.00419f, 0.00001f));
- ENSURE(fequal(b.t.z, -0.13735f, 0.00001f));
- ENSURE(fequal(b.t.w, -0.08108f, 0.00001f));
- }
- {
- const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
- , 2.2f, -5.1f, 1.1f, -7.4f
- , 3.2f, 3.3f, -3.8f, -9.2f
- , -6.8f, -2.9f, 1.0f, 4.9f
- );
- const Matrix4x4 b = get_transposed(a);
- ENSURE(fequal(b.x.x, 1.2f, 0.00001f));
- ENSURE(fequal(b.x.y, 2.2f, 0.00001f));
- ENSURE(fequal(b.x.z, 3.2f, 0.00001f));
- ENSURE(fequal(b.x.w, -6.8f, 0.00001f));
- ENSURE(fequal(b.y.x, -2.3f, 0.00001f));
- ENSURE(fequal(b.y.y, -5.1f, 0.00001f));
- ENSURE(fequal(b.y.z, 3.3f, 0.00001f));
- ENSURE(fequal(b.y.w, -2.9f, 0.00001f));
- ENSURE(fequal(b.z.x, 5.1f, 0.00001f));
- ENSURE(fequal(b.z.y, 1.1f, 0.00001f));
- ENSURE(fequal(b.z.z, -3.8f, 0.00001f));
- ENSURE(fequal(b.z.w, 1.0f, 0.00001f));
- ENSURE(fequal(b.t.x, -1.2f, 0.00001f));
- ENSURE(fequal(b.t.y, -7.4f, 0.00001f));
- ENSURE(fequal(b.t.z, -9.2f, 0.00001f));
- ENSURE(fequal(b.t.w, 4.9f, 0.00001f));
- }
- }
- static void test_aabb()
- {
- {
- AABB a;
- aabb::reset(a);
- ENSURE(a.min == VECTOR3_ZERO);
- ENSURE(a.max == VECTOR3_ZERO);
- }
- {
- AABB a;
- a.min = vector3(-2.3f, 1.2f, -4.5f);
- a.max = vector3( 3.7f, 5.3f, -2.9f);
- const Vector3 c = aabb::center(a);
- ENSURE(fequal(c.x, 0.70f, 0.00001f));
- ENSURE(fequal(c.y, 3.25f, 0.00001f));
- ENSURE(fequal(c.z, -3.70f, 0.00001f));
- }
- {
- AABB a;
- a.min = vector3(-2.3f, 1.2f, -4.5f);
- a.max = vector3( 3.7f, 5.3f, -2.9f);
- const float c = aabb::volume(a);
- ENSURE(fequal(c, 39.36f, 0.00001f));
- }
- {
- const Vector3 points[] =
- {
- { -1.2f, 3.4f, 5.5f },
- { 8.2f, -2.4f, -1.5f },
- { -5.9f, 9.2f, 6.0f }
- };
- AABB a;
- aabb::from_points(a, countof(points), points);
- ENSURE(fequal(a.min.x, -5.9f, 0.00001f));
- ENSURE(fequal(a.min.y, -2.4f, 0.00001f));
- ENSURE(fequal(a.min.z, -1.5f, 0.00001f));
- ENSURE(fequal(a.max.x, 8.2f, 0.00001f));
- ENSURE(fequal(a.max.y, 9.2f, 0.00001f));
- ENSURE(fequal(a.max.z, 6.0f, 0.00001f));
- }
- {
- const Vector3 points[] =
- {
- { -1.2f, 3.4f, 5.5f },
- { 8.2f, -2.4f, -1.5f },
- { -5.9f, 9.2f, 6.0f },
- { -2.8f, -3.5f, 1.9f },
- { -8.3f, -3.1f, 1.9f },
- { 4.0f, -3.9f, -1.4f },
- { -0.4f, -1.8f, -2.2f },
- { -8.6f, -4.8f, 2.8f },
- { 4.1f, 4.7f, -0.4f }
- };
- AABB boxes[3];
- aabb::from_points(boxes[0], countof(points)/3, &points[0]);
- aabb::from_points(boxes[1], countof(points)/3, &points[3]);
- aabb::from_points(boxes[2], countof(points)/3, &points[6]);
- AABB d;
- aabb::from_boxes(d, countof(boxes), boxes);
- ENSURE(fequal(d.min.x, -8.6f, 0.00001f));
- ENSURE(fequal(d.min.y, -4.8f, 0.00001f));
- ENSURE(fequal(d.min.z, -2.2f, 0.00001f));
- ENSURE(fequal(d.max.x, 8.2f, 0.00001f));
- ENSURE(fequal(d.max.y, 9.2f, 0.00001f));
- ENSURE(fequal(d.max.z, 6.0f, 0.00001f));
- }
- {
- AABB a;
- a.min = vector3(-2.3f, 1.2f, -4.5f);
- a.max = vector3( 3.7f, 5.3f, -2.9f);
- ENSURE( aabb::contains_point(a, vector3(1.2f, 3.0f, -4.4f)));
- ENSURE(!aabb::contains_point(a, vector3(3.8f, 3.0f, -4.4f)));
- ENSURE(!aabb::contains_point(a, vector3(1.2f, -1.0f, -4.4f)));
- ENSURE(!aabb::contains_point(a, vector3(1.2f, 3.0f, -4.6f)));
- }
- }
- static void test_sphere()
- {
- {
- Sphere a;
- sphere::reset(a);
- ENSURE(a.c == VECTOR3_ZERO);
- ENSURE(fequal(a.r, 0.0f, 0.00001f));
- }
- {
- Sphere a;
- a.c = VECTOR3_ZERO;
- a.r = 1.61f;
- const float b = sphere::volume(a);
- ENSURE(fequal(b, 17.48099f, 0.00001f));
- }
- {
- Sphere a;
- sphere::reset(a);
- const Vector3 points[] =
- {
- { -1.2f, 3.4f, 5.5f },
- { 8.2f, -2.4f, -1.5f },
- { -5.9f, 9.2f, 6.0f }
- };
- sphere::add_points(a, countof(points), points);
- ENSURE(fequal(a.c.x, 0.0f, 0.00001f));
- ENSURE(fequal(a.c.y, 0.0f, 0.00001f));
- ENSURE(fequal(a.c.z, 0.0f, 0.00001f));
- ENSURE(fequal(a.r, 12.46795f, 0.00001f));
- }
- {
- Sphere spheres[3];
- sphere::reset(spheres[0]);
- sphere::reset(spheres[1]);
- sphere::reset(spheres[2]);
- const Vector3 points[] =
- {
- { 6.6f, 3.5f, -5.7f },
- { -5.3f, -9.1f, -7.9f },
- { -1.5f, 4.4f, -5.8f },
- { 7.2f, -2.4f, -9.5f },
- { 4.0f, -8.1f, 6.6f },
- { -8.2f, 2.2f, 4.6f },
- { 2.9f, -4.8f, -6.8f },
- { -7.6f, -7.0f, 0.8f },
- { 8.2f, 2.8f, -4.8f }
- };
- sphere::add_points(spheres[0], countof(points)/3, &points[0]);
- sphere::add_points(spheres[1], countof(points)/3, &points[3]);
- sphere::add_points(spheres[2], countof(points)/3, &points[6]);
- Sphere d;
- sphere::reset(d);
- sphere::add_spheres(d, countof(spheres), spheres);
- ENSURE(fequal(d.r, 13.16472f, 0.00001f));
- }
- {
- Sphere a;
- a.c = vector3(-2.3f, 1.2f, -4.5f);
- a.r = 1.0f;
- ENSURE( sphere::contains_point(a, vector3(-2.9f, 1.6f, -4.0f)));
- ENSURE(!sphere::contains_point(a, vector3(-3.9f, 1.6f, -4.0f)));
- ENSURE(!sphere::contains_point(a, vector3(-2.9f, 2.6f, -4.0f)));
- ENSURE(!sphere::contains_point(a, vector3(-2.9f, 1.6f, -6.0f)));
- }
- }
- static void test_murmur()
- {
- const u32 m = murmur32("murmur32", 8, 0);
- ENSURE(m == 0x7c2365dbu);
- const u64 n = murmur64("murmur64", 8, 0);
- ENSURE(n == 0x90631502d1a3432bu);
- }
- static void test_string_id()
- {
- memory_globals::init();
- {
- StringId32 a("murmur32");
- ENSURE(a._id == 0x7c2365dbu);
- StringId32 b("murmur32", 8);
- ENSURE(a._id == 0x7c2365dbu);
- char str[9];
- a.to_string(str, sizeof(str));
- ENSURE(strcmp(str, "7c2365db") == 0);
- }
- {
- StringId64 a("murmur64");
- ENSURE(a._id == 0x90631502d1a3432bu);
- StringId64 b("murmur64", 8);
- ENSURE(a._id == 0x90631502d1a3432bu);
- char str[17];
- a.to_string(str, sizeof(str));
- ENSURE(strcmp(str, "90631502d1a3432b") == 0);
- }
- {
- StringId32 id(0x2dd65fa6u);
- char str[9];
- id.to_string(str, sizeof(str));
- StringId32 parsed;
- parsed.parse(str);
- ENSURE(id == parsed);
- }
- {
- StringId64 id(0xa73491922dd65fa6u);
- char str[17];
- id.to_string(str, sizeof(str));
- StringId64 parsed;
- parsed.parse(str);
- ENSURE(id == parsed);
- }
- memory_globals::shutdown();
- }
- static void test_dynamic_string()
- {
- memory_globals::init();
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- ENSURE(str.empty());
- str.set("murmur32", 8);
- ENSURE(str.length() == 8);
- const StringId32 id = str.to_string_id();
- ENSURE(id._id == 0x7c2365dbu);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- str += "Test ";
- str += "string.";
- ENSURE(strcmp(str.c_str(), "Test string.") == 0);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- str.set(" \tSushi\t ", 13);
- str.ltrim();
- ENSURE(strcmp(str.c_str(), "Sushi\t ") == 0);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- str.set(" \tSushi\t ", 13);
- str.rtrim();
- ENSURE(strcmp(str.c_str(), " \tSushi") == 0);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- str.set(" \tSushi\t ", 13);
- str.trim();
- ENSURE(strcmp(str.c_str(), "Sushi") == 0);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- str.set("Hello everyone!", 15);
- ENSURE( str.has_prefix("Hello"));
- ENSURE(!str.has_prefix("Helloo"));
- ENSURE( str.has_suffix("one!"));
- ENSURE(!str.has_suffix("one"));
- ENSURE(!str.has_prefix("Hello everyone!!!"));
- ENSURE(!str.has_suffix("Hello everyone!!!"));
- }
- {
- Guid guid = guid::parse("dd733419-bbd0-4248-bc84-e0e8363d7165");
- TempAllocator128 ta;
- DynamicString str(ta);
- str.from_guid(guid);
- ENSURE(str.length() == 36);
- ENSURE(strcmp(str.c_str(), "dd733419-bbd0-4248-bc84-e0e8363d7165") == 0);
- }
- {
- StringId32 id = StringId32("test");
- TempAllocator128 ta;
- DynamicString str(ta);
- str.from_string_id(id);
- ENSURE(str.length() == 8);
- ENSURE(strcmp(str.c_str(), "1812752e") == 0);
- }
- {
- StringId64 id = StringId64("test");
- TempAllocator128 ta;
- DynamicString str(ta);
- str.from_string_id(id);
- ENSURE(str.length() == 16);
- ENSURE(strcmp(str.c_str(), "2f4a8724618f4c63") == 0);
- }
- memory_globals::shutdown();
- }
- static void test_guid()
- {
- memory_globals::init();
- guid_globals::init();
- {
- Guid guid = guid::new_guid();
- char str[37];
- guid::to_string(str, sizeof(str), guid);
- Guid parsed = guid::parse(str);
- ENSURE(guid == parsed);
- }
- {
- Guid guid;
- ENSURE(guid::try_parse(guid, "961f8005-6a7e-4371-9272-8454dd786884"));
- ENSURE(!guid::try_parse(guid, "961f80056a7e-4371-9272-8454dd786884"));
- }
- guid_globals::shutdown();
- memory_globals::shutdown();
- }
- static void test_json()
- {
- memory_globals::init();
- {
- JsonValueType::Enum t = json::type("null");
- ENSURE(t == JsonValueType::NIL);
- }
- {
- JsonValueType::Enum t = json::type("true");
- ENSURE(t == JsonValueType::BOOL);
- }
- {
- JsonValueType::Enum t = json::type("false");
- ENSURE(t == JsonValueType::BOOL);
- }
- {
- JsonValueType::Enum t = json::type("3.14");
- ENSURE(t == JsonValueType::NUMBER);
- }
- {
- JsonValueType::Enum t = json::type("\"foo\"");
- ENSURE(t == JsonValueType::STRING);
- }
- {
- JsonValueType::Enum t = json::type("[]");
- ENSURE(t == JsonValueType::ARRAY);
- }
- {
- JsonValueType::Enum t = json::type("{}");
- ENSURE(t == JsonValueType::OBJECT);
- }
- {
- const s32 a = json::parse_int("3.14");
- ENSURE(a == 3);
- }
- {
- const f32 a = json::parse_float("3.14");
- ENSURE(fequal(a, 3.14f));
- }
- {
- const bool a = json::parse_bool("true");
- ENSURE(a == true);
- }
- {
- const bool a = json::parse_bool("false");
- ENSURE(a == false);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- json::parse_string(str, "\"This is JSON\"");
- ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
- }
- memory_globals::shutdown();
- }
- static void test_sjson()
- {
- memory_globals::init();
- {
- JsonValueType::Enum t = sjson::type("null");
- ENSURE(t == JsonValueType::NIL);
- }
- {
- JsonValueType::Enum t = sjson::type("true");
- ENSURE(t == JsonValueType::BOOL);
- }
- {
- JsonValueType::Enum t = sjson::type("false");
- ENSURE(t == JsonValueType::BOOL);
- }
- {
- JsonValueType::Enum t = sjson::type("3.14");
- ENSURE(t == JsonValueType::NUMBER);
- }
- {
- JsonValueType::Enum t = sjson::type("\"foo\"");
- ENSURE(t == JsonValueType::STRING);
- }
- {
- JsonValueType::Enum t = sjson::type("[]");
- ENSURE(t == JsonValueType::ARRAY);
- }
- {
- JsonValueType::Enum t = sjson::type("{}");
- ENSURE(t == JsonValueType::OBJECT);
- }
- {
- const s32 a = sjson::parse_int("3.14");
- ENSURE(a == 3);
- }
- {
- const f32 a = sjson::parse_float("3.14");
- ENSURE(fequal(a, 3.14f));
- }
- {
- const bool a = sjson::parse_bool("true");
- ENSURE(a == true);
- }
- {
- const bool a = sjson::parse_bool("false");
- ENSURE(a == false);
- }
- {
- TempAllocator1024 ta;
- DynamicString str(ta);
- sjson::parse_string(str, "\"This is JSON\"");
- ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
- }
- {
- const Vector2 a = sjson::parse_vector2("[ 1.2 -2.5 ]");
- ENSURE(fequal(a.x, 1.2f));
- ENSURE(fequal(a.y, -2.5f));
- }
- {
- const Vector3 a = sjson::parse_vector3("[ 3.1 0.5 -5.7]");
- ENSURE(fequal(a.x, 3.1f));
- ENSURE(fequal(a.y, 0.5f));
- ENSURE(fequal(a.z, -5.7f));
- }
- {
- const Vector4 a = sjson::parse_vector4("[ 6.7 -1.3 2.9 -0.4 ]");
- ENSURE(fequal(a.x, 6.7f));
- ENSURE(fequal(a.y, -1.3f));
- ENSURE(fequal(a.z, 2.9f));
- ENSURE(fequal(a.w, -0.4f));
- }
- {
- const Quaternion a = sjson::parse_quaternion("[ -1.5 -3.4 9.1 -3.5 ]");
- ENSURE(fequal(a.x, -1.5f));
- ENSURE(fequal(a.y, -3.4f));
- ENSURE(fequal(a.z, 9.1f));
- ENSURE(fequal(a.w, -3.5f));
- }
- {
- const Matrix4x4 a = sjson::parse_matrix4x4(
- "["
- "-3.2 5.3 -0.7 4.1 "
- " 5.6 7.0 -3.2 -1.2 "
- "-6.3 9.0 3.9 1.1 "
- " 0.4 -7.3 8.9 -0.1 "
- "]"
- );
- ENSURE(fequal(a.x.x, -3.2f));
- ENSURE(fequal(a.x.y, 5.3f));
- ENSURE(fequal(a.x.z, -0.7f));
- ENSURE(fequal(a.x.w, 4.1f));
- ENSURE(fequal(a.y.x, 5.6f));
- ENSURE(fequal(a.y.y, 7.0f));
- ENSURE(fequal(a.y.z, -3.2f));
- ENSURE(fequal(a.y.w, -1.2f));
- ENSURE(fequal(a.z.x, -6.3f));
- ENSURE(fequal(a.z.y, 9.0f));
- ENSURE(fequal(a.z.z, 3.9f));
- ENSURE(fequal(a.z.w, 1.1f));
- ENSURE(fequal(a.t.x, 0.4f));
- ENSURE(fequal(a.t.y, -7.3f));
- ENSURE(fequal(a.t.z, 8.9f));
- ENSURE(fequal(a.t.w, -0.1f));
- }
- {
- const StringId32 a = sjson::parse_string_id("\"murmur32\"");
- ENSURE(a._id == 0x7c2365dbu);
- }
- {
- const StringId64 a = sjson::parse_resource_name("\"murmur64\"");
- ENSURE(a._id == 0x90631502d1a3432bu);
- }
- {
- const Guid guid = guid::parse("0f6c3b1c-9cba-4282-9096-2a77ca047b1b");
- const Guid parsed = sjson::parse_guid("\"0f6c3b1c-9cba-4282-9096-2a77ca047b1b\"");
- ENSURE(guid == parsed);
- }
- {
- TempAllocator128 ta;
- DynamicString str(ta);
- sjson::parse_verbatim(str, "\"\"\"verbatim\"\"\"");
- ENSURE(strcmp(str.c_str(), "verbatim") == 0);
- }
- memory_globals::shutdown();
- }
- static void test_path()
- {
- #if CROWN_PLATFORM_POSIX
- {
- const bool a = path::is_absolute("/home/foo");
- ENSURE(a == true);
- const bool b = path::is_absolute("home/foo");
- ENSURE(b == false);
- }
- {
- const bool a = path::is_relative("/home/foo");
- ENSURE(a == false);
- const bool b = path::is_relative("home/foo");
- ENSURE(b == true);
- }
- {
- const bool a = path::is_root("/");
- ENSURE(a == true);
- const bool b = path::is_root("/home");
- ENSURE(b == false);
- }
- {
- ENSURE(path::has_trailing_separator("/home/foo/"));
- ENSURE(!path::has_trailing_separator("/home/foo"));
- }
- {
- TempAllocator128 ta;
- DynamicString clean(ta);
- path::reduce(clean, "/home//foo/");
- ENSURE(clean == "/home/foo");
- }
- {
- TempAllocator128 ta;
- DynamicString clean(ta);
- path::reduce(clean, "\\home\\\\foo\\");
- ENSURE(clean == "/home/foo");
- }
- #else
- {
- const bool a = path::is_absolute("C:\\Users\\foo");
- ENSURE(a == true);
- const bool b = path::is_absolute("Users\\foo");
- ENSURE(b == false);
- }
- {
- const bool a = path::is_relative("D:\\Users\\foo");
- ENSURE(a == false);
- const bool b = path::is_relative("Users\\foo");
- ENSURE(b == true);
- }
- {
- const bool a = path::is_root("E:\\");
- ENSURE(a == true);
- const bool b = path::is_root("E:\\Users");
- ENSURE(b == false);
- }
- {
- ENSURE(path::has_trailing_separator("C:\\Users\\foo\\"));
- ENSURE(!path::has_trailing_separator("C:\\Users\\foo"));
- }
- {
- TempAllocator128 ta;
- DynamicString clean(ta);
- path::reduce(clean, "C:\\Users\\\\foo\\");
- ENSURE(clean == "C:\\Users\\foo");
- }
- {
- TempAllocator128 ta;
- DynamicString clean(ta);
- path::reduce(clean, "C:/Users//foo/");
- ENSURE(clean == "C:\\Users\\foo");
- }
- #endif // CROWN_PLATFORM_POSIX
- {
- const char* p = path::basename("");
- ENSURE(strcmp(p, "") == 0);
- const char* q = path::basename("/");
- ENSURE(strcmp(q, "") == 0);
- const char* r = path::basename("boot.config");
- ENSURE(strcmp(r, "boot.config") == 0);
- const char* s = path::basename("foo/boot.config");
- ENSURE(strcmp(s, "boot.config") == 0);
- const char* t = path::basename("/foo/boot.config");
- ENSURE(strcmp(t, "boot.config") == 0);
- }
- {
- const char* p = path::extension("");
- ENSURE(p == NULL);
- const char* q = path::extension("boot");
- ENSURE(q == NULL);
- const char* r = path::extension("boot.bar.config");
- ENSURE(strcmp(r, "config") == 0);
- }
- {
- TempAllocator128 ta;
- DynamicString path(ta);
- path::join(path, "", "");
- ENSURE(path == "");
- }
- {
- TempAllocator128 ta;
- DynamicString path(ta);
- path::join(path, "foo", "");
- ENSURE(path == "foo");
- }
- {
- TempAllocator128 ta;
- DynamicString path(ta);
- path::join(path, "", "bar");
- ENSURE(path == "bar");
- }
- #if CROWN_PLATFORM_POSIX
- {
- TempAllocator128 ta;
- DynamicString path(ta);
- path::join(path, "foo", "bar");
- ENSURE(path == "foo/bar");
- }
- #else
- {
- TempAllocator128 ta;
- DynamicString path(ta);
- path::join(path, "C:\\foo", "bar");
- ENSURE(path == "C:\\foo\\bar");
- }
- #endif // CROWN_PLATFORM_POSIX
- }
- static void test_command_line()
- {
- const char* argv[] =
- {
- "args",
- "-s",
- "--switch",
- "--argument",
- "orange"
- };
- CommandLine cl(countof(argv), argv);
- ENSURE(cl.has_option("switch", 's'));
- const char* orange = cl.get_parameter(0, "argument");
- ENSURE(orange != NULL && strcmp(orange, "orange") == 0);
- }
- static void test_thread()
- {
- Thread thread;
- ENSURE(!thread.is_running());
- thread.start([](void*) { return 0xbadc0d3; }, NULL);
- thread.stop();
- ENSURE(thread.exit_code() == 0xbadc0d3);
- }
- static void test_process()
- {
- #if CROWN_PLATFORM_POSIX
- {
- const char* argv[] = {"printf", "Hello,\\nworld.\\n", NULL};
- Process pr;
- if (pr.spawn(argv, ProcessFlags::STDOUT_PIPE) == 0)
- {
- char buf[128] = {0};
- pr.fgets(buf, sizeof(buf));
- ENSURE(strcmp(buf, "Hello,\n") == 0);
- pr.fgets(buf, sizeof(buf));
- ENSURE(strcmp(buf, "world.\n") == 0);
- pr.wait();
- }
- }
- {
- const char* argv[] = {"false", NULL};
- Process pr;
- if (pr.spawn(argv) == 0)
- ENSURE(pr.wait() == 1);
- }
- #endif
- }
- static void test_filesystem()
- {
- #if CROWN_PLATFORM_POSIX
- {
- DeleteResult dr = os::delete_directory("/tmp/none");
- ENSURE(dr.error == DeleteResult::NO_ENTRY);
- }
- {
- os::delete_directory("/tmp/crown");
- CreateResult cr = os::create_directory("/tmp/crown");
- ENSURE(cr.error == CreateResult::SUCCESS);
- cr = os::create_directory("/tmp/crown");
- ENSURE(cr.error == CreateResult::ALREADY_EXISTS);
- DeleteResult dr = os::delete_directory("/tmp/crown");
- ENSURE(dr.error == DeleteResult::SUCCESS);
- }
- #endif // CROWN_PLATFORM_POSIX
- }
- #define RUN_TEST(name) \
- do { \
- printf(#name "\n"); \
- name(); \
- } while (0)
- int main_unit_tests()
- {
- RUN_TEST(test_memory);
- RUN_TEST(test_array);
- RUN_TEST(test_vector);
- RUN_TEST(test_hash_map);
- RUN_TEST(test_hash_set);
- RUN_TEST(test_vector2);
- RUN_TEST(test_vector3);
- RUN_TEST(test_vector4);
- RUN_TEST(test_quaternion);
- RUN_TEST(test_color4);
- RUN_TEST(test_matrix3x3);
- RUN_TEST(test_matrix4x4);
- RUN_TEST(test_aabb);
- RUN_TEST(test_sphere);
- RUN_TEST(test_murmur);
- RUN_TEST(test_string_id);
- RUN_TEST(test_dynamic_string);
- RUN_TEST(test_guid);
- RUN_TEST(test_json);
- RUN_TEST(test_sjson);
- RUN_TEST(test_path);
- RUN_TEST(test_command_line);
- RUN_TEST(test_thread);
- RUN_TEST(test_process);
- RUN_TEST(test_filesystem);
- return EXIT_SUCCESS;
- }
- } // namespace crown
- #endif // CROWN_BUILD_UNIT_TESTS
|