# HG changeset patch # User David Demelier # Date 1399585867 -7200 # Node ID c6513d9c696b6b9ac0a5b72ae22c7ecb67a32471 # Parent 5a99711d52f9fdfef07b21063848bcfdafbbac1d Pack: add unit tests diff -r 5a99711d52f9 -r c6513d9c696b C++/Pack.h --- 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::supported, "unsupported type"); T ret = convert(value, endian); - out.write(reinterpret_cast(&ret), TypeInfo::size); + out.write(reinterpret_cast(&ret), TypeInfo::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::supported, "unsupported type"); - in.read(reinterpret_cast(&value), TypeInfo::size); + in.read(reinterpret_cast(&value), TypeInfo::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) diff -r 5a99711d52f9 -r c6513d9c696b C++/Tests/Pack/CMakeLists.txt --- /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 +# +# 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 diff -r 5a99711d52f9 -r c6513d9c696b C++/Tests/Pack/TestPack.cpp --- /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 + * + * 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 + +#include + +#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 diff -r 5a99711d52f9 -r c6513d9c696b C++/Tests/Pack/TestPack.h --- /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 + * + * 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 +#include + +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 diff -r 5a99711d52f9 -r c6513d9c696b CMakeLists.txt --- 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 ()