changeset 223:c6513d9c696b

Pack: add unit tests
author David Demelier <markand@malikania.fr>
date Thu, 08 May 2014 23:51:07 +0200
parents 5a99711d52f9
children ca69910b1407
files C++/Pack.h C++/Tests/Pack/CMakeLists.txt C++/Tests/Pack/TestPack.cpp C++/Tests/Pack/TestPack.h CMakeLists.txt
diffstat 5 files changed, 149 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/C++/Pack.h	Thu May 08 23:31:47 2014 +0200
+++ b/C++/Pack.h	Thu May 08 23:51:07 2014 +0200
@@ -47,7 +47,7 @@
 	};
 
 private:
-	static void writeFile(std::ofstream &out, Endian endian)
+	static void writeFile(std::ofstream &, Endian)
 	{
 		// dummy, stop recursion
 	}
@@ -58,11 +58,11 @@
 		static_assert(TypeInfo<T>::supported, "unsupported type");
 
 		T ret = convert(value, endian);
-		out.write(reinterpret_cast<unsigned char *>(&ret), TypeInfo<T>::size);
+		out.write(reinterpret_cast<const char *>(&ret), TypeInfo<T>::size);
 		writeFile(out, endian, args...);
 	}
 
-	static void readFile(std::ifstream &in, Endian endian)
+	static void readFile(std::ifstream &, Endian)
 	{
 		// dummy, stop recursion
 	}
@@ -72,7 +72,7 @@
 	{
 		static_assert(TypeInfo<T>::supported, "unsupported type");
 
-		in.read(reinterpret_cast<unsigned char *>(&value), TypeInfo<T>::size);
+		in.read(reinterpret_cast<char *>(&value), TypeInfo<T>::size);
 		value = convert(value, endian);
 		readFile(in, endian, args...);
 	}
@@ -175,13 +175,13 @@
 };
 
 template <>
-uint8_t Pack::convert(uint8_t v, Endian)
+inline uint8_t Pack::convert(uint8_t v, Endian)
 {
 	return v;
 }
 
 template <>
-uint16_t Pack::convert(uint16_t v, Endian endian)
+inline uint16_t Pack::convert(uint16_t v, Endian endian)
 {
 	if (mode != endian)
 		return (((v >> 8) & 0x00FFL) | ((v << 8) & 0xFF00L));
@@ -190,7 +190,7 @@
 }
 
 template <>
-uint32_t Pack::convert(uint32_t v, Endian endian)
+inline uint32_t Pack::convert(uint32_t v, Endian endian)
 {
 	if (mode != endian)
 		return ((((v) >> 24) & 0x000000FFL)
@@ -202,7 +202,7 @@
 }
 
 template <>
-uint64_t Pack::convert(uint64_t v, Endian endian)
+inline uint64_t Pack::convert(uint64_t v, Endian endian)
 {
 	if (mode != endian)
 		return ((((v) & 0xff00000000000000ull) >> 56)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/Pack/CMakeLists.txt	Thu May 08 23:51:07 2014 +0200
@@ -0,0 +1,27 @@
+#
+# CMakeLists.txt -- tests for Pack
+#
+# Copyright (c) 2014 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+set(
+	SOURCES
+	${code_SOURCE_DIR}/C++/Pack.cpp
+	${code_SOURCE_DIR}/C++/Pack.h
+	TestPack.cpp
+	TestPack.h
+)
+
+define_test(pack "${SOURCES}")
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/Pack/TestPack.cpp	Thu May 08 23:51:07 2014 +0200
@@ -0,0 +1,68 @@
+/*
+ * TestPack.cpp -- test the pack serializer
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cppunit/TextTestRunner.h>
+
+#include <Pack.h>
+
+#include "TestPack.h"
+
+void TestPack::simpleLittleEndian()
+{
+	uint8_t u8(1), r8;
+	uint16_t u16(2), r16;
+	uint32_t u32(3), r32;
+	uint64_t u64(4), r64;
+
+	try {
+		Pack::write("simple-little.bin", Pack::Little, u8, u16, u32, u64);
+		Pack::read("simple-little.bin", Pack::Little, r8, r16, r32, r64);
+
+		CPPUNIT_ASSERT_EQUAL(r8, u8);
+		CPPUNIT_ASSERT_EQUAL(r16, u16);
+		CPPUNIT_ASSERT_EQUAL(r32, u32);
+		CPPUNIT_ASSERT_EQUAL(r64, u64);
+	} catch (const std::runtime_error &) { }
+}
+
+void TestPack::simpleBigEndian()
+{
+	uint8_t u8(1), r8;
+	uint16_t u16(2), r16;
+	uint32_t u32(3), r32;
+	uint64_t u64(4), r64;
+
+	try {
+		Pack::write("simple-big.bin", Pack::Big, u8, u16, u32, u64);
+		Pack::read("simple-big.bin", Pack::Big, r8, r16, r32, r64);
+
+		CPPUNIT_ASSERT_EQUAL(r8, u8);
+		CPPUNIT_ASSERT_EQUAL(r16, u16);
+		CPPUNIT_ASSERT_EQUAL(r32, u32);
+		CPPUNIT_ASSERT_EQUAL(r64, u64);
+	} catch (const std::runtime_error &) { }
+}
+
+int main()
+{
+	CppUnit::TextTestRunner runnerText;
+
+	runnerText.addTest(TestPack::suite());
+
+	return runnerText.run("", false) == 1 ? 0 : 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Tests/Pack/TestPack.h	Thu May 08 23:51:07 2014 +0200
@@ -0,0 +1,37 @@
+/*
+ * TestPack.h -- test the pack serializer
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _TEST_PACK_H_
+#define _TEST_PACK_H_
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/TestFixture.h>
+
+class TestPack : public CppUnit::TestFixture {
+private:
+	CPPUNIT_TEST_SUITE(TestPack);
+	CPPUNIT_TEST(simpleLittleEndian);
+	CPPUNIT_TEST(simpleBigEndian);
+	CPPUNIT_TEST_SUITE_END();
+
+public:
+	void simpleLittleEndian();
+	void simpleBigEndian();
+};
+
+#endif // !_TEST_PACK_H_
\ No newline at end of file
--- a/CMakeLists.txt	Thu May 08 23:31:47 2014 +0200
+++ b/CMakeLists.txt	Thu May 08 23:51:07 2014 +0200
@@ -41,6 +41,11 @@
 		${sources}
 	)
 
+	# Get rid of cppunit warning shit
+	if (NOT WIN32)
+		target_compile_options(${name} PRIVATE "-Wno-unused-parameter")
+	endif()
+
 	target_include_directories(${name} PRIVATE ${CPPUNIT_INCLUDE_DIR})
 	target_link_libraries(${name} ${CPPUNIT_LIBRARY})
 	add_test(${name}-test ${name})
@@ -69,3 +74,7 @@
 if (WITH_HASH)
 	add_subdirectory(C++/Tests/Hash)
 endif ()
+
+if (WITH_PACK)
+	add_subdirectory(C++/Tests/Pack)
+endif ()