mirror of https://bitbucket.org/ausocean/av.git
codecutil and audio: tests written using test tables
This commit is contained in:
parent
b0588cee14
commit
01561e363d
|
@ -25,29 +25,36 @@ LICENSE
|
|||
package codecutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LexBytes reads n bytes from src and writes them to dst every t seconds.
|
||||
func LexBytes(dst io.Writer, src io.Reader, t time.Duration, n int) error {
|
||||
if n <= 0 {
|
||||
return fmt.Errorf("invalid buffer size: %v", n)
|
||||
}
|
||||
if t < 0 {
|
||||
return fmt.Errorf("invalid delay: %v", t)
|
||||
}
|
||||
var tick <-chan time.Time
|
||||
if t == 0 {
|
||||
tick = make(chan time.Time)
|
||||
} else {
|
||||
if t > 0 {
|
||||
ticker := time.NewTicker(t)
|
||||
defer ticker.Stop()
|
||||
tick = ticker.C
|
||||
}
|
||||
|
||||
buf := make([]byte, n)
|
||||
for {
|
||||
<-tick
|
||||
buf := make([]byte, n)
|
||||
_, err := src.Read(buf)
|
||||
if t != 0 {
|
||||
<-tick
|
||||
}
|
||||
off, err := src.Read(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dst.Write(buf)
|
||||
_, err = dst.Write(buf[:off])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
NAME
|
||||
lex_test.go
|
||||
|
||||
AUTHOR
|
||||
Trek Hopton <trek@ausocean.org>
|
||||
|
||||
LICENSE
|
||||
This file 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 codecutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var lexTests = []struct {
|
||||
data []byte
|
||||
t time.Duration
|
||||
n int
|
||||
fail bool
|
||||
}{
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 4, false},
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 3, false},
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Duration(0), 2, false},
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Duration(0), 1, false},
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Nanosecond, 0, true},
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, -1, true},
|
||||
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 15, false},
|
||||
}
|
||||
|
||||
func TestLexBytes(t *testing.T) {
|
||||
for i, tt := range lexTests {
|
||||
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
||||
dst := bytes.NewBuffer([]byte{})
|
||||
err := LexBytes(dst, bytes.NewReader(tt.data), tt.t, tt.n)
|
||||
if err != nil && err != io.EOF {
|
||||
if !tt.fail {
|
||||
t.Errorf("unexpected error: %v", err.Error())
|
||||
}
|
||||
} else if !bytes.Equal(dst.Bytes(), tt.data) {
|
||||
t.Errorf("data before and after lex are not equal: want %v, got %v", tt.data, dst.Bytes())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -25,9 +25,10 @@ LICENSE
|
|||
package audio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -135,22 +136,47 @@ func TestDevice(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
dst := bytes.NewBuffer(make([]byte, 0))
|
||||
err = ai.Start()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
go codecutil.LexBytes(dst, ai, time.Duration(ac.RecPeriod*float64(time.Second)), ai.ChunkSize())
|
||||
go codecutil.LexBytes(ioutil.Discard, ai, time.Duration(ac.RecPeriod*float64(time.Second)), ai.ChunkSize())
|
||||
time.Sleep(time.Duration(ac.RecPeriod*float64(time.Second)) * time.Duration(n))
|
||||
ai.Stop()
|
||||
}
|
||||
|
||||
var powerTests = []struct {
|
||||
in int
|
||||
out int
|
||||
}{
|
||||
{36, 32},
|
||||
{47, 32},
|
||||
{3, 4},
|
||||
{46, 32},
|
||||
{7, 8},
|
||||
{2, 2},
|
||||
{36, 32},
|
||||
{757, 512},
|
||||
{2464, 2048},
|
||||
{18980, 16384},
|
||||
{70000, 65536},
|
||||
{8192, 8192},
|
||||
{2048, 2048},
|
||||
{65536, 65536},
|
||||
{-2048, 1},
|
||||
{-127, 1},
|
||||
{-1, 1},
|
||||
{0, 1},
|
||||
{1, 2},
|
||||
}
|
||||
|
||||
func TestNearestPowerOfTwo(t *testing.T) {
|
||||
testValues := []int{36, 47, 3, 46, 7, 2, 36, 757, 2464, 18980, 70000, 8192, 2048, 65536, -2048, -127, -1, 0, 1}
|
||||
testAnswers := []int{32, 32, 4, 32, 8, 2, 32, 512, 2048, 16384, 65536, 8192, 2048, 65536, 1, 1, 1, 1, 2}
|
||||
for i, v := range testValues {
|
||||
if r := nearestPowerOfTwo(v); testAnswers[i] != r {
|
||||
t.Errorf("test %v gave incorrect result: %v, should be %v", i, r, testAnswers[i])
|
||||
}
|
||||
for _, tt := range powerTests {
|
||||
t.Run(strconv.Itoa(tt.in), func(t *testing.T) {
|
||||
v := nearestPowerOfTwo(tt.in)
|
||||
if v != tt.out {
|
||||
t.Errorf("got %v, want %v", v, tt.out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue