github.com/kunnos/engine@v1.13.1/pkg/ioutils/buffer_test.go (about) 1 package ioutils 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestFixedBufferWrite(t *testing.T) { 9 buf := &fixedBuffer{buf: make([]byte, 0, 64)} 10 n, err := buf.Write([]byte("hello")) 11 if err != nil { 12 t.Fatal(err) 13 } 14 15 if n != 5 { 16 t.Fatalf("expected 5 bytes written, got %d", n) 17 } 18 19 if string(buf.buf[:5]) != "hello" { 20 t.Fatalf("expected \"hello\", got %q", string(buf.buf[:5])) 21 } 22 23 n, err = buf.Write(bytes.Repeat([]byte{1}, 64)) 24 if err != errBufferFull { 25 t.Fatalf("expected errBufferFull, got %v - %v", err, buf.buf[:64]) 26 } 27 } 28 29 func TestFixedBufferRead(t *testing.T) { 30 buf := &fixedBuffer{buf: make([]byte, 0, 64)} 31 if _, err := buf.Write([]byte("hello world")); err != nil { 32 t.Fatal(err) 33 } 34 35 b := make([]byte, 5) 36 n, err := buf.Read(b) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 if n != 5 { 42 t.Fatalf("expected 5 bytes read, got %d - %s", n, buf.String()) 43 } 44 45 if string(b) != "hello" { 46 t.Fatalf("expected \"hello\", got %q", string(b)) 47 } 48 49 n, err = buf.Read(b) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 if n != 5 { 55 t.Fatalf("expected 5 bytes read, got %d", n) 56 } 57 58 if string(b) != " worl" { 59 t.Fatalf("expected \" worl\", got %s", string(b)) 60 } 61 62 b = b[:1] 63 n, err = buf.Read(b) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 if n != 1 { 69 t.Fatalf("expected 1 byte read, got %d - %s", n, buf.String()) 70 } 71 72 if string(b) != "d" { 73 t.Fatalf("expected \"d\", got %s", string(b)) 74 } 75 }