1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* DSI utilities
*
* Copyright (C) 2014-2023 Sebastiano Vigna
*
* This program and the accompanying materials are made available under the
* terms of the GNU Lesser General Public License v2.1 or later,
* which is available at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1-standalone.html,
* or the Apache Software License 2.0, which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* SPDX-License-Identifier: LGPL-2.1-or-later OR Apache-2.0
*/
package it.unimi.dsi.io;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.junit.Ignore;
import org.junit.Test;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.util.SplitMix64Random;
public class ByteDiskQueueTest {
@Test
public void testSingleEnqueueDequeue() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, 128, true);
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
q.close();
}
@Test
public void testSomeEnqueueDequeue() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, 128, true);
for(int s = 1; s < 1 << 12; s *= 2) {
q.clear();
for(int i = 0; i < s; i++) q.enqueue((byte)(i + 64));
for(int i = 0; i < s; i++) assertEquals((byte)(i + 64), q.dequeue());
}
q.close();
}
@Test
public void testSomeEnqueueIntDequeueInt() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, 128, true);
for(int s = 1; s < 1 << 20; s *= 2) {
q.clear();
final SplitMix64Random random = new SplitMix64Random(0);
for(int i = 0; i < s; i++) {
q.enqueueInt(random.nextInt() & -1 >>> 1);
q.enqueueInt(random.nextInt() & -1 >>> 16);
}
random.setSeed(0);
for(int i = 0; i < s; i++) {
assertEquals(random.nextInt() & -1 >>> 1, q.dequeueInt());
assertEquals(random.nextInt() & -1 >>> 16, q.dequeueInt());
}
}
q.close();
queue.delete();
}
@Test
public void testSingleArrayEnqueueDequeue() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, 128, true);
final byte[] a = new byte[] { 1 };
q.enqueue(a);
final byte [] b = new byte[1];
q.dequeue(b);
assertArrayEquals(a, b);
q.close();
}
@Test
public void testSomeArrayEnqueueDequeue() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, true);
for(int s = 1; s < 1 << 13; s *= 2) {
q.clear();
final SplitMix64Random random = new SplitMix64Random(0);
final ArrayList<byte[]> arrays = new ArrayList<>();
for(int i = 0; i < s; i++) {
final byte[] a = new byte[random.nextInt(3 * bufferSize / 2)];
for(int j = a.length; j-- != 0;) a[j] = (byte)j;
arrays.add(a);
}
for(int i = 0; i < s; i++) q.enqueue(arrays.get(i));
for(int i = 0; i < s; i++) {
final byte[] a = new byte[arrays.get(i).length];
q.dequeue(a);
assertArrayEquals(arrays.get(i), a);
}
}
q.close();
}
}
@SuppressWarnings("resource")
@Test
public void testQueue() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
int bufferSize = 64;
ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize , true);
final ByteArrayList l = new ByteArrayList();
final SplitMix64Random random = new SplitMix64Random(0);
double threshold = .9;
for (int r = 10000; r-- != 0;) {
assertEquals(l.size(), q.size64());
switch(random.nextInt(7)) {
case 0:
// Invert queue tendency
if (random.nextFloat() < .01) threshold = 1 - threshold;
break;
case 1:
if (random.nextFloat() < threshold) {
final byte nextByte = (byte)random.nextInt();
q.enqueue(nextByte);
l.add(nextByte);
}
else {
assertEquals(Boolean.valueOf(q.isEmpty()), Boolean.valueOf(l.isEmpty()));
if (! q.isEmpty()) assertEquals(q.dequeue(), l.removeByte(0));
}
break;
case 2:
if (random.nextFloat() < threshold) {
final byte[] a = new byte[random.nextInt(256)];
for(int i = 0; i < a.length; i++) {
a[i] = (byte)i;
l.add((byte)i);
}
q.enqueue(a);
}
else {
final byte[] a = new byte[(int)Math.min(q.size64(), random.nextInt(256))];
q.dequeue(a);
for (final byte element : a) assertEquals(element, l.removeByte(0));
}
break;
case 3:
if (random.nextFloat() < .001) {
q.clear();
l.clear();
}
else if (random.nextFloat() < .01) {
q.suspend();
}
else if (random.nextFloat() < .01) {
q.trim();
}
break;
case 4:
q.suspend();
break;
case 5:
if (random.nextFloat() < 0.01) q.enlargeBuffer(bufferSize = Math.min(bufferSize + random.nextInt(bufferSize), 1024 * 1024));
break;
case 6:
if (random.nextFloat() < 0.005) {
q.freeze();
// Let's try to unmap to avoid out-of-memory errors
System.gc();
System.gc();
q = ByteDiskQueue.createFromFile(queue, bufferSize, true);
}
break;
}
}
q.close();
new File("testdiskqueue.serialised").delete();
}
@Test
public void testDequeueHoleBeforeStart() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, false);
q.enqueue((byte)1);
// Move start to the end
for(int i = bufferSize -2; i-- != 0;) {
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
}
for(int i = 2 * bufferSize; i-- != 0;) q.enqueue((byte)i);
assertEquals(1, q.dequeue());
for(int i = 2 * bufferSize; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
}
@Test
public void testDumpTailHoleAfterEnd() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, false);
q.enqueue((byte)1);
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
for(int i = 2 * bufferSize; i-- != 0;) q.enqueue((byte)i);
assertEquals(1, q.dequeue());
for(int i = 2 * bufferSize; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
}
@Test
public void testEnlarge() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
// start < end, no hole
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, false);
q.enlargeBuffer(bufferSize); // To increase coverage
q.enlargeBuffer(bufferSize * 2); // To increase coverage
q.enlargeBuffer(bufferSize); // To increase coverage
q.enqueue((byte)1);
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
for(int i = bufferSize / 2; i-- != 0;) q.enqueue((byte)i);
q.enlargeBuffer(bufferSize * 2);
assertEquals(1, q.dequeue());
for(int i = bufferSize / 2; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
// start > end, no hole
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, false);
q.enqueue((byte)1);
// Move start to the end
for(int i = bufferSize -2; i-- != 0;) {
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
}
for(int i = bufferSize / 2; i-- != 0;) q.enqueue((byte)i);
q.enlargeBuffer(bufferSize * 2);
assertEquals(1, q.dequeue());
for(int i = bufferSize / 2; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
// start < end, hole
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, true);
q.enqueue((byte)1);
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
for(int i = 2 * bufferSize; i-- != 0;) q.enqueue((byte)i);
q.enlargeBuffer(bufferSize * 2);
assertEquals(1, q.dequeue());
for(int i = 2 * bufferSize; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
// start > end, hole < start
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, true);
q.enqueue((byte)1);
// Move start to the end
for(int i = bufferSize - 2; i-- != 0;) {
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
}
for(int i = 2 * bufferSize; i-- != 0;) q.enqueue((byte)i);
q.enlargeBuffer(bufferSize * 2);
assertEquals(1, q.dequeue());
for(int i = 2 * bufferSize; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
// start > end, hole >= start
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, true);
q.enqueue((byte)1);
// Move start to the end
for(int i = bufferSize / 2 - 1; i-- != 0;) {
q.enqueue((byte)1);
assertEquals(1, q.dequeue());
}
for(int i = bufferSize; i-- != 0;) q.enqueue((byte)i);
q.enlargeBuffer(bufferSize * 2);
assertEquals(1, q.dequeue());
for(int i = bufferSize; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
}
@Test
public void testFreezeHoleEqualsEnd() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
for(int bufferSize = 8; bufferSize < 256; bufferSize *=2) {
ByteDiskQueue q = ByteDiskQueue.createNew(queue, bufferSize, false);
for(int i = 2 * bufferSize; i-- != 0;) q.enqueue((byte)i);
q.freeze();
q = ByteDiskQueue.createFromFile(queue, bufferSize, false);
for(int i = 2 * bufferSize; i-- != 0;) assertEquals((byte)i, q.dequeue());
q.close();
}
}
@Ignore
@Test
public void testLarge() throws IOException {
final File queue = File.createTempFile(this.getClass().getName(), ".queue");
queue.deleteOnExit();
final ByteDiskQueue q = ByteDiskQueue.createNew(queue, 128, true);
final SplitMix64Random random = new SplitMix64Random(0);
final long n = 3000000005L;
for(long i = n; i -- != 0;) q.enqueue((byte)random.nextInt(4));
assertEquals(n, q.size64());
final SplitMix64Random random2 = new SplitMix64Random(0);
for(long i = n; i -- != 0;) assertEquals((byte)random2.nextInt(4), q.dequeue());
q.close();
}
}
|