Files
alice-backend/station/main_test.go
T

55 lines
1.2 KiB
Go

package main
import (
"encoding/binary"
"io"
"math"
"strings"
"testing"
)
func TestDownsampleUsesMicZero(t *testing.T) {
raw := make([]byte, 3*16)
for i, v := range []int16{300, 600, 900} {
binary.LittleEndian.PutUint16(raw[i*16:], uint16(v))
binary.LittleEndian.PutUint16(raw[i*16+2:], 1234)
}
got := downsample(raw)
if len(got) != 1 || got[0] != 600 {
t.Fatalf("got %v", got)
}
}
func TestFeaturesAndDistance(t *testing.T) {
a := make([]int16, frameSamples)
for i := range a {
a[i] = int16(4000 * math.Sin(2*math.Pi*440*float64(i)/rate))
}
f := features(a)
if len(f) != 20 {
t.Fatal(len(f))
}
d := distance([][]float64{f, f}, [][]float64{f, f})
if math.Abs(d) > 1e-9 {
t.Fatal(d)
}
if !math.IsInf(distance(nil, [][]float64{f}), 1) {
t.Fatal("empty distance")
}
}
func TestTemplateValidation(t *testing.T) {
p := t.TempDir() + "/template.json"
if _, e := loadTemplates(p); e == nil {
t.Fatal("missing template accepted")
}
}
func TestLiveReaderEOFStaysEOF(t *testing.T) {
r := newLiveReader(strings.NewReader(""))
b := make([]byte, 1)
for i := 0; i < 2; i++ {
if _, err := r.Read(b); err != io.EOF {
t.Fatalf("read %d: %v", i, err)
}
}
}