/*
NAME
  MpegTs.go - provides a data structure intended to encapsulate the properties
  of an MpegTs packet.

DESCRIPTION
  See Readme.md

AUTHOR
  Saxon Nelson-Milton <saxon.milton@gmail.com>

LICENSE
  MpegTs.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 [GNU licenses](http://www.gnu.org/licenses).
*/

package nal

import (
  "testing"
)

var parseInput = []byte{
	0x6C, // 3NalUnitBits = 101(5), Fragment type = 1100 (type = 12 )
	0x94, // starbit = 1, endbit = 0, Reservedbit = 0, 5NalUnitBits = 10100 (20)
	0x8E, // 10001110 random frame byte
	0x26, // 00100110 random frame byte
	0xD0, // 11010000 random frame byte
}

var expectedParsing = []interface{}{
	byte(3),
	byte(12),
	bool(true),
	bool(false),
	bool(false),
	byte(20),
	[]byte{0x8E, 0x26, 0xD0},
}

const (
	nalTestType = 12
)

func TestNalFragmentParsing(t *testing.T) {
	nalUnit := ParseNALFragment(parseInput)
	value := reflect.ValueOf(*nalUnit)
	length := value.NumField()
	fields := make([]interface{}, length)
	for ii := 0; ii < length; ii++ {
		fields[ii] = value.Field(ii).Interface()
	}
	for ii := range fields {
		if !reflect.DeepEqual(fields[ii], expectedParsing[ii]) {
			t.Errorf("Bad Parsing! Field: %v wanted: %v got: %v\n", ii, expectedParsing[ii],
				fields[ii])
		}
	}
}

func TestNalFragmentToByteSlice(t *testing.T) {
	nalUnit := ParseNALFragment(parseInput)
	output := nalUnit.ToByteSlice()
	for ii := range output {
		if output[ii] != parseInput[ii] {
			t.Errorf("Bad conversion to byte slice at %vth byte! wanted: %v got: %v",
				parseInput[ii], output[ii])
		}
	}
}

func TestNalFragmentType(t *testing.T) {
	nalUnit := ParseNALFragment(parseInput)
	nalType := nalUnit.GetType()
	if nalType != nalTestType {
		t.Errorf("Returned wrong type!")
	}
}

func TestNalSpsPpsParsing(t *testing.T) {
	nalSpsPps := ParseNALSpsPps(parseInput)
	for ii := range parseInput {
		if nalSpsPps.Data[ii] != parseInput[ii] {
			t.Errorf("Bad Parsing! Byte: %v wanted: %v got: %v\n", ii, parseInput[ii],
				nalSpsPps.Data[ii])
		}
	}
}

func TestNalSpsPpsToByteSlice(t *testing.T) {
	nalSpsPps := ParseNALSpsPps(parseInput)
	nalSpsPpsByteSlice := nalSpsPps.ToByteSlice()
	for ii := range parseInput {
		if nalSpsPpsByteSlice[ii] != parseInput[ii] {
			t.Errorf("Bad conversion to byte slice! Byte: %v wanted: %v got: %v\n", ii,
				parseInput[ii], nalSpsPpsByteSlice[ii])
		}
	}
}

func TestNalSpsPpsType(t *testing.T) {
	nalSpsPps := ParseNALSpsPps(parseInput)
	if nalSpsPps.GetType() != nalTestType {
		t.Errorf("Returned wrong type!")
	}
}