test.it.unimi.dsi.util.concurrent.ReorderingBlockingQueueTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsiutils Show documentation
Show all versions of dsiutils Show documentation
The DSI utilities are a mishmash of classes accumulated during the last twenty years in projects developed at the DSI (Dipartimento di Scienze dell'Informazione, i.e., Information Sciences Department), now DI (Dipartimento di Informatica, i.e., Informatics Department), of the Universita` degli Studi di Milano.
/*
* DSI utilities
*
* Copyright (C) 2017-2020 Sebastiano Vigna
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This library 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. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see .
*
*/
package it.unimi.dsi.util.concurrent;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import it.unimi.dsi.Util;
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.util.XoRoShiRo128PlusRandom;
public class ReorderingBlockingQueueTest {
@Test
public void testNoBlocking() throws InterruptedException {
for(final int size: new int[] { 1, 10, 100, 128, 256 }) {
final ReorderingBlockingQueue q = new ReorderingBlockingQueue<>(size);
final int[] perm = Util.identity(size);
IntArrays.shuffle(perm, new XoRoShiRo128PlusRandom());
for(int i = perm.length; i-- != 0;) q.put(Integer.valueOf(perm[i]), perm[i]);
for(int i = 0; i < perm.length; i++) assertEquals(i, q.take().intValue());
assertEquals(0, q.size());
}
}
@Test
public void testBlocking() throws InterruptedException {
for(final int size: new int[] { 10, 100, 128, 256, 1024 }) {
for(final int d: new int[] { 1, 2, 3, 4 }) {
final ReorderingBlockingQueue q = new ReorderingBlockingQueue<>(size / d);
final int[] perm = Util.identity(size);
IntArrays.shuffle(perm, new XoRoShiRo128PlusRandom());
for(int i = perm.length; i-- != 0;) {
final int t = perm[i];
new Thread() {
@Override
public void run() {
try {
q.put(Integer.valueOf(t), t);
}
catch (final InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}.start();
}
for(int i = 0; i < perm.length; i++) assertEquals(i, q.take().intValue());
assertEquals(0, q.size());
}
}
}
}