public void testGet() throws Exception { File strKeyIndexFile = prepareDataStringKey(); DiskTreap<String, Integer> strKeyIndex = new DiskTreap<String, Integer>(strKeyIndexFile); assertEquals(strKeyIndex.get("aab"), new Integer(123)); assertEquals(strKeyIndex.get("IBM"), new Integer(456)); assertEquals(strKeyIndex.get("18 year"), new Integer(789)); assertEquals(strKeyIndex.get("你好世界"), new Integer(7777)); strKeyIndex.close(); strKeyIndexFile.delete(); // ======= DiskTreap<Integer, String> intKeyIndex = new DiskTreap<Integer, String>(prepareDataFastIntKey()); assertEquals(intKeyIndex.get(-12), "abc"); assertEquals(intKeyIndex.get(0), "zzz"); assertEquals(intKeyIndex.get(133), "def"); assertEquals(intKeyIndex.get(345), "中国"); intKeyIndex.close(); // ======= DiskTreap<FastString, byte[]> fastStringIndex = new DiskTreap<FastString, byte[]>(prepareDataFastStringKey()); assertEquals(new String(fastStringIndex.get(new FastString("aab"))), "123"); assertEquals(new String(fastStringIndex.get(new FastString("IBM"))), "456"); assertEquals(new String(fastStringIndex.get(new FastString("中科院计算所"))), "11789"); assertEquals(new String(fastStringIndex.get(new FastString("18 year"))), "你好"); fastStringIndex.close(); }
public void testRemove() throws Exception { new File("tmp/remove_test").delete(); new File("tmp/remove_faststr_test").delete(); DiskTreap<String, Integer> treap = new DiskTreap<String, Integer>(new File("tmp/remove_test")); for (int i = 0; i < 100; i++) { treap.put("thing" + i, i); } assertEquals(100, treap.length()); for (int i = 0; i < 9; i++) { treap.removePrefix("thing" + i); } assertEquals(11, treap.length()); assertEquals( "{thing99=99, thing98=98, thing97=97, thing96=96, thing95=95, thing94=94, thing93=93, thing92=92, thing91=91, thing90=90, thing9=9}", treap.kmax(100).toString()); treap.delete("thing9"); assertEquals(10, treap.length()); assertEquals( "{thing99=99, thing98=98, thing97=97, thing96=96, thing95=95, thing94=94, thing93=93, thing92=92, thing91=91, thing90=90}", treap.kmax(100).toString()); assertNull(treap.get("thing77")); treap.close(); treap = new DiskTreap<String, Integer>(new File("tmp/remove_test")); for (int i = 0; i < 100; i++) { treap.put("thing" + i, i); } assertEquals(100, treap.length()); DiskTreap<FastString, Integer> treap2 = new DiskTreap<FastString, Integer>(new File("tmp/remove_faststr_test")); for (int i = 0; i < 100; i++) { treap2.put(new FastString("thing" + i), i); } assertEquals(100, treap2.length()); for (int i = 0; i < 9; i++) { treap2.removePrefix(new FastString("thing" + i)); } assertEquals(11, treap2.length()); assertEquals( "{thing99=99, thing98=98, thing97=97, thing96=96, thing95=95, thing94=94, thing93=93, thing92=92, thing91=91, thing90=90, thing9=9}", treap2.kmax(100).toString()); treap2.delete(new FastString("thing9")); assertEquals(10, treap2.length()); assertEquals( "{thing99=99, thing98=98, thing97=97, thing96=96, thing95=95, thing94=94, thing93=93, thing92=92, thing91=91, thing90=90}", treap2.kmax(100).toString()); assertNull(treap2.get(new FastString("thing77"))); treap2.close(); }
public void testBlukPut() throws Exception { File indexFile = new File("tmp/bulkput"); indexFile.delete(); DiskTreap<String, Integer> index = new DiskTreap<String, Integer>(indexFile); Map<String, Integer> map = new HashMap<String, Integer>(); map.put("foo", 1); map.put("footbar", 2); map.put("azb", 3); map.put("dddddd", 4); map.put("aojiao", 5); Map<String, Integer> map2 = new HashMap<String, Integer>(); map2.put("azb", 13); map2.put("footbar", 14); map2.put("sjy", 140); index.bulkPut(map); index.bulkPut(map2); index.close(); index = new DiskTreap<String, Integer>(indexFile); assertEquals(index.prefix("a", 10).toString(), "{aojiao=5, azb=13}"); assertEquals(index.prefix("f", 10).toString(), "{foo=1, footbar=14}"); assertEquals(index.get("dddddd").toString(), "4"); assertEquals(index.length(), 6); index.close(); }