Skip to content

Commit

Permalink
Tests for chunkParser and bugfixes
Browse files Browse the repository at this point in the history
It's incredible how even simple tests help bugfix
  • Loading branch information
TCA166 committed Jul 20, 2023
1 parent 2f6c7ae commit 7f9bb86
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ modelGenerator
regionFileReader
radiusGenerator
!tests/0.0.nbt
!tests/0.1.nbt
!tests/r.0.0.mca
*.log
*.mca
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ clean:
rm -f *.ow
rm -f cNBT/*.ow

check: hTable.o regionParser.o
check: hTable.o regionParser.o chunkParser.o cNBT.o
#hTable tests
checkmk tests/hTable.check > tests/hTableCheck.c
gcc tests/hTableCheck.c hTable.o -lcheck -lm -lsubunit -Wall -o tests/hTableCheck
Expand All @@ -83,6 +83,10 @@ check: hTable.o regionParser.o
checkmk tests/regionParser.check > tests/regionParserCheck.c
gcc tests/regionParserCheck.c regionParser.o -lcheck -lm -lz -lsubunit -o tests/regionParserCheck
./tests/regionParserCheck
#chunkParser tests
checkmk tests/chunkParser.check > tests/chunkParserCheck.c
gcc tests/chunkParserCheck.c chunkParser.o cNBT.o -lcheck -lm -lsubunit -o tests/chunkParserCheck
./tests/chunkParserCheck
#unit tests run, now just run the programs
./chunkExtractor ./tests 0 0
./modelGenerator ./tests/0.0.nbt -b -out 0.0.obj
15 changes: 9 additions & 6 deletions chunkParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,18 @@ unsigned int* getBlockStates(struct section s, int* outLen){
if(l < 4){
l = 4;
}
short count = ceilf(64/(float)l) * s.blockDataLen; //amount of indices in each long
states = malloc(count * sizeof(unsigned int));
short numPerLong = (short)(64/l);
states = malloc(numPerLong * s.blockDataLen * sizeof(unsigned int));
//foreach long
for(int a=0; a < s.blockDataLen; a++){
unsigned long comp = s.blockData[a];
//foreach set of l bits
for(short b = 0; b < 64; b+=l){
unsigned long mask = createMask(b, l);
states[m] = (unsigned int)((mask & comp) >> b);
for(short b = 0; b < numPerLong; b++){
if(m >= 4096){
break;
}
short bits = b * l;
unsigned long mask = createMask(bits, l);
states[m] = (unsigned int)((mask & comp) >> bits);
m++;
}
}
Expand Down
Binary file added tests/0.1.nbt
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ regionParserCheck.c: regionParser.check
regionParserCheck: regionParserCheck.c ../regionParser.o
gcc regionParserCheck.c ../regionParser.o -lcheck -lm -lz -lsubunit -o regionParserCheck

chunkParserCheck.c: chunkParser.check
checkmk chunkParser.check > chunkParserCheck.c

chunkParserCheck: chunkParserCheck.c ../chunkParser.o ../cNBT.o
gcc chunkParserCheck.c ../chunkParser.o ../cNBT.o -lcheck -lm -lsubunit -o chunkParserCheck

clean:
rm -f *.c
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Since I use files in some tests here are the sha256 hashes for the files:

- 8583b9137dc65f5e04e36f19dd247d1159d519617028b4a3f4b3ddf02ecd8627 r.0.0.mca
- e9be2818875684b5e0802b49dcfc0ef27a4c1966d179a262bc1b8a5a1a8e780a 0.0.nbt
- a68a2fe51ce931828ed7e620263b8c25d23b4bc8e8089cfd3eaaf16a9870b9f9 0.1.nbt
72 changes: 72 additions & 0 deletions tests/chunkParser.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "../chunkParser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define getTestChunk FILE* testChunk = fopen("0.1.nbt", "rb"); if(testChunk == NULL) testChunk = fopen("tests/0.0.nbt", "rb");

#suite chunkParserTests

#test getSectionsTest
getTestChunk
fseek(testChunk, 0L, SEEK_END);
long sz = ftell(testChunk);
fseek(testChunk, 0, SEEK_SET);
unsigned char* data = calloc(sz, 1);
fread(data, sz, 1, testChunk);
fclose(testChunk);
struct section sections[maxSections];
int numSections = getSections(data, sz, sections);
ck_assert_msg(numSections > 0, "No sections were extracted");
for(int i = 0; i < numSections; i++){
ck_assert_msg(sections[i].y >= minY / 16, "Y was lower than the expected value");
ck_assert_msg(sections[i].paletteLen > 0, "Palette was empty");
ck_assert_msg(sections[i].paletteLen == 1 || sections[i].blockDataLen > 0, "Invalid block data len");
//here we check the palette property order
const char propertyOrder[12][20] = {"color", "east", "facing", "half", "north", "open", "part", "shape", "snowy", "south", "west"};
for(int n = 0; n < sections[i].paletteLen; n++){
int index = -1;
char* properties = strchr(sections[i].blockPalette[n], ';');
char* token = strtok(sections[i].blockPalette[n], ",");
while(token != NULL){
for(int j = 0; j < 12; j++){
if(strcmp(propertyOrder[j], token) == 0){
ck_assert_msg(j > index, "Incorrect property order in palette");
index = j;
}
}
token = strtok(NULL, ",");
}
}
}

#test statesTest
getTestChunk
fseek(testChunk, 0L, SEEK_END);
long sz = ftell(testChunk);
fseek(testChunk, 0, SEEK_SET);
unsigned char* data = calloc(sz, 1);
fread(data, sz, 1, testChunk);
fclose(testChunk);
struct section sections[maxSections];
int numSections = getSections(data, sz, sections);
for(int i = 0; i < numSections; i++){
int sz = 0;
unsigned int* blockStates = getBlockStates(sections[i], &sz);
ck_assert_msg(sz > 0 || sections[i].paletteLen == 1, "No block states were extracted");
if(!(sz == 0 && sections[i].paletteLen == 1)){
ck_assert_msg(sz == 16*16*16, "Invalid sz");
for(int n = 0; n < sz; n++){
ck_assert_msg(blockStates[n] < sections[i].paletteLen, "Block state larger than palette");
}
for(int n = 0; n < sections[i].paletteLen; n++){
char present = 0;
for(int j = 0; j < sz; j++){
if(blockStates[j] == n){
present = 1;
}
}
ck_assert_msg(present, "Palette item wasn't used");
}
}
}

0 comments on commit 7f9bb86

Please sign in to comment.