-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtttl_parser_test.ino
329 lines (278 loc) · 7.3 KB
/
rtttl_parser_test.ino
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "rtttl_parser.h"
/**
* Unit tests for RTTTL parser (code for converting RTTTL format to frequency/duration)
* Developed as a helper library for Lab 4 (Clocks, Timers, and Watchdogs)
* in CSCI 1600 (Real-time and embedded software) at Brown University
* (https://cs.brown.edu/courses/info/csci1600/)
* Author: Milda Zizyte
*/
void print_error(String msg, String varname, int expected, int actual) {
Serial.println(msg);
Serial.print("Expected ");
Serial.print(varname);
Serial.print(": ");
Serial.print(expected);
Serial.print(", actual: ");
Serial.println(actual);
while(true);
}
int f[10];
int d[10];
int l;
void run_test_single_note(int test_no, String rtttl, int exp_f, int exp_d) {
char itoa_buf[12];
itoa(test_no, itoa_buf, 10);
Serial.print("Running test ");
Serial.println(itoa_buf);
l = rtttl_to_buffers(rtttl, f, d);
String e_str = "Test " + (String) itoa_buf + (String) " failed";
if (l != 1) {
print_error(e_str, "length", 1, l);
}
if (f[0] != exp_f) {
print_error(e_str, "frequency", exp_f, f[0]);
}
if (d[0] != exp_d) {
print_error(e_str, "duration", exp_d, d[0]);
}
Serial.println("");
}
void setup() {
Serial.begin(9600);
while(!Serial);
int test_no = 1;
/**
* Test 1: Single note
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4c4", NOTE_C4, 1000);
test_no++;
/**
* Test 2: Single note, default octave
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4c", NOTE_C4, 1000);
test_no++;
/**
* Test 3: Single note, different octave
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4c6", 1047, 1000);
test_no++;
/**
* Test 4: Single note, default duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c4", NOTE_C4, 1000);
test_no++;
/**
* Test 5: Single note, half duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:8c4", NOTE_C4, 500);
test_no++;
/**
* Test 6: Single note, double duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:2c4", NOTE_C4, 2000);
test_no++;
/**
* Test 7: Single note, dot duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4c4.", NOTE_C4, 1500);
test_no++;
/**
* Test 8: Single note, fourth duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:16c4", NOTE_C4, 250);
test_no++;
/**
* Test 9: Single note, eighth duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:32c4", NOTE_C4, 125);
test_no++;
/**
* Test 10: Single note, quadruple duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:1c4", NOTE_C4, 4000);
test_no++;
/**
* Test 11: Single note, eighth default
*/
run_test_single_note(test_no, "a:b=60,o=4,d=8:c4", NOTE_C4, 1000);
test_no++;
/**
* Test 12: Single note, eighth default w/ duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=8:8c4", NOTE_C4, 1000);
test_no++;
/**
* Test 13: Single note, eighth default, fourth duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=8:4c4", NOTE_C4, 2000);
test_no++;
/**
* Test 14: Single note, eighth default, sixteenth duration
*/
run_test_single_note(test_no, "a:b=60,o=4,d=8:16c4", NOTE_C4, 500);
test_no++;
/**
* Test 15: C
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c", NOTE_C4, 1000);
test_no++;
/**
* Test 16: C#
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c_", NOTE_CS4, 1000);
test_no++;
/**
* Test 17: D
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:d", NOTE_D4, 1000);
test_no++;
/**
* Test 18: D#
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:d_", NOTE_DS4, 1000);
test_no++;
/**
* Test 19: E
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:e", NOTE_E4, 1000);
test_no++;
/**
* Test 20: E# (F)
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:e_", NOTE_F4, 1000);
test_no++;
/**
* Test 21: F
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:f", NOTE_F4, 1000);
test_no++;
/**
* Test 22: F#
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:f_", NOTE_FS4, 1000);
test_no++;
/**
* Test 23: G
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:g", NOTE_G4, 1000);
test_no++;
/**
* Test 24: G#
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:g_", NOTE_GS4, 1000);
test_no++;
/**
* Test 25: A
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:a", NOTE_A4, 1000);
test_no++;
/**
* Test 26: A#
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:a_", NOTE_AS4, 1000);
test_no++;
/**
* Test 27: B
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:b", NOTE_B4, 1000);
test_no++;
/**
* Test 28: B# (C one octave up)
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:b_", NOTE_C5, 1000);
test_no++;
/**
* Test 29: dot variation 1
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4.c4", NOTE_C4, 1500);
test_no++;
/**
* Test 30: dot variation 2
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4c.4", NOTE_C4, 1500);
test_no++;
/**
* Test 31: dot variation 3
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4c.", NOTE_C4, 1500);
test_no++;
/**
* Test 32: dot variation 4
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:4.c", NOTE_C4, 1500);
test_no++;
/**
* Test 33: dot variation 5
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c.4", NOTE_C4, 1500);
test_no++;
/**
* Test 34: dot variation 6
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c4.", NOTE_C4, 1500);
test_no++;
/**
* Test 35: dot variation 7
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c.", NOTE_C4, 1500);
test_no++;
/**
* Test 36: dot variation 8
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c_.4", NOTE_CS4, 1500);
test_no++;
/**
* Test 37: dot variation 9
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c_4.", NOTE_CS4, 1500);
test_no++;
/**
* Test 38: dot variation 10
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:c_.", NOTE_CS4, 1500);
test_no++;
/**
* Test 39: dot with eighth note
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:8c.", NOTE_C4, 750);
test_no++;
/**
* Test 40: rest
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:p", 0, 1000);
test_no++;
/**
* Test 41: eighth note rest
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:8p", 0, 500);
test_no++;
/**
* Test 42: dotted rest
*/
run_test_single_note(test_no, "a:b=60,o=4,d=4:p.", 0, 1500);
test_no++;
/**
* Test 43: multiple notes
*/
Serial.println("Running test 43");
l = rtttl_to_buffers("a:b=60,o=4,d=4:b5,a,8c,8f_,2g7,p.,b_", f, d);
const int song_len = 7;
int expected_f[song_len] = {NOTE_B5, NOTE_A4, NOTE_C4, NOTE_FS4, NOTE_G7, 0, NOTE_C5};
int expected_d[song_len] = {1000, 1000, 500, 500, 2000, 1500, 1000};
String e_str = "Test 43 failed";
if (l != song_len) {
print_error(e_str, "length", song_len, l);
}
for(int i = 0; i < song_len; i++) {
if (f[i] != expected_f[i]) {
print_error(e_str, (String) "f[" + (String)(char)(i + (int) '0') + (String) "]", expected_f[i], f[i]);
}
if (d[i] != expected_d[i]) {
print_error(e_str, (String) "d[" + (String)(char)(i + (int) '0') + (String) "]", expected_d[i], d[i]);
}
}
Serial.println("");
Serial.println("All tests passed!");
}
void loop() {
}