endian_str_test.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * libnbt++ - A library for the Minecraft Named Binary Tag format.
  3. * Copyright (C) 2013, 2015 ljfa-ag
  4. *
  5. * This file is part of libnbt++.
  6. *
  7. * libnbt++ is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * libnbt++ is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <cxxtest/TestSuite.h>
  21. #include "endian_str.h"
  22. #include <cstdlib>
  23. #include <iostream>
  24. #include <sstream>
  25. using namespace endian;
  26. class endian_str_test : public CxxTest::TestSuite
  27. {
  28. public:
  29. void test_uint()
  30. {
  31. std::stringstream str(std::ios::in | std::ios::out | std::ios::binary);
  32. write_little(str, uint8_t (0x01));
  33. write_little(str, uint16_t(0x0102));
  34. write (str, uint32_t(0x01020304), little);
  35. write_little(str, uint64_t(0x0102030405060708));
  36. write_big (str, uint8_t (0x09));
  37. write_big (str, uint16_t(0x090A));
  38. write_big (str, uint32_t(0x090A0B0C));
  39. write (str, uint64_t(0x090A0B0C0D0E0F10), big);
  40. std::string expected{
  41. 1,
  42. 2, 1,
  43. 4, 3, 2, 1,
  44. 8, 7, 6, 5, 4, 3, 2, 1,
  45. 9,
  46. 9, 10,
  47. 9, 10, 11, 12,
  48. 9, 10, 11, 12, 13, 14, 15, 16
  49. };
  50. TS_ASSERT_EQUALS(str.str(), expected);
  51. uint8_t u8;
  52. uint16_t u16;
  53. uint32_t u32;
  54. uint64_t u64;
  55. read_little(str, u8);
  56. TS_ASSERT_EQUALS(u8, 0x01);
  57. read_little(str, u16);
  58. TS_ASSERT_EQUALS(u16, 0x0102);
  59. read_little(str, u32);
  60. TS_ASSERT_EQUALS(u32, 0x01020304u);
  61. read(str, u64, little);
  62. TS_ASSERT_EQUALS(u64, 0x0102030405060708u);
  63. read_big(str, u8);
  64. TS_ASSERT_EQUALS(u8, 0x09);
  65. read_big(str, u16);
  66. TS_ASSERT_EQUALS(u16, 0x090A);
  67. read(str, u32, big);
  68. TS_ASSERT_EQUALS(u32, 0x090A0B0Cu);
  69. read_big(str, u64);
  70. TS_ASSERT_EQUALS(u64, 0x090A0B0C0D0E0F10u);
  71. TS_ASSERT(str); //Check if stream has failed
  72. }
  73. void test_sint()
  74. {
  75. std::stringstream str(std::ios::in | std::ios::out | std::ios::binary);
  76. write_little(str, int8_t (-0x01));
  77. write_little(str, int16_t(-0x0102));
  78. write_little(str, int32_t(-0x01020304));
  79. write (str, int64_t(-0x0102030405060708), little);
  80. write_big (str, int8_t (-0x09));
  81. write_big (str, int16_t(-0x090A));
  82. write (str, int32_t(-0x090A0B0C), big);
  83. write_big (str, int64_t(-0x090A0B0C0D0E0F10));
  84. std::string expected{ //meh, stupid narrowing conversions
  85. '\xFF',
  86. '\xFE', '\xFE',
  87. '\xFC', '\xFC', '\xFD', '\xFE',
  88. '\xF8', '\xF8', '\xF9', '\xFA', '\xFB', '\xFC', '\xFD', '\xFE',
  89. '\xF7',
  90. '\xF6', '\xF6',
  91. '\xF6', '\xF5', '\xF4', '\xF4',
  92. '\xF6', '\xF5', '\xF4', '\xF3', '\xF2', '\xF1', '\xF0', '\xF0'
  93. };
  94. TS_ASSERT_EQUALS(str.str(), expected);
  95. int8_t i8;
  96. int16_t i16;
  97. int32_t i32;
  98. int64_t i64;
  99. read_little(str, i8);
  100. TS_ASSERT_EQUALS(i8, -0x01);
  101. read_little(str, i16);
  102. TS_ASSERT_EQUALS(i16, -0x0102);
  103. read(str, i32, little);
  104. TS_ASSERT_EQUALS(i32, -0x01020304);
  105. read_little(str, i64);
  106. TS_ASSERT_EQUALS(i64, -0x0102030405060708);
  107. read_big(str, i8);
  108. TS_ASSERT_EQUALS(i8, -0x09);
  109. read_big(str, i16);
  110. TS_ASSERT_EQUALS(i16, -0x090A);
  111. read_big(str, i32);
  112. TS_ASSERT_EQUALS(i32, -0x090A0B0C);
  113. read(str, i64, big);
  114. TS_ASSERT_EQUALS(i64, -0x090A0B0C0D0E0F10);
  115. TS_ASSERT(str); //Check if stream has failed
  116. }
  117. void test_float()
  118. {
  119. std::stringstream str(std::ios::in | std::ios::out | std::ios::binary);
  120. //C99 has hexadecimal floating point literals, C++ doesn't...
  121. const float fconst = std::stof("-0xCDEF01p-63"); //-1.46325e-012
  122. const double dconst = std::stod("-0x1DEF0102030405p-375"); //-1.09484e-097
  123. //We will be assuming IEEE 754 here
  124. write_little(str, fconst);
  125. write_little(str, dconst);
  126. write_big (str, fconst);
  127. write_big (str, dconst);
  128. std::string expected{
  129. '\x01', '\xEF', '\xCD', '\xAB',
  130. '\x05', '\x04', '\x03', '\x02', '\x01', '\xEF', '\xCD', '\xAB',
  131. '\xAB', '\xCD', '\xEF', '\x01',
  132. '\xAB', '\xCD', '\xEF', '\x01', '\x02', '\x03', '\x04', '\x05'
  133. };
  134. TS_ASSERT_EQUALS(str.str(), expected);
  135. float f;
  136. double d;
  137. read_little(str, f);
  138. TS_ASSERT_EQUALS(f, fconst);
  139. read_little(str, d);
  140. TS_ASSERT_EQUALS(d, dconst);
  141. read_big(str, f);
  142. TS_ASSERT_EQUALS(f, fconst);
  143. read_big(str, d);
  144. TS_ASSERT_EQUALS(d, dconst);
  145. TS_ASSERT(str); //Check if stream has failed
  146. }
  147. };