Compare commits

...

3 Commits

Author SHA1 Message Date
BodgeMaster 9b21dfaee5 test/nbt_tags: Implement Int32Array test 2022-10-14 22:33:57 +02:00
BodgeMaster 996154fbbc lib/nbt: Return the correct error code from soem functions for i32 arrays. 2022-10-14 22:33:32 +02:00
BodgeMaster 77dd79398f test/nbt_tags: Test compound serialization 2022-10-14 21:46:55 +02:00
2 changed files with 355 additions and 3 deletions

View File

@ -1066,7 +1066,7 @@ namespace NBT {
ErrorOr<int32_t> Int32Array::getValue(uint64_t position) {
if (position >= this->data.size()) {
return ErrorOr<int32_t>(true, ErrorCodes::OVERRUN);
return ErrorOr<int32_t>(true, ErrorCodes::OUT_OF_RANGE);
}
return ErrorOr<int32_t>(this->data.at(position));
@ -1078,7 +1078,7 @@ namespace NBT {
ErrorOrVoid Int32Array::setValue(uint64_t position, int32_t value) {
if (position >= this->data.size()) {
return ErrorOrVoid(true, ErrorCodes::OVERRUN);
return ErrorOrVoid(true, ErrorCodes::OUT_OF_RANGE);
}
this->data[position] = value;

View File

@ -762,6 +762,7 @@ int main(){
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);
@ -873,6 +874,7 @@ int main(){
vector.clear();
std::cout << "Passed List test." << std::endl;
// COMPOUND ################################################################
// Things to put in the compound
std::vector<NBT::Tag::Generic*> compoundDataVector = std::vector<NBT::Tag::Generic*>();
@ -906,7 +908,357 @@ int main(){
ASSERT(compound_0.length() == 1);
ASSERT(compound_1.length() == 3);
ASSERT(compound_2.length() == 4);
//TODO: test serialization
compound_0.serialize(&vector);
compound_1.serialize(&vector);
compound_2.serialize(&vector);
ASSERT(vector.size() == 95);
ASSERT(
vector.at( 0) == 10 &&
vector.at( 1) == 0 &&
vector.at( 2) == 10 &&
vector.at( 3) == 'c' &&
vector.at( 4) == 'o' &&
vector.at( 5) == 'm' &&
vector.at( 6) == 'p' &&
vector.at( 7) == 'o' &&
vector.at( 8) == 'u' &&
vector.at( 9) == 'n' &&
vector.at(10) == 'd' &&
vector.at(11) == '_' &&
vector.at(12) == '0' &&
vector.at(13) == 0 &&
vector.at(14) == 10 &&
vector.at(15) == 0 &&
vector.at(16) == 10 &&
vector.at(17) == 'c' &&
vector.at(18) == 'o' &&
vector.at(19) == 'm' &&
vector.at(20) == 'p' &&
vector.at(21) == 'o' &&
vector.at(22) == 'u' &&
vector.at(23) == 'n' &&
vector.at(24) == 'd' &&
vector.at(25) == '_' &&
vector.at(26) == '1' &&
vector.at(27) == 3 &&
vector.at(28) == 0 &&
vector.at(29) == 1 &&
vector.at(30) == '0' &&
vector.at(31) == 0x00 &&
vector.at(32) == 0x01 &&
vector.at(33) == 0x0f &&
vector.at(34) == 0x2c &&
vector.at(35) == 2 &&
vector.at(36) == 0 &&
vector.at(37) == 11 &&
vector.at(38) == 's' &&
vector.at(39) == 'e' &&
vector.at(40) == 't' &&
vector.at(41) == '_' &&
vector.at(42) == 'e' &&
vector.at(43) == 'l' &&
vector.at(44) == 'e' &&
vector.at(45) == 'm' &&
vector.at(46) == 'e' &&
vector.at(47) == 'n' &&
vector.at(48) == 't' &&
vector.at(49) == 0x0F &&
vector.at(50) == 0xFF &&
vector.at(51) == 0 &&
vector.at(52) == 10 &&
vector.at(53) == 0 &&
vector.at(54) == 10 &&
vector.at(55) == 'c' &&
vector.at(56) == 'o' &&
vector.at(57) == 'm' &&
vector.at(58) == 'p' &&
vector.at(59) == 'o' &&
vector.at(60) == 'u' &&
vector.at(61) == 'n' &&
vector.at(62) == 'd' &&
vector.at(63) == '_' &&
vector.at(64) == '2' &&
vector.at(65) == 1 &&
vector.at(66) == 0 &&
vector.at(67) == 1 &&
vector.at(68) == '0' &&
vector.at(69) == 0x0 &&
vector.at(70) == 2 &&
vector.at(71) == 0 &&
vector.at(72) == 1 &&
vector.at(73) == '1' &&
vector.at(74) == 0x13 &&
vector.at(75) == 0x37 &&
vector.at(76) == 8 &&
vector.at(77) == 0 &&
vector.at(78) == 1 &&
vector.at(79) == '2' &&
vector.at(80) == 0 &&
vector.at(81) == 12 &&
vector.at(82) == 'H' &&
vector.at(83) == 'e' &&
vector.at(84) == 'l' &&
vector.at(85) == 'l' &&
vector.at(86) == 'o' &&
vector.at(87) == ' ' &&
vector.at(88) == 'W' &&
vector.at(89) == 'o' &&
vector.at(90) == 'r' &&
vector.at(91) == 'l' &&
vector.at(92) == 'd' &&
vector.at(93) == '!' &&
vector.at(94) == 0
);
vector.clear();
compound_0.serializeWithoutHeader(&vector);
compound_1.serializeWithoutHeader(&vector);
compound_2.serializeWithoutHeader(&vector);
ASSERT(vector.size() == 56);
ASSERT(
vector.at( 0) == 0 &&
vector.at( 1) == 3 &&
vector.at( 2) == 0 &&
vector.at( 3) == 1 &&
vector.at( 4) == '0' &&
vector.at( 5) == 0x00 &&
vector.at( 6) == 0x01 &&
vector.at( 7) == 0x0f &&
vector.at( 8) == 0x2c &&
vector.at( 9) == 2 &&
vector.at(10) == 0 &&
vector.at(11) == 11 &&
vector.at(12) == 's' &&
vector.at(13) == 'e' &&
vector.at(14) == 't' &&
vector.at(15) == '_' &&
vector.at(16) == 'e' &&
vector.at(17) == 'l' &&
vector.at(18) == 'e' &&
vector.at(19) == 'm' &&
vector.at(20) == 'e' &&
vector.at(21) == 'n' &&
vector.at(22) == 't' &&
vector.at(23) == 0x0F &&
vector.at(24) == 0xFF &&
vector.at(25) == 0 &&
vector.at(26) == 1 &&
vector.at(27) == 0 &&
vector.at(28) == 1 &&
vector.at(29) == '0' &&
vector.at(30) == 0x0 &&
vector.at(31) == 2 &&
vector.at(32) == 0 &&
vector.at(33) == 1 &&
vector.at(34) == '1' &&
vector.at(35) == 0x13 &&
vector.at(36) == 0x37 &&
vector.at(37) == 8 &&
vector.at(38) == 0 &&
vector.at(39) == 1 &&
vector.at(40) == '2' &&
vector.at(41) == 0 &&
vector.at(42) == 12 &&
vector.at(43) == 'H' &&
vector.at(44) == 'e' &&
vector.at(45) == 'l' &&
vector.at(46) == 'l' &&
vector.at(47) == 'o' &&
vector.at(48) == ' ' &&
vector.at(49) == 'W' &&
vector.at(50) == 'o' &&
vector.at(51) == 'r' &&
vector.at(52) == 'l' &&
vector.at(53) == 'd' &&
vector.at(54) == '!' &&
vector.at(55) == 0
);
vector.clear();
std::cout << "Passed Compound test." << std::endl;
// INT32_ARRAY #############################################################
std::vector<int32_t> int32DataVector = std::vector<int32_t>();
int32DataVector.push_back(-2147483648);
int32DataVector.push_back(2147483647);
int32DataVector.push_back(0);
NBT::Tag::Int32Array int32a_0 = NBT::Tag::Int32Array();
int32a_0.name = "int32a_0";
int32a_0.setData(int32DataVector);
NBT::Tag::Int32Array int32a_1 = NBT::Tag::Int32Array("int32a_1", int32DataVector);
NBT::Tag::Int32Array int32a_2 = NBT::Tag::Int32Array("int32a_2", int32DataVector.size(), int32DataVector.data());
ASSERT(int32a_0.getTagType() == 11 && int32a_1.getTagType() == 11 && int32a_2.getTagType() == 11);
ASSERT(
int32a_0.getData() == int32a_1.getData() &&
int32a_1.getData() == int32a_2.getData()
);
ASSERT(
!int32a_0.getValue(0).isError &&
!int32a_1.getValue(1).isError &&
!int32a_2.getValue(2).isError
);
ASSERT(int32a_0.getValue(3).isError);
ASSERT(int32a_2.getValue(3).errorCode == ErrorCodes::OUT_OF_RANGE);
int32a_1.addElement(-2147483647);
resultGood = int32a_2.removeElement(1);
resultError = int32a_2.removeElement(2);
ASSERT(!resultGood.isError && resultError.isError && resultError.errorCode==ErrorCodes::OUT_OF_RANGE);
resultGood = int32a_2.setValue(1, 15);
resultError = int32a_2.setValue(2, 7);
ASSERT(!resultGood.isError && resultError.isError && resultError.errorCode==ErrorCodes::OUT_OF_RANGE);
ASSERT(
int32a_0.length()==3 &&
int32a_1.length()==4 &&
int32a_2.length()==2
);
ASSERT(
!int32a_0.serialize(&vector).isError &&
!int32a_1.serialize(&vector).isError &&
!int32a_2.serialize(&vector).isError
);
ASSERT(vector.size() == 81);
ASSERT(
vector.at( 0) == 11 &&
vector.at( 1) == 0 &&
vector.at( 2) == 8 &&
vector.at( 3) == 'i' &&
vector.at( 4) == 'n' &&
vector.at( 5) == 't' &&
vector.at( 6) == '3' &&
vector.at( 7) == '2' &&
vector.at( 8) == 'a' &&
vector.at( 9) == '_' &&
vector.at(10) == '0' &&
vector.at(11) == 0 &&
vector.at(12) == 0 &&
vector.at(13) == 0 &&
vector.at(14) == 3 &&
vector.at(15) == 0x80 &&
vector.at(16) == 0x00 &&
vector.at(17) == 0x00 &&
vector.at(18) == 0x00 &&
vector.at(19) == 0x7F &&
vector.at(20) == 0xFF &&
vector.at(21) == 0xFF &&
vector.at(22) == 0xFF &&
vector.at(23) == 0x00 &&
vector.at(24) == 0x00 &&
vector.at(25) == 0x00 &&
vector.at(26) == 0x00 &&
vector.at(27) == 11 &&
vector.at(28) == 0 &&
vector.at(29) == 8 &&
vector.at(30) == 'i' &&
vector.at(31) == 'n' &&
vector.at(32) == 't' &&
vector.at(33) == '3' &&
vector.at(34) == '2' &&
vector.at(35) == 'a' &&
vector.at(36) == '_' &&
vector.at(37) == '1' &&
vector.at(38) == 0 &&
vector.at(39) == 0 &&
vector.at(40) == 0 &&
vector.at(41) == 4 &&
vector.at(42) == 0x80 &&
vector.at(43) == 0x00 &&
vector.at(44) == 0x00 &&
vector.at(45) == 0x00 &&
vector.at(46) == 0x7F &&
vector.at(47) == 0xFF &&
vector.at(48) == 0xFF &&
vector.at(49) == 0xFF &&
vector.at(50) == 0x00 &&
vector.at(51) == 0x00 &&
vector.at(52) == 0x00 &&
vector.at(53) == 0x00 &&
vector.at(54) == 0x80 &&
vector.at(55) == 0x00 &&
vector.at(56) == 0x00 &&
vector.at(57) == 0x01 &&
vector.at(58) == 11 &&
vector.at(59) == 0 &&
vector.at(60) == 8 &&
vector.at(61) == 'i' &&
vector.at(62) == 'n' &&
vector.at(63) == 't' &&
vector.at(64) == '3' &&
vector.at(65) == '2' &&
vector.at(66) == 'a' &&
vector.at(67) == '_' &&
vector.at(68) == '2' &&
vector.at(69) == 0 &&
vector.at(70) == 0 &&
vector.at(71) == 0 &&
vector.at(72) == 2 &&
vector.at(73) == 0x80 &&
vector.at(74) == 0x00 &&
vector.at(75) == 0x00 &&
vector.at(76) == 0x00 &&
vector.at(77) == 0x00 &&
vector.at(78) == 0x00 &&
vector.at(70) == 0x00 &&
vector.at(80) == 0x0F
);
vector.clear();
ASSERT(
!int32a_0.serializeWithoutHeader(&vector).isError &&
!int32a_1.serializeWithoutHeader(&vector).isError &&
!int32a_2.serializeWithoutHeader(&vector).isError
);
ASSERT(vector.size() == 48);
ASSERT(
vector.at( 0) == 0 &&
vector.at( 1) == 0 &&
vector.at( 2) == 0 &&
vector.at( 3) == 3 &&
vector.at( 4) == 0x80 &&
vector.at( 5) == 0x00 &&
vector.at( 6) == 0x00 &&
vector.at( 7) == 0x00 &&
vector.at( 8) == 0x7F &&
vector.at( 9) == 0xFF &&
vector.at(10) == 0xFF &&
vector.at(11) == 0xFF &&
vector.at(12) == 0x00 &&
vector.at(13) == 0x00 &&
vector.at(14) == 0x00 &&
vector.at(15) == 0x00 &&
vector.at(16) == 0 &&
vector.at(17) == 0 &&
vector.at(18) == 0 &&
vector.at(19) == 4 &&
vector.at(20) == 0x80 &&
vector.at(21) == 0x00 &&
vector.at(22) == 0x00 &&
vector.at(23) == 0x00 &&
vector.at(24) == 0x7F &&
vector.at(25) == 0xFF &&
vector.at(26) == 0xFF &&
vector.at(27) == 0xFF &&
vector.at(28) == 0x00 &&
vector.at(29) == 0x00 &&
vector.at(30) == 0x00 &&
vector.at(31) == 0x00 &&
vector.at(32) == 0x80 &&
vector.at(33) == 0x00 &&
vector.at(34) == 0x00 &&
vector.at(35) == 0x01 &&
vector.at(36) == 0 &&
vector.at(37) == 0 &&
vector.at(38) == 0 &&
vector.at(39) == 2 &&
vector.at(40) == 0x80 &&
vector.at(41) == 0x00 &&
vector.at(42) == 0x00 &&
vector.at(43) == 0x00 &&
vector.at(44) == 0x00 &&
vector.at(45) == 0x00 &&
vector.at(46) == 0x00 &&
vector.at(47) == 0x0F
);
vector.clear();
std::cout << "Passed Int32Array test." << std::endl;
return 0;
}