av/codec/wav/wav_test.go

74 lines
2.9 KiB
Go
Raw Normal View History

/*
NAME
wav.go
DESCRIPTION
wav.go contains functions for processing wav.
AUTHOR
David Sutton <davidsutton@ausocean.org>
LICENSE
wav.go is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
// Package wav provides functions for converting wav audio.
package wav
import (
"testing"
)
func TestWavWriter(t *testing.T) {
tests := []struct {
name string
md Metadata
input []byte
wantN int
wantErr error
}{
{name: "Header Only", md: Metadata{AudioFormat: PCMFormat, Channels: 1, SampleRate: 48000, BitDepth: 16}, input: nil, wantN: 44, wantErr: nil},
{name: "4 bytes", md: Metadata{AudioFormat: PCMFormat, Channels: 1, SampleRate: 48000, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 48, wantErr: nil},
{name: "No format", md: Metadata{Channels: 1, SampleRate: 48000, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidFormat},
{name: "Invalid format", md: Metadata{AudioFormat: 2, Channels: 1, SampleRate: 48000, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidFormat},
{name: "No channels", md: Metadata{AudioFormat: PCMFormat, SampleRate: 48000, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidChannels},
{name: "Invalid channels", md: Metadata{AudioFormat: PCMFormat, Channels: 0, SampleRate: 48000, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidChannels},
{name: "No sample rate", md: Metadata{AudioFormat: PCMFormat, Channels: 1, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidRate},
{name: "Invalid sample rate", md: Metadata{AudioFormat: PCMFormat, Channels: 1, SampleRate: 0, BitDepth: 16}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidRate},
{name: "No bit depth", md: Metadata{AudioFormat: PCMFormat, Channels: 1, SampleRate: 48000}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidBitDepth},
{name: "Invalid bit depth", md: Metadata{AudioFormat: PCMFormat, Channels: 1, SampleRate: 48000, BitDepth: 0}, input: []byte{0,0,0,0}, wantN: 0, wantErr: errInvalidBitDepth},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &WAV{
Metadata: tt.md,
}
gotN, err := w.Write(tt.input)
if err != tt.wantErr {
t.Errorf("WAV.Write() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotN != tt.wantN {
t.Errorf("WAV.Write() = %v, want %v", gotN, tt.wantN)
}
})
}
}