test_stream_peer_buffer.h 5.5 KB

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