/** @throws Exception If failed. */ public void testCompact() throws Exception { File file = new File(UUID.randomUUID().toString()); X.println("file: " + file.getPath()); FileSwapSpaceSpi.SwapFile f = new FileSwapSpaceSpi.SwapFile(file, 8); Random rnd = new Random(); ArrayList<FileSwapSpaceSpi.SwapValue> arr = new ArrayList<>(); int size = 0; for (int a = 0; a < 100; a++) { FileSwapSpaceSpi.SwapValue[] vals = new FileSwapSpaceSpi.SwapValue[1 + rnd.nextInt(10)]; int size0 = 0; for (int i = 0; i < vals.length; i++) { byte[] bytes = new byte[1 + rnd.nextInt(49)]; rnd.nextBytes(bytes); size0 += bytes.length; vals[i] = new FileSwapSpaceSpi.SwapValue(bytes); arr.add(vals[i]); } f.write(new FileSwapSpaceSpi.SwapValues(vals, size0), 1); size += size0; assertEquals(f.length(), size); assertEquals(file.length(), size); } int i = 0; for (FileSwapSpaceSpi.SwapValue val : arr) assertEquals(val.idx(), ++i); i = 0; for (int cnt = arr.size() / 2; i < cnt; i++) { FileSwapSpaceSpi.SwapValue v = arr.remove(rnd.nextInt(arr.size())); assertTrue(f.tryRemove(v.idx(), v)); } int hash0 = 0; for (FileSwapSpaceSpi.SwapValue val : arr) hash0 += Arrays.hashCode(val.readValue(f.readCh)); ArrayList<T2<ByteBuffer, ArrayDeque<FileSwapSpaceSpi.SwapValue>>> bufs = new ArrayList(); for (; ; ) { ArrayDeque<FileSwapSpaceSpi.SwapValue> que = new ArrayDeque<>(); ByteBuffer buf = f.compact(que, 1024); if (buf == null) break; bufs.add(new T2(buf, que)); } f.delete(); int hash1 = 0; for (FileSwapSpaceSpi.SwapValue val : arr) hash1 += Arrays.hashCode(val.value(null)); assertEquals(hash0, hash1); File file0 = new File(UUID.randomUUID().toString()); FileSwapSpaceSpi.SwapFile f0 = new FileSwapSpaceSpi.SwapFile(file0, 8); for (T2<ByteBuffer, ArrayDeque<FileSwapSpaceSpi.SwapValue>> t : bufs) f0.write(t.get2(), t.get1(), 1); int hash2 = 0; for (FileSwapSpaceSpi.SwapValue val : arr) hash2 += Arrays.hashCode(val.readValue(f0.readCh)); assertEquals(hash2, hash1); }
/** @throws Exception Thrown if test failed. */ public void testA() throws Exception { Collection<Integer> set = new GridConcurrentWeakHashSet<>(); Integer i = 1; assert set.add(i); assert !set.add(i); assert set.contains(i); assert set.size() == 1; Collection<Integer> c = F.asList(2, 3, 4, 5); assert set.addAll(c); assert !set.addAll(c); assert set.containsAll(c); assert set.size() == 1 + c.size(); assert set.remove(i); assert !set.remove(i); assert !set.contains(i); assert set.size() == c.size(); assert set.removeAll(c); assert !set.removeAll(c); assert !set.containsAll(c); assert set.isEmpty(); Collection<Integer> c1 = Arrays.asList(1, 3, 5, 7, 9); int cnt = 0; for (Iterator<Integer> iter = set.iterator(); iter.hasNext(); cnt++) c1.contains(iter.next()); assert set.size() == cnt; assert set.size() == set.toArray().length; assert set.addAll(c1); assert set.retainAll(c); assert !set.retainAll(c); Collection<Integer> c2 = F.retain(c1, true, c); assert set.containsAll(c2); assert !set.containsAll(c1); assert !set.containsAll(c); assert set.size() == c2.size(); set.clear(); assert set.isEmpty(); try { set.iterator().next(); assert false; } catch (NoSuchElementException ignored) { assert true; } try { set.add(null); assert false; } catch (NullPointerException ignored) { assert true; } }