Finish development of fastnbt

This commit is contained in:
Tnze
2023-04-24 21:59:30 +08:00
parent ad3f69e40b
commit 37d4179bb2
4 changed files with 169 additions and 29 deletions

View File

@ -1 +1,75 @@
package fastnbt
import (
"bytes"
"testing"
"github.com/Tnze/go-mc/nbt"
)
func TestValue_new(t *testing.T) {
if val := NewBoolean(true); val.Boolean() != true {
t.Error("encode bool error")
}
if val := NewBoolean(false); val.Boolean() != false {
t.Error("encode bool error")
}
if val := NewByte(127); val.Byte() != 127 {
t.Error("encode byte error")
}
if val := NewShort(32767); val.Short() != 32767 {
t.Error("encode short error")
}
if val := NewInt(2147483647); val.Int() != 2147483647 {
t.Error("encode int error")
}
if val := NewLong(9223372036854775807); val.Long() != 9223372036854775807 {
t.Error("encode long error")
}
if val := NewString("HELLO WORLD THIS IS A TEST STRING ÅÄÖ!"); val.String() != "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!" {
t.Error("encode string error")
}
if val := NewFloat(0.49823147); val.Float() != 0.49823147 {
t.Error("encode float error")
}
if val := NewDouble(0.4931287132182315); val.Double() != 0.4931287132182315 {
t.Error("encode double error")
}
byteArray := make([]byte, 1000)
for n := 0; n < 1000; n++ {
byteArray[n] = byte((n*n*255 + n*7) % 100)
}
if val := NewByteArray(byteArray); !bytes.Equal(byteArray, val.ByteArray()) {
t.Error("encode byteArray error")
}
val := NewCompound()
val.Set("a", NewString("tnze"))
if val.Get("a").String() != "tnze" || val.Compound().Len() != 1 {
t.Error("encode compound error")
}
}
func TestValue_bigTest(t *testing.T) {
data, err := nbt.Marshal(nbt.StringifiedMessage(bigTestSNBT))
if err != nil {
t.Fatal(err)
}
var val Value
err = nbt.Unmarshal(data, &val)
if err != nil {
t.Fatal(err)
}
var data2 []byte
data2, err = nbt.Marshal(&val)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, data2) {
t.Fail()
}
}