From 5c896ea24fe0acdf873b6bf9bd0ba8a688d736a0 Mon Sep 17 00:00:00 2001 From: David Sutton Date: Thu, 23 Nov 2023 17:18:24 +1030 Subject: [PATCH] codec/wav: Add unit tests for wav encoder --- codec/wav/wav_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 codec/wav/wav_test.go diff --git a/codec/wav/wav_test.go b/codec/wav/wav_test.go new file mode 100644 index 00000000..b0fdb1b6 --- /dev/null +++ b/codec/wav/wav_test.go @@ -0,0 +1,73 @@ +/* +NAME + wav.go + +DESCRIPTION + wav.go contains functions for processing wav. + +AUTHOR + David Sutton + +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) + } + + }) + } +}