stream/mts/psi: cleaned up descriptor_test.go

This commit is contained in:
saxon 2019-01-29 18:22:57 +10:30
parent 23e03eeddc
commit 568840f691
1 changed files with 124 additions and 140 deletions

View File

@ -37,7 +37,8 @@ const (
errUnexpectedErr = "Unexpected error: %v\n"
)
var tstPsi1 = PSI{
var (
tstPsi1 = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
@ -67,7 +68,7 @@ var tstPsi1 = PSI{
},
}
var tstPsi2 = PSI{
tstPsi2 = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
@ -90,7 +91,7 @@ var tstPsi2 = PSI{
},
}
var tstPsi3 = PSI{
tstPsi3 = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
@ -124,37 +125,35 @@ var tstPsi3 = PSI{
},
},
}
)
func TestHasDescriptor(t *testing.T) {
func TestHasDescriptorExists(t *testing.T) {
p := PSIBytes(tstPsi3.Bytes())
// Try getting descriptor that exists
_, got := p.HasDescriptor(LocationDescTag)
want := []byte{
LocationDescTag,
LocationDataSize,
}
want = append(want, make([]byte, LocationDataSize)...)
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
}
// Try getting descriptor that doesnt exist
func TestHasDescriptorAbsent(t *testing.T) {
p := PSIBytes(tstPsi3.Bytes())
const fakeTag = 236
_, got = p.HasDescriptor(fakeTag)
want = nil
_, got := p.HasDescriptor(fakeTag)
var want []byte
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
}
// Try getting descriptor from psi that has no descriptors
p = PSIBytes(tstPsi2.Bytes())
_, got = p.HasDescriptor(LocationDescTag)
want = nil
func TestHasDescriptorNone(t *testing.T) {
p := PSIBytes(tstPsi2.Bytes())
_, got := p.HasDescriptor(LocationDescTag)
var want []byte
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
@ -170,7 +169,6 @@ func TestProgramInfoLen(t *testing.T) {
}
func TestDescriptors(t *testing.T) {
// Try psi with descriptors
p := PSIBytes(tstPsi1.Bytes())
got := p.descriptors()
want := []byte{
@ -178,71 +176,57 @@ func TestDescriptors(t *testing.T) {
TimeDataSize,
}
want = append(want, make([]byte, TimeDataSize)...)
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
// Now try psi with empty descriptors
p = PSIBytes(tstPsi2.Bytes())
got = p.descriptors()
want = nil
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
}
func TestCreateDescriptor(t *testing.T) {
// Test with PSI containing no descriptors
func TestDescriptorsNone(t *testing.T) {
p := PSIBytes(tstPsi2.Bytes())
p.createDescriptor(TimeDescTag, make([]byte, TimeDataSize))
// update crc
noPadding := p.trimPadding()
updateCrc(noPadding[1:])
want := PSIBytes(tstPsi1.Bytes())
got := p
if !bytes.Equal(want, got) {
t.Errorf(errNotExpectedOut, got, want)
}
// Test with psi containing descriptor already
p = PSIBytes(tstPsi1.Bytes())
p.createDescriptor(LocationDescTag, make([]byte, LocationDataSize))
// update crc
noPadding = p.trimPadding()
updateCrc(noPadding[1:])
want = PSIBytes(tstPsi3.Bytes())
got = p
if !bytes.Equal(want, got) {
got := p.descriptors()
var want []byte
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
}
func TestAddDescriptor(t *testing.T) {
// Add descriptor to psi without descriptors
func TestCreateDescriptorEmpty(t *testing.T) {
got := PSIBytes(tstPsi2.Bytes())
got.createDescriptor(TimeDescTag, make([]byte, TimeDataSize))
updateCrc(got[1:])
want := PSIBytes(tstPsi1.Bytes())
if !bytes.Equal(want, got) {
t.Errorf(errNotExpectedOut, got, want)
}
}
func TestCreateDescriptorNotEmpty(t *testing.T) {
got := PSIBytes(tstPsi1.Bytes())
got.createDescriptor(LocationDescTag, make([]byte, LocationDataSize))
updateCrc(got[1:])
want := PSIBytes(tstPsi3.Bytes())
if !bytes.Equal(want, got) {
t.Errorf(errNotExpectedOut, got, want)
}
}
func TestAddDescriptorEmpty(t *testing.T) {
got := PSIBytes(tstPsi2.Bytes())
if err := got.AddDescriptor(TimeDescTag, make([]byte, TimeDataSize)); err != nil {
t.Errorf(errUnexpectedErr, err.Error())
}
want := PSIBytes(tstPsi1.Bytes())
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}
}
// Add to psi already with descriptor
got = PSIBytes(tstPsi1.Bytes())
func TestAddDescriptorNonEmpty(t *testing.T) {
got := PSIBytes(tstPsi1.Bytes())
if err := got.AddDescriptor(LocationDescTag, make([]byte, LocationDataSize)); err != nil {
t.Errorf(errUnexpectedErr, err.Error())
}
want = PSIBytes(tstPsi3.Bytes())
want := PSIBytes(tstPsi3.Bytes())
if !bytes.Equal(got, want) {
t.Errorf(errNotExpectedOut, got, want)
}