test_stream_peer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #ifndef TEST_STREAM_PEER_H
  31. #define TEST_STREAM_PEER_H
  32. #include "core/io/stream_peer.h"
  33. #include "tests/test_macros.h"
  34. namespace TestStreamPeer {
  35. TEST_CASE("[StreamPeer] Initialization through StreamPeerBuffer") {
  36. Ref<StreamPeerBuffer> spb;
  37. spb.instantiate();
  38. CHECK_EQ(spb->is_big_endian_enabled(), false);
  39. }
  40. TEST_CASE("[StreamPeer] Get and sets through StreamPeerBuffer") {
  41. Ref<StreamPeerBuffer> spb;
  42. spb.instantiate();
  43. SUBCASE("A int8_t value") {
  44. int8_t value = 42;
  45. spb->clear();
  46. spb->put_8(value);
  47. spb->seek(0);
  48. CHECK_EQ(spb->get_8(), value);
  49. }
  50. SUBCASE("A uint8_t value") {
  51. uint8_t value = 42;
  52. spb->clear();
  53. spb->put_u8(value);
  54. spb->seek(0);
  55. CHECK_EQ(spb->get_u8(), value);
  56. }
  57. SUBCASE("A int16_t value") {
  58. int16_t value = 42;
  59. spb->clear();
  60. spb->put_16(value);
  61. spb->seek(0);
  62. CHECK_EQ(spb->get_16(), value);
  63. }
  64. SUBCASE("A uint16_t value") {
  65. uint16_t value = 42;
  66. spb->clear();
  67. spb->put_u16(value);
  68. spb->seek(0);
  69. CHECK_EQ(spb->get_u16(), value);
  70. }
  71. SUBCASE("A int32_t value") {
  72. int32_t value = 42;
  73. spb->clear();
  74. spb->put_32(value);
  75. spb->seek(0);
  76. CHECK_EQ(spb->get_32(), value);
  77. }
  78. SUBCASE("A uint32_t value") {
  79. uint32_t value = 42;
  80. spb->clear();
  81. spb->put_u32(value);
  82. spb->seek(0);
  83. CHECK_EQ(spb->get_u32(), value);
  84. }
  85. SUBCASE("A int64_t value") {
  86. int64_t value = 42;
  87. spb->clear();
  88. spb->put_64(value);
  89. spb->seek(0);
  90. CHECK_EQ(spb->get_64(), value);
  91. }
  92. SUBCASE("A int64_t value") {
  93. uint64_t value = 42;
  94. spb->clear();
  95. spb->put_u64(value);
  96. spb->seek(0);
  97. CHECK_EQ(spb->get_u64(), value);
  98. }
  99. SUBCASE("A half-precision float value") {
  100. float value = 3.1415927f;
  101. float expected = 3.14062f;
  102. spb->clear();
  103. spb->put_half(value);
  104. spb->seek(0);
  105. CHECK(spb->get_half() == doctest::Approx(expected));
  106. }
  107. SUBCASE("A float value") {
  108. float value = 42.0f;
  109. spb->clear();
  110. spb->put_float(value);
  111. spb->seek(0);
  112. CHECK_EQ(spb->get_float(), value);
  113. }
  114. SUBCASE("A double value") {
  115. double value = 42.0;
  116. spb->clear();
  117. spb->put_double(value);
  118. spb->seek(0);
  119. CHECK_EQ(spb->get_double(), value);
  120. }
  121. SUBCASE("A string value") {
  122. String value = "Hello, World!";
  123. spb->clear();
  124. spb->put_string(value);
  125. spb->seek(0);
  126. CHECK_EQ(spb->get_string(), value);
  127. }
  128. SUBCASE("A utf8 string value") {
  129. String value = String::utf8("Hello✩, World✩!");
  130. spb->clear();
  131. spb->put_utf8_string(value);
  132. spb->seek(0);
  133. CHECK_EQ(spb->get_utf8_string(), value);
  134. }
  135. SUBCASE("A variant value") {
  136. Array value;
  137. value.push_front(42);
  138. value.push_front("Hello, World!");
  139. spb->clear();
  140. spb->put_var(value);
  141. spb->seek(0);
  142. CHECK_EQ(spb->get_var(), value);
  143. }
  144. }
  145. TEST_CASE("[StreamPeer] Get and sets big endian through StreamPeerBuffer") {
  146. Ref<StreamPeerBuffer> spb;
  147. spb.instantiate();
  148. spb->set_big_endian(true);
  149. SUBCASE("A int16_t value") {
  150. int16_t value = 42;
  151. spb->clear();
  152. spb->put_16(value);
  153. spb->seek(0);
  154. CHECK_EQ(spb->get_16(), value);
  155. }
  156. SUBCASE("A uint16_t value") {
  157. uint16_t value = 42;
  158. spb->clear();
  159. spb->put_u16(value);
  160. spb->seek(0);
  161. CHECK_EQ(spb->get_u16(), value);
  162. }
  163. SUBCASE("A int32_t value") {
  164. int32_t value = 42;
  165. spb->clear();
  166. spb->put_32(value);
  167. spb->seek(0);
  168. CHECK_EQ(spb->get_32(), value);
  169. }
  170. SUBCASE("A uint32_t value") {
  171. uint32_t value = 42;
  172. spb->clear();
  173. spb->put_u32(value);
  174. spb->seek(0);
  175. CHECK_EQ(spb->get_u32(), value);
  176. }
  177. SUBCASE("A int64_t value") {
  178. int64_t value = 42;
  179. spb->clear();
  180. spb->put_64(value);
  181. spb->seek(0);
  182. CHECK_EQ(spb->get_64(), value);
  183. }
  184. SUBCASE("A int64_t value") {
  185. uint64_t value = 42;
  186. spb->clear();
  187. spb->put_u64(value);
  188. spb->seek(0);
  189. CHECK_EQ(spb->get_u64(), value);
  190. }
  191. SUBCASE("A float value") {
  192. float value = 42.0f;
  193. spb->clear();
  194. spb->put_float(value);
  195. spb->seek(0);
  196. CHECK_EQ(spb->get_float(), value);
  197. }
  198. SUBCASE("A half-precision float value") {
  199. float value = 3.1415927f;
  200. float expected = 3.14062f;
  201. spb->clear();
  202. spb->put_half(value);
  203. spb->seek(0);
  204. CHECK(spb->get_half() == doctest::Approx(expected));
  205. }
  206. SUBCASE("A double value") {
  207. double value = 42.0;
  208. spb->clear();
  209. spb->put_double(value);
  210. spb->seek(0);
  211. CHECK_EQ(spb->get_double(), value);
  212. }
  213. }
  214. TEST_CASE("[StreamPeer] Get string when there is no string") {
  215. Ref<StreamPeerBuffer> spb;
  216. spb.instantiate();
  217. ERR_PRINT_OFF;
  218. CHECK_EQ(spb->get_string(), "");
  219. ERR_PRINT_ON;
  220. }
  221. TEST_CASE("[StreamPeer] Get UTF8 string when there is no string") {
  222. Ref<StreamPeerBuffer> spb;
  223. spb.instantiate();
  224. ERR_PRINT_OFF;
  225. CHECK_EQ(spb->get_utf8_string(), "");
  226. ERR_PRINT_ON;
  227. }
  228. } // namespace TestStreamPeer
  229. #endif // TEST_STREAM_PEER_H