All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.it.unimi.dsi.bits.AbstractBitVectorTest Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.7.3
Show newest version
/*
 * DSI utilities
 *
 * Copyright (C) 2010-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.bits;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import it.unimi.dsi.fastutil.Size64;
import it.unimi.dsi.fastutil.booleans.BooleanBigArrayBigList;

public class AbstractBitVectorTest {

	private final static class MinimalAlternatingBitVector extends AbstractBitVector {
		private long length = 129;

		@Override
		public boolean getBoolean(final long index) { return index % 2 != 0; }
		@Override
		public long length() { return length; }

		@Override
		public MinimalAlternatingBitVector length(final long newLength) {
			this.length  = newLength;
			return this;
		}
	}

	@SuppressWarnings("deprecation")
	@Test
	public void testUnsupported() {
		final BitVector v = new MinimalAlternatingBitVector();

		v.getBoolean(0);
		v.length();

		boolean ok = false;
		try {
			v.removeBoolean(0);
		}
		catch(final UnsupportedOperationException e) {
			ok = true;
		}

		assertTrue(ok);

		ok = false;
		try {
			v.set(0, 0);
		}
		catch(final UnsupportedOperationException e) {
			ok = true;
		}

		assertTrue(ok);

		ok = false;
		try {
			v.add(0, 0);
		}
		catch(final UnsupportedOperationException e) {
			ok = true;
		}

		assertTrue(ok);

		v.length(1L<<32);

		ok = false;
		try {
			v.size();
		}
		catch(final IllegalStateException e) {
			ok = true;
		}

		assertTrue(ok);

		ok = false;
		try {
			v.asLongBigList(1).size();
		}
		catch(final IllegalStateException e) {
			ok = true;
		}

		assertTrue(ok);
	}

	@Test
	public void testCopy() {
		assertEquals(new MinimalAlternatingBitVector(), new MinimalAlternatingBitVector().copy());
		assertEquals(new MinimalAlternatingBitVector().subVector(2, 20), new MinimalAlternatingBitVector().subVector(2, 20).copy());
		assertEquals(new MinimalAlternatingBitVector().subVector(5, 12), new MinimalAlternatingBitVector().subVector(2, 20).subVector(3, 10));
		assertEquals(new MinimalAlternatingBitVector().subVector(5, 12), new MinimalAlternatingBitVector().subVector(2, 20).subVector(3, 10).copy());
		assertEquals(new MinimalAlternatingBitVector().subList(2, 20), new BooleanBigArrayBigList(new MinimalAlternatingBitVector().subList(2, 20)));
		assertEquals(new MinimalAlternatingBitVector().subList(5, 12), new MinimalAlternatingBitVector().subList(2, 20).subList(3, 10));
		assertEquals(new MinimalAlternatingBitVector().subList(5, 12), new BooleanBigArrayBigList(new MinimalAlternatingBitVector().subList(2, 20).subList(3, 10)));
	}

	@Test
	public void testCount() {
		final MinimalAlternatingBitVector v = new MinimalAlternatingBitVector();
		assertEquals(v.length() / 2, v.count());
	}

	@Test
	public void testRemove() {
		BitVectorTestCase.testRemove(new AbstractBitVector.SubBitVector(BooleanListBitVector.getInstance().length(1000), 10, 100));
	}

	@Test
	public void testAdd() {
		BitVectorTestCase.testAdd(new AbstractBitVector.SubBitVector(BooleanListBitVector.getInstance().length(1000), 10, 100));
	}

	@Test
	public void testCompareTo() {
		final MinimalAlternatingBitVector v = new MinimalAlternatingBitVector();
		LongArrayBitVector w = LongArrayBitVector.copy(v);
		assertEquals(0, w.compareTo(v));
		assertEquals(0, v.compareTo(w));
		w.set(100);
		assertEquals(1, w.compareTo(v));
		assertEquals(-1, v.compareTo(w));
		w = LongArrayBitVector.ofLength(10);
		assertEquals(-1, w.compareTo(v));
		assertEquals(1, v.compareTo(w));
		w = LongArrayBitVector.of(1);
		assertEquals(1, w.compareTo(v));
		assertEquals(-1, v.compareTo(w));
	}

	@Test
	public void testSize64() {
		final MinimalAlternatingBitVector v = new MinimalAlternatingBitVector();
		v.length(1L << 32);
		assertEquals(1L << 32, v.size64());
		assertEquals(1L << 31, ((Size64)v.asLongSet()).size64());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy