package protocol import "testing" // The codec-spec table from FEATURES_MUSIC.md, verbatim. func TestCodecSpecTable(t *testing.T) { type vec struct { spec string mime string } vecs := []vec{ {"mp3-128kbps-VBR", "audio/mpeg"}, {"mp3-320kbps-CBR", "audio/mpeg"}, {"aac-256kbps-VBR", "audio/mp4"}, {"ogg-vorbis-q8", "audio/ogg"}, {"opus-96kbps", "audio/ogg"}, {"flac-16bit-44.1kHz", "audio/flac"}, {"flac-24bit-96kHz", "audio/flac"}, {"wav-24bit-48kHz", "audio/wav"}, {"alac-16bit-44.1kHz", "audio/mp4"}, } for _, v := range vecs { c, err := ParseCodecSpec(v.spec) if err != nil { t.Fatalf("%s: %s", v.spec, err.Error()) } if c.String() != v.spec { t.Fatalf("%s: re-rendered as %q", v.spec, c.String()) } if m := MediaType(c.Codec); m != v.mime { t.Fatalf("%s: media type %q, want %q", v.spec, m, v.mime) } } } func TestCodecSpecFields(t *testing.T) { c, err := ParseCodecSpec("mp3-128kbps-VBR") if err != nil { t.Fatal(err.Error()) } if c.Codec != "mp3" || c.Bitrate != 128 || c.Mode != "VBR" { t.Fatalf("parsed %+v", c) } if c.BitDepth != 0 || c.Quality != 0 || c.SampleRate != "" { t.Fatalf("lossy spec carries lossless fields: %+v", c) } if c.IsLossless() { t.Fatal("mp3 reported as lossless") } l, err := ParseCodecSpec("flac-24bit-96kHz") if err != nil { t.Fatal(err.Error()) } if l.Codec != "flac" || l.BitDepth != 24 || l.SampleRate != "96" { t.Fatalf("parsed %+v", l) } if !l.IsLossless() { t.Fatal("flac reported as lossy") } // A codec name may contain a dash: the form is read from the right. o, err := ParseCodecSpec("ogg-vorbis-q8") if err != nil { t.Fatal(err.Error()) } if o.Codec != "ogg-vorbis" || o.Quality != 8 { t.Fatalf("parsed %+v", o) } b, err := ParseCodecSpec("aac-256kbps-ABR") if err != nil { t.Fatal(err.Error()) } if b.Codec != "aac" || b.Bitrate != 256 || b.Mode != "ABR" { t.Fatalf("parsed %+v", b) } } func TestCodecSpecRejects(t *testing.T) { for _, s := range []string{ "", "mp3", "mp3-", "mp3-kbps", "mp3-128kbps-XYZ", "flac-24bit-kHz", "flac-24bit-96Mhz", "opus-q", "opus-96kbps-VBR-CBR", } { if _, err := ParseCodecSpec(s); err == nil { t.Errorf("ParseCodecSpec(%q) accepted a bad spec", s) } } } func TestSplitDash(t *testing.T) { f := splitDash("flac-24bit-96kHz") if len(f) != 3 || f[0] != "flac" || f[1] != "24bit" || f[2] != "96kHz" { t.Fatalf("split = %v", f) } if got := splitDash(""); got != nil { t.Fatalf("split of empty = %v", got) } } func TestIsDecimal(t *testing.T) { for _, s := range []string{"0", "44.1", "96", "192.5"} { if !isDecimal(s) { t.Errorf("isDecimal(%q) = false", s) } } for _, s := range []string{"", ".", ".5", "5.", "4.4.1", "4,1", "-1", "x"} { if isDecimal(s) { t.Errorf("isDecimal(%q) = true", s) } } }