Merge pull request #95784 from pafuent/adding_stream_peer_buffer_tests

Add unit tests for `StreamPeer` and `StreamPeerBuffer`
This commit is contained in:
Rémi Verschelde 2024-10-01 17:30:33 +02:00
commit d71d95448c
No known key found for this signature in database
GPG key ID: C3336907360768E1
3 changed files with 476 additions and 0 deletions

View file

@ -0,0 +1,289 @@
/**************************************************************************/
/* test_stream_peer.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef TEST_STREAM_PEER_H
#define TEST_STREAM_PEER_H
#include "core/io/stream_peer.h"
#include "tests/test_macros.h"
namespace TestStreamPeer {
TEST_CASE("[StreamPeer] Initialization through StreamPeerBuffer") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
CHECK_EQ(spb->is_big_endian_enabled(), false);
}
TEST_CASE("[StreamPeer] Get and sets through StreamPeerBuffer") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
SUBCASE("A int8_t value") {
int8_t value = 42;
spb->clear();
spb->put_8(value);
spb->seek(0);
CHECK_EQ(spb->get_8(), value);
}
SUBCASE("A uint8_t value") {
uint8_t value = 42;
spb->clear();
spb->put_u8(value);
spb->seek(0);
CHECK_EQ(spb->get_u8(), value);
}
SUBCASE("A int16_t value") {
int16_t value = 42;
spb->clear();
spb->put_16(value);
spb->seek(0);
CHECK_EQ(spb->get_16(), value);
}
SUBCASE("A uint16_t value") {
uint16_t value = 42;
spb->clear();
spb->put_u16(value);
spb->seek(0);
CHECK_EQ(spb->get_u16(), value);
}
SUBCASE("A int32_t value") {
int32_t value = 42;
spb->clear();
spb->put_32(value);
spb->seek(0);
CHECK_EQ(spb->get_32(), value);
}
SUBCASE("A uint32_t value") {
uint32_t value = 42;
spb->clear();
spb->put_u32(value);
spb->seek(0);
CHECK_EQ(spb->get_u32(), value);
}
SUBCASE("A int64_t value") {
int64_t value = 42;
spb->clear();
spb->put_64(value);
spb->seek(0);
CHECK_EQ(spb->get_64(), value);
}
SUBCASE("A int64_t value") {
uint64_t value = 42;
spb->clear();
spb->put_u64(value);
spb->seek(0);
CHECK_EQ(spb->get_u64(), value);
}
SUBCASE("A float value") {
float value = 42.0f;
spb->clear();
spb->put_float(value);
spb->seek(0);
CHECK_EQ(spb->get_float(), value);
}
SUBCASE("A double value") {
double value = 42.0;
spb->clear();
spb->put_double(value);
spb->seek(0);
CHECK_EQ(spb->get_double(), value);
}
SUBCASE("A string value") {
String value = "Hello, World!";
spb->clear();
spb->put_string(value);
spb->seek(0);
CHECK_EQ(spb->get_string(), value);
}
SUBCASE("A utf8 string value") {
String value = String::utf8("Hello✩, World✩!");
spb->clear();
spb->put_utf8_string(value);
spb->seek(0);
CHECK_EQ(spb->get_utf8_string(), value);
}
SUBCASE("A variant value") {
Array value;
value.push_front(42);
value.push_front("Hello, World!");
spb->clear();
spb->put_var(value);
spb->seek(0);
CHECK_EQ(spb->get_var(), value);
}
}
TEST_CASE("[StreamPeer] Get and sets big endian through StreamPeerBuffer") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
spb->set_big_endian(true);
SUBCASE("A int16_t value") {
int16_t value = 42;
spb->clear();
spb->put_16(value);
spb->seek(0);
CHECK_EQ(spb->get_16(), value);
}
SUBCASE("A uint16_t value") {
uint16_t value = 42;
spb->clear();
spb->put_u16(value);
spb->seek(0);
CHECK_EQ(spb->get_u16(), value);
}
SUBCASE("A int32_t value") {
int32_t value = 42;
spb->clear();
spb->put_32(value);
spb->seek(0);
CHECK_EQ(spb->get_32(), value);
}
SUBCASE("A uint32_t value") {
uint32_t value = 42;
spb->clear();
spb->put_u32(value);
spb->seek(0);
CHECK_EQ(spb->get_u32(), value);
}
SUBCASE("A int64_t value") {
int64_t value = 42;
spb->clear();
spb->put_64(value);
spb->seek(0);
CHECK_EQ(spb->get_64(), value);
}
SUBCASE("A int64_t value") {
uint64_t value = 42;
spb->clear();
spb->put_u64(value);
spb->seek(0);
CHECK_EQ(spb->get_u64(), value);
}
SUBCASE("A float value") {
float value = 42.0f;
spb->clear();
spb->put_float(value);
spb->seek(0);
CHECK_EQ(spb->get_float(), value);
}
SUBCASE("A double value") {
double value = 42.0;
spb->clear();
spb->put_double(value);
spb->seek(0);
CHECK_EQ(spb->get_double(), value);
}
}
TEST_CASE("[StreamPeer] Get string when there is no string") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
ERR_PRINT_OFF;
CHECK_EQ(spb->get_string(), "");
ERR_PRINT_ON;
}
TEST_CASE("[StreamPeer] Get UTF8 string when there is no string") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
ERR_PRINT_OFF;
CHECK_EQ(spb->get_utf8_string(), "");
ERR_PRINT_ON;
}
} // namespace TestStreamPeer
#endif // TEST_STREAM_PEER_H

View file

@ -0,0 +1,185 @@
/**************************************************************************/
/* test_stream_peer_buffer.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef TEST_STREAM_PEER_BUFFER_H
#define TEST_STREAM_PEER_BUFFER_H
#include "core/io/stream_peer.h"
#include "tests/test_macros.h"
namespace TestStreamPeerBuffer {
TEST_CASE("[StreamPeerBuffer] Initialization") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
CHECK_EQ(spb->get_size(), 0);
CHECK_EQ(spb->get_position(), 0);
CHECK_EQ(spb->get_available_bytes(), 0);
}
TEST_CASE("[StreamPeerBuffer] Seek") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
uint8_t first = 5;
uint8_t second = 7;
uint8_t third = 11;
spb->put_u8(first);
spb->put_u8(second);
spb->put_u8(third);
spb->seek(0);
CHECK_EQ(spb->get_u8(), first);
CHECK_EQ(spb->get_u8(), second);
CHECK_EQ(spb->get_u8(), third);
spb->seek(1);
CHECK_EQ(spb->get_position(), 1);
CHECK_EQ(spb->get_u8(), second);
spb->seek(1);
ERR_PRINT_OFF;
spb->seek(-1);
ERR_PRINT_ON;
CHECK_EQ(spb->get_position(), 1);
ERR_PRINT_OFF;
spb->seek(5);
ERR_PRINT_ON;
CHECK_EQ(spb->get_position(), 1);
}
TEST_CASE("[StreamPeerBuffer] Resize") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
CHECK_EQ(spb->get_size(), 0);
CHECK_EQ(spb->get_position(), 0);
CHECK_EQ(spb->get_available_bytes(), 0);
spb->resize(42);
CHECK_EQ(spb->get_size(), 42);
CHECK_EQ(spb->get_position(), 0);
CHECK_EQ(spb->get_available_bytes(), 42);
spb->seek(21);
CHECK_EQ(spb->get_size(), 42);
CHECK_EQ(spb->get_position(), 21);
CHECK_EQ(spb->get_available_bytes(), 21);
}
TEST_CASE("[StreamPeerBuffer] Get underlying data array") {
uint8_t first = 5;
uint8_t second = 7;
uint8_t third = 11;
Ref<StreamPeerBuffer> spb;
spb.instantiate();
spb->put_u8(first);
spb->put_u8(second);
spb->put_u8(third);
Vector<uint8_t> data_array = spb->get_data_array();
CHECK_EQ(data_array[0], first);
CHECK_EQ(data_array[1], second);
CHECK_EQ(data_array[2], third);
}
TEST_CASE("[StreamPeerBuffer] Set underlying data array") {
uint8_t first = 5;
uint8_t second = 7;
uint8_t third = 11;
Ref<StreamPeerBuffer> spb;
spb.instantiate();
spb->put_u8(1);
spb->put_u8(2);
spb->put_u8(3);
Vector<uint8_t> new_data_array;
new_data_array.push_back(first);
new_data_array.push_back(second);
new_data_array.push_back(third);
spb->set_data_array(new_data_array);
CHECK_EQ(spb->get_u8(), first);
CHECK_EQ(spb->get_u8(), second);
CHECK_EQ(spb->get_u8(), third);
}
TEST_CASE("[StreamPeerBuffer] Duplicate") {
uint8_t first = 5;
uint8_t second = 7;
uint8_t third = 11;
Ref<StreamPeerBuffer> spb;
spb.instantiate();
spb->put_u8(first);
spb->put_u8(second);
spb->put_u8(third);
Ref<StreamPeerBuffer> spb2 = spb->duplicate();
CHECK_EQ(spb2->get_u8(), first);
CHECK_EQ(spb2->get_u8(), second);
CHECK_EQ(spb2->get_u8(), third);
}
TEST_CASE("[StreamPeerBuffer] Put data with size equal to zero does nothing") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
uint8_t data = 42;
Error error = spb->put_data((const uint8_t *)&data, 0);
CHECK_EQ(error, OK);
CHECK_EQ(spb->get_size(), 0);
CHECK_EQ(spb->get_position(), 0);
CHECK_EQ(spb->get_available_bytes(), 0);
}
TEST_CASE("[StreamPeerBuffer] Get data with invalid size returns an error") {
Ref<StreamPeerBuffer> spb;
spb.instantiate();
uint8_t data = 42;
spb->put_u8(data);
spb->seek(0);
uint8_t data_out = 0;
Error error = spb->get_data(&data_out, 3);
CHECK_EQ(error, ERR_INVALID_PARAMETER);
CHECK_EQ(spb->get_size(), 1);
CHECK_EQ(spb->get_position(), 1);
}
} // namespace TestStreamPeerBuffer
#endif // TEST_STREAM_PEER_BUFFER_H

View file

@ -52,6 +52,8 @@
#include "tests/core/io/test_marshalls.h"
#include "tests/core/io/test_pck_packer.h"
#include "tests/core/io/test_resource.h"
#include "tests/core/io/test_stream_peer.h"
#include "tests/core/io/test_stream_peer_buffer.h"
#include "tests/core/io/test_xml_parser.h"
#include "tests/core/math/test_aabb.h"
#include "tests/core/math/test_astar.h"