Compare commits

...

7 Commits

Author SHA1 Message Date
BodgeMaster 7c12a92b2a NBT: implement the integer helper functions 2022-06-28 22:04:04 +02:00
BodgeMaster 12b4a8bb55 Tests: fix up alias and implement first tests 2022-06-28 22:03:27 +02:00
BodgeMaster d37f5581c3 assert.h++: fix failure handling 2022-06-28 20:11:24 +02:00
BodgeMaster c61aca5b72 assert.h++: simple ASSERT() macro 2022-06-28 19:55:22 +02:00
BodgeMaster 9b0d54165d test.sh: add copyright notice (and fix a linking issue caused by argument order -_-) 2022-06-28 19:53:38 +02:00
BodgeMaster 63b25ed749 fixed test script 2022-06-28 18:55:35 +02:00
BodgeMaster ebfa4738b6 formatting 2022-06-28 18:54:50 +02:00
7 changed files with 209 additions and 40 deletions

View File

@ -50,7 +50,7 @@ echo "Building tools..."
mkdir -pv bin/tools
# add compile commands to this array
COMPILE_COMMANDS=(
"$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -Lbin/lib -l:nbt.so -o bin/tools/dumpnbt"
"$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -Lbin/lib -l:nbt.so -o bin/tools/dumpnbt"
)
for command in ${!COMPILE_COMMANDS[@]}; do
echo "${COMPILE_COMMANDS[command]}"

View File

@ -1,5 +1,20 @@
#!/usr/bin/env bash
# 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
if [ -z "$CXX" ]; then
CXX="c++"
fi
@ -18,8 +33,21 @@ echo "$LD_LIBRARY_PATH"
mkdir -pv bin/test
echo "Building and running tests one by one..."
echo "Building tests..."
set -v
# add compile commands to this array
COMPILE_COMMANDS=(
"$CXX_WITH_FLAGS src/test/nbt_helpers.cpp -Lbin/lib -l:nbt.so -o bin/test/nbt_helpers"
)
for command in ${!COMPILE_COMMANDS[@]}; do
echo "${COMPILE_COMMANDS[command]}"
${COMPILE_COMMANDS[command]} &
done
"CXX_WITH_FLAGS" -Lbin/lib -l:nbt.so -o bin/test/nbt_helpers src/test/nbt_helpers.cpp
wait
echo "Running tests..."
for test in $(ls bin/test); do
bin/test/$test
done

View File

@ -20,7 +20,12 @@ PROJECT_BASE_DIR="$( cd -- "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 ; pw
alias clean="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/clean.sh; popd >/dev/null 2>&1"
alias clean_dependencies="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/clean_dependencies.sh; popd >/dev/null 2>&1"
alias setup_project="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/setup_project.sh; popd >/dev/null 2>&1"
alias run_tests="pushd \"$PROJECT_BASE_DIR\" >/dev/null 2>&1; scripts/test.sh; popd >/dev/null 2>&1"
function run_tests {
pushd "$PROJECT_BASE_DIR" >/dev/null 2>&1
scripts/test.sh
popd >/dev/null 2>&1
}
function build {
pushd "$PROJECT_BASE_DIR" >/dev/null 2>&1

View File

@ -21,46 +21,53 @@
#include "error.h++"
// This is just an example for how to find out if the system is big endian
// or little endian. Do not use this.
int endianness_example() {
if constexpr (std::endian::native == std::endian::big)
{
// Big-endian system
return 0;
}
else if constexpr (std::endian::native == std::endian::little)
{
// Little-endian system
return 1;
}
else
{
// Something else
return 2;
// How did we even end up here?
}
}
// or little endian.
//int endianness_example() {
// if constexpr (std::endian::native == std::endian::big)
// {
// // Big-endian system
// return 0;
// }
// else if constexpr (std::endian::native == std::endian::little)
// {
// // Little-endian system
// return 1;
// }
// else
// {
// // Something else
// return 2;
// // How did we even end up here?
// }
//}
// There is supposedly also a function htobe64 and be64toh to deal with
// converting 64 bit integers between host order and big endian.
// Though converting between host order and BE may not be necessary if the
// raw NBT data is to be used as instructions for rebuilding the data in memory.
// Doing the opposite may just be very painful.
namespace NBT {
namespace helper {
ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
if (dataSize<=currentPosition) return ErrorOr<int8_t>(true, ErrorCodes::RANGE_ERROR);
if (dataSize<currentPosition+1) return ErrorOr<int8_t>(true, ErrorCodes::RANGE_ERROR);
return ErrorOr<int8_t>((int8_t) data[currentPosition]);
}
ErrorOr<int16_t> readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int16_t>((int16_t) 0);
if (dataSize<currentPosition+2) return ErrorOr<int16_t>(true, ErrorCodes::RANGE_ERROR);
return ErrorOr<int16_t>((int16_t) ((static_cast<int16_t>(data[currentPosition]) << 8) | static_cast<int16_t>(data[currentPosition+1])));
}
ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int32_t>((int32_t) 0);
if (dataSize<currentPosition+4) return ErrorOr<int32_t>(true, ErrorCodes::RANGE_ERROR);
return ErrorOr<int32_t>((int32_t) ((static_cast<int32_t>(data[currentPosition]) << 24) | (static_cast<int32_t>(data[currentPosition+1]) << 16) | (static_cast<int32_t>(data[currentPosition+2]) << 8) | static_cast<int32_t>(data[currentPosition+3])));
}
ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int64_t>((int64_t) 0);
if (dataSize<currentPosition+8) return ErrorOr<int64_t>(true, ErrorCodes::RANGE_ERROR);
return ErrorOr<int64_t>((int64_t) ((static_cast<int64_t>(data[currentPosition]) << 56) | (static_cast<int64_t>(data[currentPosition+1]) << 48) | (static_cast<int64_t>(data[currentPosition+2]) << 40) | (static_cast<int64_t>(data[currentPosition+3]) << 32) | (static_cast<int64_t>(data[currentPosition]) << 24) | (static_cast<int64_t>(data[currentPosition+1]) << 16) | (static_cast<int64_t>(data[currentPosition+2]) << 8) | static_cast<int64_t>(data[currentPosition+3])));
}
//FIXME: we just assume that float is a single-precision IEEE754

View File

@ -42,27 +42,18 @@
namespace NBT {
namespace helper {
ErrorOr<int8_t> readInt8(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int16_t> readInt16(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int32_t> readInt32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int64_t> readInt64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
//FIXME: we just assume that float is a single-precision IEEE754
// floating point number
ErrorOr<float> readFloat32(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
//FIXME: we just assume that double is a double-precision IEEE754
// floating point number
ErrorOr<double> readFloat64(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<std::vector<int8_t>> readInt8Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
//ErrorOr<> readString(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<std::vector<int32_t>> readInt32Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<std::vector<int64_t>> readInt64Array(uint8_t data[], uint64_t dataSize, uint64_t currentPosition);
}

18
src/test/assert.h++ Normal file
View File

@ -0,0 +1,18 @@
// 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 <iostream>
#define ASSERT(truth) if ((truth)); else { std::cout << "Assertion failed: " << #truth << std::endl; return 1; }

View File

@ -1,3 +1,123 @@
// 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 <iostream>
#include <cstdint>
#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 == 2242829044932683046L);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
// begin of data
currentPosition = 0;
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2170488872094606374L);
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).isError == false);
// end of data
currentPosition = 2;
ASSERT(NBT::helper::readInt64(data, dataSize, currentPosition).value == 2315169217770759719L);
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;
}