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