github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/coder/arith/writer_test.go (about) 1 package arith 2 3 import ( 4 "bytes" 5 "math/rand" 6 "testing" 7 ) 8 9 // Tests a model provided by the constructor 10 func WriterReaderTest(t *testing.T, N int, model func() Model) { 11 data := make([]byte, N) 12 for i := range data { 13 data[i] = byte(rand.Int()) 14 } 15 16 w := NewWriter(model()) 17 n, err := w.Write(data) 18 if n != N || err != nil { 19 t.Fatalf("failed write (%v,%v): %v", n, N, err) 20 } 21 22 if err := w.Close(); err != nil { 23 t.Fatalf("failed close: %v", err) 24 } 25 26 got := make([]byte, N) 27 28 r := NewReader(w.Bytes(), model()) 29 n, err = r.Read(got) 30 if n != N || err != nil { 31 t.Fatalf("failed read (%v,%v): %v", n, N, err) 32 } 33 34 if !bytes.Equal(got, data) { 35 t.Fatalf("different content: %v, %v", got, data) 36 } 37 } 38 39 func TestShiftWriter(t *testing.T) { 40 WriterReaderTest(t, 3, func() Model { return &Shift{Prob(0.5), 3} }) 41 WriterReaderTest(t, 21, func() Model { return &Shift{Prob(0.1), 2} }) 42 WriterReaderTest(t, 312532, func() Model { return &Shift{Prob(0.9), 1} }) 43 }