test/nbt_tags: Implement string test and partial list test

Soda
BodgeMaster 2022-10-09 10:29:28 +02:00
parent d5ce50a4a9
commit 6672a4f149
1 changed files with 199 additions and 0 deletions

View File

@ -32,6 +32,7 @@ int main(){
std::vector<uint8_t> vector; std::vector<uint8_t> vector;
// GENERIC #################################################################
NBT::Tag::Generic generic = NBT::Tag::Generic(); NBT::Tag::Generic generic = NBT::Tag::Generic();
ASSERT(generic.getTagType() == NBT::TagType::INVALID); ASSERT(generic.getTagType() == NBT::TagType::INVALID);
ASSERT(generic.serialize(&vector).isError); ASSERT(generic.serialize(&vector).isError);
@ -56,6 +57,7 @@ int main(){
std::cout << "Passed Generic (super type) test." << std::endl; std::cout << "Passed Generic (super type) test." << std::endl;
// END #####################################################################
NBT::Tag::End end = NBT::Tag::End(); NBT::Tag::End end = NBT::Tag::End();
ASSERT(end.getTagType() == NBT::TagType::END); ASSERT(end.getTagType() == NBT::TagType::END);
ASSERT(!end.serialize(&vector).isError); ASSERT(!end.serialize(&vector).isError);
@ -66,6 +68,7 @@ int main(){
std::cout << "Passed End test." << std::endl; std::cout << "Passed End test." << std::endl;
// INT8 ####################################################################
NBT::Tag::Int8 int8_0 = NBT::Tag::Int8(); NBT::Tag::Int8 int8_0 = NBT::Tag::Int8();
int8_0.setValue(-128); int8_0.setValue(-128);
int8_0.name = "int8_0"; int8_0.name = "int8_0";
@ -120,6 +123,7 @@ int main(){
std::cout << "Passed Int8 test." << std::endl; std::cout << "Passed Int8 test." << std::endl;
// INT16 ###################################################################
NBT::Tag::Int16 int16_0 = NBT::Tag::Int16(); NBT::Tag::Int16 int16_0 = NBT::Tag::Int16();
int16_0.name = "int16_0"; int16_0.name = "int16_0";
int16_0.setValue(-32768); int16_0.setValue(-32768);
@ -181,6 +185,7 @@ int main(){
std::cout << "Passed Int16 test." << std::endl; std::cout << "Passed Int16 test." << std::endl;
// INT32 ###################################################################
NBT::Tag::Int32 int32_0 = NBT::Tag::Int32(); NBT::Tag::Int32 int32_0 = NBT::Tag::Int32();
int32_0.name = "int32_0"; int32_0.name = "int32_0";
int32_0.setValue(-2147483648); int32_0.setValue(-2147483648);
@ -252,6 +257,7 @@ int main(){
std::cout << "Passed Int32 test." << std::endl; std::cout << "Passed Int32 test." << std::endl;
// INT64 ###################################################################
NBT::Tag::Int64 int64_0 = NBT::Tag::Int64(); NBT::Tag::Int64 int64_0 = NBT::Tag::Int64();
int64_0.name = "int64_0"; int64_0.name = "int64_0";
int64_0.setValue(-9223372036854775808); int64_0.setValue(-9223372036854775808);
@ -343,6 +349,7 @@ int main(){
std::cout << "Passed Int64 test." << std::endl; std::cout << "Passed Int64 test." << std::endl;
// FLOAT ###################################################################
// float data: 0xC7777777 => -63351.46484375f // float data: 0xC7777777 => -63351.46484375f
NBT::Tag::Float float_0 = NBT::Tag::Float(); NBT::Tag::Float float_0 = NBT::Tag::Float();
float_0.name = "float_0"; float_0.name = "float_0";
@ -402,6 +409,7 @@ int main(){
std::cout << "Passed Float test." << std::endl; std::cout << "Passed Float test." << std::endl;
// DOUBLE ##################################################################
// double data 0xC034040008000000 => -20.015625476837158203125 // double data 0xC034040008000000 => -20.015625476837158203125
NBT::Tag::Double double_0 = NBT::Tag::Double(); NBT::Tag::Double double_0 = NBT::Tag::Double();
double_0.name = "double_0"; double_0.name = "double_0";
@ -479,6 +487,7 @@ int main(){
std::cout << "Passed Double test." << std::endl; std::cout << "Passed Double test." << std::endl;
// INT8_ARRAY ##############################################################
std::vector<int8_t> int8DataVector = std::vector<int8_t>(); std::vector<int8_t> int8DataVector = std::vector<int8_t>();
int8DataVector.push_back(-128); int8DataVector.push_back(-128);
int8DataVector.push_back(127); int8DataVector.push_back(127);
@ -607,5 +616,195 @@ int main(){
vector.clear(); vector.clear();
std::cout << "Passed Int8Array test." << std::endl; std::cout << "Passed Int8Array test." << std::endl;
// STRING ##################################################################
NBT::Tag::String string_0 = NBT::Tag::String();
string_0.name = "string_0";
string_0.setValue("I'm blue dabadee dabadie");
NBT::Tag::String string_1 = NBT::Tag::String("string_1", "Never gonna give you up");
ASSERT(string_0.getValue() == "I'm blue dabadee dabadie");
ASSERT(string_1.getValue() == "Never gonna give you up");
ASSERT(!string_0.serialize(&vector).isError);
ASSERT(!string_1.serialize(&vector).isError);
ASSERT(vector.size() == 73);
ASSERT(
vector.at( 0) == 8 &&
vector.at( 1) == 0 &&
vector.at( 2) == 8 &&
vector.at( 3) == 's' &&
vector.at( 4) == 't' &&
vector.at( 5) == 'r' &&
vector.at( 6) == 'i' &&
vector.at( 7) == 'n' &&
vector.at( 8) == 'g' &&
vector.at( 9) == '_' &&
vector.at(10) == '0' &&
vector.at(11) == 0 &&
vector.at(12) == 24 &&
vector.at(13) == 'I' &&
vector.at(14) == '\'' &&
vector.at(15) == 'm' &&
vector.at(16) == ' ' &&
vector.at(17) == 'b' &&
vector.at(18) == 'l' &&
vector.at(19) == 'u' &&
vector.at(20) == 'e' &&
vector.at(21) == ' ' &&
vector.at(22) == 'd' &&
vector.at(23) == 'a' &&
vector.at(24) == 'b' &&
vector.at(25) == 'a' &&
vector.at(26) == 'd' &&
vector.at(27) == 'e' &&
vector.at(28) == 'e' &&
vector.at(29) == ' ' &&
vector.at(30) == 'd' &&
vector.at(31) == 'a' &&
vector.at(32) == 'b' &&
vector.at(33) == 'a' &&
vector.at(34) == 'd' &&
vector.at(35) == 'i' &&
vector.at(36) == 'e' &&
vector.at(37) == 8 &&
vector.at(38) == 0 &&
vector.at(39) == 8 &&
vector.at(40) == 's' &&
vector.at(41) == 't' &&
vector.at(42) == 'r' &&
vector.at(43) == 'i' &&
vector.at(44) == 'n' &&
vector.at(45) == 'g' &&
vector.at(46) == '_' &&
vector.at(47) == '1' &&
vector.at(48) == 0 &&
vector.at(49) == 23 &&
vector.at(50) == 'N' &&
vector.at(51) == 'e' &&
vector.at(52) == 'v' &&
vector.at(53) == 'e' &&
vector.at(54) == 'r' &&
vector.at(55) == ' ' &&
vector.at(56) == 'g' &&
vector.at(57) == 'o' &&
vector.at(58) == 'n' &&
vector.at(59) == 'n' &&
vector.at(60) == 'a' &&
vector.at(61) == ' ' &&
vector.at(62) == 'g' &&
vector.at(63) == 'i' &&
vector.at(64) == 'v' &&
vector.at(65) == 'e' &&
vector.at(66) == ' ' &&
vector.at(67) == 'y' &&
vector.at(68) == 'o' &&
vector.at(69) == 'u' &&
vector.at(70) == ' ' &&
vector.at(71) == 'u' &&
vector.at(72) == 'p'
);
vector.clear();
ASSERT(!string_0.serializeWithoutHeader(&vector).isError);
ASSERT(!string_1.serializeWithoutHeader(&vector).isError);
ASSERT(vector.size() == 51);
ASSERT(
vector.at( 0) == 0 &&
vector.at( 1) == 24 &&
vector.at( 2) == 'I' &&
vector.at( 3) == '\'' &&
vector.at( 4) == 'm' &&
vector.at( 5) == ' ' &&
vector.at( 6) == 'b' &&
vector.at( 7) == 'l' &&
vector.at( 8) == 'u' &&
vector.at( 9) == 'e' &&
vector.at(10) == ' ' &&
vector.at(11) == 'd' &&
vector.at(12) == 'a' &&
vector.at(13) == 'b' &&
vector.at(14) == 'a' &&
vector.at(15) == 'd' &&
vector.at(16) == 'e' &&
vector.at(17) == 'e' &&
vector.at(18) == ' ' &&
vector.at(19) == 'd' &&
vector.at(20) == 'a' &&
vector.at(21) == 'b' &&
vector.at(22) == 'a' &&
vector.at(23) == 'd' &&
vector.at(24) == 'i' &&
vector.at(25) == 'e' &&
vector.at(26) == 0 &&
vector.at(27) == 23 &&
vector.at(28) == 'N' &&
vector.at(29) == 'e' &&
vector.at(30) == 'v' &&
vector.at(31) == 'e' &&
vector.at(32) == 'r' &&
vector.at(33) == ' ' &&
vector.at(34) == 'g' &&
vector.at(35) == 'o' &&
vector.at(36) == 'n' &&
vector.at(37) == 'n' &&
vector.at(38) == 'a' &&
vector.at(39) == ' ' &&
vector.at(40) == 'g' &&
vector.at(41) == 'i' &&
vector.at(42) == 'v' &&
vector.at(43) == 'e' &&
vector.at(44) == ' ' &&
vector.at(45) == 'y' &&
vector.at(46) == 'o' &&
vector.at(47) == 'u' &&
vector.at(48) == ' ' &&
vector.at(49) == 'u' &&
vector.at(50) == 'p'
);
vector.clear();
std::cout << "Passed String test." << std::endl;
// LIST ####################################################################
// Things to put in the list
NBT::Tag::Int8* pointer_1_0 = new NBT::Tag::Int8("entry_1_0", 6);
NBT::Tag::Int8* pointer_1_1 = new NBT::Tag::Int8("entry_1_1", 38);
NBT::Tag::Int8* pointer_1_2 = new NBT::Tag::Int8("entry_1_2", 125);
NBT::Tag::Int8* pointer_1_3 = new NBT::Tag::Int8("entry_1_3", -64); // 0xC0 (?)
NBT::Tag::Int8* pointer_2_0 = new NBT::Tag::Int8("entry_2_0", 56);
NBT::Tag::Int8* pointer_2_1 = new NBT::Tag::Int8("entry_2_1", 28);
NBT::Tag::Int8* pointer_2_2 = new NBT::Tag::Int8("entry_2_2", 45);
NBT::Tag::Int8* pointer_2_3 = new NBT::Tag::Int8("entry_2_3", 78);
std::vector<NBT::Tag::Generic*> listDataVector;
listDataVector.push_back(pointer_2_0);
listDataVector.push_back(pointer_2_1);
listDataVector.push_back(pointer_2_2);
listDataVector.push_back(pointer_2_3);
NBT::Tag::List list_0 = NBT::Tag::List();
ASSERT(list_0.serialize(&vector).isError);
ASSERT(list_0.serialize(&vector).errorCode == ErrorCodes::INVALID_TYPE);
vector.clear();
NBT::Tag::List list_1 = NBT::Tag::List("list_1", NBT::TagType::INT8);
ASSERT(!list_1.appendPointer(pointer_1_0).isError);
ASSERT(!list_1.appendPointer(pointer_1_1).isError);
ASSERT(!list_1.appendPointer(pointer_1_2).isError);
ASSERT(!list_1.appendPointer(pointer_1_3).isError);
NBT::Tag::Int16* pointer_invalid = new NBT::Tag::Int16("invalid", 400);
resultError = list_1.appendPointer(pointer_invalid);
ASSERT(resultError.isError && resultError.errorCode == ErrorCodes::INVALID_TYPE);
//TODO: Check that constructing with a vector of mixed tags
// results in a clearly defined failure mode (issue #60)
NBT::Tag::List list_2 = NBT::Tag::List("list_2", listDataVector);
ASSERT(list_1.length() == 4 && list_2.length() == 4);
ASSERT(!list_2.deleteElement(1).isError);
ASSERT(list_2.deleteElement(3).isError && list_2.deleteElement(3).errorCode == ErrorCodes::OUT_OF_RANGE);
ASSERT(!list_2.getElementPointer(2).isError && dynamic_cast<NBT::Tag::Int8*>(list_2.getElementPointer(2).value)->getValue() == 78);
ASSERT(!list_2.setElementPointerAt(0, new NBT::Tag::Int8("set_entry", 3)).isError);
ErrorOrVoid resultRange = list_2.setElementPointerAt(3, new NBT::Tag::Int8("overrun_entry", 2));
ASSERT(resultRange.isError);
ASSERT(resultRange.errorCode == ErrorCodes::OUT_OF_RANGE);
ErrorOrVoid resultType = list_2.setElementPointerAt(0, new NBT::Tag::Int16());
ASSERT(resultType.isError);
ASSERT(resultType.errorCode == ErrorCodes::INVALID_TYPE);
ASSERT(list_2.length() == 3);
return 0; return 0;
} }