/** Example of using unit tests for this assignment. To run them on the command line, make * sure that the junit-cs211.jar is in the same directory. * * On Mac/Linux: * javac -cp .:junit-cs211.jar *.java # compile everything * java -cp .:junit-cs211.jar P3tester # run tests * * On windows replace colons with semicolons: (: with ;) * demo$ javac -cp .;junit-cs211.jar *.java # compile everything * demo$ java -cp .;junit-cs211.jar P3tester # run tests */ import org.junit.*; import static org.junit.Assert.*; import java.util.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class P3tester { public static void main(String args[]){ org.junit.runner.JUnitCore.main("P3tester"); } static ByteArrayOutputStream localOut, localErr; static PrintStream sOut, sErr; @BeforeClass public static void setup() throws Exception { sOut = System.out; sErr = System.err; } private class DSTest extends DataSource { public Double next() { return 0.0; } public boolean hasNext() { return false; } } @Test public void datasource_exists() { DSTest test = new DSTest(); test.next(); test.hasNext(); test.toString(); test.display(); assertEquals( "DataSource should implement Iterator", true, test instanceof java.util.Iterator ); } double[] convert(Double[] list) { double[] results = new double[list.length]; for (int i = 0; i < results.length; i++) results[i] = list[i]; return results; } // once the source hits the end of data, it shouldn't change its mind about the fact private void test_exhaustible(DataSource src) { while (src.hasNext()) src.next(); for (int i = 0; i < 3; i++) assertEquals("hasNext should not change its mind", false, src.hasNext() ); } // makes sure that the output of a source matches the data in the given list private void test_contents(DataSource src, Double[] list) { Double[] output = new Double[list.length]; for (int i = 0; i < output.length; i++) { assertEquals("unexpected end of output", true, src.hasNext()); output[i] = src.next(); } assertEquals("output does not end when expected", false, src.hasNext()); assertArrayEquals(convert(list), convert(output), 0.001); } // basic check for sanity private void check_class(DataSource src) { Double x = src.next(); boolean b = src.hasNext(); test_exhaustible(src); } @Test public void arraysource_exists() { Double[] a = {1.0, 2.0, 3.0}; ArraySource asrc = new ArraySource(a); check_class(asrc); assertEquals( "ArraySource should derive from DataSource", true, asrc instanceof DataSource ); } // creates a list of doubles based on the input val (each digit in val turns into a number) private Double[] genlist(int val) { int numdigits = 0; for (int v2 = val; v2 > 0; v2 /= 10) numdigits++; Double[] list = new Double[numdigits]; for (int i=0; i < list.length; i++) { int d = val - 10*(val/10); list[i] = 0.6*(d - 4.5); val /= 10; } return list; } @Test public void arraysource_test() { for (int i = 1; i < 10000; i++) { Double[] list = genlist(i); ArraySource asrc = new ArraySource(list); test_contents(asrc, genlist(i)); } } @Test public void sinesource_exists() { SineSource ssrc = new SineSource(1.0, 1.0, 1.0, 10); check_class(ssrc); assertEquals( "SineSource should derive from DataSource", true, ssrc instanceof DataSource ); } private Double[][] sinesrcparams = {{1.0, 1.0, 0.0, 20.0}, {1.2, 3.3, 2.1, 10.0}, {4.2, 7.2, -1.1, 12.0} }; private Double[][] sinesrc = {{0.0,0.8414709848078965,0.9092974268256817,0.1411200080598672,-0.7568024953079282,-0.9589242746631385,-0.27941549819892586,0.6569865987187891,0.9893582466233818,0.4121184852417566,-0.5440211108893698,-0.9999902065507035,-0.5365729180004349,0.4201670368266409,0.9906073556948704,0.6502878401571168,-0.2879033166650653,-0.9613974918795568,-0.750987246771676,0.14987720966295234}, {1.0358512399786484,-0.9273173850671845,0.79556307609862,-0.6438875016005238,0.4760886877567364,-0.29636839408394505,0.10922689943981743,0.0806496870305699,-0.2685067682241557,0.4496403163793544}, {-3.743070912258029,-0.7650825179428027,2.81219300122573,4.186685135970957,2.2817578107754413,-1.410464408711996,-3.9978735650572514,-3.4537588685606635,-0.2043239304753665,3.2051574051704725,4.104047371911871,1.7882478220399525} }; @Test public void sinesource_test() { for (int i=0; i < sinesrcparams.length; i++) { Double[] params = sinesrcparams[i]; Double[] output = sinesrc[i]; int steps = params[3].intValue(); SineSource src = new SineSource(params[0], params[1], params[2], steps); test_contents(src, output); } } @Test public void datafilter_exists() { Double[] a = {1.0, 2.0, 3.0}; ArraySource asrc = new ArraySource(a); DataFilter filt = new DataFilter(asrc); check_class(filt); assertEquals( "DataFilter should derive from DataSource", true, filt instanceof DataSource ); } @Test public void datafilter_test() { for (int i = 1; i < 1000; i++) { Double[] list = genlist(i); ArraySource asrc = new ArraySource(list); DataFilter filt = new DataFilter(asrc); test_contents(filt, genlist(i)); } } @Test public void scalefilter_exists() { Double[] a = {1.0, 2.0, 3.0}; ArraySource asrc = new ArraySource(a); ScaleFilter filt = new ScaleFilter(asrc,1.0); check_class(filt); assertEquals( "ScaleFilter should derive from DataFilter", true, filt instanceof DataFilter ); } @Test public void scalefilter_test() { for (int i = 1; i < 1000; i++) { for (Double x=-2.0; x < 2.1; x += 0.2) { Double[] list = genlist(i); ArraySource asrc = new ArraySource(list); ScaleFilter filt = new ScaleFilter(asrc, x); Double[] output = genlist(i); for (int j = 0; j < output.length; j++) output[j] *= x; test_contents(filt, output); } } } private Double[][] ssfparams = {{1.0, 1.0, 0.0}, {1.2, 3.3, 2.1}, {4.2, 7.2, -1.1} }; private Double[][] ssfin = { {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, {1.0, 1.81, -1.2, 3.4, 2.1, 2.1, -2.3, 1.222, 1.2, 3.2}, {1.23, 133.1, 13.2, 1.12, 1.111, 13.88, 2.111, 13.3333} }; private Double[][] ssfout = { {0.0,0.8414709848078965,0.9092974268256817,0.1411200080598672,-0.7568024953079282,-0.9589242746631385,-0.27941549819892586,0.6569865987187891,0.9893582466233818,0.4121184852417566,-0.5440211108893698 }, {1.0358512399786484,-1.678444466971604,-0.954675691318344,-2.1892175054417806,0.9997862442891465,-0.6223736275762847,-0.2512218687115801,0.09855391755135642,-0.32220812186898684,1.4388490124139341}, {-4.603977222077376,-101.83248313818703,37.120947616179635,4.689087352287472,2.5350329277715153,-19.577245992922506,-8.439511095835858,-46.050003122179895 } }; @Test public void sinescalefilter_exists() { Double[] a = {1.0, 2.0, 3.0}; ArraySource asrc = new ArraySource(a); SineScaleFilter filt = new SineScaleFilter(asrc,1.0,1.0,0.0); check_class(filt); assertEquals( "SineScaleFilter should derive from DataFilter", true, filt instanceof DataFilter ); } @Test public void sinescalefilter_test() { for (int i=0; i < ssfparams.length; i++) { Double[] params = ssfparams[i]; Double[] input = ssfin[i]; Double[] output = ssfout[i]; ArraySource asrc = new ArraySource(input); SineScaleFilter filt = new SineScaleFilter(asrc, params[0], params[1], params[2]); test_contents(filt, output); } } @Test public void bufferedfilter_exists() { Double[] a = {1.0, 2.0, 3.0}; ArraySource asrc = new ArraySource(a); BufferedFilter filt = new BufferedFilter(asrc,5); Double x = filt.getLast(0); x = filt.inputNext(); check_class(filt); assertEquals( "BufferedFilter should derive from DataFilter", true, filt instanceof DataFilter ); } @Test public void bufferedfilter_test() { // check a range of input lists for (int i = 1; i < 100000; i++) { // the output corresponding to list number i Double[] output = genlist(i); // test all possible buffer sizes, from 1 to the length of data for (int j = 1; j <= output.length; j++) { // prepare the data source Double[] list = genlist(i); ArraySource asrc = new ArraySource(list); BufferedFilter filt = new BufferedFilter(asrc, j); // read the characters from the data source one by one for (int k=0; k < output.length; k++) { // read a character and check that it's correct assertEquals("unexpected end of output", true, filt.hasNext()); Double input = filt.next(); assertEquals(output[k], input, 0.0001); // make sure that the current buffer reflects everything that should be in the list for (int l=0; l <= k; l++) { int d = k - l; // if the index is within the range of buffer size, check it if (d < j) { Double last = filt.getLast(d); assertEquals(output[l], last, 0.0001); } } } // make sure the buffer ends when expected assertEquals("output does not end when expected", false, filt.hasNext()); } } } @Test public void echofilter_exists() { Double[] a = {1.0, 2.0, 3.0}; ArraySource asrc = new ArraySource(a); EchoFilter filt = new EchoFilter(asrc,5); check_class(filt); assertEquals( "EchoFilter should derive from BufferedFilter", true, filt instanceof BufferedFilter ); } @Test public void echofilter_test() { // check a range of input lists for (int i = 1; i < 100000; i++) { // the output corresponding to list number i Double[] output = genlist(i); // test all possible echo delays, from 0 to just within the size of the data for (int j = 0; j < output.length; j++) { // prepare the data source Double[] list = genlist(i); ArraySource asrc = new ArraySource(list); EchoFilter filt = new EchoFilter(asrc, j); // read the characters from the data source one by one for (int k=0; k < output.length; k++) { // read a character and check that it's correct assertEquals("unexpected end of output", true, filt.hasNext()); Double input = filt.next(); Double result = output[k]; if (k >= j) result += output[k-j]; assertEquals(result, input, 0.0001); } // make sure the buffer ends when expected assertEquals("output does not end when expected", false, filt.hasNext()); } } } }