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