-
Notifications
You must be signed in to change notification settings - Fork 0
/
song_test.go
182 lines (173 loc) · 4.22 KB
/
song_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"reflect"
"testing"
)
var testNewSongCases = []struct {
title string
tempo int
song *Song
}{
{
"",
60,
&Song{Name: defaultSongName, Tempo: 60},
},
{
"November Rain",
128,
&Song{Name: "November Rain", Tempo: 128},
},
}
var testSongMaxDurCases = []struct {
song *Song
want int
}{
{
&Song{
Name: "One",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "1", Beats: map[int]int{15: 1, 7: 1, 11: 1, 3: 1}, Duration: 16},
Pattern{Name: "2", Beats: map[int]int{1: 1, 2: 1, 6: 1}, Duration: 8},
},
},
16,
},
{
&Song{
Name: "Two",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "1", Beats: map[int]int{15: 1, 7: 1, 11: 1, 3: 1}, Duration: 16},
Pattern{Name: "2", Beats: map[int]int{1: 1, 2: 1, 6: 1}, Duration: 8},
Pattern{Name: "3", Beats: map[int]int{1: 1, 2: 1, 6: 1, 28: 1}, Duration: 32},
},
},
32,
},
}
var testPatternCases = []struct {
songIn *Song
name string
beats map[int]int
songOut *Song
}{
{
&Song{Name: "One", Tempo: 70},
"HiHat",
map[int]int{15: 1, 7: 1, 11: 1, 3: 1},
&Song{
Name: "One",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "HiHat", Beats: map[int]int{15: 1, 7: 1, 11: 1, 3: 1}, Duration: 16},
},
},
},
{
&Song{Name: "Two", Tempo: 70},
"HiHat",
map[int]int{16: 1, 7: 1, 11: 1, 3: 1},
&Song{
Name: "Two",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "HiHat", Beats: map[int]int{16: 1, 7: 1, 11: 1, 3: 1}, Duration: 16},
},
},
},
{
&Song{
Name: "With One",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "HiHat", Beats: map[int]int{3: 1, 7: 1, 11: 1, 15: 1}, Duration: 16},
},
},
"Snare Drum",
map[int]int{5: 1, 13: 1},
&Song{
Name: "With One",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "HiHat", Beats: map[int]int{3: 1, 7: 1, 11: 1, 15: 1}, Duration: 16},
Pattern{Name: "Snare Drum", Beats: map[int]int{5: 1, 13: 1}, Duration: 16},
},
},
},
}
var testSongPlayCases = []struct {
song *Song
step int
want string
}{
{
&Song{
Name: "Four On The Floor",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "Kick", Beats: map[int]int{1: 1, 5: 1}, Duration: 8},
Pattern{Name: "Snare", Beats: map[int]int{5: 1}, Duration: 8},
Pattern{Name: "HiHat", Beats: map[int]int{3: 1, 7: 1}, Duration: 8},
},
},
4,
"\033[1;16H 4 \033[2;17H_|\033[3;17H_|\033[4;17H_|\033[5;1H * ",
},
{
&Song{
Name: "Four On The Floor",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "Kick", Beats: map[int]int{1: 1, 5: 1}, Duration: 8},
Pattern{Name: "Snare", Beats: map[int]int{5: 1}, Duration: 8},
Pattern{Name: "HiHat", Beats: map[int]int{3: 1, 7: 1}, Duration: 8},
},
},
1,
"\033[1;1H \n\033[2;1H Kick: |\n\033[3;1H Snare: |\n\033[4;1H HiHat: |\n\033[1;10H 1 \033[2;11HX|\033[3;11H_|\033[4;11H_|\033[5;1H * ",
},
{
&Song{
Name: "Four On The Floor",
Tempo: 70,
Patterns: []Pattern{
Pattern{Name: "Kick", Beats: map[int]int{1: 1, 5: 1}, Duration: 8},
Pattern{Name: "Snare", Beats: map[int]int{5: 1}, Duration: 8},
Pattern{Name: "HiHat", Beats: map[int]int{3: 1, 7: 1}, Duration: 8},
},
},
8,
"\033[1;24H 8 \033[2;25H_|\033[3;25H_|\033[4;25H_|\033[5;1H * ",
},
}
func TestSongPlay(t *testing.T) {
for _, test := range testSongPlayCases {
if got, _, _ := test.song.Play(test.step); got != test.want {
t.Fatalf("song.Play(step=%d) Want: %q Got: %q", test.step, test.want, got)
}
}
}
func TestSongMaxDur(t *testing.T) {
for _, test := range testSongMaxDurCases {
if out := test.song.MaxPatDur(); out != test.want {
t.Fatalf("In: %v Want: %d Got: %d", test.song, test.want, out)
}
}
}
func TestSong(t *testing.T) {
for _, test := range testNewSongCases {
if song := NewSong(test.title, test.tempo); !reflect.DeepEqual(song, test.song) {
t.Fatalf("Title: %s | Tempo: %d\nGot: %v | Want: %v", test.title, test.tempo, song, test.song)
}
}
}
func TestPatterns(t *testing.T) {
for _, test := range testPatternCases {
test.songIn.AddPattern(test.name, test.beats, "")
if !reflect.DeepEqual(test.songIn, test.songOut) {
t.Fatalf("In: %v\nGot: %v\nWant: %v", test, test.songIn, test.songOut)
}
}
}