github.com/iDigitalFlame/xmt@v0.5.4/data/chunk_test.go (about) 1 // Copyright (C) 2020 - 2023 iDigitalFlame 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU General Public License for more details. 12 // 13 // You should have received a copy of the GNU General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 // 16 17 package data 18 19 import "testing" 20 21 func TestChunk(t *testing.T) { 22 var b Chunk 23 b.WriteInt32(0xFF) 24 b.WriteFloat32(1.45) 25 b.WriteInt8(120) 26 b.WriteString("derp123") 27 b.WriteUint64(0xFF00FF00FF123) 28 b.WriteUint16Pos(0, 0xFF) 29 r := NewChunk(b.Payload()) 30 v, err := r.Int32() 31 if err != nil { 32 t.Fatalf("TestChunk(): Int32 failed with error: %s!", err.Error()) 33 } 34 if v != 0xFF00FF { 35 t.Fatalf(`TestChunk(): Int32 result "0x%X" does not match the expected value "0xFF00FF"!`, v) 36 } 37 f, err := r.Float32() 38 if err != nil { 39 t.Fatalf("TestChunk(): Float32 failed with error: %s!", err.Error()) 40 } 41 if f != 1.45 { 42 t.Fatalf(`TestChunk(): Float32 result "%.2f" does not match the expected value "1.45"!`, f) 43 } 44 n, err := r.Int8() 45 if err != nil { 46 t.Fatalf("TestChunk(): Int8 failed with error: %s!", err.Error()) 47 } 48 if n != 120 { 49 t.Fatalf(`TestChunk(): Int8 result "%d" does not match the expected value "120"!`, n) 50 } 51 s, err := r.StringVal() 52 if err != nil { 53 t.Fatalf("TestChunk(): StringVal failed with error: %s!", err.Error()) 54 } 55 if s != "derp123" { 56 t.Fatalf(`TestChunk(): StringVal result "%s" does not match the expected value 120!`, s) 57 } 58 u, err := r.Uint64() 59 if err != nil { 60 t.Fatalf("TestChunk(): Uint64 failed with error: %s!", err.Error()) 61 } 62 if u != 0xFF00FF00FF123 { 63 t.Fatalf(`TestChunk(): Uint64 result "0x%X" does not match the expected value "0xFF00FF00FF123"!`, u) 64 } 65 }