Debian Codegraph @

tests/core/io/test_stream_peer_buffer.h

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