Forráskód Böngészése

Add initial unit tests

Daniele Bartolini 10 éve
szülő
commit
d418c95943
2 módosított fájl, 150 hozzáadás és 0 törlés
  1. 4 0
      src/config.h
  2. 146 0
      src/core/unit_tests.cpp

+ 4 - 0
src/config.h

@@ -22,6 +22,10 @@
 
 #define CROWN_RELEASE (!CROWN_DEBUG && !CROWN_DEVELOPMENT)
 
+#ifndef CROWN_BUILD_UNIT_TESTS
+	#define CROWN_BUILD_UNIT_TESTS 1
+#endif // CROWN_BUILD_UNIT_TESTS
+
 #if !defined(CROWN_PHYSICS_BULLET) \
 	&& !defined(CROWN_PHYSICS_PHYSX) \
 	&& !defined(CROWN_PHYSICS_NULL)

+ 146 - 0
src/core/unit_tests.cpp

@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#if CROWN_BUILD_UNIT_TESTS
+
+#include "array.h"
+#include "config.h"
+#include "dynamic_string.h"
+#include "json.h"
+#include "math_utils.h"
+#include "memory.h"
+#include "murmur.h"
+#include "sjson.h"
+#include "string_utils.h"
+#include "temp_allocator.h"
+#include "vector.h"
+
+namespace crown
+{
+
+static void test_array()
+{
+	memory_globals::init();
+	Allocator& a = default_allocator();
+	{
+		Array<int> v(a);
+
+		CE_ENSURE(array::size(v) == 0);
+		array::push_back(v, 1);
+		CE_ENSURE(array::size(v) == 1);
+		CE_ENSURE(v[0] == 1);
+	}
+	memory_globals::shutdown();
+}
+
+static void test_vector()
+{
+	memory_globals::init();
+	Allocator& a = default_allocator();
+	{
+		Vector<int> v(a);
+
+		CE_ENSURE(vector::size(v) == 0);
+		vector::push_back(v, 1);
+		CE_ENSURE(vector::size(v) == 1);
+		CE_ENSURE(v[0] == 1);
+	}
+	memory_globals::shutdown();
+}
+
+static void test_murmur()
+{
+	const u32 m = murmur32("murmur32", 8, 0);
+	CE_ENSURE(m == 0x7c2365dbu);
+	const u64 n = murmur64("murmur64", 8, 0);
+	CE_ENSURE(n == 0x90631502d1a3432bu);
+}
+
+static void test_json()
+{
+	memory_globals::init();
+	{
+		JsonValueType::Enum type = json::type("null");
+		CE_ENSURE(type == JsonValueType::NIL);
+		type = json::type("true");
+		CE_ENSURE(type == JsonValueType::BOOL);
+		type = json::type("false");
+		CE_ENSURE(type == JsonValueType::BOOL);
+		type = json::type("3.14");
+		CE_ENSURE(type == JsonValueType::NUMBER);
+		type = json::type("\"foo\"");
+		CE_ENSURE(type == JsonValueType::STRING);
+		type = json::type("[]");
+		CE_ENSURE(type == JsonValueType::ARRAY);
+		type = json::type("{}");
+		CE_ENSURE(type == JsonValueType::OBJECT);
+
+		const s32 i = json::parse_int("3.14");
+		CE_ENSURE(i == 3);
+		const f32 f = json::parse_float("3.14");
+		CE_ENSURE(fequal(f, 3.14f));
+
+		const bool b = json::parse_bool("true");
+		CE_ENSURE(b == true);
+		const bool c = json::parse_bool("false");
+		CE_ENSURE(c == false);
+
+		TempAllocator1024 ta;
+		DynamicString str(ta);
+		json::parse_string("\"This is JSON\"", str);
+		CE_ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
+	}
+	memory_globals::shutdown();
+}
+
+static void test_sjson()
+{
+	memory_globals::init();
+	{
+		JsonValueType::Enum type = json::type("null");
+		CE_ENSURE(type == JsonValueType::NIL);
+		type = sjson::type("true");
+		CE_ENSURE(type == JsonValueType::BOOL);
+		type = sjson::type("false");
+		CE_ENSURE(type == JsonValueType::BOOL);
+		type = sjson::type("3.14");
+		CE_ENSURE(type == JsonValueType::NUMBER);
+		type = sjson::type("\"foo\"");
+		CE_ENSURE(type == JsonValueType::STRING);
+		type = sjson::type("[]");
+		CE_ENSURE(type == JsonValueType::ARRAY);
+		type = sjson::type("{}");
+		CE_ENSURE(type == JsonValueType::OBJECT);
+
+		const s32 i = sjson::parse_int("3.14");
+		CE_ENSURE(i == 3);
+		const f32 f = sjson::parse_float("3.14");
+		CE_ENSURE(fequal(f, 3.14f));
+
+		const bool b = sjson::parse_bool("true");
+		CE_ENSURE(b == true);
+		const bool c = sjson::parse_bool("false");
+		CE_ENSURE(c == false);
+
+		TempAllocator1024 ta;
+		DynamicString str(ta);
+		sjson::parse_string("\"This is SJSON\"", str);
+		CE_ENSURE(strcmp(str.c_str(), "This is SJSON") == 0);
+	}
+	memory_globals::shutdown();
+}
+
+static void run_unit_tests()
+{
+	test_array();
+	test_vector();
+	test_murmur();
+	test_json();
+	test_sjson();
+}
+
+} // namespace crown
+
+#endif // CROWN_BUILD_UNIT_TESTS