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

test.it.unimi.dsi.big.util.ImmutableExternalPrefixMapTest 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
package it.unimi.dsi.big.util;

/*
 * DSI utilities
 *
 * Copyright (C) 2002-2019 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 .
 *
 */


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectRBTreeSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
import it.unimi.dsi.util.LongInterval;
import it.unimi.dsi.util.LongIntervals;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import org.junit.Test;

public class ImmutableExternalPrefixMapTest {

	public void testLargeSet(final int blockSize) throws IOException {
		Collection c = Arrays.asList(TernaryIntervalSearchTreeTest.WORDS);
		TernaryIntervalSearchTree t = new TernaryIntervalSearchTree(c);
		ImmutableExternalPrefixMap d = new ImmutableExternalPrefixMap(c, blockSize);

		for(int i = 0; i < TernaryIntervalSearchTreeTest.WORDS.length; i++) assertTrue(TernaryIntervalSearchTreeTest.WORDS[i], d.containsKey(TernaryIntervalSearchTreeTest.WORDS[i]));
		for(int i = 0; i < TernaryIntervalSearchTreeTest.WORDS.length; i++) assertEquals(TernaryIntervalSearchTreeTest.WORDS[i], d.list().get(i).toString());
		for(int i = 0; i < TernaryIntervalSearchTreeTest.WORDS.length; i++)
			for(int j = 0; j < TernaryIntervalSearchTreeTest.WORDS[i].length(); j++) {
				String s = TernaryIntervalSearchTreeTest.WORDS[i].substring(0, j + 1);
				assertEquals(s, t.rangeMap().get(s), d.getInterval(s));
				s = s + " ";
				assertEquals(s, t.rangeMap().get(s), d.getInterval(s));
				s = s.substring(0, s.length() - 1) + "~";
				assertEquals(s, t.rangeMap().get(s), d.getInterval(s));
			}

		// Similar tests, using all prefixes of all strings in WORDS.
		Collection p = new ObjectRBTreeSet<>();
		for(int i = 0; i < TernaryIntervalSearchTreeTest.WORDS.length; i++)
			for(int j = 0; j < TernaryIntervalSearchTreeTest.WORDS[i].length(); j++)
				p.add(TernaryIntervalSearchTreeTest.WORDS[i].substring(0, j + 1));

		d = new ImmutableExternalPrefixMap(p, blockSize);
		t = new TernaryIntervalSearchTree(p);

		int j = 0;
		for(Iterator i = p.iterator(); i.hasNext();) {
			String s = i.next();
			assertTrue(s, d.containsKey(s));
			assertEquals(s, d.list().get(j++).toString());
			assertEquals(s, t.rangeMap().get(s), d.getInterval(s));
		}

		final Iterator k = d.iterator();
		for(final Iterator i = p.iterator(); i.hasNext();) {
			assertTrue(i.hasNext() == k.hasNext());
			assertEquals(i.next().toString(), k.next().toString());
		}

		// Test negatives
		for(long i = 1000000000000L; i < 1000000002000L; i++) assertEquals(-1, d.getLong(Long.toBinaryString(i)));

	}

	@Test
	public void testLargeSet64() throws IOException {
		testLargeSet(64);
	}

	@Test
	public void testLargeSet128() throws IOException {
		testLargeSet(128);
	}

	@Test
	public void testLargeSet256() throws IOException {
		testLargeSet(256);
	}

	@Test
	public void testLargeSet1024() throws IOException {
		testLargeSet(1024);
	}

	@Test
	public void testLargeSet16384() throws IOException {
		testLargeSet(16384);
	}

	@Test
	public void testPrefixes() throws IOException {
		ImmutableExternalPrefixMap d = new ImmutableExternalPrefixMap(new ObjectLinkedOpenHashSet(new String[] { "ab", "ba", "bb" }));
		assertEquals(LongInterval.valueOf(1, 2), d.getInterval("b"));
	}

	@Test
	public void testLargeRootPrefixes() throws IOException {
		ImmutableExternalPrefixMap d = new ImmutableExternalPrefixMap(new ObjectLinkedOpenHashSet(new String[] { "aab", "aac", "aad" }), 2);
		assertEquals(LongInterval.valueOf(0, 2), d.getInterval(""));
		assertEquals(LongInterval.valueOf(0, 2), d.getInterval("aa"));
		assertEquals(LongInterval.valueOf(0, 2), d.getInterval("aa"));
	}

	@Test
	public void testSingleton() throws IOException {
		ImmutableExternalPrefixMap d = new ImmutableExternalPrefixMap(ObjectSets.singleton("a"), 1024);
		assertTrue(d.containsKey("a"));
		assertFalse(d.containsKey("b"));
		assertFalse(d.containsKey("0"));
	}

	@Test
	public void testPrefixOutOfRange() throws IOException {
		ImmutableExternalPrefixMap d = new ImmutableExternalPrefixMap(new ObjectLinkedOpenHashSet(new String[] { "ab", "ac" }));
		assertEquals(LongIntervals.EMPTY_INTERVAL, d.getInterval("b"));
		assertEquals(LongInterval.valueOf(0, 1), d.getInterval("a"));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy