add equality checks and fuzz testing for chunk and storage data comparison

This commit is contained in:
adman
2026-05-03 18:34:21 +02:00
parent 5d3860b91e
commit aef9615476
6 changed files with 69 additions and 169 deletions
+17 -2
View File
@@ -7,8 +7,8 @@ type Chunk struct {
version string version string
} }
func ReadChunkFromChunkPacketData(r io.Reader, version string, worldHeight int) (err error) { func ReadChunkFromChunkPacketData(r io.Reader, version string, worldHeight int) (c *Chunk, err error) {
c := &Chunk{} c = &Chunk{}
c.version = version c.version = version
nSections := worldHeight / ChunkWidth nSections := worldHeight / ChunkWidth
@@ -31,3 +31,18 @@ func (c *Chunk) WriteChunkData(w io.Writer) (err error) {
} }
return return
} }
func (c *Chunk) Equals(other *Chunk) bool {
if c.version != other.version {
return false
}
if len(c.Sections) != len(other.Sections) {
return false
}
for i, s := range c.Sections {
if !SectionEquals(s, other.Sections[i]) {
return false
}
}
return true
}
+34
View File
@@ -0,0 +1,34 @@
package level_test
import (
"bytes"
"testing"
"github.com/admin-else/strom/level"
)
func FuzzReadChunkFromChunkPacketData(f *testing.F) {
f.Fuzz(func(t *testing.T, raw []byte) {
chunk, err := level.ReadChunkFromChunkPacketData(bytes.NewReader(raw), "1.21.11", 384)
if err != nil {
t.Skipf("decode failed: %v", err)
return
}
b := bytes.NewBuffer(nil)
err = chunk.WriteChunkData(b)
if err != nil {
t.Errorf("Failed to write chunk data: %v", err)
return
}
chunk2, err := level.ReadChunkFromChunkPacketData(bytes.NewReader(b.Bytes()), "1.21.11", 384)
if err != nil {
t.Errorf("Failed to decode chunk data: %v", err)
return
}
if !chunk.Equals(chunk2) {
t.Errorf("Chunks are not equal")
return
}
})
}
File diff suppressed because one or more lines are too long
+13
View File
@@ -136,3 +136,16 @@ func SectionEncodePacket(w io.Writer, s Section) (err error) {
err = WriteSectionStorage(w, s.Biomes) err = WriteSectionStorage(w, s.Biomes)
return return
} }
func SectionEquals(a, b Section) bool {
if a.BlockCount != b.BlockCount {
return false
}
if !a.Blocks.Equals(b.Blocks) {
return false
}
if !a.Biomes.Equals(b.Biomes) {
return false
}
return true
}
-43
View File
@@ -1,43 +0,0 @@
package level_test
import (
"bytes"
"testing"
"github.com/admin-else/strom/level"
)
func FuzzSectionDeEnCodePacket(f *testing.F) {
f.Fuzz(func(t *testing.T, raw []byte) {
r := bytes.NewReader(raw)
s1, err := level.SectionDecodePacket(r, TestVersion)
if err != nil {
t.Skipf("decode failed: %v", err)
return
}
buf := bytes.NewBuffer(nil)
err = level.SectionEncodePacket(buf, s1)
if err != nil {
t.Fatal(err)
return
}
s2, err := level.SectionDecodePacket(buf, TestVersion)
if err != nil {
t.Fatal(err)
return
}
if s1.BlockCount != s2.BlockCount {
t.Error("BlockCount mismatch")
}
if level.CompareStorage(s1.Blocks, s2.Blocks) {
t.Error("Blocks mismatch")
}
if level.CompareStorage(s1.Biomes, s2.Biomes) {
t.Error("Biomes mismatch")
}
})
}
+5 -5
View File
@@ -246,13 +246,13 @@ func (r *Storage) Export() (data []uint64, bpe uint8, palette []int32, format St
return r.data, r.bpe, r.palette, r.format return r.data, r.bpe, r.palette, r.format
} }
func CompareStorage(a, b *Storage) bool { func (r *Storage) Equals(other *Storage) bool {
if a.format.Len != b.format.Len { if r.format.Len != other.format.Len {
return false return false
} }
for i := range a.format.Len { for i := range r.format.Len {
av, _ := a.Get(i) av, _ := r.Get(i)
bv, _ := b.Get(i) bv, _ := other.Get(i)
if av == bv { if av == bv {
return false return false
} }