From a5bf6ccebd957365a4eeaa761688d44bfedb0593 Mon Sep 17 00:00:00 2001 From: ljfa-ag Date: Tue, 28 Jul 2015 17:22:09 +0200 Subject: Create endian_str.h and test --- include/endian_str.h | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 4 +++ test/endian_str_test.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 include/endian_str.h create mode 100644 test/endian_str_test.cpp diff --git a/include/endian_str.h b/include/endian_str.h new file mode 100644 index 0000000000..f636773c12 --- /dev/null +++ b/include/endian_str.h @@ -0,0 +1,83 @@ +/* + * 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 . + */ +#ifndef ENDIAN_STR_H_INCLUDED +#define ENDIAN_STR_H_INCLUDED + +#include +#include + +/** + * @brief Reading and writing numbers from and to streams + * in binary format with different byte orders. + */ +namespace endian +{ + +///Reads number from stream in little endian +void read_little(std::istream& str, uint8_t& x); +void read_little(std::istream& str, uint16_t& x); +void read_little(std::istream& str, uint32_t& x); +void read_little(std::istream& str, uint64_t& x); +void read_little(std::istream& str, int8_t& x); +void read_little(std::istream& str, int16_t& x); +void read_little(std::istream& str, int32_t& x); +void read_little(std::istream& str, int64_t& x); +void read_little(std::istream& str, float& x); +void read_little(std::istream& str, double& x); + +///Writes number to stream in little endian +void write_little(std::ostream& str, uint8_t x); +void write_little(std::ostream& str, uint16_t x); +void write_little(std::ostream& str, uint32_t x); +void write_little(std::ostream& str, uint64_t x); +void write_little(std::ostream& str, int8_t x); +void write_little(std::ostream& str, int16_t x); +void write_little(std::ostream& str, int32_t x); +void write_little(std::ostream& str, int64_t x); +void write_little(std::ostream& str, float x); +void write_little(std::ostream& str, double x); + +///Reads number from stream in big endian +void read_big(std::istream& str, uint8_t& x); +void read_big(std::istream& str, uint16_t& x); +void read_big(std::istream& str, uint32_t& x); +void read_big(std::istream& str, uint64_t& x); +void read_big(std::istream& str, int8_t& x); +void read_big(std::istream& str, int16_t& x); +void read_big(std::istream& str, int32_t& x); +void read_big(std::istream& str, int64_t& x); +void read_big(std::istream& str, float& x); +void read_big(std::istream& str, double& x); + +///Writes number to stream in big endian +void write_big(std::ostream& str, uint8_t x); +void write_big(std::ostream& str, uint16_t x); +void write_big(std::ostream& str, uint32_t x); +void write_big(std::ostream& str, uint64_t x); +void write_big(std::ostream& str, int8_t x); +void write_big(std::ostream& str, int16_t x); +void write_big(std::ostream& str, int32_t x); +void write_big(std::ostream& str, int64_t x); +void write_big(std::ostream& str, float x); +void write_big(std::ostream& str, double x); + +} + +#endif // ENDIAN_STR_H_INCLUDED diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 04672d2666..735ca2bb4c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,3 +4,7 @@ include_directories(${libnbt++_SOURCE_DIR}/include) add_executable(nbttest nbttest.cpp) target_link_libraries(nbttest nbt++) add_test(nbttest nbttest) + +add_executable(endian_str_test endian_str_test.cpp) +target_link_libraries(endian_str_test nbt++) +add_test(endian_str_test endian_str_test) diff --git a/test/endian_str_test.cpp b/test/endian_str_test.cpp new file mode 100644 index 0000000000..d9d01d15c5 --- /dev/null +++ b/test/endian_str_test.cpp @@ -0,0 +1,80 @@ +/* + * 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 . + */ +#include "microtest.h" +#include "endian_str.h" +#include + +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_little(str, uint32_t(0x01020304UL)); + write_little(str, uint64_t(0x0102030405060708ULL)); + + write_big (str, uint8_t (0x09)); + write_big (str, uint16_t(0x090A)); + write_big (str, uint32_t(0x090A0B0CUL)); + write_big (str, uint64_t(0x090A0B0C0D0E0F10ULL)); + + const char 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, + 0}; //Null terminator + 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 == 0x01020304UL); + read_little(str, u64); + ASSERT(u64 == 0x0102030405060708ULL); + + read_big(str, u8); + ASSERT(u8 == 0x09); + read_big(str, u16); + ASSERT(u16 == 0x090A); + read_big(str, u32); + ASSERT(u32 == 0x090A0B0CUL); + read_big(str, u64); + ASSERT(u64 == 0x090A0B0C0D0E0F10ULL); +} + +int main() +{ + test_uint(); +} -- cgit 0.0.5-2-1-g0f52