-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdetector_test.go
64 lines (50 loc) · 1.99 KB
/
detector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package goertzel
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDetectTone(t *testing.T) {
found, err := testDetectToneFromFile("1400hz3s.slin", 1400.0, 8000.0, time.Second)
assert.True(t, found, "1400Hz tone should be found")
assert.Nil(t, err, "no error should be returned")
found, err = testDetectToneFromFile("1400hz3s.slin", 1400.0, 8000.0, 5*time.Second)
assert.False(t, found, "1400Hz tone should be NOT found")
assert.Nil(t, err, "no error should be returned")
found, err = testDetectToneFromFile("1400hz3s.slin", 2300.0, 8000.0, 50*time.Millisecond)
assert.False(t, found, "2300Hz tone should NOT be found")
assert.Nil(t, err, "no error should be returned")
found, err = testDetectToneFromFile("1400hz3s.slin", -2300.0, 8000.0, 50*time.Millisecond)
assert.True(t, found, "2300Hz tone absence should be found")
assert.Nil(t, err, "no error should be returned")
found, err = testDetectToneFromFile("combo15s.slin", 2300.0, 8000.0, time.Second)
assert.True(t, found, "2300Hz tone should be found")
assert.Nil(t, err, "no error should be returned")
found, err = testDetectToneFromFile("combo15s.slin", 500.0, 8000.0, time.Second)
assert.False(t, found, "500Hz tone should NOT be found")
assert.Nil(t, err, "no should be returned")
}
func testDetectToneFromFile(fn string, freq, rate float64, minDur time.Duration) (bool, error) {
f, err := os.Open("test/" + fn)
if err != nil {
return false, err
}
defer f.Close() // nolint
return DetectTone(context.Background(), freq, rate, minDur, f)
}
func TestDetectToneCancel(t *testing.T) {
f, err := os.Open("test/combo15s.slin")
if err != nil {
t.Errorf("failed to open test file %s: %v", "combo15s.slin", err)
t.SkipNow()
return
}
defer f.Close() // nolint
ctx, cancel := context.WithCancel(context.Background())
cancel()
found, err := DetectTone(ctx, 500.0, 8000.0, time.Second, f)
assert.False(t, found, "500Hz tone should NOT be found")
assert.Nil(t, err, "no error should be returned")
}