github.com/bhojpur/cache@v0.0.4/pkg/ioutils/buffer_test.go (about) 1 package ioutils 2 3 // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved. 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 import ( 24 "bytes" 25 "testing" 26 ) 27 28 func TestFixedBufferCap(t *testing.T) { 29 buf := &fixedBuffer{buf: make([]byte, 0, 5)} 30 31 n := buf.Cap() 32 if n != 5 { 33 t.Fatalf("expected buffer capacity to be 5 bytes, got %d", n) 34 } 35 } 36 37 func TestFixedBufferLen(t *testing.T) { 38 buf := &fixedBuffer{buf: make([]byte, 0, 10)} 39 40 buf.Write([]byte("hello")) 41 l := buf.Len() 42 if l != 5 { 43 t.Fatalf("expected buffer length to be 5 bytes, got %d", l) 44 } 45 46 buf.Write([]byte("world")) 47 l = buf.Len() 48 if l != 10 { 49 t.Fatalf("expected buffer length to be 10 bytes, got %d", l) 50 } 51 52 // read 5 bytes 53 b := make([]byte, 5) 54 buf.Read(b) 55 56 l = buf.Len() 57 if l != 5 { 58 t.Fatalf("expected buffer length to be 5 bytes, got %d", l) 59 } 60 61 n, err := buf.Write([]byte("i-wont-fit")) 62 if n != 0 { 63 t.Fatalf("expected no bytes to be written to buffer, got %d", n) 64 } 65 if err != errBufferFull { 66 t.Fatalf("expected errBufferFull, got %v", err) 67 } 68 69 l = buf.Len() 70 if l != 5 { 71 t.Fatalf("expected buffer length to still be 5 bytes, got %d", l) 72 } 73 74 buf.Reset() 75 l = buf.Len() 76 if l != 0 { 77 t.Fatalf("expected buffer length to still be 0 bytes, got %d", l) 78 } 79 } 80 81 func TestFixedBufferString(t *testing.T) { 82 buf := &fixedBuffer{buf: make([]byte, 0, 10)} 83 84 buf.Write([]byte("hello")) 85 buf.Write([]byte("world")) 86 87 out := buf.String() 88 if out != "helloworld" { 89 t.Fatalf("expected output to be \"helloworld\", got %q", out) 90 } 91 92 // read 5 bytes 93 b := make([]byte, 5) 94 buf.Read(b) 95 96 // test that fixedBuffer.String() only returns the part that hasn't been read 97 out = buf.String() 98 if out != "world" { 99 t.Fatalf("expected output to be \"world\", got %q", out) 100 } 101 } 102 103 func TestFixedBufferWrite(t *testing.T) { 104 buf := &fixedBuffer{buf: make([]byte, 0, 64)} 105 n, err := buf.Write([]byte("hello")) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 if n != 5 { 111 t.Fatalf("expected 5 bytes written, got %d", n) 112 } 113 114 if string(buf.buf[:5]) != "hello" { 115 t.Fatalf("expected \"hello\", got %q", string(buf.buf[:5])) 116 } 117 118 n, err = buf.Write(bytes.Repeat([]byte{1}, 64)) 119 if n != 59 { 120 t.Fatalf("expected 59 bytes written before buffer is full, got %d", n) 121 } 122 if err != errBufferFull { 123 t.Fatalf("expected errBufferFull, got %v - %v", err, buf.buf[:64]) 124 } 125 } 126 127 func TestFixedBufferRead(t *testing.T) { 128 buf := &fixedBuffer{buf: make([]byte, 0, 64)} 129 if _, err := buf.Write([]byte("hello world")); err != nil { 130 t.Fatal(err) 131 } 132 133 b := make([]byte, 5) 134 n, err := buf.Read(b) 135 if err != nil { 136 t.Fatal(err) 137 } 138 139 if n != 5 { 140 t.Fatalf("expected 5 bytes read, got %d - %s", n, buf.String()) 141 } 142 143 if string(b) != "hello" { 144 t.Fatalf("expected \"hello\", got %q", string(b)) 145 } 146 147 n, err = buf.Read(b) 148 if err != nil { 149 t.Fatal(err) 150 } 151 152 if n != 5 { 153 t.Fatalf("expected 5 bytes read, got %d", n) 154 } 155 156 if string(b) != " worl" { 157 t.Fatalf("expected \" worl\", got %s", string(b)) 158 } 159 160 b = b[:1] 161 n, err = buf.Read(b) 162 if err != nil { 163 t.Fatal(err) 164 } 165 166 if n != 1 { 167 t.Fatalf("expected 1 byte read, got %d - %s", n, buf.String()) 168 } 169 170 if string(b) != "d" { 171 t.Fatalf("expected \"d\", got %s", string(b)) 172 } 173 }