public void testAddWithWrapAround() { Table<Integer> buf = new Table<Integer>(3, 10, 5); for (int i = 6; i <= 15; i++) assert buf.add(i, i) : "addition of seqno " + i + " failed"; System.out.println("buf = " + buf); for (int i = 0; i < 3; i++) { Integer val = buf.remove(false); System.out.println("removed " + val); assert val != null; } System.out.println("buf = " + buf); long low = buf.getLow(); buf.purge(8); System.out.println("buf = " + buf); assert buf.getLow() == 8; for (long i = low; i <= 8; i++) assert buf._get(i) == null : "message with seqno=" + i + " is not null"; for (int i = 16; i <= 18; i++) assert buf.add(i, i); System.out.println("buf = " + buf); while (buf.remove(false) != null) ; System.out.println("buf = " + buf); assert buf.isEmpty(); assert buf.getNumMissing() == 0; low = buf.getLow(); buf.purge(18); assert buf.getLow() == 18; for (long i = low; i <= 18; i++) assert buf._get(i) == null : "message with seqno=" + i + " is not null"; }
@Test public void mustAllowDoubleCommitAndRollback() { SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null); { WriteTransaction trans = db.beginWrite(); Table tbl = trans.getTable("EmployeeTable"); tbl.addColumn(RealmFieldType.STRING, "name"); tbl.addColumn(RealmFieldType.INTEGER, "number"); // allow commit before any changes assertEquals(0, tbl.size()); tbl.add("Hello", 1); trans.commit(); } { WriteTransaction trans = db.beginWrite(); Table tbl = trans.getTable("EmployeeTable"); // allow double rollback tbl.add("Hello", 2); assertEquals(2, tbl.size()); trans.rollback(); trans.rollback(); trans.rollback(); trans.rollback(); } { ReadTransaction trans = db.beginRead(); Table tbl = trans.getTable("EmployeeTable"); assertEquals(1, tbl.size()); trans.endRead(); } }
public void testPurgeForce() { Table<Integer> table = new Table<Integer>(3, 10, 0); for (int i = 1; i <= 30; i++) table.add(i, i); System.out.println("table = " + table); table.purge(15, true); System.out.println("table = " + table); assertIndices(table, 15, 15, 30); for (int i = 1; i <= 15; i++) assert table._get(i) == null; for (int i = 16; i <= 30; i++) assert table._get(i) != null; assert table.get(5) == null && table.get(25) != null; table.purge(30, true); System.out.println("table = " + table); assertIndices(table, 30, 30, 30); assert table.isEmpty(); for (int i = 1; i <= 30; i++) assert table._get(i) == null; for (int i = 31; i <= 40; i++) table.add(i, i); System.out.println("table = " + table); assert table.size() == 10; assertIndices(table, 30, 30, 40); table.purge(50, true); System.out.println("table = " + table); assert table.isEmpty(); assertIndices(table, 40, 40, 40); }
// Test that primary key constraints are actually removed @Test public void removingPrimaryKeyRemovesConstraint() { SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null); WriteTransaction trans = db.beginWrite(); Table tbl = trans.getTable("EmployeeTable"); tbl.addColumn(RealmFieldType.STRING, "name"); tbl.addColumn(RealmFieldType.INTEGER, "number"); tbl.setPrimaryKey("name"); tbl.add("Foo", 42); try { tbl.add("Foo", 41); } catch (RealmPrimaryKeyConstraintException e1) { // Primary key check worked, now remove it and try again. tbl.setPrimaryKey(""); try { tbl.add("Foo", 41); return; } catch (RealmException e2) { fail("Primary key not removed"); } } fail("Primary key not enforced."); }
public void testGetMissing5() { Table<Integer> buf = new Table<Integer>(3, 10, 0); buf.add(1, 1); SeqnoList missing = buf.getMissing(); System.out.println("missing = " + missing); assert missing == null && buf.getNumMissing() == 0; buf = new Table<Integer>(3, 10, 0); buf.add(10, 10); missing = buf.getMissing(); System.out.println("missing = " + missing); assert buf.getNumMissing() == missing.size(); buf = new Table<Integer>(3, 10, 0); buf.add(5, 5); missing = buf.getMissing(); System.out.println("missing = " + missing); assert buf.getNumMissing() == missing.size(); buf = new Table<Integer>(3, 10, 0); buf.add(5, 5); buf.add(7, 7); missing = buf.getMissing(); System.out.println("missing = " + missing); assert missing.size() == 5; assert buf.getNumMissing() == missing.size(); }
public void testAddMissing() { Table<Integer> buf = new Table<Integer>(3, 10, 0); for (int i : Arrays.asList(1, 2, 4, 5, 6)) buf.add(i, i); System.out.println("buf = " + buf); assert buf.size() == 5 && buf.getNumMissing() == 1; Integer num = buf.remove(); assert num == 1; num = buf.remove(); assert num == 2; num = buf.remove(); assert num == null; buf.add(3, 3); System.out.println("buf = " + buf); assert buf.size() == 4 && buf.getNumMissing() == 0; for (int i = 3; i <= 6; i++) { num = buf.remove(); System.out.println("buf = " + buf); assert num == i; } num = buf.remove(); assert num == null; }
public void testAddWithInvalidSeqno() { Table<Integer> buf = new Table<Integer>(3, 10, 20); boolean success = buf.add(10, 0); assert !success; success = buf.add(20, 0); assert !success; assert buf.isEmpty(); }
public static void testMove3() { Table<Integer> table = new Table<Integer>(3, 10, 0); for (int i = 1; i < 30; i++) table.add(i, i); table.removeMany(true, 23); System.out.println("table = " + table); table.add(30, 30); // triggers a resize() --> move() for (int i = 1; i <= 23; i++) assert table._get(i) == null; for (int i = 24; i < 30; i++) assert table._get(i) != null; }
@Test public void frequenciesAreIncreased() { sut = new Table(3); sut.add(_(1, 0, 1)); sut.add(_(1, 0, 1)); sut.add(_(0, 0, 1)); assertRows(_(1, 0, 1), _(0, 0, 1)); assertFreqs(2, 1); }
@Test public void testTable() { List<String> newTable = Arrays.asList("one", "two", "three"); Table t = new Table("t", newTable); assertEquals(3, t.numColumns()); assertEquals("two", t.title(1)); assertEquals(2, t.columnIndex("three")); Row myRow = new Row(new String[] {"Any", "Amy", "Abbie"}); assertEquals(true, t.add(myRow)); assertEquals(false, t.add(myRow)); }
// Test if toString() returns a correct PrimaryKey field description from a Table public void testTableToStringWithPrimaryKey() { Table t = getTableWithStringPrimaryKey(); t.addColumn(RealmFieldType.INTEGER, "intCol"); t.addColumn(RealmFieldType.BOOLEAN, "boolCol"); t.add("s1", 1, true); t.add("s2", 2, false); String expected = "The Table has 'colName' field as a PrimaryKey, and contains 3 columns: colName, intCol, boolCol. And 2 rows."; assertEquals(expected, t.toString()); }
public static void testGetNullMessages() { Table<Integer> table = new Table<Integer>(3, 10, 0); table.add(1, 1); table.add(100, 100); System.out.println("table = " + table); int num_null_elements = table.getNumMissing(); assert num_null_elements == 98; // [2 .. 99] table.add(50, 50); System.out.println("table = " + table); assert table.size() == 3; assert table.getNumMissing() == 97; }
public void testGetHighestDeliverable() { Table<Integer> table = new Table<Integer>(3, 10, 0); System.out.println("table = " + table); long highest_deliverable = table.getHighestDeliverable(), hd = table.getHighestDelivered(); System.out.println("highest delivered=" + hd + ", highest deliverable=" + highest_deliverable); assert hd == 0; assert highest_deliverable == 0; for (int num : Arrays.asList(1, 2, 3, 4, 5, 6, 8)) table.add(num, num); System.out.println("table = " + table); highest_deliverable = table.getHighestDeliverable(); hd = table.getHighestDelivered(); System.out.println("highest delivered=" + hd + ", highest deliverable=" + highest_deliverable); assert hd == 0; assert highest_deliverable == 6; table.removeMany(true, 4); System.out.println("table = " + table); highest_deliverable = table.getHighestDeliverable(); hd = table.getHighestDelivered(); System.out.println("highest delivered=" + hd + ", highest deliverable=" + highest_deliverable); assert hd == 4; assert highest_deliverable == 6; table.removeMany(true, 100); System.out.println("table = " + table); highest_deliverable = table.getHighestDeliverable(); hd = table.getHighestDelivered(); System.out.println("highest delivered=" + hd + ", highest deliverable=" + highest_deliverable); assert hd == 6; assert highest_deliverable == 6; table.add(7, 7); System.out.println("table = " + table); highest_deliverable = table.getHighestDeliverable(); hd = table.getHighestDelivered(); System.out.println("highest delivered=" + hd + ", highest deliverable=" + highest_deliverable); assert hd == 6; assert highest_deliverable == 8; table.removeMany(true, 100); System.out.println("table = " + table); highest_deliverable = table.getHighestDeliverable(); hd = table.getHighestDelivered(); System.out.println("highest delivered=" + hd + ", highest deliverable=" + highest_deliverable); assert hd == 8; assert highest_deliverable == 8; }
public static void testAdditionList() { Table<Integer> table = new Table<Integer>(3, 10, 0); List<Tuple<Long, Integer>> msgs = createList(0); assert !table.add(msgs); long[] seqnos = {1, 5, 9, 10, 11, 19, 20, 29}; msgs = createList(seqnos); assert table.add(msgs); System.out.println("table: " + table.dump()); for (long seqno : seqnos) assert table.get(seqno) == seqno; assert table.size() == 8; int size = table.computeSize(); assert size == 8; assert table.size() == table.computeSize(); assertCapacity(table.capacity(), 3, 10); assertIndices(table, 0, 0, 29); }
public static void testForEach() { class MyVisitor<T> implements Table.Visitor<T> { List<int[]> list = new ArrayList<int[]>(20); public boolean visit(long seqno, T element, int row, int column) { System.out.println("#" + seqno + ": " + element + ", row=" + row + ", column=" + column); list.add(new int[] {row, column}); return true; } } MyVisitor<Integer> visitor = new MyVisitor<Integer>(); Table<Integer> table = new Table<Integer>(3, 10, 0); for (int i = 1; i <= 20; i++) table.add(i, i); System.out.println("table = " + table); table.forEach(table.getLow() + 1, table.getHighestReceived() - 1, visitor); int count = 1; for (int[] pair : visitor.list) { int row = pair[0], column = pair[1]; if (count < Util.getNextHigherPowerOfTwo(10)) { assert row == 0; assert column == count; } else { assert row == 1; assert column == count - Util.getNextHigherPowerOfTwo(10); } count++; } }
public void testPurge3() { Table<Integer> table = new Table<Integer>(3, 10, 0); for (int i = 1; i <= 100; i++) table.add(i, i); System.out.println("table = " + table); table.removeMany(true, 53); for (int i = 54; i <= 100; i++) assert table.get(i) == i; }
public void testAdd() { Table<Integer> buf = new Table<Integer>(3, 10, 0); buf.add(1, 322649); buf.add(2, 100000); System.out.println("buf = " + buf); assert buf.size() == 2; }
public static void testPurge() { Table<Integer> table = new Table<Integer>(5, 10, 0); for (int seqno = 1; seqno <= 25; seqno++) table.add(seqno, seqno); int[] seqnos = {30, 31, 32, 37, 38, 39, 40, 41, 42, 47, 48, 49}; for (int seqno : seqnos) table.add(seqno, seqno); System.out.println("table (before remove):\n" + table.dump()); for (int seqno = 1; seqno <= 22; seqno++) table.remove(false); System.out.println("\ntable (after remove 22, before purge):\n" + table.dump()); table.purge(22); System.out.println("\ntable: (after purge 22):\n" + table.dump()); assert table.size() == 3 + seqnos.length; assert table.computeSize() == table.size(); }
protected static void addAndGet(Table<Integer> table, int... seqnos) { for (int seqno : seqnos) { boolean added = table.add((long) seqno, seqno); assert added; Integer val = table.get(seqno); assert val != null && val == seqno; } }
public static void testDuplicateAddition() { Table<Integer> table = new Table<Integer>(3, 10, 0); addAndGet(table, 1, 5, 9, 10); assert !table.add(5, 5); assert table.get(5) == 5; assert table.size() == 4; assertIndices(table, 0, 0, 10); }
public void testAddList() { Table<Integer> buf = new Table<Integer>(3, 10, 0); List<Tuple<Long, Integer>> msgs = createList(1, 2); boolean rc = buf.add(msgs); System.out.println("buf = " + buf); assert rc; assert buf.size() == 2; }
@Test public void rowsCanBeAdded() { sut = new Table(3); sut.add(_(1, 0, 1)); assertRows(_(1, 0, 1)); assertFreqs(1); }
private static Table buildTable(String keyPhrase) { keyPhrase = keyPhrase.replaceAll("\\s+", ""); Table t = new Table(); boolean[] used = new boolean['z' - 'a' + 1]; for (int i = 0; i < keyPhrase.length(); i++) { char c = keyPhrase.charAt(i); if (!used[c - 'a']) { t.add(c); used[c - 'a'] = true; } } for (int i = 0; i < used.length; i++) { if (!used[i] && i != 16) { t.add((char) ('a' + i)); } } return t; }
public void run() { try { latch.await(); Util.sleepRandom(10, 500); buf.add(seqno, seqno); } catch (InterruptedException e) { e.printStackTrace(); } }
public void testGet() { final Table<Integer> buf = new Table<Integer>(3, 10, 0); for (int i : Arrays.asList(1, 2, 3, 4, 5)) buf.add(i, i); assert buf.get(0) == null; assert buf.get(1) == 1; assert buf.get(10) == null; assert buf.get(5) == 5; assert buf.get(6) == null; }
public static void testGetMissingFirst() { Table<Integer> table = new Table<Integer>(3, 10, 0); for (int num : Arrays.asList(2, 3, 4, 5)) table.add(num, num); System.out.println("table = " + table); SeqnoList missing = table.getMissing(); System.out.println("missing=" + missing); assert missing.size() == 1; assert table.getNumMissing() == 1; }
public static void testAddListWithResizing2() { Table<Integer> table = new Table<Integer>(3, 500, 0); List<Tuple<Long, Integer>> msgs = new ArrayList<Tuple<Long, Integer>>(); for (int i = 1; i < 100; i++) msgs.add(new Tuple<Long, Integer>((long) i, i)); table.add(msgs, false); System.out.println("table = " + table); int num_resizes = table.getNumResizes(); System.out.println("num_resizes = " + num_resizes); assert num_resizes == 0 : "number of resizings=" + num_resizes + " (expected 0)"; }
@Test public void internalFrequenciesCannotBeAltered() { sut = new Table(1); sut.add(_(1)); int[] freqs = sut.getRowFrequencies(); freqs[0] = 2; assertFreqs(1); }
public void testRemoveMany2() { Table<Integer> buf = new Table<Integer>(3, 10, 0); for (int i : Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10)) buf.add(i, i); List<Integer> list = buf.removeMany(false, 3); System.out.println("list = " + list); assert list != null && list.size() == 3; list = buf.removeMany(false, 0); System.out.println("list = " + list); assert list != null && list.size() == 4; list = buf.removeMany(false, 10); assert list == null; buf.add(8, 8); list = buf.removeMany(false, 0); System.out.println("list = " + list); assert list != null && list.size() == 3; }
public static void testMassAddition() { Table<Integer> table = new Table<Integer>(3, 10, 0); final int NUM_ELEMENTS = 10005; for (int i = 1; i <= NUM_ELEMENTS; i++) table.add(i, i); System.out.println("table = " + table); assert table.size() == NUM_ELEMENTS; assertCapacity(table.capacity(), table.getNumRows(), 10); assertIndices(table, 0, 0, NUM_ELEMENTS); assert table.getNumMissing() == 0; }