Permalink
Browse files

Added Bankers (failing tests)

  • Loading branch information...
1 parent 24127e2 commit b8b2eb9714b6adf3617354027cc9584add6201ed @au-phiware committed Dec 30, 2011
@@ -0,0 +1,46 @@
+/**
+ *
+ */
+package au.com.phiware.math.bankers;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import au.com.phiware.math.ring.BigIntegerArithmetic;
+import au.com.phiware.math.ring.BitArithmetic;
+import au.com.phiware.math.ring.IntegerArithmetic;
+import au.com.phiware.math.ring.LongArithmetic;
+
+/**
+ * @author Corin Lawson <me@corinlawson.com.au>
+ *
+ */
+public class Bankers {
+ private BitArithmetic<? extends Number> arithmetic;
+ private int length;
+
+ private Bankers(int length) {
+ if (length <= 32)
+ arithmetic = IntegerArithmetic.getInstance();
+ else if (length <= 64)
+ arithmetic = LongArithmetic.getInstance();
+ else
+ arithmetic = BigIntegerArithmetic.getInstance();
+ this.length = length;
+ }
+
+ public int length() {
+ return length;
+ }
+
+ private static Map<Integer, Bankers> instances = new HashMap<Integer, Bankers>();
+ public static Bankers getBanker(int length) {
+ if (!instances.containsKey(length))
+ instances.put(length, new Bankers(length));
+ return instances.get(length);
+ }
+
+ public <V extends Number> V next(V b) {
+ return null;
+ }
+}
@@ -0,0 +1,56 @@
+/**
+ *
+ */
+package au.com.phiware.math.bankers;
+
+import static org.junit.Assert.*;
+
+import java.util.BitSet;
+
+import org.junit.Test;
+
+/**
+ * @author Corin Lawson <me@corinlawson.com.au>
+ *
+ */
+public class BankersTest {
+
+ public void testMonotonicity(Bankers bankers) {
+ int b = 0;
+ int c = 0;
+ for (int i = 0; i < 1 << bankers.length(); i++) {
+ b = bankers.next(b);
+ assertTrue(b+" at "+i+" should have no less than "+c+" ones", Integer.bitCount(b) >= c);
+ c = Integer.bitCount(b);
+ }
+ }
+
+ /**
+ * The number of ones in the Banker's sequence should never decrease.
+ */
+ @Test
+ public void testMonotonicityUpTo10() {
+ for (int n = 2; n <= 10; n++)
+ testMonotonicity(Bankers.getBanker(n));
+ }
+
+ public void testIsomorphism(Bankers bankers) {
+ int b = 0;
+ BitSet seen = new BitSet(1 << bankers.length());
+ for (int i = 0; i < 1 << bankers.length(); i++) {
+ b = bankers.next(b);
+ assertFalse(b+" at "+i+" should not have been seen", seen.get(b));
+ seen.set(b);
+ }
+ }
+
+ /**
+ * The natural numbers should have a one-to-one mapping to the Banker's numbers.
+ */
+ @Test
+ public void testIsomorphismUpTo10() {
+ for (int n = 2; n <= 10; n++)
+ testIsomorphism(Bankers.getBanker(n));
+ }
+
+}

0 comments on commit b8b2eb9

Please sign in to comment.