test_stream_peer_buffer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**************************************************************************/
  2. /* test_stream_peer_buffer.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_BUFFER_H
  31. #define TEST_STREAM_PEER_BUFFER_H
  32. #include "core/io/stream_peer.h"
  33. #include "tests/test_macros.h"
  34. namespace TestStreamPeerBuffer {
  35. TEST_CASE("[StreamPeerBuffer] Initialization") {
  36. Ref<StreamPeerBuffer> spb;
  37. spb.instantiate();
  38. CHECK_EQ(spb->get_size(), 0);
  39. CHECK_EQ(spb->get_position(), 0);
  40. CHECK_EQ(spb->get_available_bytes(), 0);
  41. }
  42. TEST_CASE("[StreamPeerBuffer] Seek") {
  43. Ref<StreamPeerBuffer> spb;
  44. spb.instantiate();
  45. uint8_t first = 5;
  46. uint8_t second = 7;
  47. uint8_t third = 11;
  48. spb->put_u8(first);
  49. spb->put_u8(second);
  50. spb->put_u8(third);
  51. spb->seek(0);
  52. CHECK_EQ(spb->get_u8(), first);
  53. CHECK_EQ(spb->get_u8(), second);
  54. CHECK_EQ(spb->get_u8(), third);
  55. spb->seek(1);
  56. CHECK_EQ(spb->get_position(), 1);
  57. CHECK_EQ(spb->get_u8(), second);
  58. spb->seek(1);
  59. ERR_PRINT_OFF;
  60. spb->seek(-1);
  61. ERR_PRINT_ON;
  62. CHECK_EQ(spb->get_position(), 1);
  63. ERR_PRINT_OFF;
  64. spb->seek(5);
  65. ERR_PRINT_ON;
  66. CHECK_EQ(spb->get_position(), 1);
  67. }
  68. TEST_CASE("[StreamPeerBuffer] Resize") {
  69. Ref<StreamPeerBuffer> spb;
  70. spb.instantiate();
  71. CHECK_EQ(spb->get_size(), 0);
  72. CHECK_EQ(spb->get_position(), 0);
  73. CHECK_EQ(spb->get_available_bytes(), 0);
  74. spb->resize(42);
  75. CHECK_EQ(spb->get_size(), 42);
  76. CHECK_EQ(spb->get_position(), 0);
  77. CHECK_EQ(spb->get_available_bytes(), 42);
  78. spb->seek(21);
  79. CHECK_EQ(spb->get_size(), 42);
  80. CHECK_EQ(spb->get_position(), 21);
  81. CHECK_EQ(spb->get_available_bytes(), 21);
  82. }
  83. TEST_CASE("[StreamPeerBuffer] Get underlying data array") {
  84. uint8_t first = 5;
  85. uint8_t second = 7;
  86. uint8_t third = 11;
  87. Ref<StreamPeerBuffer> spb;
  88. spb.instantiate();
  89. spb->put_u8(first);
  90. spb->put_u8(second);
  91. spb->put_u8(third);
  92. Vector<uint8_t> data_array = spb->get_data_array();
  93. CHECK_EQ(data_array[0], first);
  94. CHECK_EQ(data_array[1], second);
  95. CHECK_EQ(data_array[2], third);
  96. }
  97. TEST_CASE("[StreamPeerBuffer] Set underlying data array") {
  98. uint8_t first = 5;
  99. uint8_t second = 7;
  100. uint8_t third = 11;
  101. Ref<StreamPeerBuffer> spb;
  102. spb.instantiate();
  103. spb->put_u8(1);
  104. spb->put_u8(2);
  105. spb->put_u8(3);
  106. Vector<uint8_t> new_data_array;
  107. new_data_array.push_back(first);
  108. new_data_array.push_back(second);
  109. new_data_array.push_back(third);
  110. spb->set_data_array(new_data_array);
  111. CHECK_EQ(spb->get_u8(), first);
  112. CHECK_EQ(spb->get_u8(), second);
  113. CHECK_EQ(spb->get_u8(), third);
  114. }
  115. TEST_CASE("[StreamPeerBuffer] Duplicate") {
  116. uint8_t first = 5;
  117. uint8_t second = 7;
  118. uint8_t third = 11;
  119. Ref<StreamPeerBuffer> spb;
  120. spb.instantiate();
  121. spb->put_u8(first);
  122. spb->put_u8(second);
  123. spb->put_u8(third);
  124. Ref<StreamPeerBuffer> spb2 = spb->duplicate();
  125. CHECK_EQ(spb2->get_u8(), first);
  126. CHECK_EQ(spb2->get_u8(), second);
  127. CHECK_EQ(spb2->get_u8(), third);
  128. }
  129. TEST_CASE("[StreamPeerBuffer] Put data with size equal to zero does nothing") {
  130. Ref<StreamPeerBuffer> spb;
  131. spb.instantiate();
  132. uint8_t data = 42;
  133. Error error = spb->put_data((const uint8_t *)&data, 0);
  134. CHECK_EQ(error, OK);
  135. CHECK_EQ(spb->get_size(), 0);
  136. CHECK_EQ(spb->get_position(), 0);
  137. CHECK_EQ(spb->get_available_bytes(), 0);
  138. }
  139. TEST_CASE("[StreamPeerBuffer] Get data with invalid size returns an error") {
  140. Ref<StreamPeerBuffer> spb;
  141. spb.instantiate();
  142. uint8_t data = 42;
  143. spb->put_u8(data);
  144. spb->seek(0);
  145. uint8_t data_out = 0;
  146. Error error = spb->get_data(&data_out, 3);
  147. CHECK_EQ(error, ERR_INVALID_PARAMETER);
  148. CHECK_EQ(spb->get_size(), 1);
  149. CHECK_EQ(spb->get_position(), 1);
  150. }
  151. } // namespace TestStreamPeerBuffer
  152. #endif // TEST_STREAM_PEER_BUFFER_H