### Question:
Persistent23TreeMap { public MutableMap<K, V> beginWrite() { return new MutableMap<>(set); } Persistent23TreeMap();   Persistent23TreeMap(@Nullable final AbstractPersistent23Tree.RootNode<Entry<K, V>> root); ImmutableMap<K, V> beginRead(); Persistent23TreeMap<K, V> getClone(); MutableMap<K, V> beginWrite(); boolean endWrite(MutableMap<K, V> tree); Entry<K, V> createEntry(K key);  }### Answer:
@SuppressWarnings({"OverlyLongMethod"}) @Test public void iterationTest() { Random random = new Random(8234890); Persistent23TreeMap<Integer, String> map = new Persistent23TreeMap<>(); Persistent23TreeMap.MutableMap<Integer, String> write = map.beginWrite(); int[] p = genPermutation(random); TreeSet<Integer> added = new TreeSet<>(); for (int i = 0; i < ENTRIES_TO_ADD; i++) { int size = write.size(); Assert.assertEquals(i, size); if ((size & 1023) == 0 || size < 100) { Iterator<Integer> iterator = added.iterator(); for (Persistent23TreeMap.Entry<Integer, String> key : write) { Assert.assertTrue(iterator.hasNext()); Integer next = iterator.next(); Assert.assertEquals(next, key.getKey()); Assert.assertEquals(String.valueOf(next), key.getValue()); } Assert.assertFalse(iterator.hasNext()); iterator = added.iterator(); Iterator<Persistent23TreeMap.Entry<Integer, String>> treeItr = write.iterator(); for (int j = 0; j < size; j++) { Persistent23TreeMap.Entry<Integer, String> key = treeItr.next(); Assert.assertTrue(iterator.hasNext()); Integer next = iterator.next(); Assert.assertEquals(next, key.getKey()); Assert.assertEquals(String.valueOf(next), key.getValue()); } Assert.assertFalse(iterator.hasNext()); try { treeItr.next(); Assert.fail(); } catch (NoSuchElementException e) { } Assert.assertFalse(treeItr.hasNext()); } write.put(p[i], String.valueOf(p[i])); added.add(p[i]); } }
@SuppressWarnings({"OverlyLongMethod"}) @Test public void reverseIterationTest() { Random random = new Random(5743); Persistent23TreeMap.MutableMap<Integer, String> tree = new Persistent23TreeMap<Integer, String>().beginWrite(); int[] p = genPermutation(random); TreeSet<Integer> added = new TreeSet<>(); for (int i = 0; i < ENTRIES_TO_ADD; i++) { int size = tree.size(); Assert.assertEquals(i, size); if ((size & 1023) == 0 || size < 100) { Iterator<Integer> iterator = added.descendingIterator(); for (Iterator<Persistent23TreeMap.Entry<Integer, String>> treeItr = tree.reverseIterator(); treeItr.hasNext(); ) { Assert.assertTrue(iterator.hasNext()); Persistent23TreeMap.Entry<Integer, String> key = treeItr.next(); Integer next = iterator.next(); Assert.assertEquals(next, key.getKey()); Assert.assertEquals(String.valueOf(next), key.getValue()); } Assert.assertFalse(iterator.hasNext()); iterator = added.descendingIterator(); Iterator<Persistent23TreeMap.Entry<Integer, String>> treeItr = tree.reverseIterator(); for (int j = 0; j < size; j++) { Persistent23TreeMap.Entry<Integer, String> key = treeItr.next(); Assert.assertTrue(iterator.hasNext()); Integer next = iterator.next(); Assert.assertEquals(next, key.getKey()); Assert.assertEquals(String.valueOf(next), key.getValue()); } Assert.assertFalse(iterator.hasNext()); try { treeItr.next(); Assert.fail(); } catch (NoSuchElementException e) { } Assert.assertFalse(treeItr.hasNext()); } tree.put(p[i], String.valueOf(p[i])); added.add(p[i]); } }