2019-04-26 14:01:12 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
lex_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-05-03 13:22:23 +03:00
|
|
|
lex_test.go provides testing for the lexer in lex.go.
|
2019-04-26 14:01:12 +03:00
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
lex_test.go 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
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-05-06 09:17:51 +03:00
|
|
|
// lex_test.go provides testing for the lexer in lex.go.
|
|
|
|
|
2021-01-18 07:59:21 +03:00
|
|
|
package jpeg
|
2019-04-26 14:01:12 +03:00
|
|
|
|
|
|
|
import (
|
2021-01-16 08:52:56 +03:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-04-26 14:01:12 +03:00
|
|
|
"time"
|
2021-01-16 08:52:56 +03:00
|
|
|
|
2022-05-27 09:12:52 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logging"
|
2019-04-26 14:01:12 +03:00
|
|
|
)
|
|
|
|
|
2021-01-16 08:52:56 +03:00
|
|
|
var jpegTests = []struct {
|
2019-04-26 14:01:12 +03:00
|
|
|
name string
|
|
|
|
input []byte
|
|
|
|
delay time.Duration
|
|
|
|
want [][]byte
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
2021-01-16 08:52:56 +03:00
|
|
|
err: io.ErrUnexpectedEOF,
|
2019-04-26 14:01:12 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "null",
|
|
|
|
input: []byte{0xff, 0xd8, 0xff, 0xd9},
|
|
|
|
delay: 0,
|
|
|
|
want: [][]byte{{0xff, 0xd8, 0xff, 0xd9}},
|
2021-01-20 04:19:01 +03:00
|
|
|
err: io.ErrUnexpectedEOF,
|
2019-04-26 14:01:12 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "null delayed",
|
|
|
|
input: []byte{0xff, 0xd8, 0xff, 0xd9},
|
|
|
|
delay: time.Millisecond,
|
|
|
|
want: [][]byte{{0xff, 0xd8, 0xff, 0xd9}},
|
2021-01-20 04:19:01 +03:00
|
|
|
err: io.ErrUnexpectedEOF,
|
2019-04-26 14:01:12 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "full",
|
|
|
|
input: []byte{
|
|
|
|
0xff, 0xd8, 'f', 'u', 'l', 'l', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 'f', 'r', 'a', 'm', 'e', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 'w', 'i', 't', 'h', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 'l', 'e', 'n', 'g', 't', 'h', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 's', 'p', 'r', 'e', 'a', 'd', 0xff, 0xd9,
|
|
|
|
},
|
|
|
|
delay: 0,
|
|
|
|
want: [][]byte{
|
|
|
|
{0xff, 0xd8, 'f', 'u', 'l', 'l', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 'f', 'r', 'a', 'm', 'e', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 'w', 'i', 't', 'h', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 'l', 'e', 'n', 'g', 't', 'h', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 's', 'p', 'r', 'e', 'a', 'd', 0xff, 0xd9},
|
|
|
|
},
|
2021-01-20 04:19:01 +03:00
|
|
|
err: io.ErrUnexpectedEOF,
|
2019-04-26 14:01:12 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "full delayed",
|
|
|
|
input: []byte{
|
|
|
|
0xff, 0xd8, 'f', 'u', 'l', 'l', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 'f', 'r', 'a', 'm', 'e', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 'w', 'i', 't', 'h', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 'l', 'e', 'n', 'g', 't', 'h', 0xff, 0xd9,
|
|
|
|
0xff, 0xd8, 's', 'p', 'r', 'e', 'a', 'd', 0xff, 0xd9,
|
|
|
|
},
|
|
|
|
delay: time.Millisecond,
|
|
|
|
want: [][]byte{
|
|
|
|
{0xff, 0xd8, 'f', 'u', 'l', 'l', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 'f', 'r', 'a', 'm', 'e', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 'w', 'i', 't', 'h', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 'l', 'e', 'n', 'g', 't', 'h', 0xff, 0xd9},
|
|
|
|
{0xff, 0xd8, 's', 'p', 'r', 'e', 'a', 'd', 0xff, 0xd9},
|
|
|
|
},
|
2021-01-20 04:19:01 +03:00
|
|
|
err: io.ErrUnexpectedEOF,
|
2019-04-26 14:01:12 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-16 08:52:56 +03:00
|
|
|
func TestLex(t *testing.T) {
|
2022-05-27 09:12:52 +03:00
|
|
|
Log = (*logging.TestLogger)(t)
|
2021-01-16 08:52:56 +03:00
|
|
|
for _, test := range jpegTests {
|
2019-04-26 14:01:12 +03:00
|
|
|
var buf chunkEncoder
|
2021-01-16 08:52:56 +03:00
|
|
|
err := Lex(&buf, bytes.NewReader(test.input), test.delay)
|
2019-04-26 14:01:12 +03:00
|
|
|
if fmt.Sprint(err) != fmt.Sprint(test.err) {
|
|
|
|
t.Errorf("unexpected error for %q: got:%v want:%v", test.name, err, test.err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got := [][]byte(buf)
|
|
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
|
|
t.Errorf("unexpected result for %q:\ngot :%#v\nwant:%#v", test.name, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 08:52:56 +03:00
|
|
|
|
|
|
|
type chunkEncoder [][]byte
|
|
|
|
|
|
|
|
func (e *chunkEncoder) Write(b []byte) (int, error) {
|
|
|
|
*e = append(*e, b)
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*chunkEncoder) Stream() <-chan []byte { panic("INVALID USE") }
|