static void clrTest(int n, Map s) { String nm = "Remove Present "; timer.start(nm, n); s.clear(); timer.finish(); reallyAssert(s.isEmpty()); }
static void itTest4(Map s, int size, int pos) { IdentityHashMap seen = new IdentityHashMap(size); reallyAssert(s.size() == size); int sum = 0; timer.start("Iter XEntry ", size); Iterator it = s.entrySet().iterator(); Object k = null; Object v = null; for (int i = 0; i < size - pos; ++i) { Map.Entry x = (Map.Entry) (it.next()); k = x.getKey(); v = x.getValue(); seen.put(k, k); if (x != MISSING) ++sum; } reallyAssert(s.containsKey(k)); it.remove(); reallyAssert(!s.containsKey(k)); while (it.hasNext()) { Map.Entry x = (Map.Entry) (it.next()); Object k2 = x.getKey(); seen.put(k2, k2); if (x != MISSING) ++sum; } reallyAssert(s.size() == size - 1); s.put(k, v); reallyAssert(seen.size() == size); timer.finish(); reallyAssert(sum == size); reallyAssert(s.size() == size); }
static void valTest(Map s, Object[] key) { int size = s.size(); int sum = 0; timer.start("Traverse key or value ", size); if (s.containsValue(MISSING)) ++sum; timer.finish(); reallyAssert(sum == 0); checkSum += sum; }
// unused static void getTestBoxed(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; Map<Integer, Integer> intMap = (Map<Integer, Integer>) s; timer.start(nm, n); for (int i = 0; i < n; i++) { if (intMap.get(i) != i) ++sum; } timer.finish(); reallyAssert(sum == expect); }
static void keyTest(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; timer.start(nm, n); for (int i = 0; i < n; i++) { if (s.containsKey(key[i])) ++sum; } timer.finish(); reallyAssert(sum == expect); checkSum += sum; }
static void remHalfTest(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; timer.start(nm, n / 2); for (int i = n - 2; i >= 0; i -= 2) { if (s.remove(key[i]) != null) ++sum; } timer.finish(); reallyAssert(sum == expect); checkSum += sum; }
static void getTest(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; timer.start(nm, n); for (int i = 0; i < n; i++) { Object v = s.get(key[i]); if (v != null && v.getClass() == eclass) ++sum; } timer.finish(); reallyAssert(sum == expect); checkSum += sum; }
static void putTest(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; timer.start(nm, n); for (int i = 0; i < n; i++) { Object k = key[i]; Object v = s.put(k, k); if (v == null) ++sum; } timer.finish(); reallyAssert(sum == expect); checkSum += sum; }
static void eitTest(Map s, int size) { int sum = 0; timer.start("Traverse entry ", size); for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry) it.next(); Object k = e.getKey(); Object v = e.getValue(); if (k != null && k.getClass() == eclass && v != null && v.getClass() == eclass) ++sum; } timer.finish(); reallyAssert(sum == size); checkSum += sum; }
static void itRemTest(Map s, int size) { int sz = s.size(); reallyAssert(sz == size); timer.start("Remove Present ", size); int sum = 0; for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { it.next(); it.remove(); ++sum; } timer.finish(); reallyAssert(sum == sz); checkSum += sum; }
static Object vitTest(Map s, int size) { Object last = null; int sum = 0; timer.start("Traverse key or value ", size); for (Iterator it = s.values().iterator(); it.hasNext(); ) { Object x = it.next(); if (x != last && x != null && x.getClass() == eclass) ++sum; last = x; } timer.finish(); reallyAssert(sum == size); checkSum += sum; return last; }
@Test public void ball_rolls_for_a_random_time_between_30_and_40_seconds() throws Exception { when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(33001); assertFalse(rt.isBallRolling()); }
@Test public void players_can_place_bets_when_the_ball_starts_rolling_up_to_10_seconds() throws Exception { rt.roll(); timer.moveTime(5000); rt.placeBet(p, Field.forNumber(1), 1); }
@Test public void winning_bets_contain_all_bets_with_field_winning_strategy_covering_ball_position() throws Exception { // arrange Player p2 = new Player(); Player p3 = new Player(); WinningStrategy loses = mock(WinningStrategy.class); when(loses.winsOn(anyInt())).thenReturn(false); WinningStrategy wins = mock(WinningStrategy.class); when(wins.winsOn(anyInt())).thenReturn(true); rt.placeBet(p, new Field("A", loses, 36), 1); rt.placeBet(p2, new Field("B", wins, 36), 2); rt.placeBet(p3, new Field("C", loses, 36), 3); // act when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(34000); // assert Set<Bet> winningBets = new HashSet<Bet>(); winningBets.add(new Bet(p2, 2)); assertEquals(winningBets, rt.getWinningBets()); }
public static void main(String[] args) throws Exception { int numTests = 100; int size = 36864; // about midway of HashMap resize interval if (args.length == 0) System.out.println( "Usage: MapCheck mapclass [int|float|string|object] [trials] [size] [serialtest]"); if (args.length > 0) { try { mapClass = Class.forName(args[0]); } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } } if (args.length > 1) { String et = args[1].toLowerCase(); if (et.startsWith("i")) eclass = java.lang.Integer.class; else if (et.startsWith("f")) eclass = java.lang.Float.class; else if (et.startsWith("s")) eclass = java.lang.String.class; else if (et.startsWith("d")) eclass = java.lang.Double.class; } if (eclass == null) eclass = Object.class; if (args.length > 2) numTests = Integer.parseInt(args[2]); if (args.length > 3) size = Integer.parseInt(args[3]); boolean doSerializeTest = args.length > 4; while ((size & 3) != 0) ++size; System.out.print("Class: " + mapClass.getName()); System.out.print(" elements: " + eclass.getName()); System.out.print(" trials: " + numTests); System.out.print(" size: " + size); System.out.println(); Object[] key = new Object[size]; Object[] absent = new Object[size]; initializeKeys(key, absent, size); precheck(size, key, absent); for (int rep = 0; rep < numTests; ++rep) { mainTest(newMap(), key, absent); if ((rep & 3) == 3 && rep < numTests - 1) { shuffle(key); // Thread.sleep(10); } } TestTimer.printStats(); checkNullKey(); if (doSerializeTest) serTest(newMap(), size); }
@Test(expected = NoMoreBetsException.class) public void players_cannot_place_bets_when_the_ball_starts_rolling_after_10_seconds() throws Exception { when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(10001); rt.placeBet(p, Field.forNumber(1), 1); }
@Test public void game_gets_a_random_outcome_when_the_ball_stops_rolling() { // act when(rng.generate(0, 36)).thenReturn(13); when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(34000); // assert assertEquals(13, rt.getBallPosition()); }
@Test public void system_will_not_pay_out_anything_for_losing_bets() throws Exception { WinningStrategy loses = mock(WinningStrategy.class); when(loses.winsOn(anyInt())).thenReturn(false); rt.placeBet(p, new Field("B", loses, 3), 2); when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(34000); verify(walletService, times(1)).adjustBalance(eq(p), anyInt()); }
@Test public void system_will_automatically_pay_out_winning_bets_with_multiplier_on_field() throws Exception { WinningStrategy wins = mock(WinningStrategy.class); when(wins.winsOn(anyInt())).thenReturn(true); rt.placeBet(p, new Field("B", wins, 3), 2); when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(34000); verify(walletService).adjustBalance(p, -2); verify(walletService).adjustBalance(p, 6); }
@Test public void multibets_paid_out_divided_by_number_of_fields() throws Exception { WinningStrategy loses = mock(WinningStrategy.class); when(loses.winsOn(anyInt())).thenReturn(false); WinningStrategy wins = mock(WinningStrategy.class); when(wins.winsOn(anyInt())).thenReturn(true); rt.placeBet(p, new Field[] {new Field("1", wins, 36), new Field("2", loses, 36)}, 1); when(rng.generate(30, 40)).thenReturn(33); rt.roll(); timer.moveTime(34000); verify(walletService).adjustBalance(p, -1); verify(walletService).adjustBalance(p, 18); }
static void twoMapTest2(Map s, Object[] key, Object[] absent) { int size = key.length; Map s2 = newMap(); putAllTest("Add Absent ", size, s, s2); putAllTest("Modify Present ", size, s, s2); Object lastkey = kitTest(s2, size); Object hold = s2.get(lastkey); int sum = 0; timer.start("Traverse entry ", size * 12); // 12 until finish int sh1 = s.hashCode() - s2.hashCode(); reallyAssert(sh1 == 0); boolean eq1 = s2.equals(s); boolean eq2 = s.equals(s2); reallyAssert(eq1 && eq2); Set es2 = s2.entrySet(); for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { Object entry = it.next(); if (es2.contains(entry)) ++sum; } reallyAssert(sum == size); s2.put(lastkey, MISSING); int sh2 = s.hashCode() - s2.hashCode(); reallyAssert(sh2 != 0); eq1 = s2.equals(s); eq2 = s.equals(s2); reallyAssert(!eq1 && !eq2); sum = 0; for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry) it.next(); e.setValue(absent[sum++]); } reallyAssert(sum == size); for (Iterator it = s2.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry) it.next(); e.setValue(s.get(e.getKey())); } timer.finish(); int rmiss = 0; timer.start("Remove Present ", size * 2); Iterator s2i = s2.entrySet().iterator(); Set es = s.entrySet(); while (s2i.hasNext()) { if (!es.remove(s2i.next())) ++rmiss; } timer.finish(); reallyAssert(rmiss == 0); clrTest(size, s2); reallyAssert(s2.isEmpty() && s.isEmpty()); }
static void putAllTest(String nm, int n, Map src, Map dst) { timer.start(nm, n); dst.putAll(src); timer.finish(); reallyAssert(src.size() == dst.size()); }