瀏覽代碼

tests: Add numeric tests

Ole Krüger 10 年之前
父節點
當前提交
7894fd8072
共有 3 個文件被更改,包括 71 次插入5 次删除
  1. 4 1
      Makefile
  2. 0 4
      tests/all.cpp
  3. 67 0
      tests/types.cpp

+ 4 - 1
Makefile

@@ -6,7 +6,7 @@ EXEC            := exec
 # Test artifacts
 TEST_DIR        := tests
 TEST_OUT        := $(TEST_DIR)/all
-TEST_SRCS       := all.cpp
+TEST_SRCS       := all.cpp types.cpp
 TEST_DEPS       := $(TEST_SRCS:%.cpp=$(TEST_DIR)/%.d)
 TEST_OBJS       := $(TEST_SRCS:%.cpp=$(TEST_DIR)/%.o)
 
@@ -31,10 +31,13 @@ clean:
 test: $(TEST_OUT)
 	./$(TEST_OUT)
 
+-include $(TEST_DEPS)
+
 $(TEST_OUT): $(TEST_OBJS)
 	$(CXX) $(LDFLAGS) -o$@ $(TEST_OBJS) $(LDLIBS)
 
 $(TEST_DIR)/%.o: $(TEST_DIR)/%.cpp Makefile
+	echo Compile test
 	$(CXX) -c $(CXXFLAGS) -MMD -MF$(@:%.o=%.d) -MT$@ -o$@ $<
 
 # Examples

+ 0 - 4
tests/all.cpp

@@ -1,6 +1,2 @@
 #define CATCH_CONFIG_MAIN
 #include "catch.hpp"
-
-TEST_CASE("Description here", "my_test_case") {
-	REQUIRE(false);
-}

+ 67 - 0
tests/types.cpp

@@ -0,0 +1,67 @@
+#include "catch.hpp"
+
+#include <lua.hpp>
+#include <luwra.hpp>
+
+#include <iostream>
+#include <utility>
+#include <type_traits>
+
+template <typename I>
+struct NumericTest {
+	static
+	void test(lua_State* state) {
+		const I max_value = std::numeric_limits<I>::max();
+		const I min_value = std::numeric_limits<I>::lowest();
+		const I avg_value = (max_value + min_value) / 2;
+
+		// Largest value
+		REQUIRE(luwra::Value<I>::push(state, max_value) == 1);
+		REQUIRE(luwra::Value<I>::read(state, -1) == max_value);
+		lua_pop(state, 1);
+
+		// Lowest value
+		REQUIRE(luwra::Value<I>::push(state, min_value) == 1);
+		REQUIRE(luwra::Value<I>::read(state, -1) == min_value);
+		lua_pop(state, 1);
+
+		// Average value
+		REQUIRE(luwra::Value<I>::push(state, avg_value) == 1);
+		REQUIRE(luwra::Value<I>::read(state, -1) == avg_value);
+		lua_pop(state, 1);
+	}
+};
+
+struct TautologyTest {
+	static
+	void test(lua_State*) {}
+};
+
+template <typename B, typename I>
+using SelectNumericTest =
+	typename std::conditional<
+		luwra::internal::NumericContainedValueBase<I, B>::qualifies,
+		NumericTest<I>,
+		TautologyTest
+	>::type;
+
+TEST_CASE("Test Value specialization for numeric C types", "types_numeric") {
+	lua_State* state = luaL_newstate();
+
+	// Integer-based types
+	SelectNumericTest<lua_Integer, signed short>::test(state);
+	SelectNumericTest<lua_Integer, unsigned short>::test(state);
+	SelectNumericTest<lua_Integer, signed int>::test(state);
+	SelectNumericTest<lua_Integer, unsigned int>::test(state);
+	SelectNumericTest<lua_Integer, signed long int>::test(state);
+	SelectNumericTest<lua_Integer, unsigned long int>::test(state);
+	SelectNumericTest<lua_Integer, signed long long int>::test(state);
+	SelectNumericTest<lua_Integer, unsigned long long int>::test(state);
+
+	// Number-based types
+	SelectNumericTest<lua_Number, float>::test(state);
+	SelectNumericTest<lua_Number, double>::test(state);
+	SelectNumericTest<lua_Number, long double>::test(state);
+
+	lua_close(state);
+}