Skip to content

Commit

Permalink
Added Bankers (failing tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
au-phiware committed Dec 30, 2011
1 parent 24127e2 commit b8b2eb9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
46 changes: 46 additions & 0 deletions java/src/au/com/phiware/math/bankers/Bankers.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
56 changes: 56 additions & 0 deletions java/test/au/com/phiware/math/bankers/BankersTest.java
Original file line number Diff line number Diff line change
@@ -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.