⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semiexternaloffsetlisttest.java

📁 MG4J (Managing Gigabytes for Java) is a free full-text search engine for large document collections
💻 JAVA
字号:
package test.it.unimi.dsi.mg4j.util;

/*		 
 * MG4J: Managing Gigabytes for Java
 *
 * Copyright (C) 2006-2007 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 2.1 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import it.unimi.dsi.io.InputBitStream;
import it.unimi.dsi.io.OutputBitStream;
import it.unimi.dsi.mg4j.util.SemiExternalOffsetList;

import java.io.IOException;

import junit.framework.TestCase;

/**
 * @author Fabien Campagne
 */
public class SemiExternalOffsetListTest extends TestCase {

	private static InputBitStream buildInputStream( LongList offsets ) throws IOException {
		byte[] array = new byte[ offsets.size() * 4 ];
		OutputBitStream streamer = new OutputBitStream( array );
		long previous = 0;
		for ( int i = 0; i < offsets.size(); i++ ) {
			final long value = offsets.getLong( i );
			streamer.writeLongGamma( value - previous );
			previous = value;
		}
		int size = (int)( streamer.writtenBits() / 8 ) + ( ( streamer.writtenBits() % 8 ) == 0 ? 0 : 1 );
		byte[] smaller = new byte[ size ];
		System.arraycopy( array, 0, smaller, 0, size );

		return new InputBitStream( smaller );

	}

	
    public void testSemiExternalOffsetListGammaCoding() throws IOException {

		long[] offsets = { 10, 300, 450, 650, 1000, 1290, 1699 };
		LongList listOffsets = new LongArrayList( offsets );

		SemiExternalOffsetList list = new SemiExternalOffsetList( buildInputStream( listOffsets ), 1, listOffsets.size() );
		for ( int i = 0; i < offsets.length; ++i ) {
			assertEquals( ( "test failed for index: " + i ), offsets[ i ], list.getLong( i ) );
		}

		list = new SemiExternalOffsetList( buildInputStream( listOffsets ), 2, listOffsets.size() );
		for ( int i = 0; i < offsets.length; ++i ) {
			assertEquals( ( "test failed for index: " + i ), offsets[ i ], list.getLong( i ) );
		}

		list = new SemiExternalOffsetList( buildInputStream( listOffsets ), 4, listOffsets.size() );
		for ( int i = 0; i < offsets.length; ++i ) {
			assertEquals( ( "test failed for index: " + i ), offsets[ i ], list.getLong( i ) );
		}

		list = new SemiExternalOffsetList( buildInputStream( listOffsets ), 7, listOffsets.size() );
		for ( int i = 0; i < offsets.length; ++i ) {
			assertEquals( ( "test failed for index: " + i ), offsets[ i ], list.getLong( i ) );
		}
		
		list = new SemiExternalOffsetList( buildInputStream( listOffsets ), 8, listOffsets.size() );
		for ( int i = 0; i < offsets.length; ++i ) {
			assertEquals( ( "test failed for index: " + i ), offsets[ i ], list.getLong( i ) );
		}
    }

    public void testEmptySemiExternalOffsetListGammaCoding() throws IOException {

		long[] offsets = {  };
		LongList listOffsets = new LongArrayList( offsets );

		new SemiExternalOffsetList( buildInputStream( listOffsets ), 1, listOffsets.size() );
		assertTrue( true );
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -