From 9ce35b5c6b82f8d7c59bda20a13d526e47a5de30 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sat, 8 Oct 2022 08:25:57 +0200 Subject: [PATCH] test/nbt_tags: Implement more tests --- src/test/nbt_tags.cpp | 229 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 224 insertions(+), 5 deletions(-) diff --git a/src/test/nbt_tags.cpp b/src/test/nbt_tags.cpp index 31bf51a..cd2ec95 100644 --- a/src/test/nbt_tags.cpp +++ b/src/test/nbt_tags.cpp @@ -37,6 +37,9 @@ int main(){ ASSERT(generic.serialize(&vector).errorCode == ErrorCodes::INVALID_TYPE); ASSERT(generic.serializeWithoutHeader(&vector).isError); ASSERT(generic.serializeWithoutHeader(&vector).errorCode == ErrorCodes::INVALID_TYPE); + // Normally, at this point, we would discard the vector as garbage. + // In this case, the assertions below exist to test the common + // serializer code. ASSERT(vector.size() == 6); ASSERT( vector.at(0) == 255 && @@ -46,6 +49,8 @@ int main(){ vector.at(4) == 0 && vector.at(5) == 0 ); + generic.name = tiny_utf8::string(std::string(0x10000, 'E')); + ASSERT(generic.serialize(&vector).isError && generic.serialize(&vector).errorCode == ErrorCodes::OVERRUN); vector.clear(); std::cout << "Passed Generic (super type) test." << std::endl; @@ -68,7 +73,6 @@ int main(){ ASSERT(!int8_0.serialize(&vector).isError); ASSERT(!int8_1.serialize(&vector).isError); ASSERT(vector.size() == 20); - std::cout << (int) vector.at(9) << std::endl; ASSERT( vector.at( 0) == 1 && vector.at( 1) == 0 && @@ -112,10 +116,225 @@ int main(){ vector.clear(); std::cout << "Passed Int8 test." << std::endl; - //NBT::Tag::Int16 int16_0 = NBT::Tag::Int16(); - //int16_0.name = "int16_0"; - //int16_0.setValue(32767); - //TODO: Check that we actually get overrun when trying to store a name that's too long + NBT::Tag::Int16 int16_0 = NBT::Tag::Int16(); + int16_0.name = "int16_0"; + int16_0.setValue(-32768); + NBT::Tag::Int16 int16_1 = NBT::Tag::Int16("int16_1", 32767); + ASSERT(int16_0.getValue() == -0x8000); + ASSERT(int16_1.getValue() == 0x7FFF); + ASSERT(int16_0.getTagType() == 2 && int16_1.getTagType() == 2); + ASSERT(!int16_0.serialize(&vector).isError); + ASSERT(!int16_1.serialize(&vector).isError); + ASSERT(vector.size() == 24); + ASSERT( + vector.at( 0) == 2 && + vector.at( 1) == 0 && + vector.at( 2) == 7 && + vector.at( 3) == 'i' && + vector.at( 4) == 'n' && + vector.at( 5) == 't' && + vector.at( 6) == '1' && + vector.at( 7) == '6' && + vector.at( 8) == '_' && + vector.at( 9) == '0' && + vector.at(10) == 0x80 && + vector.at(11) == 0x00 && + vector.at(12) == 2 && + vector.at(13) == 0 && + vector.at(14) == 7 && + vector.at(15) == 'i' && + vector.at(16) == 'n' && + vector.at(17) == 't' && + vector.at(18) == '1' && + vector.at(19) == '6' && + vector.at(20) == '_' && + vector.at(21) == '1' && + vector.at(22) == 0x7F && + vector.at(23) == 0xFF + ); + vector.clear(); + int16_0.name = ""; + ASSERT(!int16_0.serialize(&vector).isError); + ASSERT(vector.size() == 5); + ASSERT( + vector.at(0) == 2 && + vector.at(1) == 0 && + vector.at(2) == 0 && + vector.at(3) == 0x80 && + vector.at(4) == 0 + ); + vector.clear(); + ASSERT(!int16_0.serializeWithoutHeader(&vector).isError); + ASSERT(!int16_1.serializeWithoutHeader(&vector).isError); + ASSERT(vector.size() == 4); + ASSERT( + vector.at(0) == 0x80 && + vector.at(1) == 0 && + vector.at(2) == 0x7F && + vector.at(3) == 0xFF + ); + vector.clear(); + std::cout << "Passed Int16 test." << std::endl; + + NBT::Tag::Int32 int32_0 = NBT::Tag::Int32(); + int32_0.name = "int32_0"; + int32_0.setValue(-2147483648); + NBT::Tag::Int32 int32_1 = NBT::Tag::Int32("int32_1", 2147483647); + ASSERT(int32_0.getValue() == (int32_t) -0x80000000); + ASSERT(int32_1.getValue() == 0x7FFFFFFF); + ASSERT(int32_0.getTagType() == 3 && int32_1.getTagType() == 3); + ASSERT(!int32_0.serialize(&vector).isError); + ASSERT(!int32_1.serialize(&vector).isError); + ASSERT(vector.size() == 28); + ASSERT( + vector.at( 0) == 3 && + vector.at( 1) == 0 && + vector.at( 2) == 7 && + vector.at( 3) == 'i' && + vector.at( 4) == 'n' && + vector.at( 5) == 't' && + vector.at( 6) == '3' && + vector.at( 7) == '2' && + vector.at( 8) == '_' && + vector.at( 9) == '0' && + vector.at(10) == 0x80 && + vector.at(11) == 0x00 && + vector.at(12) == 0x00 && + vector.at(13) == 0x00 && + vector.at(14) == 3 && + vector.at(15) == 0 && + vector.at(16) == 7 && + vector.at(17) == 'i' && + vector.at(18) == 'n' && + vector.at(19) == 't' && + vector.at(20) == '3' && + vector.at(21) == '2' && + vector.at(22) == '_' && + vector.at(23) == '1' && + vector.at(24) == 0x7F && + vector.at(25) == 0xFF && + vector.at(26) == 0xFF && + vector.at(27) == 0xFF + ); + vector.clear(); + int32_0.name = ""; + ASSERT(!int32_0.serialize(&vector).isError); + ASSERT(vector.size() == 7); + ASSERT( + vector.at(0) == 3 && + vector.at(1) == 0 && + vector.at(2) == 0 && + vector.at(3) == 0x80 && + vector.at(4) == 0 && + vector.at(5) == 0 && + vector.at(6) == 0 + ); + vector.clear(); + ASSERT(!int32_0.serializeWithoutHeader(&vector).isError); + ASSERT(!int32_1.serializeWithoutHeader(&vector).isError); + ASSERT(vector.size() == 8); + ASSERT( + vector.at(0) == 0x80 && + vector.at(1) == 0 && + vector.at(2) == 0 && + vector.at(3) == 0 && + vector.at(4) == 0x7F && + vector.at(5) == 0xFF && + vector.at(6) == 0xFF && + vector.at(7) == 0xFF + ); + vector.clear(); + std::cout << "Passed Int32 test." << std::endl; + + NBT::Tag::Int64 int64_0 = NBT::Tag::Int64(); + int64_0.name = "int64_0"; + int64_0.setValue(-9223372036854775808); + NBT::Tag::Int64 int64_1 = NBT::Tag::Int64("int64_1", 9223372036854775807); + ASSERT(int64_0.getValue() == (int64_t) -0x8000000000000000); + ASSERT(int64_1.getValue() == 0x7FFFFFFFFFFFFFFF); + ASSERT(int64_0.getTagType() == 4 && int64_1.getTagType() == 4); + ASSERT(!int64_0.serialize(&vector).isError); + ASSERT(!int64_1.serialize(&vector).isError); + ASSERT(vector.size() == 36); + ASSERT( + vector.at( 0) == 4 && + vector.at( 1) == 0 && + vector.at( 2) == 7 && + vector.at( 3) == 'i' && + vector.at( 4) == 'n' && + vector.at( 5) == 't' && + vector.at( 6) == '6' && + vector.at( 7) == '4' && + vector.at( 8) == '_' && + vector.at( 9) == '0' && + vector.at(10) == 0x80 && + vector.at(11) == 0x00 && + vector.at(12) == 0x00 && + vector.at(13) == 0x00 && + vector.at(14) == 0x00 && + vector.at(15) == 0x00 && + vector.at(16) == 0x00 && + vector.at(17) == 0x00 && + vector.at(18) == 4 && + vector.at(19) == 0 && + vector.at(20) == 7 && + vector.at(21) == 'i' && + vector.at(22) == 'n' && + vector.at(23) == 't' && + vector.at(24) == '6' && + vector.at(25) == '4' && + vector.at(26) == '_' && + vector.at(27) == '1' && + vector.at(28) == 0x7F && + vector.at(29) == 0xFF && + vector.at(30) == 0xFF && + vector.at(31) == 0xFF && + vector.at(32) == 0xFF && + vector.at(33) == 0xFF && + vector.at(34) == 0xFF && + vector.at(35) == 0xFF + ); + vector.clear(); + int64_0.name = ""; + ASSERT(!int64_0.serialize(&vector).isError); + ASSERT(vector.size() == 11); + ASSERT( + vector.at( 0) == 4 && + vector.at( 1) == 0 && + vector.at( 2) == 0 && + vector.at( 3) == 0x80 && + vector.at( 4) == 0 && + vector.at( 5) == 0 && + vector.at( 6) == 0 && + vector.at( 7) == 0 && + vector.at( 8) == 0 && + vector.at( 9) == 0 && + vector.at(10) == 0 + ); + vector.clear(); + ASSERT(!int64_0.serializeWithoutHeader(&vector).isError); + ASSERT(!int64_1.serializeWithoutHeader(&vector).isError); + ASSERT(vector.size() == 16); + ASSERT( + vector.at( 0) == 0x80 && + vector.at( 1) == 0 && + vector.at( 2) == 0 && + vector.at( 3) == 0 && + vector.at( 4) == 0 && + vector.at( 5) == 0 && + vector.at( 6) == 0 && + vector.at( 7) == 0 && + vector.at( 8) == 0x7F && + vector.at( 9) == 0xFF && + vector.at(10) == 0xFF && + vector.at(11) == 0xFF && + vector.at(12) == 0xFF && + vector.at(13) == 0xFF && + vector.at(14) == 0xFF && + vector.at(15) == 0xFF + ); + vector.clear(); + std::cout << "Passed Int64 test." << std::endl; return 0; }