Skip to content

Commit 6a76ced

Browse files
committed
#23 Power of 2 test refactor and unit test.
1 parent 3f6012b commit 6a76ced

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/jwave/tools/MathToolKit.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,8 @@ public static int compose( int[ ] ancientEgyptianMultipliers ) throws JWaveExcep
181181
* @return true if is 2^p else false
182182
*/
183183
public static boolean isBinary( int number ) {
184-
185-
boolean isBinary = false;
186-
187-
int power = (int)( Math.log( number ) / Math.log( 2. ) );
188-
189-
double result = 1. * Math.pow( 2., power );
190-
191-
if( result == number )
192-
isBinary = true;
193-
194-
return isBinary;
184+
// DLM#23 Power of 2 test
185+
return number > 0 && ((number & (number - 1)) == 0);
195186

196187
} // isBinary
197188

test/jwave/tools/MathToolKitTest.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)