test_stream_peer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**************************************************************************/
  2. /* test_stream_peer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/io/stream_peer.h"
  32. #include "tests/test_macros.h"
  33. namespace TestStreamPeer {
  34. TEST_CASE("[StreamPeer] Initialization through StreamPeerBuffer") {
  35. Ref<StreamPeerBuffer> spb;
  36. spb.instantiate();
  37. CHECK_EQ(spb->is_big_endian_enabled(), false);
  38. }
  39. TEST_CASE("[StreamPeer] Get and sets through StreamPeerBuffer") {
  40. Ref<StreamPeerBuffer> spb;
  41. spb.instantiate();
  42. SUBCASE("A int8_t value") {
  43. int8_t value = 42;
  44. spb->clear();
  45. spb->put_8(value);
  46. spb->seek(0);
  47. CHECK_EQ(spb->get_8(), value);
  48. }
  49. SUBCASE("A uint8_t value") {
  50. uint8_t value = 42;
  51. spb->clear();
  52. spb->put_u8(value);
  53. spb->seek(0);
  54. CHECK_EQ(spb->get_u8(), value);
  55. }
  56. SUBCASE("A int16_t value") {
  57. int16_t value = 42;
  58. spb->clear();
  59. spb->put_16(value);
  60. spb->seek(0);
  61. CHECK_EQ(spb->get_16(), value);
  62. }
  63. SUBCASE("A uint16_t value") {
  64. uint16_t value = 42;
  65. spb->clear();
  66. spb->put_u16(value);
  67. spb->seek(0);
  68. CHECK_EQ(spb->get_u16(), value);
  69. }
  70. SUBCASE("A int32_t value") {
  71. int32_t value = 42;
  72. spb->clear();
  73. spb->put_32(value);
  74. spb->seek(0);
  75. CHECK_EQ(spb->get_32(), value);
  76. }
  77. SUBCASE("A uint32_t value") {
  78. uint32_t value = 42;
  79. spb->clear();
  80. spb->put_u32(value);
  81. spb->seek(0);
  82. CHECK_EQ(spb->get_u32(), value);
  83. }
  84. SUBCASE("A int64_t value") {
  85. int64_t value = 42;
  86. spb->clear();
  87. spb->put_64(value);
  88. spb->seek(0);
  89. CHECK_EQ(spb->get_64(), value);
  90. }
  91. SUBCASE("A int64_t value") {
  92. uint64_t value = 42;
  93. spb->clear();
  94. spb->put_u64(value);
  95. spb->seek(0);
  96. CHECK_EQ(spb->get_u64(), value);
  97. }
  98. SUBCASE("A half-precision float value") {
  99. float value = 3.1415927f;
  100. float expected = 3.14062f;
  101. spb->clear();
  102. spb->put_half(value);
  103. spb->seek(0);
  104. CHECK(spb->get_half() == doctest::Approx(expected));
  105. }
  106. SUBCASE("A float value") {
  107. float value = 42.0f;
  108. spb->clear();
  109. spb->put_float(value);
  110. spb->seek(0);
  111. CHECK_EQ(spb->get_float(), value);
  112. }
  113. SUBCASE("A double value") {
  114. double value = 42.0;
  115. spb->clear();
  116. spb->put_double(value);
  117. spb->seek(0);
  118. CHECK_EQ(spb->get_double(), value);
  119. }
  120. SUBCASE("A string value") {
  121. String value = "Hello, World!";
  122. spb->clear();
  123. spb->put_string(value);
  124. spb->seek(0);
  125. CHECK_EQ(spb->get_string(), value);
  126. }
  127. SUBCASE("A utf8 string value") {
  128. String value = String::utf8("Hello✩, World✩!");
  129. spb->clear();
  130. spb->put_utf8_string(value);
  131. spb->seek(0);
  132. CHECK_EQ(spb->get_utf8_string(), value);
  133. }
  134. SUBCASE("A variant value") {
  135. Array value;
  136. value.push_front(42);
  137. value.push_front("Hello, World!");
  138. spb->clear();
  139. spb->put_var(value);
  140. spb->seek(0);
  141. CHECK_EQ(spb->get_var(), value);
  142. }
  143. }
  144. TEST_CASE("[StreamPeer] Get and sets big endian through StreamPeerBuffer") {
  145. Ref<StreamPeerBuffer> spb;
  146. spb.instantiate();
  147. spb->set_big_endian(true);
  148. SUBCASE("A int16_t value") {
  149. int16_t value = 42;
  150. spb->clear();
  151. spb->put_16(value);
  152. spb->seek(0);
  153. CHECK_EQ(spb->get_16(), value);
  154. }
  155. SUBCASE("A uint16_t value") {
  156. uint16_t value = 42;
  157. spb->clear();
  158. spb->put_u16(value);
  159. spb->seek(0);
  160. CHECK_EQ(spb->get_u16(), value);
  161. }
  162. SUBCASE("A int32_t value") {
  163. int32_t value = 42;
  164. spb->clear();
  165. spb->put_32(value);
  166. spb->seek(0);
  167. CHECK_EQ(spb->get_32(), value);
  168. }
  169. SUBCASE("A uint32_t value") {
  170. uint32_t value = 42;
  171. spb->clear();
  172. spb->put_u32(value);
  173. spb->seek(0);
  174. CHECK_EQ(spb->get_u32(), value);
  175. }
  176. SUBCASE("A int64_t value") {
  177. int64_t value = 42;
  178. spb->clear();
  179. spb->put_64(value);
  180. spb->seek(0);
  181. CHECK_EQ(spb->get_64(), value);
  182. }
  183. SUBCASE("A int64_t value") {
  184. uint64_t value = 42;
  185. spb->clear();
  186. spb->put_u64(value);
  187. spb->seek(0);
  188. CHECK_EQ(spb->get_u64(), value);
  189. }
  190. SUBCASE("A float value") {
  191. float value = 42.0f;
  192. spb->clear();
  193. spb->put_float(value);
  194. spb->seek(0);
  195. CHECK_EQ(spb->get_float(), value);
  196. }
  197. SUBCASE("A half-precision float value") {
  198. float value = 3.1415927f;
  199. float expected = 3.14062f;
  200. spb->clear();
  201. spb->put_half(value);
  202. spb->seek(0);
  203. CHECK(spb->get_half() == doctest::Approx(expected));
  204. }
  205. SUBCASE("A double value") {
  206. double value = 42.0;
  207. spb->clear();
  208. spb->put_double(value);
  209. spb->seek(0);
  210. CHECK_EQ(spb->get_double(), value);
  211. }
  212. }
  213. TEST_CASE("[StreamPeer] Get string when there is no string") {
  214. Ref<StreamPeerBuffer> spb;
  215. spb.instantiate();
  216. ERR_PRINT_OFF;
  217. CHECK_EQ(spb->get_string(), "");
  218. ERR_PRINT_ON;
  219. }
  220. TEST_CASE("[StreamPeer] Get UTF8 string when there is no string") {
  221. Ref<StreamPeerBuffer> spb;
  222. spb.instantiate();
  223. ERR_PRINT_OFF;
  224. CHECK_EQ(spb->get_utf8_string(), "");
  225. ERR_PRINT_ON;
  226. }
  227. } // namespace TestStreamPeer