codecutil and audio: tests written using test tables

This commit is contained in:
Trek H 2019-06-14 19:49:49 +09:30
parent b0588cee14
commit 01561e363d
3 changed files with 113 additions and 16 deletions

View File

@ -25,29 +25,36 @@ LICENSE
package codecutil package codecutil
import ( import (
"fmt"
"io" "io"
"time" "time"
) )
// LexBytes reads n bytes from src and writes them to dst every t seconds. // 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 { 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 var tick <-chan time.Time
if t == 0 { if t > 0 {
tick = make(chan time.Time)
} else {
ticker := time.NewTicker(t) ticker := time.NewTicker(t)
defer ticker.Stop() defer ticker.Stop()
tick = ticker.C tick = ticker.C
} }
for {
<-tick
buf := make([]byte, n) buf := make([]byte, n)
_, err := src.Read(buf) for {
if t != 0 {
<-tick
}
off, err := src.Read(buf)
if err != nil { if err != nil {
return err return err
} }
_, err = dst.Write(buf) _, err = dst.Write(buf[:off])
if err != nil { if err != nil {
return err return err
} }

View File

@ -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())
}
})
}
}

View File

@ -25,9 +25,10 @@ LICENSE
package audio package audio
import ( import (
"bytes"
"errors" "errors"
"io/ioutil"
"os" "os"
"strconv"
"testing" "testing"
"time" "time"
@ -135,22 +136,47 @@ func TestDevice(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
dst := bytes.NewBuffer(make([]byte, 0))
err = ai.Start() err = ai.Start()
if err != nil { if err != nil {
t.Error(err) 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)) time.Sleep(time.Duration(ac.RecPeriod*float64(time.Second)) * time.Duration(n))
ai.Stop() 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) { 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} for _, tt := range powerTests {
testAnswers := []int{32, 32, 4, 32, 8, 2, 32, 512, 2048, 16384, 65536, 8192, 2048, 65536, 1, 1, 1, 1, 2} t.Run(strconv.Itoa(tt.in), func(t *testing.T) {
for i, v := range testValues { v := nearestPowerOfTwo(tt.in)
if r := nearestPowerOfTwo(v); testAnswers[i] != r { if v != tt.out {
t.Errorf("test %v gave incorrect result: %v, should be %v", i, r, testAnswers[i]) t.Errorf("got %v, want %v", v, tt.out)
} }
})
} }
} }