Skip to content

Commit 0d4d70d

Browse files
committed
Merge pull request godotengine#102587 from bruvzg/s_pa_tst
[Tests] Add `Packed*Array` `to_byte_array` variant call tests.
2 parents 3a44dc9 + 7f7f12d commit 0d4d70d

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

tests/core/templates/test_vector.h

+148
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,154 @@ TEST_CASE("[Vector] Get, set") {
215215
CHECK(vector.get(4) == 4);
216216
}
217217

218+
TEST_CASE("[Vector] To byte array (variant call)") {
219+
// PackedInt32Array.
220+
{
221+
PackedInt32Array vector[] = { { 0, -1, 2008 }, {} };
222+
PackedByteArray out[] = { { /* 0 */ 0x00, 0x00, 0x00, 0x00, /* -1 */ 0xFF, 0xFF, 0xFF, 0xFF, /* 2008 */ 0xD8, 0x07, 0x00, 0x00 }, {} };
223+
224+
for (size_t i = 0; i < std::size(vector); i++) {
225+
Callable::CallError err;
226+
Variant v_ret;
227+
Variant v_vector = vector[i];
228+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
229+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
230+
CHECK(v_ret.operator PackedByteArray() == out[i]);
231+
}
232+
}
233+
234+
// PackedInt64Array.
235+
{
236+
PackedInt64Array vector[] = { { 0, -1, 2008 }, {} };
237+
PackedByteArray out[] = { { /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* -1 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 2008 */ 0xD8, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, {} };
238+
239+
for (size_t i = 0; i < std::size(vector); i++) {
240+
Callable::CallError err;
241+
Variant v_ret;
242+
Variant v_vector = vector[i];
243+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
244+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
245+
CHECK(v_ret.operator PackedByteArray() == out[i]);
246+
}
247+
}
248+
249+
// PackedFloat32Array.
250+
{
251+
PackedFloat32Array vector[] = { { 0.0, -1.0, 200e24 }, {} };
252+
PackedByteArray out[] = { { /* 0.0 */ 0x00, 0x00, 0x00, 0x00, /* -1.0 */ 0x00, 0x00, 0x80, 0xBF, /* 200e24 */ 0xA6, 0x6F, 0x25, 0x6B }, {} };
253+
254+
for (size_t i = 0; i < std::size(vector); i++) {
255+
Callable::CallError err;
256+
Variant v_ret;
257+
Variant v_vector = vector[i];
258+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
259+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
260+
CHECK(v_ret.operator PackedByteArray() == out[i]);
261+
}
262+
}
263+
// PackedFloat64Array.
264+
{
265+
PackedFloat64Array vector[] = { { 0.0, -1.0, 200e24 }, {} };
266+
PackedByteArray out[] = { { /* 0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* -1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF, /* 200e24 */ 0x35, 0x03, 0x32, 0xB7, 0xF4, 0xAD, 0x64, 0x45 }, {} };
267+
268+
for (size_t i = 0; i < std::size(vector); i++) {
269+
Callable::CallError err;
270+
Variant v_ret;
271+
Variant v_vector = vector[i];
272+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
273+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
274+
CHECK(v_ret.operator PackedByteArray() == out[i]);
275+
}
276+
}
277+
278+
// PackedStringArray.
279+
{
280+
PackedStringArray vector[] = { { "test", "string" }, {}, { "", "test" } };
281+
PackedByteArray out[] = { { /* test */ 0x74, 0x65, 0x73, 0x74, /* null */ 0x00, /* string */ 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, /* null */ 0x00 }, {}, { /* null */ 0x00, /* test */ 0x74, 0x65, 0x73, 0x74, /* null */ 0x00 } };
282+
283+
for (size_t i = 0; i < std::size(vector); i++) {
284+
Callable::CallError err;
285+
Variant v_ret;
286+
Variant v_vector = vector[i];
287+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
288+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
289+
CHECK(v_ret.operator PackedByteArray() == out[i]);
290+
}
291+
}
292+
293+
// PackedVector2Array.
294+
{
295+
PackedVector2Array vector[] = { { Vector2(), Vector2(1, -1) }, {} };
296+
#ifdef REAL_T_IS_DOUBLE
297+
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF }, {} };
298+
#else
299+
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x80, 0xBF }, {} };
300+
#endif
301+
302+
for (size_t i = 0; i < std::size(vector); i++) {
303+
Callable::CallError err;
304+
Variant v_ret;
305+
Variant v_vector = vector[i];
306+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
307+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
308+
CHECK(v_ret.operator PackedByteArray() == out[i]);
309+
}
310+
}
311+
312+
// PackedVector3Array.
313+
{
314+
PackedVector3Array vector[] = { { Vector3(), Vector3(1, 1, -1) }, {} };
315+
#ifdef REAL_T_IS_DOUBLE
316+
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Z=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Y=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Z=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF }, {} };
317+
#else
318+
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Z=0.0 */ 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Y=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Z=-1.0 */ 0x00, 0x00, 0x80, 0xBF }, {} };
319+
#endif
320+
321+
for (size_t i = 0; i < std::size(vector); i++) {
322+
Callable::CallError err;
323+
Variant v_ret;
324+
Variant v_vector = vector[i];
325+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
326+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
327+
CHECK(v_ret.operator PackedByteArray() == out[i]);
328+
}
329+
}
330+
331+
// PackedColorArray.
332+
{
333+
PackedColorArray vector[] = { { Color(), Color(1, 1, 1) }, {} };
334+
PackedByteArray out[] = { { /* R=0.0 */ 0x00, 0x00, 0x00, 0x00, /* G=0.0 */ 0x00, 0x00, 0x00, 0x00, /* B=0.0 */ 0x00, 0x00, 0x00, 0x00, /* A=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* R=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* G=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* B=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* A=1.0 */ 0x00, 0x00, 0x80, 0x3F }, {} };
335+
336+
for (size_t i = 0; i < std::size(vector); i++) {
337+
Callable::CallError err;
338+
Variant v_ret;
339+
Variant v_vector = vector[i];
340+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
341+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
342+
CHECK(v_ret.operator PackedByteArray() == out[i]);
343+
}
344+
}
345+
346+
// PackedVector4Array.
347+
{
348+
PackedVector4Array vector[] = { { Vector4(), Vector4(1, -1, 1, -1) }, {} };
349+
#ifdef REAL_T_IS_DOUBLE
350+
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Z 0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* W=0.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* X=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF, /* Z=1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, /* W=-1.0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xBF }, {} };
351+
#else
352+
PackedByteArray out[] = { { /* X=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Y=0.0 */ 0x00, 0x00, 0x00, 0x00, /* Z=0.0 */ 0x00, 0x00, 0x00, 0x00, /* W 0.0 */ 0x00, 0x00, 0x00, 0x00, /* X 1.0 */ 0x00, 0x00, 0x80, 0x3F, /* Y=-1.0 */ 0x00, 0x00, 0x80, 0xBF, /* Z=1.0 */ 0x00, 0x00, 0x80, 0x3F, /* W=-1.0 */ 0x00, 0x00, 0x80, 0xBF }, {} };
353+
#endif
354+
355+
for (size_t i = 0; i < std::size(vector); i++) {
356+
Callable::CallError err;
357+
Variant v_ret;
358+
Variant v_vector = vector[i];
359+
v_vector.callp("to_byte_array", nullptr, 0, v_ret, err);
360+
CHECK(v_ret.get_type() == Variant::PACKED_BYTE_ARRAY);
361+
CHECK(v_ret.operator PackedByteArray() == out[i]);
362+
}
363+
}
364+
}
365+
218366
TEST_CASE("[Vector] To byte array") {
219367
Vector<int> vector;
220368
vector.push_back(0);

0 commit comments

Comments
 (0)