From b1ba33b39fa6b5f23b543502fd9baea0526916b0 Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Sat, 8 Oct 2022 12:56:13 +0200 Subject: [PATCH] test/nbt_tags: Implement unit tests for Float, Double, and Int8Array --- src/test/nbt_tags.cpp | 271 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 271 insertions(+) diff --git a/src/test/nbt_tags.cpp b/src/test/nbt_tags.cpp index cd2ec95..443d0b7 100644 --- a/src/test/nbt_tags.cpp +++ b/src/test/nbt_tags.cpp @@ -31,6 +31,7 @@ int main(){ std::vector vector; + NBT::Tag::Generic generic = NBT::Tag::Generic(); ASSERT(generic.getTagType() == NBT::TagType::INVALID); ASSERT(generic.serialize(&vector).isError); @@ -54,6 +55,7 @@ int main(){ vector.clear(); std::cout << "Passed Generic (super type) test." << std::endl; + NBT::Tag::End end = NBT::Tag::End(); ASSERT(end.getTagType() == NBT::TagType::END); ASSERT(!end.serialize(&vector).isError); @@ -63,6 +65,7 @@ int main(){ vector.clear(); std::cout << "Passed End test." << std::endl; + NBT::Tag::Int8 int8_0 = NBT::Tag::Int8(); int8_0.setValue(-128); int8_0.name = "int8_0"; @@ -116,6 +119,7 @@ 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(-32768); @@ -176,6 +180,7 @@ int main(){ 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); @@ -246,6 +251,7 @@ int main(){ 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); @@ -336,5 +342,270 @@ int main(){ vector.clear(); std::cout << "Passed Int64 test." << std::endl; + + // float data: 0xC7777777 => -63351.46484375f + NBT::Tag::Float float_0 = NBT::Tag::Float(); + float_0.name = "float_0"; + float_0.setValue(-63351.46484375f); + NBT::Tag::Float float_1 = NBT::Tag::Float("float_1", -63351.46484375f); + ASSERT(float_0.getValue() == -63351.46484375f); + ASSERT(float_1.getValue() == -63351.46484375f); + ASSERT(float_0.getTagType() == 5 && float_1.getTagType() == 5); + ASSERT(!float_0.serialize(&vector).isError); + ASSERT(!float_1.serialize(&vector).isError); + ASSERT(vector.size() == 28); + ASSERT( + vector.at( 0) == 5 && + vector.at( 1) == 0 && + vector.at( 2) == 7 && + vector.at( 3) == 'f' && + vector.at( 4) == 'l' && + vector.at( 5) == 'o' && + vector.at( 6) == 'a' && + vector.at( 7) == 't' && + vector.at( 8) == '_' && + vector.at( 9) == '0' && + vector.at(10) == 0xC7 && + vector.at(11) == 0x77 && + vector.at(12) == 0x77 && + vector.at(13) == 0x77 && + vector.at(14) == 5 && + vector.at(15) == 0 && + vector.at(16) == 7 && + vector.at(17) == 'f' && + vector.at(18) == 'l' && + vector.at(19) == 'o' && + vector.at(20) == 'a' && + vector.at(21) == 't' && + vector.at(22) == '_' && + vector.at(23) == '1' && + vector.at(24) == 0xC7 && + vector.at(25) == 0x77 && + vector.at(26) == 0x77 && + vector.at(27) == 0x77 + ); + vector.clear(); + ASSERT(!float_0.serializeWithoutHeader(&vector).isError); + ASSERT(!float_1.serializeWithoutHeader(&vector).isError); + ASSERT(vector.size() == 8); + ASSERT( + vector.at(0) == 0xC7 && + vector.at(1) == 0x77 && + vector.at(2) == 0x77 && + vector.at(3) == 0x77 && + vector.at(4) == 0xC7 && + vector.at(5) == 0x77 && + vector.at(6) == 0x77 && + vector.at(7) == 0x77 + ); + vector.clear(); + std::cout << "Passed Float test." << std::endl; + + + // double data 0xC034040008000000 => -20.015625476837158203125 + NBT::Tag::Double double_0 = NBT::Tag::Double(); + double_0.name = "double_0"; + double_0.setValue(-20.015625476837158203125); + NBT::Tag::Double double_1 = NBT::Tag::Double("double_1", -20.015625476837158203125); + ASSERT(double_0.getValue() == -20.015625476837158203125); + ASSERT(double_1.getValue() == -20.015625476837158203125); + ASSERT(double_0.getTagType() == 6 && double_1.getTagType() == 6); + ASSERT(!double_0.serialize(&vector).isError); + ASSERT(!double_1.serialize(&vector).isError); + ASSERT(vector.size() == 38); + ASSERT( + vector.at( 0) == 6 && + vector.at( 1) == 0 && + vector.at( 2) == 8 && + vector.at( 3) == 'd' && + vector.at( 4) == 'o' && + vector.at( 5) == 'u' && + vector.at( 6) == 'b' && + vector.at( 7) == 'l' && + vector.at( 8) == 'e' && + vector.at( 9) == '_' && + vector.at(10) == '0' && + vector.at(11) == 0xC0 && + vector.at(12) == 0x34 && + vector.at(13) == 0x04 && + vector.at(14) == 0x00 && + vector.at(15) == 0x08 && + vector.at(16) == 0x00 && + vector.at(17) == 0x00 && + vector.at(18) == 0x00 && + vector.at(19) == 6 && + vector.at(20) == 0 && + vector.at(21) == 8 && + vector.at(22) == 'd' && + vector.at(23) == 'o' && + vector.at(24) == 'u' && + vector.at(25) == 'b' && + vector.at(26) == 'l' && + vector.at(27) == 'e' && + vector.at(28) == '_' && + vector.at(29) == '1' && + vector.at(30) == 0xC0 && + vector.at(31) == 0x34 && + vector.at(32) == 0x04 && + vector.at(33) == 0x00 && + vector.at(34) == 0x08 && + vector.at(35) == 0x00 && + vector.at(36) == 0x00 && + vector.at(37) == 0x00 + ); + vector.clear(); + ASSERT(!double_0.serializeWithoutHeader(&vector).isError); + ASSERT(!double_1.serializeWithoutHeader(&vector).isError); + ASSERT(vector.size() == 16); + ASSERT( + vector.at( 0) == 0xC0 && + vector.at( 1) == 0x34 && + vector.at( 2) == 0x04 && + vector.at( 3) == 0x00 && + vector.at( 4) == 0x08 && + vector.at( 5) == 0x00 && + vector.at( 6) == 0x00 && + vector.at( 7) == 0x00 && + vector.at( 8) == 0xC0 && + vector.at( 9) == 0x34 && + vector.at(10) == 0x04 && + vector.at(11) == 0x00 && + vector.at(12) == 0x08 && + vector.at(13) == 0x00 && + vector.at(14) == 0x00 && + vector.at(15) == 0x00 + ); + vector.clear(); + std::cout << "Passed Double test." << std::endl; + + + std::vector int8DataVector = std::vector(); + int8DataVector.push_back(-128); + int8DataVector.push_back(127); + int8DataVector.push_back(0); + + NBT::Tag::Int8Array int8a_0 = NBT::Tag::Int8Array(); + int8a_0.name = "int8a_0"; + int8a_0.setData(int8DataVector); + NBT::Tag::Int8Array int8a_1 = NBT::Tag::Int8Array("int8a_1", int8DataVector); + NBT::Tag::Int8Array int8a_2 = NBT::Tag::Int8Array("int8a_2", int8DataVector.size(), int8DataVector.data()); + ASSERT(int8a_0.getTagType() == 7 && int8a_1.getTagType() == 7 && int8a_2.getTagType() == 7); + ASSERT( + int8a_0.getData() == int8a_1.getData() && + int8a_1.getData() == int8a_2.getData() + ); + ASSERT( + !int8a_0.getValue(0).isError && + !int8a_1.getValue(1).isError && + !int8a_2.getValue(2).isError + ); + ASSERT( + int8a_0.getValue(3).isError && + int8a_2.getValue(3).errorCode == ErrorCodes::OUT_OF_RANGE + ); + int8a_1.addElement(-127); + ErrorOrVoid resultGood = int8a_2.removeElement(1); + ErrorOrVoid resultError = int8a_2.removeElement(2); + ASSERT(!resultGood.isError && resultError.isError && resultError.errorCode==ErrorCodes::OUT_OF_RANGE); + resultGood = int8a_2.setValue(1, 15); + resultError = int8a_2.setValue(2, 7); + ASSERT(!resultGood.isError && resultError.isError && resultError.errorCode==ErrorCodes::OUT_OF_RANGE); + ASSERT( + int8a_0.length()==3 && + int8a_1.length()==4 && + int8a_2.length()==2 + ); + ASSERT( + !int8a_0.serialize(&vector).isError && + !int8a_1.serialize(&vector).isError && + !int8a_2.serialize(&vector).isError + ); + ASSERT(vector.size() == 51); + ASSERT( + vector.at( 0) == 7 && + vector.at( 1) == 0 && + vector.at( 2) == 7 && + vector.at( 3) == 'i' && + vector.at( 4) == 'n' && + vector.at( 5) == 't' && + vector.at( 6) == '8' && + vector.at( 7) == 'a' && + vector.at( 8) == '_' && + vector.at( 9) == '0' && + vector.at(10) == 0 && + vector.at(11) == 0 && + vector.at(12) == 0 && + vector.at(13) == 3 && + vector.at(14) == 0x80 && + vector.at(15) == 0x7F && + vector.at(16) == 0x0 && + vector.at(17) == 7 && + vector.at(18) == 0 && + vector.at(19) == 7 && + vector.at(20) == 'i' && + vector.at(21) == 'n' && + vector.at(22) == 't' && + vector.at(23) == '8' && + vector.at(24) == 'a' && + vector.at(25) == '_' && + vector.at(26) == '1' && + vector.at(27) == 0 && + vector.at(28) == 0 && + vector.at(29) == 0 && + vector.at(30) == 4 && + vector.at(31) == 0x80 && + vector.at(32) == 0x7F && + vector.at(33) == 0x0 && + vector.at(34) == 0x81 && + vector.at(35) == 7 && + vector.at(36) == 0 && + vector.at(37) == 7 && + vector.at(38) == 'i' && + vector.at(39) == 'n' && + vector.at(40) == 't' && + vector.at(41) == '8' && + vector.at(42) == 'a' && + vector.at(43) == '_' && + vector.at(44) == '2' && + vector.at(45) == 0 && + vector.at(46) == 0 && + vector.at(47) == 0 && + vector.at(48) == 2 && + vector.at(49) == 0x80 && + vector.at(50) == 0xF + ); + vector.clear(); + ASSERT( + !int8a_0.serializeWithoutHeader(&vector).isError && + !int8a_1.serializeWithoutHeader(&vector).isError && + !int8a_2.serializeWithoutHeader(&vector).isError + ); + ASSERT(vector.size() == 21); + ASSERT( + vector.at( 0) == 0 && + vector.at( 1) == 0 && + vector.at( 2) == 0 && + vector.at( 3) == 3 && + vector.at( 4) == 0x80 && + vector.at( 5) == 0x7F && + vector.at( 6) == 0x00 && + vector.at( 7) == 0 && + vector.at( 8) == 0 && + vector.at( 9) == 0 && + vector.at(10) == 4 && + vector.at(11) == 0x80 && + vector.at(12) == 0x7F && + vector.at(13) == 0x00 && + vector.at(14) == 0x81 && + vector.at(15) == 0 && + vector.at(16) == 0 && + vector.at(17) == 0 && + vector.at(18) == 2 && + vector.at(19) == 0x80 && + vector.at(20) == 0xF + ); + vector.clear(); + std::cout << "Passed Int8Array test." << std::endl; + return 0; }