| 1 |
package buffer |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/tdewolff/test" |
| 9 |
) |
| 10 |
|
| 11 |
func TestBufferPool(t *testing.T) { |
| 12 |
z := &bufferPool{} |
| 13 |
|
| 14 |
lorem := []byte("Lorem ipsum") |
| 15 |
dolor := []byte("dolor sit amet") |
| 16 |
consectetur := []byte("consectetur adipiscing elit") |
| 17 |
|
| 18 |
// set lorem as first buffer and get new dolor buffer |
| 19 |
b := z.swap(lorem, len(dolor)) |
| 20 |
test.That(t, len(b) == 0) |
| 21 |
test.That(t, cap(b) == len(dolor)) |
| 22 |
b = append(b, dolor...) |
| 23 |
|
| 24 |
// free first buffer so it will be reused |
| 25 |
z.free(len(lorem)) |
| 26 |
b = z.swap(b, len(lorem)) |
| 27 |
b = b[:len(lorem)] |
| 28 |
test.Bytes(t, b, lorem) |
| 29 |
|
| 30 |
b = z.swap(b, len(consectetur)) |
| 31 |
b = append(b, consectetur...) |
| 32 |
|
| 33 |
// free in advance to reuse the same buffer |
| 34 |
z.free(len(dolor) + len(lorem) + len(consectetur)) |
| 35 |
test.That(t, z.head == 0) |
| 36 |
b = z.swap(b, len(consectetur)) |
| 37 |
b = b[:len(consectetur)] |
| 38 |
test.Bytes(t, b, consectetur) |
| 39 |
|
| 40 |
// free in advance but request larger buffer |
| 41 |
z.free(len(consectetur)) |
| 42 |
b = z.swap(b, len(consectetur)+1) |
| 43 |
b = append(b, consectetur...) |
| 44 |
b = append(b, '.') |
| 45 |
test.That(t, cap(b) == len(consectetur)+1) |
| 46 |
} |
| 47 |
|
| 48 |
func TestStreamLexer(t *testing.T) { |
| 49 |
s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` |
| 50 |
z := NewStreamLexer(bytes.NewBufferString(s)) |
| 51 |
|
| 52 |
test.T(t, z.err, io.EOF, "buffer must be fully in memory") |
| 53 |
test.T(t, z.Err(), nil, "buffer is at EOF but must not return EOF until we reach that") |
| 54 |
test.That(t, z.Pos() == 0, "buffer must start at position 0") |
| 55 |
test.That(t, z.Peek(0) == 'L', "first character must be 'L'") |
| 56 |
test.That(t, z.Peek(1) == 'o', "second character must be 'o'") |
| 57 |
|
| 58 |
z.Move(1) |
| 59 |
test.That(t, z.Peek(0) == 'o', "must be 'o' at position 1") |
| 60 |
test.That(t, z.Peek(1) == 'r', "must be 'r' at position 1") |
| 61 |
z.Rewind(6) |
| 62 |
test.That(t, z.Peek(0) == 'i', "must be 'i' at position 6") |
| 63 |
test.That(t, z.Peek(1) == 'p', "must be 'p' at position 7") |
| 64 |
|
| 65 |
test.Bytes(t, z.Lexeme(), []byte("Lorem "), "buffered string must now read 'Lorem ' when at position 6") |
| 66 |
test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") |
| 67 |
test.That(t, z.ShiftLen() == len("Lorem "), "shifted length must equal last shift") |
| 68 |
test.That(t, z.Pos() == 0, "after shifting position must be 0") |
| 69 |
test.That(t, z.Peek(0) == 'i', "must be 'i' at position 0 after shifting") |
| 70 |
test.That(t, z.Peek(1) == 'p', "must be 'p' at position 1 after shifting") |
| 71 |
test.T(t, z.Err(), nil, "error must be nil at this point") |
| 72 |
|
| 73 |
z.Move(len(s) - len("Lorem ") - 1) |
| 74 |
test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer") |
| 75 |
z.Skip() |
| 76 |
test.That(t, z.Pos() == 0, "after skipping position must be 0") |
| 77 |
z.Move(1) |
| 78 |
test.T(t, z.Err(), io.EOF, "error must be EOF when past the buffer") |
| 79 |
z.Move(-1) |
| 80 |
test.T(t, z.Err(), nil, "error must be nil just before the end of the buffer, even when it has been past the buffer") |
| 81 |
z.Free(0) // has already been tested |
| 82 |
} |
| 83 |
|
| 84 |
func TestStreamLexerShift(t *testing.T) { |
| 85 |
s := `Lorem ipsum dolor sit amet, consectetur adipiscing elit.` |
| 86 |
z := NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 5) |
| 87 |
|
| 88 |
z.Move(len("Lorem ")) |
| 89 |
test.Bytes(t, z.Shift(), []byte("Lorem "), "shift must return the buffered string") |
| 90 |
test.That(t, z.ShiftLen() == len("Lorem "), "shifted length must equal last shift") |
| 91 |
} |
| 92 |
|
| 93 |
func TestStreamLexerSmall(t *testing.T) { |
| 94 |
s := `abcdefghijklm` |
| 95 |
z := NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) |
| 96 |
test.That(t, z.Peek(8) == 'i', "first character must be 'i' at position 8") |
| 97 |
|
| 98 |
z = NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 4) |
| 99 |
test.That(t, z.Peek(12) == 'm', "first character must be 'm' at position 12") |
| 100 |
|
| 101 |
z = NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 0) |
| 102 |
test.That(t, z.Peek(4) == 'e', "first character must be 'e' at position 4") |
| 103 |
|
| 104 |
z = NewStreamLexerSize(test.NewPlainReader(bytes.NewBufferString(s)), 13) |
| 105 |
test.That(t, z.Peek(13) == 0, "must yield error at position 13") |
| 106 |
} |
| 107 |
|
| 108 |
func TestStreamLexerSingle(t *testing.T) { |
| 109 |
z := NewStreamLexer(test.NewInfiniteReader()) |
| 110 |
test.That(t, z.Peek(0) == '.') |
| 111 |
test.That(t, z.Peek(1) == '.') |
| 112 |
test.That(t, z.Peek(3) == '.', "required two successful reads") |
| 113 |
} |
| 114 |
|
| 115 |
func TestStreamLexerRunes(t *testing.T) { |
| 116 |
z := NewStreamLexer(bytes.NewBufferString("aæ†\U00100000")) |
| 117 |
r, n := z.PeekRune(0) |
| 118 |
test.That(t, n == 1, "first character must be length 1") |
| 119 |
test.That(t, r == 'a', "first character must be rune 'a'") |
| 120 |
r, n = z.PeekRune(1) |
| 121 |
test.That(t, n == 2, "second character must be length 2") |
| 122 |
test.That(t, r == 'æ', "second character must be rune 'æ'") |
| 123 |
r, n = z.PeekRune(3) |
| 124 |
test.That(t, n == 3, "fourth character must be length 3") |
| 125 |
test.That(t, r == '†', "fourth character must be rune '†'") |
| 126 |
r, n = z.PeekRune(6) |
| 127 |
test.That(t, n == 4, "seventh character must be length 4") |
| 128 |
test.That(t, r == '\U00100000', "seventh character must be rune '\U00100000'") |
| 129 |
} |
| 130 |
|
| 131 |
func TestStreamLexerBadRune(t *testing.T) { |
| 132 |
z := NewStreamLexer(bytes.NewBufferString("\xF0")) // expect four byte rune |
| 133 |
r, n := z.PeekRune(0) |
| 134 |
test.T(t, n, 4, "length") |
| 135 |
test.T(t, r, rune(0), "rune") |
| 136 |
} |
| 137 |
|
| 138 |
func TestStreamLexerZeroLen(t *testing.T) { |
| 139 |
z := NewStreamLexer(test.NewPlainReader(bytes.NewBufferString(""))) |
| 140 |
test.That(t, z.Peek(0) == 0, "first character must yield error") |
| 141 |
} |
| 142 |
|
| 143 |
func TestStreamLexerEmptyReader(t *testing.T) { |
| 144 |
z := NewStreamLexer(test.NewEmptyReader()) |
| 145 |
test.That(t, z.Peek(0) == 0, "first character must yield error") |
| 146 |
test.T(t, z.Err(), io.EOF, "error must be EOF") |
| 147 |
test.That(t, z.Peek(0) == 0, "second peek must also yield error") |
| 148 |
} |