-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTestGuitarString.java
executable file
·61 lines (48 loc) · 1.67 KB
/
TestGuitarString.java
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
package synthesizer;
/* Since this test is part of a package, we have to import the package version of StdAudio. */
/* Don't worry too much about this, we'll get there in due time. */
import edu.princeton.cs.introcs.StdAudio;
import org.junit.Test;
import static org.junit.Assert.*;
/** Tests the GuitarString class.
* @author Josh Hug
*/
public class TestGuitarString {
@Test
public void testPluckTheAString() {
double CONCERT_A = 440.0;
GuitarString aString = new GuitarString(CONCERT_A);
aString.pluck();
for (int i = 0; i < 50000; i += 1) {
StdAudio.play(aString.sample());
aString.tic();
}
}
@Test
public void testTic() {
// Create a GuitarString of frequency 11025, which
// is an ArrayRingBuffer of length 4.
GuitarString s = new GuitarString(11025);
s.pluck();
// Record the front four values, ticcing as we go.
double s1 = s.sample();
s.tic();
double s2 = s.sample();
s.tic();
double s3 = s.sample();
s.tic();
double s4 = s.sample();
// If we tic once more, it should be equal to 0.996*0.5*(s1 + s2)
s.tic();
double s5 = s.sample();
double expected = 0.996 * 0.5 * (s1 + s2);
// Check that new sample is correct, using tolerance of 0.001.
// See JUnit documentation for a description of how tolerances work
// for assertEquals(double, double)
assertEquals(expected, s5, 0.001);
}
/** Calls tests for GuitarString. */
public static void main(String[] args) {
jh61b.junit.textui.runClasses(TestGuitarString.class);
}
}