test.it.unimi.dsi.big.util.LiterallySignedStringMapTest 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) 2002-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.big.util;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import it.unimi.dsi.fastutil.Hash;
import it.unimi.dsi.fastutil.Size64;
import it.unimi.dsi.fastutil.io.BinIO;
import it.unimi.dsi.fastutil.objects.AbstractObject2LongFunction;
import it.unimi.dsi.fastutil.objects.AbstractObjectBigList;
import it.unimi.dsi.fastutil.objects.Object2LongOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.ObjectBigLists;
import it.unimi.dsi.lang.MutableString;
import it.unimi.dsi.util.FrontCodedStringList;
public class LiterallySignedStringMapTest {
private final static class CharSequenceStrategy implements Hash.Strategy, Serializable {
private static final long serialVersionUID = 1L;
@Override
public boolean equals(final CharSequence a, final CharSequence b) {
if (a == null) return b == null;
if (b == null) return false;
return a.toString().equals(b.toString());
}
@Override
public int hashCode(final CharSequence o) {
return o.toString().hashCode();
}
}
@Test
public void testNumbers() throws IOException, ClassNotFoundException {
for(int n = 10; n < 10000; n *= 10) {
final String[] s = new String[n];
for(int i = s.length; i-- != 0;) s[i] = Integer.toString(i);
Collections.shuffle(Arrays.asList(s));
final FrontCodedStringList fcl = new FrontCodedStringList(Arrays.asList(s), 8, true);
// Test with mph
final Object2LongOpenCustomHashMap mph = new Object2LongOpenCustomHashMap<>(new CharSequenceStrategy());
mph.defaultReturnValue(-1);
for(int i = 0; i < s.length; i++) mph.put(new MutableString(s[i]), i);
LiterallySignedStringMap map = new LiterallySignedStringMap(mph, ObjectBigLists.asBigList(fcl));
for(int i = s.length; i-- != 0;) assertEquals(i, map.getLong(s[i]));
for(int i = s.length + n; i-- != s.length;) assertEquals(-1, map.getLong(Integer.toString(i)));
final File temp = File.createTempFile(getClass().getSimpleName(), "test");
temp.deleteOnExit();
BinIO.storeObject(map, temp);
map = (LiterallySignedStringMap)BinIO.loadObject(temp);
for(int i = s.length; i-- != 0;) assertEquals(i, map.getLong(s[i]));
for(int i = s.length + n; i-- != s.length;) assertEquals(-1, map.getLong(Integer.toString(i)));
}
}
private final class LargeFunction extends AbstractObject2LongFunction implements Size64 {
private static final long serialVersionUID = 1L;
@Override
public long getLong(final Object key) {
try {
final long l = Long.parseLong(key.toString());
return l < 1L << 31 ? l : -1;
}
catch(final Exception e) {
return -1;
}
}
@Override
public boolean containsKey(final Object key) {
try {
final long l = Long.parseLong(key.toString());
return l < 1L << 31;
}
catch(final Exception e) {
return false;
}
}
@Override
public int size() {
return Integer.MAX_VALUE;
}
@Override
public long size64() {
return 1L << 31;
}
}
@Test
public void testLarge() {
new LiterallySignedStringMap(new LargeFunction(), new AbstractObjectBigList() {
@Override
public MutableString get(final long index) {
return new MutableString(Long.toString(index));
}
@Override
public long size64() {
return 1L << 31;
}
});
}
}