2019-01-21 15:22:17 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
flac_test.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
flac_test.go provides utilities to test FLAC audio decoding
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
flac_test.go is Copyright (C) 2017-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
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
2020-04-23 06:04:11 +03:00
|
|
|
|
2019-01-21 08:11:49 +03:00
|
|
|
package flac
|
|
|
|
|
|
|
|
import (
|
2019-01-22 03:15:36 +03:00
|
|
|
"io"
|
2019-01-21 08:11:49 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-22 04:57:52 +03:00
|
|
|
testFile = "../../../test/test-data/av/input/robot.flac"
|
2019-01-21 10:04:15 +03:00
|
|
|
outFile = "testOut.wav"
|
2019-01-21 08:11:49 +03:00
|
|
|
)
|
|
|
|
|
2019-01-22 03:45:39 +03:00
|
|
|
// TestWriteSeekerWrite checks that basic writing to the ws works as expected.
|
2019-01-22 03:15:36 +03:00
|
|
|
func TestWriteSeekerWrite(t *testing.T) {
|
2019-01-22 03:45:39 +03:00
|
|
|
ws := &writeSeeker{}
|
2019-01-22 03:15:36 +03:00
|
|
|
|
2019-01-22 03:45:39 +03:00
|
|
|
const tstStr1 = "hello"
|
|
|
|
ws.Write([]byte(tstStr1))
|
|
|
|
got := string(ws.buf)
|
|
|
|
if got != tstStr1 {
|
|
|
|
t.Errorf("Write failed, got: %v, want: %v", got, tstStr1)
|
2019-01-22 03:15:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 03:45:39 +03:00
|
|
|
const tstStr2 = " world"
|
|
|
|
const want = "hello world"
|
|
|
|
ws.Write([]byte(tstStr2))
|
|
|
|
got = string(ws.buf)
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("Second write failed, got: %v, want: %v", got, want)
|
2019-01-22 03:15:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 03:45:39 +03:00
|
|
|
// TestWriteSeekerSeek checks that writing and seeking works as expected, i.e. we
|
|
|
|
// can write, then seek to a knew place in the buf, and write again, either replacing
|
|
|
|
// bytes, or appending bytes.
|
2019-01-22 03:15:36 +03:00
|
|
|
func TestWriteSeekerSeek(t *testing.T) {
|
2019-01-22 03:45:39 +03:00
|
|
|
ws := &writeSeeker{}
|
2019-01-22 03:15:36 +03:00
|
|
|
|
2019-01-22 04:44:40 +03:00
|
|
|
const tstStr1 = "hello"
|
|
|
|
want1 := tstStr1
|
|
|
|
ws.Write([]byte(tstStr1))
|
|
|
|
got := string(ws.buf)
|
|
|
|
if got != tstStr1 {
|
|
|
|
t.Errorf("Unexpected output, got: %v, want: %v", got, want1)
|
2019-01-22 03:15:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 04:44:40 +03:00
|
|
|
const tstStr2 = " world"
|
|
|
|
const want2 = tstStr1 + tstStr2
|
|
|
|
ws.Write([]byte(tstStr2))
|
|
|
|
got = string(ws.buf)
|
|
|
|
if got != want2 {
|
|
|
|
t.Errorf("Unexpected output, got: %v, want: %v", got, want2)
|
2019-01-22 03:15:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 04:44:40 +03:00
|
|
|
const tstStr3 = "k!"
|
|
|
|
const want3 = "hello work!"
|
2019-01-22 03:15:36 +03:00
|
|
|
ws.Seek(-2, io.SeekEnd)
|
2019-01-22 04:44:40 +03:00
|
|
|
ws.Write([]byte(tstStr3))
|
|
|
|
got = string(ws.buf)
|
|
|
|
if got != want3 {
|
|
|
|
t.Errorf("Unexpected output, got: %v, want: %v", got, want3)
|
2019-01-22 03:15:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-22 04:44:40 +03:00
|
|
|
const tstStr4 = "gopher"
|
|
|
|
const want4 = "hello gopher"
|
2019-01-22 03:15:36 +03:00
|
|
|
ws.Seek(6, io.SeekStart)
|
2019-01-22 04:44:40 +03:00
|
|
|
ws.Write([]byte(tstStr4))
|
|
|
|
got = string(ws.buf)
|
|
|
|
if got != want4 {
|
|
|
|
t.Errorf("Unexpected output, got: %v, want: %v", got, want4)
|
2019-01-22 03:15:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 04:44:40 +03:00
|
|
|
// TestDecodeFlac checks that we can load a flac file and decode to wav, writing
|
|
|
|
// to a wav file.
|
2019-01-21 08:11:49 +03:00
|
|
|
func TestDecodeFlac(t *testing.T) {
|
|
|
|
b, err := ioutil.ReadFile(testFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not read test file, failed with err: %v", err.Error())
|
|
|
|
}
|
|
|
|
out, err := Decode(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not decode, failed with err: %v", err.Error())
|
|
|
|
}
|
|
|
|
f, err := os.Create(outFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create output file, failed with err: %v", err.Error())
|
|
|
|
}
|
|
|
|
_, err = f.Write(out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not write to output file, failed with err: %v", err.Error())
|
|
|
|
}
|
|
|
|
}
|