Neale Pickett
·
2026-02-17
midi_test.go
1package midi
2
3import (
4 "math/bits"
5 "testing"
6)
7
8// WriteCounter just counts how many bytes were written
9type WriteCounter struct {
10 len int
11}
12func (w *WriteCounter) Write(p []byte) (int, error) {
13 w.len += len(p)
14 return len(p), nil
15}
16
17func (n *Polyphony) OnesCount() int {
18 return bits.OnesCount64(n.playing[0]) + bits.OnesCount64(n.playing[1])
19}
20
21func TestWriter(t *testing.T) {
22 counter := new(WriteCounter)
23 w := NewWriter(counter, 0)
24
25 if w.Playing.OnesCount() != 0 {
26 t.Error("polyphony tracking is busted")
27 }
28
29 w.NoteOn(12)
30 if w.Playing.OnesCount() != 1 {
31 t.Error("polyphony tracking is busted")
32 }
33
34 w.NoteOn(12)
35 if w.Playing.OnesCount() != 1 {
36 t.Error("polyphony tracking is busted")
37 }
38
39 w.NoteOn(20)
40 if w.Playing.OnesCount() != 2 {
41 t.Error("polyphony tracking is busted")
42 }
43
44 w.NoteOn(20)
45 if w.Playing.OnesCount() != 2 {
46 t.Error("polyphony tracking is busted")
47 }
48
49 w.NoteOff(12)
50 if w.Playing.OnesCount() != 1 {
51 t.Error("polyphony tracking is busted")
52 }
53
54 w.NoteOff(12)
55 if w.Playing.OnesCount() != 1 {
56 t.Error("polyphony tracking is busted")
57 }
58
59 w.NoteOff(20)
60 if w.Playing.OnesCount() != 0 {
61 t.Error("polyphony tracking is busted")
62 }
63}