/*
NAME
  lex_test.go

DESCRIPTION
  See Readme.md

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.
*/

package lex

import (
	"bytes"
	"fmt"
	"reflect"
	"testing"
	"time"
)

var h264Tests = []struct {
	name  string
	input []byte
	delay time.Duration
	want  [][]byte
	err   error
}{
	{
		name: "empty",
	},
	{
		name:  "null short",
		input: []byte{0x00, 0x00, 0x01, 0x0},
		delay: 0,
		want:  [][]byte{{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x0}},
	},
	{
		name:  "null short delayed",
		input: []byte{0x00, 0x00, 0x01, 0x0},
		delay: time.Millisecond,
		want:  [][]byte{{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x0}},
	},
	{
		name: "full short type 1",
		input: []byte{
			0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: 0,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full short type 5",
		input: []byte{
			0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x01, 0x45, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: 0,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x01, 0x45, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full short type 8",
		input: []byte{
			0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x01, 0x48, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: 0,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x01, 0x48, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full short delayed",
		input: []byte{
			0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: time.Millisecond,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full long type 1",
		input: []byte{
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: 0,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full long type 5",
		input: []byte{
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x00, 0x01, 0x45, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: 0,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x00, 0x01, 0x45, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full long type 8",
		input: []byte{
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x00, 0x01, 0x48, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: 0,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x00, 0x01, 0x48, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
	{
		name: "full long delayed",
		input: []byte{
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
			0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
			0x00, 0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
			0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd',
		},
		delay: time.Millisecond,
		want: [][]byte{
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'u', 'l', 'l',
				0x00, 0x00, 0x00, 0x01, 0x00, 'f', 'r', 'a', 'm', 'e',
				0x00, 0x00, 0x00, 0x01, 0x41, 'w', 'i', 't', 'h'},
			{0x0, 0x0, 0x1, 0x9, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 'l', 'e', 'n', 'g', 't', 'h',
				0x00, 0x00, 0x00, 0x01, 0x00, 's', 'p', 'r', 'e', 'a', 'd'},
		},
	},
}

func TestH264(t *testing.T) {
	for _, test := range h264Tests {
		var buf chunkEncoder
		err := H264(&buf, bytes.NewReader(test.input), test.delay)
		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)
		}
	}
}

var mjpegTests = []struct {
	name  string
	input []byte
	delay time.Duration
	want  [][]byte
	err   error
}{
	{
		name: "empty",
	},
	{
		name:  "null",
		input: []byte{0xff, 0xd8, 0xff, 0xd9},
		delay: 0,
		want:  [][]byte{{0xff, 0xd8, 0xff, 0xd9}},
	},
	{
		name:  "null delayed",
		input: []byte{0xff, 0xd8, 0xff, 0xd9},
		delay: time.Millisecond,
		want:  [][]byte{{0xff, 0xd8, 0xff, 0xd9}},
	},
	{
		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},
		},
	},
	{
		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},
		},
	},
}

func TestMJEG(t *testing.T) {
	for _, test := range mjpegTests {
		var buf chunkEncoder
		err := MJPEG(&buf, bytes.NewReader(test.input), test.delay)
		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)
		}
	}
}

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} {
		r := newScanner(bytes.NewReader(data), make([]byte, size))
		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} {
		r := newScanner(bytes.NewReader(data), make([]byte, size))
		var got [][]byte
		for {
			buf, _, err := r.scanUntilZeroInto(nil)
			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)
		}
	}
}