play_test.go (994B)
1 package play_test 2 3 import ( 4 "testing" 5 "time" 6 7 "git.sr.ht/~jackmordaunt/nativeaudio" 8 "git.sr.ht/~jackmordaunt/nativeaudio/play" 9 ) 10 11 // TestDuration covers the calculation that bounds the playback wait, so 12 // a mistake there cannot quietly reintroduce an unbounded one. 13 func TestDuration(t *testing.T) { 14 stereo := nativeaudio.Format{SampleRate: 44100, Channels: 2, BytesPerSample: 2} 15 mono := nativeaudio.Format{SampleRate: 8000, Channels: 1, BytesPerSample: 2} 16 17 for _, tc := range []struct { 18 name string 19 bytes int 20 format nativeaudio.Format 21 want time.Duration 22 }{ 23 {"one second stereo", 44100 * 2 * 2, stereo, time.Second}, 24 {"half a second stereo", 44100 * 2, stereo, time.Second / 2}, 25 {"one second mono", 8000 * 2, mono, time.Second}, 26 {"nothing", 0, stereo, 0}, 27 {"degenerate format", 1024, nativeaudio.Format{}, 0}, 28 } { 29 got := play.Duration(make([]byte, tc.bytes), tc.format) 30 if got != tc.want { 31 t.Errorf("%s: got %v, want %v", tc.name, got, tc.want) 32 } 33 } 34 }