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

📄 streamutilstest.java

📁 BT JAVA LIBRARY,BTLib package is a J2ME Bluetooth library usefull when developing applications for J
💻 JAVA
字号:
/* The Bluetooth Library for client-server communication Copyright (C) 2006 Martin Vysny This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. */package net.sf.btw.tools;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.IOException;import java.util.Random;import junit.framework.TestCase;/** * Test suite for the {@link StreamUtils} class. *  * @author Martin Vysny */public final class StreamUtilsTest extends TestCase {	private static final Random random = new Random();	/**	 * Tests the {@link StreamUtils#readPackedInt(java.io.InputStream)} and	 * {@link StreamUtils#writePackedInt(int, java.io.OutputStream)}.	 * 	 * @throws Throwable	 *             test error	 */	public void testPackedIntMethods() throws Throwable {		internTestPackedInt(0);		internTestPackedInt(-1);		internTestPackedInt(1);		internTestPackedInt(1 << 6);		internTestPackedInt(1 << 5);		internTestPackedInt((1 << 5) - 1);		internTestPackedInt(-(1 << 6));		internTestPackedInt(-(1 << 5));		internTestPackedInt(1 << 7);		internTestPackedInt(1 << 13);		internTestPackedInt(1 << 14);		internTestPackedInt((1 << 13) - 1);		internTestPackedInt(-(1 << 13));		internTestPackedInt(-(1 << 12));		internTestPackedInt(-(1 << 14));		internTestPackedInt(Integer.MAX_VALUE);		internTestPackedInt(Integer.MIN_VALUE);		// random test		for (int i = 0; i < 1000; i++) {			int r = random.nextInt();			internTestPackedInt(r);			internTestPackedInt(-r);		}	}	private void internTestPackedInt(final int number) throws IOException {		ByteArrayOutputStream out = new ByteArrayOutputStream();		StreamUtils.writePackedInt(number, out);		assertEquals("Written data length mismatch", //$NON-NLS-1$				getEncodedIntLength(number), out.toByteArray().length);		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());		assertEquals("Numbers do not match", number, StreamUtils //$NON-NLS-1$				.readPackedInt(in));	}	private int getEncodedIntLength(int num) {		if (num < 0)			num = ~num;		int numberOfBytes = num < 1 << 6 ? 1 : (num < 1 << 13 ? 2				: (num < 1 << 20 ? 3 : (num < 1 << 27 ? 4 : 5)));		return numberOfBytes;	}	/**	 * Tests the {@link StreamUtils#readByteArray(java.io.InputStream)} and	 * {@link StreamUtils#writeByteArray(byte[], java.io.OutputStream)}.	 * 	 * @throws Exception	 * 	 */	public void testByteArrayMethods() throws Exception {		internTestByteArray(null);		internTestByteArray(new byte[0]);		internTestByteArray(new byte[] { 4, 10, 25, 100, -25 });		for (int i = 0; i < 20; i++) {			internTestByteArray(constructRandomArray(-1));		}	}	/**	 * Constructs random array.	 * @param maxLength max. array length. If -1 then a random length is chosen.	 * @return random array instance.	 */	public static byte[] constructRandomArray(int maxLength) {		final int arrayLen = random.nextInt() & 0xFFFF;		final byte[] result = new byte[arrayLen];		for (int i = 0; i < arrayLen; i++) {			result[i] = (byte) random.nextInt();		}		return result;	}	private void compareArrays(final byte[] expected, final byte[] got) {		assertEquals("Array length do not match", expected.length, got.length); //$NON-NLS-1$		for (int i = 0; i < expected.length; i++) {			assertEquals("Array item at " + i + " does not match", expected[i], //$NON-NLS-1$ //$NON-NLS-2$					got[i]);		}	}	private void internTestByteArray(final byte[] array) throws IOException {		ByteArrayOutputStream out = new ByteArrayOutputStream();		StreamUtils.writeByteArray(array, out);		assertEquals("Written data length mismatch", array == null ? 1 //$NON-NLS-1$				: getEncodedIntLength(array.length) + array.length, out				.toByteArray().length);		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());		final byte[] returnedArray = StreamUtils.readByteArray(in);		if (array == null)			assertNull("Returned array is not null", returnedArray); //$NON-NLS-1$		else			compareArrays(array, returnedArray);	}	/**	 * Test.	 * @throws Exception 	 */	public void testStringToByteArray() throws Exception {		internTestStringToByteArray(""); //$NON-NLS-1$		internTestStringToByteArray("abc"); //$NON-NLS-1$		internTestStringToByteArray("huhu haha huhu"); //$NON-NLS-1$	}	private void internTestStringToByteArray(final String string) throws IOException{		final byte[] bytes=StreamUtils.stringToByteArray(string);		final String decodedString=new DataInputStream(new ByteArrayInputStream(bytes)).readUTF();		assertEquals("Strings do not match", string, decodedString); //$NON-NLS-1$	}}

⌨️ 快捷键说明

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