hnakra/protocol/int_test.go
2023-05-25 18:08:56 -04:00

60 lines
1.3 KiB
Go

package protocol
import "bytes"
import "testing"
func TestU8 (test *testing.T) {
var conduit bytes.Buffer
numbers := []uint8 { 0, 1, 255, 100, 18, 16, 127, 99 }
for _, number := range numbers {
err := WriteU8(&conduit, number)
if err != nil { test.Fatal(err) }
}
for _, correct := range numbers {
number, err := ReadU8(&conduit)
if err != nil { test.Fatal(err) }
if number != correct {
test.Fatalf("expected %d, read back %d", correct, number)
}
}
}
func TestU16 (test *testing.T) {
var conduit bytes.Buffer
numbers := []uint16 { 0, 57, 900, 1000, 10000, 7881, 50023 }
for _, number := range numbers {
err := WriteU16(&conduit, number)
if err != nil { test.Fatal(err) }
}
for _, correct := range numbers {
number, err := ReadU16(&conduit)
if err != nil { test.Fatal(err) }
if number != correct {
test.Fatalf("expected %d, read back %d", correct, number)
}
}
}
func TestU32 (test *testing.T) {
var conduit bytes.Buffer
numbers := []uint32 { 0, 57, 901239, 1000, 3480238490, 10000, 7881, 50023 }
for _, number := range numbers {
err := WriteU32(&conduit, number)
if err != nil { test.Fatal(err) }
}
for _, correct := range numbers {
number, err := ReadU32(&conduit)
if err != nil { test.Fatal(err) }
if number != correct {
test.Fatalf("expected %d, read back %d", correct, number)
}
}
}