github.com/QuangTung97/bigcache@v0.1.0/ringbuf_test.go (about) 1 package bigcache 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestRingBuf_Append(t *testing.T) { 9 rb := newRingBuf(16) 10 11 assert.Equal(t, 0, rb.getEnd()) 12 offset := rb.append([]byte{10, 11, 12, 13}) 13 assert.Equal(t, 0, offset) 14 assert.Equal(t, 4, rb.getEnd()) 15 assert.Equal(t, 12, rb.getAvailable()) 16 17 data := make([]byte, 4) 18 rb.readAt(data, 0) 19 20 assert.Equal(t, []byte{10, 11, 12, 13}, data) 21 22 offset = rb.append([]byte{20, 21}) 23 assert.Equal(t, 4, offset) 24 assert.Equal(t, 6, rb.getEnd()) 25 assert.Equal(t, 10, rb.getAvailable()) 26 27 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 28 rb.readAt(data, 6) 29 assert.Equal(t, []byte{1, 2, 3, 4}, data) 30 31 assert.Equal(t, 14, rb.getEnd()) 32 33 assert.Equal(t, 2, rb.getAvailable()) 34 35 rb.skip(4) 36 assert.Equal(t, 6, rb.getAvailable()) 37 } 38 39 func TestRingBuf_Append_WrapAround(t *testing.T) { 40 rb := newRingBuf(16) 41 rb.append([]byte{20, 21, 22, 23}) 42 rb.append([]byte{20, 21, 22, 23}) 43 rb.append([]byte{20, 21, 22, 23}) 44 45 rb.skip(8) 46 rb.append([]byte{1, 2, 3, 4, 5, 6, 7}) 47 assert.Equal(t, 3, rb.getEnd()) 48 49 rb.append([]byte{10, 11}) 50 assert.Equal(t, 5, rb.getEnd()) 51 assert.Equal(t, 3, rb.getAvailable()) 52 53 data := make([]byte, 5) 54 rb.readAt(data, 0) 55 assert.Equal(t, []byte{5, 6, 7, 10, 11}, data) 56 57 data = make([]byte, 7) 58 rb.readAt(data, 12) 59 assert.Equal(t, []byte{1, 2, 3, 4, 5, 6, 7}, data) 60 } 61 62 func TestRingBuf_Skip_Wrap_Around(t *testing.T) { 63 rb := newRingBuf(16) 64 rb.append([]byte{20, 21, 22, 23}) 65 rb.append([]byte{20, 21, 22, 23}) 66 rb.append([]byte{20, 21, 22, 23}) 67 rb.skip(4) 68 rb.skip(4) 69 rb.append([]byte{20, 21, 22, 23}) 70 71 rb.skip(4) 72 rb.skip(4) 73 rb.skip(4) 74 rb.append([]byte{20, 21, 22, 23}) 75 rb.append([]byte{20, 21, 22, 23}) 76 rb.append([]byte{20, 21, 22, 23}) 77 78 assert.Equal(t, 4, rb.getBegin()) 79 assert.Equal(t, 12, rb.getEnd()) 80 } 81 82 func TestRingBuf_BytesEqual(t *testing.T) { 83 rb := newRingBuf(16) 84 rb.append([]byte{10, 11, 12, 13}) 85 86 result := rb.bytesEqual(0, []byte{1, 2}) 87 assert.Equal(t, false, result) 88 89 result = rb.bytesEqual(0, []byte{10, 11}) 90 assert.Equal(t, true, result) 91 92 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 93 rb.append([]byte{30, 31, 32, 33}) 94 95 // Wrap Around 96 97 result = rb.bytesEqual(13, []byte{9, 10, 11, 50, 51}) 98 assert.Equal(t, false, result) 99 100 result = rb.bytesEqual(13, []byte{31, 32, 33, 50, 51}) 101 assert.Equal(t, false, result) 102 103 result = rb.bytesEqual(13, []byte{31, 32, 8, 10, 11}) 104 assert.Equal(t, false, result) 105 106 result = rb.bytesEqual(13, []byte{31, 32, 33, 10, 11}) 107 assert.Equal(t, true, result) 108 } 109 110 func TestRingBuf_Evacuate_Simple(t *testing.T) { 111 rb := newRingBuf(16) 112 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 113 rb.evacuate(4) 114 115 assert.Equal(t, 4, rb.getBegin()) 116 assert.Equal(t, 12, rb.getEnd()) 117 118 data := make([]byte, 8) 119 rb.readAt(data, 4) 120 assert.Equal(t, []byte{5, 6, 7, 8, 1, 2, 3, 4}, data) 121 } 122 123 func TestRingBuf_Evacuate_End_WrapAround(t *testing.T) { 124 rb := newRingBuf(16) 125 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 126 rb.append([]byte{21, 22, 23, 24, 25, 26}) 127 rb.evacuate(6) 128 129 assert.Equal(t, 6, rb.getBegin()) 130 assert.Equal(t, 4, rb.getEnd()) 131 132 data := make([]byte, 6) 133 rb.readAt(data, 14) 134 assert.Equal(t, []byte{1, 2, 3, 4, 5, 6}, data) 135 } 136 137 func TestRingBuf_Evacuate_Backward(t *testing.T) { 138 rb := newRingBuf(16) 139 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 140 rb.append([]byte{21, 22, 23, 24, 25, 26}) 141 rb.skip(4) 142 rb.append([]byte{31, 32, 33, 34}) 143 144 assert.Equal(t, 4, rb.getBegin()) 145 assert.Equal(t, 2, rb.getEnd()) 146 147 prevEnd := rb.evacuate(5) 148 assert.Equal(t, 2, prevEnd) 149 150 data := make([]byte, 5) 151 rb.readAt(data, 2) 152 assert.Equal(t, []byte{5, 6, 7, 8, 21}, data) 153 assert.Equal(t, 9, rb.getBegin()) 154 assert.Equal(t, 7, rb.getEnd()) 155 } 156 157 func TestRingBuf_Evacuate_Both_From_And_To_Are_Wrap_Around_End_Smaller_Than_Begin(t *testing.T) { 158 rb := newRingBuf(16) 159 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 160 rb.append([]byte{9, 10, 11, 12, 13, 14, 15, 16}) 161 rb.skip(13) 162 rb.append([]byte{ 163 51, 52, 53, 54, 164 55, 56, 57, 58, 165 59, 60, 61, 166 }) 167 rb.evacuate(6) 168 data := make([]byte, 6) 169 rb.readAt(data, 11) 170 assert.Equal(t, []byte{14, 15, 16, 51, 52, 53}, data) 171 assert.Equal(t, 1, rb.getEnd()) 172 assert.Equal(t, 3, rb.getBegin()) 173 } 174 175 func TestRingBuf_Evacuate_Begin_Near_Max(t *testing.T) { 176 rb := newRingBuf(16) 177 rb.append([]byte{1, 2, 3, 4, 5, 6, 7, 8}) 178 rb.append([]byte{21, 22, 23, 24, 25}) 179 180 rb.evacuate(10) 181 assert.Equal(t, 10, rb.getBegin()) 182 assert.Equal(t, 7, rb.getEnd()) 183 184 data := make([]byte, 13) 185 rb.readAt(data, 10) 186 assert.Equal(t, []byte{23, 24, 25, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22}, data) 187 188 prevEnd := rb.evacuate(8) 189 assert.Equal(t, 7, prevEnd) 190 assert.Equal(t, 2, rb.getBegin()) 191 assert.Equal(t, 15, rb.getEnd()) 192 193 data = make([]byte, 13) 194 rb.readAt(data, 2) 195 assert.Equal(t, []byte{6, 7, 8, 21, 22, 23, 24, 25, 1, 2, 3, 4, 5}, data) 196 } 197 198 func TestRingBuf_WriteAt_WrapAround(t *testing.T) { 199 rb := newRingBuf(7) 200 201 rb.writeAt([]byte{10, 11, 12, 13, 14}, 5) 202 203 assert.Equal(t, []byte{12, 13, 14, 0, 0, 10, 11}, rb.data) 204 205 data := make([]byte, 5) 206 rb.readAt(data, 5) 207 assert.Equal(t, []byte{10, 11, 12, 13, 14}, data) 208 }