// Copyright 2022, FOSS-VG Developers and Contributers // // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, version 3. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // version 3 along with this program. // If not, see https://www.gnu.org/licenses/agpl-3.0.en.html #include #include #include "assert.h++" #include "../lib/nbt.h++" #include "../lib/error.h++" int main(){ // used for all integer tests uint8_t data[] = {30, 31, 32, 33, 34, 35, 36, 37, 38, 39}; uint64_t dataSize = 10; // int8 ############################################################ // read successfully uint64_t currentPosition = 5; ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).value == 35); ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).isError == false); // begin of data currentPosition = 0; ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).value == 30); ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).isError == false); // end of data currentPosition = 9; ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).value == 39); ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).isError == false); // out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt8(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); std::cout << "Passed readInt8 NBT helper test" << std::endl; // int16 ########################################################### // read successfully currentPosition = 5; ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).value == 8996); ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).isError == false); // begin of data currentPosition = 0; ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).value == 7711); ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).isError == false); // end of data currentPosition = 8; ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).value == 9767); ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).isError == false); // partially out of bounds currentPosition = 9; ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); // fully out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt16(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); std::cout << "Passed readInt16 NBT helper test" << std::endl; // int32 ########################################################### // read successfully currentPosition = 5; ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).value == 589571366); ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).isError == false); // begin of data currentPosition = 0; ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).value == 505356321); ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).isError == false); // end of data currentPosition = 6; ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).value == 606414375); ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).isError == false); // partially out of bounds currentPosition = 7; ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); // fully out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt32(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); std::cout << "Passed readInt32 NBT helper test" << std::endl; // int64 ########################################################### // read successfully currentPosition = 1; ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2242829044932683046); ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false); // begin of data currentPosition = 0; ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2170488872094606373); ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false); // end of data currentPosition = 2; ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2315169217770759719); ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false); // partially out of bounds currentPosition = 3; ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); // fully out of bounds currentPosition = 10; ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == true); ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).errorCode == ErrorCodes::RANGE_ERROR); std::cout << "Passed int64 NBT helper test" << std::endl; return 0; }