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,124 +37,123 @@ const (
errUnexpectedErr = "Unexpected error: %v\n" errUnexpectedErr = "Unexpected error: %v\n"
) )
var tstPsi1 = PSI{ var (
Pf: 0x00, tstPsi1 = PSI{
Tid: 0x02, Pf: 0x00,
Ssi: true, Tid: 0x02,
Sl: 0x1c, Ssi: true,
Tss: &TSS{ Sl: 0x1c,
Tide: 0x01, Tss: &TSS{
V: 0, Tide: 0x01,
Cni: true, V: 0,
Sn: 0, Cni: true,
Lsn: 0, Sn: 0,
Sd: &PMT{ Lsn: 0,
Pcrpid: 0x0100, // wrong Sd: &PMT{
Pil: 10, Pcrpid: 0x0100, // wrong
Pd: []Desc{ Pil: 10,
{ Pd: []Desc{
Dt: TimeDescTag, {
Dl: TimeDataSize, Dt: TimeDescTag,
Dd: make([]byte, TimeDataSize), Dl: TimeDataSize,
Dd: make([]byte, TimeDataSize),
},
},
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
}, },
}, },
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
}, },
}, }
}
var tstPsi2 = PSI{ tstPsi2 = PSI{
Pf: 0x00, Pf: 0x00,
Tid: 0x02, Tid: 0x02,
Ssi: true, Ssi: true,
Sl: 0x12, Sl: 0x12,
Tss: &TSS{ Tss: &TSS{
Tide: 0x01, Tide: 0x01,
V: 0, V: 0,
Cni: true, Cni: true,
Sn: 0, Sn: 0,
Lsn: 0, Lsn: 0,
Sd: &PMT{ Sd: &PMT{
Pcrpid: 0x0100, Pcrpid: 0x0100,
Pil: 0, Pil: 0,
Essd: &ESSD{ Essd: &ESSD{
St: 0x1b, St: 0x1b,
Epid: 0x0100, Epid: 0x0100,
Esil: 0x00, Esil: 0x00,
},
},
},
}
var tstPsi3 = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x3e,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100,
Pil: PmtTimeLocationPil,
Pd: []Desc{
{
Dt: TimeDescTag,
Dl: TimeDataSize,
Dd: make([]byte, TimeDataSize),
},
{
Dt: LocationDescTag,
Dl: LocationDataSize,
Dd: make([]byte, LocationDataSize),
}, },
}, },
Essd: &ESSD{ },
St: 0x1b, }
Epid: 0x0100,
Esil: 0x00, tstPsi3 = PSI{
Pf: 0x00,
Tid: 0x02,
Ssi: true,
Sl: 0x3e,
Tss: &TSS{
Tide: 0x01,
V: 0,
Cni: true,
Sn: 0,
Lsn: 0,
Sd: &PMT{
Pcrpid: 0x0100,
Pil: PmtTimeLocationPil,
Pd: []Desc{
{
Dt: TimeDescTag,
Dl: TimeDataSize,
Dd: make([]byte, TimeDataSize),
},
{
Dt: LocationDescTag,
Dl: LocationDataSize,
Dd: make([]byte, LocationDataSize),
},
},
Essd: &ESSD{
St: 0x1b,
Epid: 0x0100,
Esil: 0x00,
},
}, },
}, },
}, }
} )
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) t.Errorf(errNotExpectedOut, got, want)
} }
}
// Test with psi containing descriptor already func TestCreateDescriptorEmpty(t *testing.T) {
p = PSIBytes(tstPsi1.Bytes()) got := PSIBytes(tstPsi2.Bytes())
p.createDescriptor(LocationDescTag, make([]byte, LocationDataSize)) got.createDescriptor(TimeDescTag, make([]byte, TimeDataSize))
// update crc updateCrc(got[1:])
noPadding = p.trimPadding() want := PSIBytes(tstPsi1.Bytes())
updateCrc(noPadding[1:])
want = PSIBytes(tstPsi3.Bytes())
got = p
if !bytes.Equal(want, got) { if !bytes.Equal(want, got) {
t.Errorf(errNotExpectedOut, got, want) t.Errorf(errNotExpectedOut, got, want)
} }
} }
func TestAddDescriptor(t *testing.T) { func TestCreateDescriptorNotEmpty(t *testing.T) {
// Add descriptor to psi without descriptors got := PSIBytes(tstPsi1.Bytes())
got := PSIBytes(tstPsi2.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)
} }