diff options
| author | ljfa-ag <ljfa-ag@web.de> | 2015-08-20 21:41:16 +0200 |
|---|---|---|
| committer | ljfa-ag <ljfa-ag@web.de> | 2015-08-20 21:41:16 +0200 |
| commit | b7b3abf11dcb94249eae0b51332c0604af134946 (patch) | |
| tree | 1401a4e8fc1b3ca6b90e718c7f18501a792c9489 | |
| parent | 7fbf75e5f8c0c98f77626520aab32c3fc75e88cf (diff) | |
| download | Project-Tick-b7b3abf11dcb94249eae0b51332c0604af134946.tar.gz Project-Tick-b7b3abf11dcb94249eae0b51332c0604af134946.zip | |
Make endian_str_test use CxxTest
| -rw-r--r-- | test/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | test/endian_str_test.cpp | 181 | ||||
| -rw-r--r-- | test/endian_str_test.h | 175 |
3 files changed, 176 insertions, 183 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8b26efcab9..4e74c0ab93 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,9 +5,8 @@ include_directories(${CXXTEST_INCLUDE_DIR}) CXXTEST_ADD_TEST(nbttest nbttest.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nbttest.h) target_link_libraries(nbttest nbt++) -add_executable(endian_str_test endian_str_test.cpp) +CXXTEST_ADD_TEST(endian_str_test endian_str_test.cpp ${CMAKE_CURRENT_SOURCE_DIR}/endian_str_test.h) target_link_libraries(endian_str_test nbt++) -add_test(endian_str_test endian_str_test) add_executable(read_test read_test.cpp) target_link_libraries(read_test nbt++) diff --git a/test/endian_str_test.cpp b/test/endian_str_test.cpp deleted file mode 100644 index c8db4ea674..0000000000 --- a/test/endian_str_test.cpp +++ /dev/null @@ -1,181 +0,0 @@ -/* - * libnbt++ - A library for the Minecraft Named Binary Tag format. - * Copyright (C) 2013, 2015 ljfa-ag - * - * This file is part of libnbt++. - * - * libnbt++ is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * libnbt++ is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with libnbt++. If not, see <http://www.gnu.org/licenses/>. - */ -#include "microtest.h" -#include "endian_str.h" -#include <cstdlib> -#include <iostream> -#include <sstream> - -using namespace endian; - -void test_uint() -{ - std::stringstream str(std::ios::in | std::ios::out | std::ios::binary); - - write_little(str, uint8_t (0x01)); - write_little(str, uint16_t(0x0102)); - write (str, uint32_t(0x01020304), endian::little); - write_little(str, uint64_t(0x0102030405060708)); - - write_big (str, uint8_t (0x09)); - write_big (str, uint16_t(0x090A)); - write_big (str, uint32_t(0x090A0B0C)); - write (str, uint64_t(0x090A0B0C0D0E0F10), endian::big); - - std::string expected{ - 1, - 2, 1, - 4, 3, 2, 1, - 8, 7, 6, 5, 4, 3, 2, 1, - - 9, - 9, 10, - 9, 10, 11, 12, - 9, 10, 11, 12, 13, 14, 15, 16 - }; - ASSERT(str.str() == expected); - - uint8_t u8; - uint16_t u16; - uint32_t u32; - uint64_t u64; - - read_little(str, u8); - ASSERT(u8 == 0x01); - read_little(str, u16); - ASSERT(u16 == 0x0102); - read_little(str, u32); - ASSERT(u32 == 0x01020304); - read(str, u64, endian::little); - ASSERT(u64 == 0x0102030405060708); - - read_big(str, u8); - ASSERT(u8 == 0x09); - read_big(str, u16); - ASSERT(u16 == 0x090A); - read(str, u32, endian::big); - ASSERT(u32 == 0x090A0B0C); - read_big(str, u64); - ASSERT(u64 == 0x090A0B0C0D0E0F10); - - ASSERT(str); //Check if stream has failed - std::clog << "test_uint passed" << std::endl; -} - -void test_sint() -{ - std::stringstream str(std::ios::in | std::ios::out | std::ios::binary); - - write_little(str, int8_t (-0x01)); - write_little(str, int16_t(-0x0102)); - write_little(str, int32_t(-0x01020304)); - write (str, int64_t(-0x0102030405060708), endian::little); - - write_big (str, int8_t (-0x09)); - write_big (str, int16_t(-0x090A)); - write (str, int32_t(-0x090A0B0C), endian::big); - write_big (str, int64_t(-0x090A0B0C0D0E0F10)); - - std::string expected{ //meh, stupid narrowing conversions - '\xFF', - '\xFE', '\xFE', - '\xFC', '\xFC', '\xFD', '\xFE', - '\xF8', '\xF8', '\xF9', '\xFA', '\xFB', '\xFC', '\xFD', '\xFE', - - '\xF7', - '\xF6', '\xF6', - '\xF6', '\xF5', '\xF4', '\xF4', - '\xF6', '\xF5', '\xF4', '\xF3', '\xF2', '\xF1', '\xF0', '\xF0' - }; - ASSERT(str.str() == expected); - - int8_t i8; - int16_t i16; - int32_t i32; - int64_t i64; - - read_little(str, i8); - ASSERT(i8 == -0x01); - read_little(str, i16); - ASSERT(i16 == -0x0102); - read(str, i32, endian::little); - ASSERT(i32 == -0x01020304); - read_little(str, i64); - ASSERT(i64 == -0x0102030405060708); - - read_big(str, i8); - ASSERT(i8 == -0x09); - read_big(str, i16); - ASSERT(i16 == -0x090A); - read_big(str, i32); - ASSERT(i32 == -0x090A0B0C); - read(str, i64, endian::big); - ASSERT(i64 == -0x090A0B0C0D0E0F10); - - ASSERT(str); //Check if stream has failed - std::clog << "test_sint passed" << std::endl; -} - -void test_float() -{ - std::stringstream str(std::ios::in | std::ios::out | std::ios::binary); - - //C99 has hexadecimal floating point literals, C++ doesn't... - const float fconst = std::stof("-0xCDEF01p-63"); //-1.46325e-012 - const double dconst = std::stod("-0x1DEF0102030405p-375"); //-1.09484e-097 - //We will be assuming IEEE 754 here - - write_little(str, fconst); - write_little(str, dconst); - write_big (str, fconst); - write_big (str, dconst); - - std::string expected{ - '\x01', '\xEF', '\xCD', '\xAB', - '\x05', '\x04', '\x03', '\x02', '\x01', '\xEF', '\xCD', '\xAB', - - '\xAB', '\xCD', '\xEF', '\x01', - '\xAB', '\xCD', '\xEF', '\x01', '\x02', '\x03', '\x04', '\x05' - }; - ASSERT(str.str() == expected); - - float f; - double d; - - read_little(str, f); - ASSERT(f == fconst); - read_little(str, d); - ASSERT(d == dconst); - - read_big(str, f); - ASSERT(f == fconst); - read_big(str, d); - ASSERT(d == dconst); - - ASSERT(str); //Check if stream has failed - std::clog << "test_float passed" << std::endl; -} - -int main() -{ - test_uint(); - test_sint(); - test_float(); -} diff --git a/test/endian_str_test.h b/test/endian_str_test.h new file mode 100644 index 0000000000..e0b960ee54 --- /dev/null +++ b/test/endian_str_test.h @@ -0,0 +1,175 @@ +/* + * libnbt++ - A library for the Minecraft Named Binary Tag format. + * Copyright (C) 2013, 2015 ljfa-ag + * + * This file is part of libnbt++. + * + * libnbt++ is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * libnbt++ is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libnbt++. If not, see <http://www.gnu.org/licenses/>. + */ +#include <cxxtest/TestSuite.h> +#include "endian_str.h" +#include <cstdlib> +#include <iostream> +#include <sstream> + +using namespace endian; + +class endian_str_test : public CxxTest::TestSuite +{ +public: + void test_uint() + { + std::stringstream str(std::ios::in | std::ios::out | std::ios::binary); + + write_little(str, uint8_t (0x01)); + write_little(str, uint16_t(0x0102)); + write (str, uint32_t(0x01020304), endian::little); + write_little(str, uint64_t(0x0102030405060708)); + + write_big (str, uint8_t (0x09)); + write_big (str, uint16_t(0x090A)); + write_big (str, uint32_t(0x090A0B0C)); + write (str, uint64_t(0x090A0B0C0D0E0F10), endian::big); + + std::string expected{ + 1, + 2, 1, + 4, 3, 2, 1, + 8, 7, 6, 5, 4, 3, 2, 1, + + 9, + 9, 10, + 9, 10, 11, 12, + 9, 10, 11, 12, 13, 14, 15, 16 + }; + TS_ASSERT_EQUALS(str.str(), expected); + + uint8_t u8; + uint16_t u16; + uint32_t u32; + uint64_t u64; + + read_little(str, u8); + TS_ASSERT_EQUALS(u8, 0x01); + read_little(str, u16); + TS_ASSERT_EQUALS(u16, 0x0102); + read_little(str, u32); + TS_ASSERT_EQUALS(u32, 0x01020304); + read(str, u64, endian::little); + TS_ASSERT_EQUALS(u64, 0x0102030405060708); + + read_big(str, u8); + TS_ASSERT_EQUALS(u8, 0x09); + read_big(str, u16); + TS_ASSERT_EQUALS(u16, 0x090A); + read(str, u32, endian::big); + TS_ASSERT_EQUALS(u32, 0x090A0B0C); + read_big(str, u64); + TS_ASSERT_EQUALS(u64, 0x090A0B0C0D0E0F10); + + TS_ASSERT(str); //Check if stream has failed + } + + void test_sint() + { + std::stringstream str(std::ios::in | std::ios::out | std::ios::binary); + + write_little(str, int8_t (-0x01)); + write_little(str, int16_t(-0x0102)); + write_little(str, int32_t(-0x01020304)); + write (str, int64_t(-0x0102030405060708), endian::little); + + write_big (str, int8_t (-0x09)); + write_big (str, int16_t(-0x090A)); + write (str, int32_t(-0x090A0B0C), endian::big); + write_big (str, int64_t(-0x090A0B0C0D0E0F10)); + + std::string expected{ //meh, stupid narrowing conversions + '\xFF', + '\xFE', '\xFE', + '\xFC', '\xFC', '\xFD', '\xFE', + '\xF8', '\xF8', '\xF9', '\xFA', '\xFB', '\xFC', '\xFD', '\xFE', + + '\xF7', + '\xF6', '\xF6', + '\xF6', '\xF5', '\xF4', '\xF4', + '\xF6', '\xF5', '\xF4', '\xF3', '\xF2', '\xF1', '\xF0', '\xF0' + }; + TS_ASSERT_EQUALS(str.str(), expected); + + int8_t i8; + int16_t i16; + int32_t i32; + int64_t i64; + + read_little(str, i8); + TS_ASSERT_EQUALS(i8, -0x01); + read_little(str, i16); + TS_ASSERT_EQUALS(i16, -0x0102); + read(str, i32, endian::little); + TS_ASSERT_EQUALS(i32, -0x01020304); + read_little(str, i64); + TS_ASSERT_EQUALS(i64, -0x0102030405060708); + + read_big(str, i8); + TS_ASSERT_EQUALS(i8, -0x09); + read_big(str, i16); + TS_ASSERT_EQUALS(i16, -0x090A); + read_big(str, i32); + TS_ASSERT_EQUALS(i32, -0x090A0B0C); + read(str, i64, endian::big); + TS_ASSERT_EQUALS(i64, -0x090A0B0C0D0E0F10); + + TS_ASSERT(str); //Check if stream has failed + } + + void test_float() + { + std::stringstream str(std::ios::in | std::ios::out | std::ios::binary); + + //C99 has hexadecimal floating point literals, C++ doesn't... + const float fconst = std::stof("-0xCDEF01p-63"); //-1.46325e-012 + const double dconst = std::stod("-0x1DEF0102030405p-375"); //-1.09484e-097 + //We will be assuming IEEE 754 here + + write_little(str, fconst); + write_little(str, dconst); + write_big (str, fconst); + write_big (str, dconst); + + std::string expected{ + '\x01', '\xEF', '\xCD', '\xAB', + '\x05', '\x04', '\x03', '\x02', '\x01', '\xEF', '\xCD', '\xAB', + + '\xAB', '\xCD', '\xEF', '\x01', + '\xAB', '\xCD', '\xEF', '\x01', '\x02', '\x03', '\x04', '\x05' + }; + TS_ASSERT_EQUALS(str.str(), expected); + + float f; + double d; + + read_little(str, f); + TS_ASSERT_EQUALS(f, fconst); + read_little(str, d); + TS_ASSERT_EQUALS(d, dconst); + + read_big(str, f); + TS_ASSERT_EQUALS(f, fconst); + read_big(str, d); + TS_ASSERT_EQUALS(d, dconst); + + TS_ASSERT(str); //Check if stream has failed + } +}; |
