#! Contains the tests of Ogg Vorbis: streams the tests make decoded to the #! frames the spec defines, coupled channels and single, short blocks and long, #! cut where their granule positions say; read in pieces and from the start #! again; sought anywhere; their loop points read of their comments; and #! broken streams refused. #! #! Real encoders' files were checked apart from these: 63 of them, from 1 KB to #! 9 MB, decoded as libvorbis decodes them, every sample within 5e-7. import std::testing; import malunal::audio; @test def decodes_two_coupled_channels_as_the_spec_defines(t: &mut tester): void { val made := made_vorbis(2, 0); val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; }; t.expects(read.rate == 48000 && read.channels == 2); t.expects(read.frames == made.expected.length / 2); t.expects(apart(read.samples.span, made.expected.span) < 1e-4); } @test def decodes_one_channel_as_the_spec_defines(t: &mut tester): void { val made := made_vorbis(1, 0); val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; }; t.expects(read.channels == 1 && read.frames == made.expected.length); t.expects(apart(read.samples.span, made.expected.span) < 1e-4); } @test def cuts_the_frames_its_last_granule_position_leaves_out(t: &mut tester): void { val made := made_vorbis(2, 37); val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; }; t.expects(read.frames == made.expected.length / 2); t.expects(apart(read.samples.span, made.expected.span) < 1e-4); val opened := audio::vorbis::decoder::open(made.bytes.span) else (e) { t.expects(false); return; }; t.expects(opened.frames == read.frames && opened.rate == 48000 && opened.channels == 2); } @test def reads_in_any_pieces_and_from_the_start_again(t: &mut tester): void { val made := made_vorbis(2, 5); var reading := audio::vorbis::decoder::open(made.bytes.span) else (e) { t.expects(false); return; }; for _ in 0..2 { var all := vector::::new(); var piece := vector::::new(); piece.resize(2 * 13); while true { val count := reading.read(piece.slice) else (e) { t.expects(false); return; }; break if count == 0; for i in 0..count * 2 { all.append(piece[i]); } } t.expects(apart(all.span, made.expected.span) < 1e-4); reading.rewind(); } } @test def refuses_broken_streams(t: &mut tester): void { # A byte of a page changed, so its CRC is wrong. var changed := made_vorbis(1, 0); changed.bytes[changed.bytes.length - 3] ^= 0x10; t.expects(fails(audio::decode(changed.bytes.span), audio_error::malformed)); # Cut short, partway through its last page. var cut := made_vorbis(1, 0); cut.bytes.truncate(); t.expects(fails(audio::decode(cut.bytes.span), audio_error::malformed)); # A floor of type 0. val old := made_vorbis(1, 0, true); t.expects(fails(audio::decode(old.bytes.span), audio_error::unsupported)); # An Ogg file of something besides Vorbis. var other := vector::::new(); page_of(&mut other, 2, 0, 0, "\x80theora", span::::blank()); t.expects(fails(audio::decode(other.span), audio_error::unrecognized)); } @test def seeks_anywhere_in_a_stream(t: &mut tester): void { val made := made_vorbis(2, 0, false, 300); var reading := audio::vorbis::decoder::open(made.bytes.span) else (e) { t.expects(false); return; }; val total := made.expected.length / 2; t.expects(reading.frames == total); var piece := vector::::new(); piece.resize(2000 * 2); val targets: u64[7] = [0, 10, 600, 5000, 12345, total - 100, total]; for &target in targets { t.expects(reading.seek(target) is value(_)); val count := reading.read(piece.slice) else (e) { t.expects(false); return; }; val wanted: u64 = total - target < 2000 ? total - target : 2000; t.expects(count == wanted); t.expects(apart(piece.span[0:count * 2], made.expected.span[target * 2:count * 2]) < 1e-4); } } @test def reads_where_a_stream_loops_of_its_comments(t: &mut tester): void { val made := made_vorbis(1, 0, false, 9, true); val read := audio::decode(made.bytes.span) else (e) { t.expects(false); return; }; t.expects(read.loop_points.start == 100 && read.loop_points.end == 400); val plain := audio::decode(made_vorbis(1, 0).bytes.span) else (e) { t.expects(false); return; }; t.expects(plain.loop_points.start == 0 && plain.loop_points.end == 0); }