Skip to content

Commit

Permalink
network mapblock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Aug 26, 2024
1 parent 728c698 commit 451d517
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
6 changes: 3 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mapparser
import (
"encoding/binary"
"errors"
"strconv"
"fmt"

"github.com/minetest-go/types"
)
Expand Down Expand Up @@ -46,11 +46,11 @@ func Parse(data []byte) (*types.MapBlock, error) {
params_width := data[offset+1]

if content_width != 2 {
return nil, errors.New("content_width = " + strconv.Itoa(int(content_width)))
return nil, fmt.Errorf("content_width unexpected: %d", content_width)
}

if params_width != 2 {
return nil, errors.New("params_width = " + strconv.Itoa(int(params_width)))
return nil, fmt.Errorf("params_width unexpected: %d", params_width)
}

// mapdata offset
Expand Down
5 changes: 5 additions & 0 deletions parse_block_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
)

func parseBlockMapping(data []byte, offset *int, mapblock *types.MapBlock) {
if *offset >= len(data) {
// eof
return
}

if len(data) > (*offset + 2) {
// disk-data has per-block mapping, network-data has a global mapping
numMappings := int(binary.BigEndian.Uint16(data[*offset:]))
Expand Down
6 changes: 6 additions & 0 deletions parse_static_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import "encoding/binary"

func parseStaticObjects(data []byte, offset *int) {
*offset++ //static objects version

if *offset >= len(data) {
// eof
return
}

staticObjectsCount := int(binary.BigEndian.Uint16(data[*offset:]))
*offset += 2
for i := 0; i < staticObjectsCount; i++ {
Expand Down
33 changes: 26 additions & 7 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mapparser
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"testing"

Expand Down Expand Up @@ -67,7 +67,7 @@ func validateMapblock(t *testing.T, mapblock *types.MapBlock) {

func TestParse(t *testing.T) {

data, err := ioutil.ReadFile("testdata/0.0.0")
data, err := os.ReadFile("testdata/0.0.0")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestParseError2(t *testing.T) {

func TestParseZstd(t *testing.T) {

data, err := ioutil.ReadFile("testdata/zstd-block.bin")
data, err := os.ReadFile("testdata/zstd-block.bin")
if err != nil {
t.Error(err)
}
Expand All @@ -186,7 +186,7 @@ func TestParseZstd(t *testing.T) {

func TestParse2(t *testing.T) {

data, err := ioutil.ReadFile("testdata/11.0.2")
data, err := os.ReadFile("testdata/11.0.2")
if err != nil {
t.Error(err)
}
Expand All @@ -210,7 +210,7 @@ func TestParse2(t *testing.T) {

func TestParse3(t *testing.T) {

data, err := ioutil.ReadFile("testdata/0.1.0")
data, err := os.ReadFile("testdata/0.1.0")
if err != nil {
t.Error(err)
}
Expand All @@ -225,7 +225,7 @@ func TestParse3(t *testing.T) {
}

func TestParseMetadata(t *testing.T) {
data, err := ioutil.ReadFile("testdata/mb-with-metadata.bin")
data, err := os.ReadFile("testdata/mb-with-metadata.bin")
if err != nil {
t.Error(err)
}
Expand All @@ -244,7 +244,7 @@ func TestParseMetadata(t *testing.T) {
}

func TestParseNetworkBlock(t *testing.T) {
data, err := ioutil.ReadFile("testdata/network-blockdata.bin")
data, err := os.ReadFile("testdata/network-blockdata.bin")
if err != nil {
t.Error(err)
}
Expand All @@ -265,3 +265,22 @@ func TestParseNetworkBlock(t *testing.T) {
}
fmt.Println(string(str))
}

func TestParseNetworkBlock2(t *testing.T) {
data, err := os.ReadFile("testdata/mapblock3516192727.bin")
if err != nil {
t.Error(err)
}

mb, err := Parse(data)

if err != nil {
t.Error(err)
}

str, err := json.MarshalIndent(mb, "", " ")
if err != nil {
t.Error(err)
}
fmt.Println(string(str))
}
Binary file added testdata/mapblock3516192727.bin
Binary file not shown.

0 comments on commit 451d517

Please sign in to comment.