2019-05-03 13:06:34 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2019-05-10 08:09:47 +03:00
|
|
|
bytescanner_test.go
|
2019-05-03 13:06:34 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-05-22 04:37:39 +03:00
|
|
|
package codecutil
|
2019-05-03 13:06:34 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type chunkEncoder [][]byte
|
|
|
|
|
|
|
|
func (e *chunkEncoder) Encode(b []byte) error {
|
|
|
|
*e = append(*e, b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*chunkEncoder) Stream() <-chan []byte { panic("INVALID USE") }
|
|
|
|
|
|
|
|
func TestScannerReadByte(t *testing.T) {
|
|
|
|
data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
|
|
|
|
|
|
|
|
for _, size := range []int{1, 2, 8, 1 << 10} {
|
2019-05-09 07:21:59 +03:00
|
|
|
r := NewByteScanner(bytes.NewReader(data), make([]byte, size))
|
2019-05-03 13:06:34 +03:00
|
|
|
var got []byte
|
|
|
|
for {
|
|
|
|
b, err := r.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
got = append(got, b)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(got, data) {
|
|
|
|
t.Errorf("unexpected result for buffer size %d:\ngot :%q\nwant:%q", size, got, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScannerScanUntilZero(t *testing.T) {
|
|
|
|
data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit,\x00 sed do eiusmod tempor incididunt ut \x00labore et dolore magna aliqua.")
|
|
|
|
|
|
|
|
for _, size := range []int{1, 2, 8, 1 << 10} {
|
2019-05-09 07:21:59 +03:00
|
|
|
r := NewByteScanner(bytes.NewReader(data), make([]byte, size))
|
2019-05-03 13:06:34 +03:00
|
|
|
var got [][]byte
|
|
|
|
for {
|
|
|
|
buf, _, err := r.ScanUntil(nil, 0x0)
|
|
|
|
got = append(got, buf)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
want := bytes.SplitAfter(data, []byte{0})
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("unexpected result for buffer zie %d:\ngot :%q\nwant:%q", size, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|