|
| 1 | +package jwave.tools; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +public class MathToolKitTest { |
| 7 | + |
| 8 | + private final int[] powersOfTwo= {1,2,4,8,16,32,64,128,256,512,1024,2048}; |
| 9 | + private final int[] notPowersOfTwo= {0,3,7,9,21,43,63,65,90,1001,}; |
| 10 | + |
| 11 | + /** |
| 12 | + * NOTE: Not certain what versions of Java we are supporting so I will avoid the iteration/collects enhancements. |
| 13 | + * |
| 14 | + * Test for jwave.tools.MathToolKit.isBinary(int) : boolean, function. This test |
| 15 | + * attempts to verify that powers of two are correctly recognized. |
| 16 | + * |
| 17 | + * The list of candidate numbers is infinite, making this test difficult to implement. |
| 18 | + * Feel free to add more numbers to the list but please do not add so many that the |
| 19 | + * test takes an unreasonable amount of time. |
| 20 | + */ |
| 21 | + @Test |
| 22 | + public void testPowersOfTwo() { |
| 23 | + MathToolKitTest.checkValue(powersOfTwo, true); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * NOTE: Not certain what versions of Java we are supporting so I will avoid the iteration/collects enhancements. |
| 28 | + * |
| 29 | + * Test for jwave.tools.MathToolKit.isBinary(int) : boolean, function. This test |
| 30 | + * attempts to verify that powers of two are correctly recognized. |
| 31 | + * |
| 32 | + * The list of candidate numbers is infinite, making this test difficult to implement. |
| 33 | + * Feel free to add more numbers to the list but please do not add so many that the |
| 34 | + * test takes an unreasonable amount of time. |
| 35 | + */ |
| 36 | + @Test |
| 37 | + public void testNotPowersOfTwo() { |
| 38 | + MathToolKitTest.checkValue(notPowersOfTwo, false); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * |
| 43 | + * @param values the list of integers to check |
| 44 | + * @param condition boolean indicating the expected result for ALL of the integers in the array. |
| 45 | + */ |
| 46 | + private static void checkValue(int[] values, boolean condition) { |
| 47 | + for(int i = 0; i < values.length; i++) { |
| 48 | + Assert.assertTrue("Value, " + values[i] +", failed test", condition == MathToolKit.isBinary(values[i])); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments