Path: blob/master/tests/core/io/test_stream_peer.cpp
45997 views
/**************************************************************************/1/* test_stream_peer.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_stream_peer)3334#include "core/io/stream_peer.h"3536namespace TestStreamPeer {3738TEST_CASE("[StreamPeer] Initialization through StreamPeerBuffer") {39Ref<StreamPeerBuffer> spb;40spb.instantiate();4142CHECK_EQ(spb->is_big_endian_enabled(), false);43}4445TEST_CASE("[StreamPeer] Get and sets through StreamPeerBuffer") {46Ref<StreamPeerBuffer> spb;47spb.instantiate();4849SUBCASE("A int8_t value") {50int8_t value = 42;5152spb->clear();53spb->put_8(value);54spb->seek(0);5556CHECK_EQ(spb->get_8(), value);57}5859SUBCASE("A uint8_t value") {60uint8_t value = 42;6162spb->clear();63spb->put_u8(value);64spb->seek(0);6566CHECK_EQ(spb->get_u8(), value);67}6869SUBCASE("A int16_t value") {70int16_t value = 42;7172spb->clear();73spb->put_16(value);74spb->seek(0);7576CHECK_EQ(spb->get_16(), value);77}7879SUBCASE("A uint16_t value") {80uint16_t value = 42;8182spb->clear();83spb->put_u16(value);84spb->seek(0);8586CHECK_EQ(spb->get_u16(), value);87}8889SUBCASE("A int32_t value") {90int32_t value = 42;9192spb->clear();93spb->put_32(value);94spb->seek(0);9596CHECK_EQ(spb->get_32(), value);97}9899SUBCASE("A uint32_t value") {100uint32_t value = 42;101102spb->clear();103spb->put_u32(value);104spb->seek(0);105106CHECK_EQ(spb->get_u32(), value);107}108109SUBCASE("A int64_t value") {110int64_t value = 42;111112spb->clear();113spb->put_64(value);114spb->seek(0);115116CHECK_EQ(spb->get_64(), value);117}118119SUBCASE("A int64_t value") {120uint64_t value = 42;121122spb->clear();123spb->put_u64(value);124spb->seek(0);125126CHECK_EQ(spb->get_u64(), value);127}128129SUBCASE("A half-precision float value") {130float value = 3.1415927f;131float expected = 3.14062f;132133spb->clear();134spb->put_half(value);135spb->seek(0);136137CHECK(spb->get_half() == doctest::Approx(expected));138}139140SUBCASE("A float value") {141float value = 42.0f;142143spb->clear();144spb->put_float(value);145spb->seek(0);146147CHECK_EQ(spb->get_float(), value);148}149150SUBCASE("A double value") {151double value = 42.0;152153spb->clear();154spb->put_double(value);155spb->seek(0);156157CHECK_EQ(spb->get_double(), value);158}159160SUBCASE("A string value") {161String value = "Hello, World!";162163spb->clear();164spb->put_string(value);165spb->seek(0);166167CHECK_EQ(spb->get_string(), value);168}169170SUBCASE("A utf8 string value") {171String value = String::utf8("Hello✩, World✩!");172173spb->clear();174spb->put_utf8_string(value);175spb->seek(0);176177CHECK_EQ(spb->get_utf8_string(), value);178}179180SUBCASE("A variant value") {181Array value;182value.push_front(42);183value.push_front("Hello, World!");184185spb->clear();186spb->put_var(value);187spb->seek(0);188189CHECK_EQ(spb->get_var(), value);190}191}192193TEST_CASE("[StreamPeer] Get and sets big endian through StreamPeerBuffer") {194Ref<StreamPeerBuffer> spb;195spb.instantiate();196spb->set_big_endian(true);197198SUBCASE("A int16_t value") {199int16_t value = 42;200201spb->clear();202spb->put_16(value);203spb->seek(0);204205CHECK_EQ(spb->get_16(), value);206}207208SUBCASE("A uint16_t value") {209uint16_t value = 42;210211spb->clear();212spb->put_u16(value);213spb->seek(0);214215CHECK_EQ(spb->get_u16(), value);216}217218SUBCASE("A int32_t value") {219int32_t value = 42;220221spb->clear();222spb->put_32(value);223spb->seek(0);224225CHECK_EQ(spb->get_32(), value);226}227228SUBCASE("A uint32_t value") {229uint32_t value = 42;230231spb->clear();232spb->put_u32(value);233spb->seek(0);234235CHECK_EQ(spb->get_u32(), value);236}237238SUBCASE("A int64_t value") {239int64_t value = 42;240241spb->clear();242spb->put_64(value);243spb->seek(0);244245CHECK_EQ(spb->get_64(), value);246}247248SUBCASE("A int64_t value") {249uint64_t value = 42;250251spb->clear();252spb->put_u64(value);253spb->seek(0);254255CHECK_EQ(spb->get_u64(), value);256}257258SUBCASE("A float value") {259float value = 42.0f;260261spb->clear();262spb->put_float(value);263spb->seek(0);264265CHECK_EQ(spb->get_float(), value);266}267268SUBCASE("A half-precision float value") {269float value = 3.1415927f;270float expected = 3.14062f;271272spb->clear();273spb->put_half(value);274spb->seek(0);275276CHECK(spb->get_half() == doctest::Approx(expected));277}278279SUBCASE("A double value") {280double value = 42.0;281282spb->clear();283spb->put_double(value);284spb->seek(0);285286CHECK_EQ(spb->get_double(), value);287}288}289290TEST_CASE("[StreamPeer] Get string when there is no string") {291Ref<StreamPeerBuffer> spb;292spb.instantiate();293294ERR_PRINT_OFF;295CHECK_EQ(spb->get_string(), "");296ERR_PRINT_ON;297}298299TEST_CASE("[StreamPeer] Get UTF8 string when there is no string") {300Ref<StreamPeerBuffer> spb;301spb.instantiate();302303ERR_PRINT_OFF;304CHECK_EQ(spb->get_utf8_string(), "");305ERR_PRINT_ON;306}307308} // namespace TestStreamPeer309310311