/** * Use coinflipper to return a random height for an entry to be added, probability of promotion is * 0.5 for each level * * @return */ private int getHeight() { int height = 1; CoinFlipper.Coin result = coinFlipper.flipCoin(); while (result != CoinFlipper.Coin.TAILS) { height++; result = coinFlipper.flipCoin(); } return height; }
// This is an example of how your SkipList is tested // for efficiency. @Test(timeout = 200) public void testPutSingle() { list.put(1); assertEquals(1, list.size()); assertEquals(new Integer(1), list.first()); assertEquals(new Integer(1), list.last()); assertEquals(2, randomness.getNumFlips()); }
@Test(timeout = 1000) public void stressTesting_LevelPromoting() { int quantity = 10000; Random r = new Random(System.currentTimeMillis()); Set<Integer> set = new HashSet<>(); for (int i = 0; i < quantity; i++) { Integer num = r.nextInt(); set.add(num); if (!list.contains(num)) { list.put(num); } } Random rand = new Random(10); int flips = 0; for (int i = 0; i < quantity; i++) { while (rand.nextBoolean()) { flips++; } flips++; } assertEquals("Adding is not working properly", set, list.dataSet()); assertEquals("CoinFlipper is not being used properly", flips, randomness.getNumFlips()); }