Browse Source

More tests

master
Zack Marvel 2 years ago
parent
commit
c9a24b1653
  1. 8
      include/nbt.hpp
  2. 2
      src/nbt.cpp
  3. 5
      test/data/Makefile
  4. 5
      test/data/ends_unexpectedly_compound.hex
  5. 4
      test/data/ends_unexpectedly_int.hex
  6. 11
      test/data/ends_unexpectedly_list.hex
  7. 11
      test/data/ends_unexpectedly_long_array.hex
  8. 4
      test/data/ends_unexpectedly_name.hex
  9. 86
      test/test_nbt.cpp

8
include/nbt.hpp

@ -111,7 +111,11 @@ struct ArrayTag : TagBase {
value().push_back(val);
}
T& at(size_t i) const {
const T& at(size_t i) const {
return mValue.at(i);
}
T& at(size_t i) {
return mValue.at(i);
}
@ -470,6 +474,7 @@ class NBTFile {
}
TagID readID();
std::string readName();
template <typename T>
T readTag();
@ -490,7 +495,6 @@ class NBTFile {
CompoundTag readCompoundTag(std::string name);
private:
std::string readName();
int32_t readListSize();
int32_t readSize();
std::ifstream file;

2
src/nbt.cpp

@ -27,7 +27,7 @@ NBTFile::NBTFile(std::string filename)
: file{filename, std::ios_base::in | std::ios_base::binary}
{
if (!file.is_open()) {
std::cerr << "ERR: NBTFile is not open" << std::endl;
throw NBTException("Unable to open file");
}
}

5
test/data/Makefile

@ -15,6 +15,11 @@ DATS = \
list_string_tag.dat \
list_compound_tag.dat \
compound_tag.dat \
ends_unexpectedly_list.dat \
ends_unexpectedly_compound.dat \
ends_unexpectedly_int.dat \
ends_unexpectedly_name.dat \
ends_unexpectedly_long_array.dat \
HEXS = $(DATS:.dat=.hex)

5
test/data/ends_unexpectedly_compound.hex

@ -0,0 +1,5 @@
0a
02
00 08
73 68 6f 72 74 20 3a 29

4
test/data/ends_unexpectedly_int.hex

@ -0,0 +1,4 @@
03
00 07
62 61 64 20 69 6e 74
01 02 03

11
test/data/ends_unexpectedly_list.hex

@ -0,0 +1,11 @@
09
00 0a
62 61 64 20 73 74 72 69 6e 67
03
00 00 00 04
00 01 02 03
01 02 03 04
02 03 04 05

11
test/data/ends_unexpectedly_long_array.hex

@ -0,0 +1,11 @@
0c
0a
6c 6f 6e 67 20 61 72 72 61 79
00 00 00 04
00 11 22 33 44 55 66 77
11 22 33 44 55 66 77 88
22 33 44 55 66 77 88 99
33 44 55 66 77 88 99

4
test/data/ends_unexpectedly_name.hex

@ -0,0 +1,4 @@
0b
0a
69 6e 74 20 61 72 72 61 79

86
test/test_nbt.cpp

@ -210,18 +210,82 @@ TEST_CASE("Reading from test files", "[nbtfile]") {
REQUIRE(tag.size() == 2);
{
//CompoundTag child0 = tag.at(0);
//REQUIRE(child0.at(0)->id() == TagID::STRING);
//StringTag string = *std::dynamic_pointer_cast<StringTag>(child0.at(0));
//REQUIRE(string.name() == "string child");
//REQUIRE(string.value() == "asdfsdfg");
//REQUIRE(child0.at(1)->id() == TagID::LONG_ARRAY);
CompoundTag child = tag.at(0);
REQUIRE(child.at(0)->id() == TagID::STRING);
std::shared_ptr<StringTag> pString =
std::dynamic_pointer_cast<StringTag>(child.at(0));
REQUIRE(pString->name() == "string child");
REQUIRE(pString->value() == "asdfsdfg");
REQUIRE(child.at(1)->id() == TagID::LONG_ARRAY);
std::shared_ptr<LongArrayTag> pLongArray =
std::dynamic_pointer_cast<LongArrayTag>(child.at(1));
REQUIRE(pLongArray->name() == "long array child");
REQUIRE(pLongArray->size() == 2);
REQUIRE(pLongArray->at(0) == 0x0001020304050607);
REQUIRE(pLongArray->at(1) == 0x08090a0b0c0d0e0f);
}
{
CompoundTag child = tag.at(1);
REQUIRE(child.at(0)->id() == TagID::INT);
std::shared_ptr<IntTag> pInt =
std::dynamic_pointer_cast<IntTag>(child.at(0));
REQUIRE(pInt->name() == "int child");
REQUIRE(pInt->value() == 0x01020304);
REQUIRE(child.at(1)->id() == TagID::SHORT);
std::shared_ptr<ShortTag> pShort0 =
std::dynamic_pointer_cast<ShortTag>(child.at(1));
REQUIRE(pShort0->name() == "short child");
REQUIRE(pShort0->value() == 0x0506);
REQUIRE(child.at(2)->id() == TagID::SHORT);
std::shared_ptr<ShortTag> pShort1 =
std::dynamic_pointer_cast<ShortTag>(child.at(2));
REQUIRE(pShort1->name() == "short child2");
REQUIRE(pShort1->value() == 0x0708);
}
}
}
// TODO
// Test bad paths:
// - File doesn't exist
// - File ends unexpectedly (e.g. list with length 2 and 0 real members)
// - CompoundTag with no EndTag (falls into "file ends unexpectedly")
SECTION("Error conditions") {
SECTION("Input file doesn't exist") {
auto constructFile = []() {
NBTFile file{"./file/doesnt/exist.dat"};
};
REQUIRE_THROWS(constructFile());
}
SECTION("File ends unexpectedly (list)") {
NBTFile file{"./test/data/ends_unexpectedly_list.dat"};
REQUIRE(file.readID() == TagID::LIST);
std::string name = file.readName();
REQUIRE(name == "bad string");
TagID childID = file.readID();
REQUIRE(childID == TagID::INT);
REQUIRE_THROWS(file.readTagList<IntTag>(childID, name));
}
SECTION("File ends unexpectedly (compound)") {
NBTFile file{"./test/data/ends_unexpectedly_compound.dat"};
REQUIRE(file.readID() == TagID::COMPOUND);
REQUIRE_THROWS(file.readCompoundTag(""));
}
SECTION("File ends unexpectedly (int)") {
NBTFile file{"./test/data/ends_unexpectedly_int.dat"};
REQUIRE(file.readID() == TagID::INT);
std::string name = file.readName();
REQUIRE(name == "bad int");
REQUIRE_THROWS(file.readTag<IntTag>(name));
}
SECTION("File ends unexpectedly (name)") {
NBTFile file{"./test/data/ends_unexpectedly_name.dat"};
REQUIRE(file.readID() == TagID::INT_ARRAY);
REQUIRE_THROWS(file.readName());
}
SECTION("File ends unexpectedly (long array)") {
NBTFile file{"./test/data/ends_unexpectedly_long_array.dat"};
REQUIRE(file.readID() == TagID::LONG_ARRAY);
REQUIRE_THROWS(file.readName());
}
}
}

Loading…
Cancel
Save