001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import java.util.Arrays;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * JUnit TestCase to exercise the Conversion class.
012: * <p>
013: * TODO: change tests to use min and max values where appropriate.
014: *
015: * @see Conversion
016: * @author orion
017: */
018: public class ConversionTest extends TestCase {
019:
020: /**
021: * Constructor for ConversionTest.
022: *
023: * @param arg0
024: */
025: public ConversionTest(String arg0) {
026: super (arg0);
027: }
028:
029: static final byte[] MAX_UINT_BYTES = new byte[] { (byte) 0xFF,
030: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
031: static final byte[] ONE_UINT_BYTES = new byte[] { (byte) 0x00,
032: (byte) 0x00, (byte) 0x00, (byte) 0x01 };
033: static final byte[] ZERO_UINT_BYTES = new byte[] { (byte) 0x00,
034: (byte) 0x00, (byte) 0x00, (byte) 0x00 };
035:
036: public void testBits() {
037: byte flags = 0;
038:
039: flags = Conversion.setFlag(flags, 1, true);
040: flags = Conversion.setFlag(flags, 2, false);
041: flags = Conversion.setFlag(flags, 4, true);
042: flags = Conversion.setFlag(flags, 8, false);
043: flags = Conversion.setFlag(flags, 16, true);
044:
045: assertTrue(Conversion.getFlag(flags, 1));
046: assertFalse(Conversion.getFlag(flags, 2));
047: assertTrue(Conversion.getFlag(flags, 4));
048: assertFalse(Conversion.getFlag(flags, 8));
049: assertTrue(Conversion.getFlag(flags, 16));
050:
051: flags = Conversion.setFlag(flags, 1, false);
052: flags = Conversion.setFlag(flags, 2, true);
053: flags = Conversion.setFlag(flags, 4, false);
054: flags = Conversion.setFlag(flags, 8, true);
055: flags = Conversion.setFlag(flags, 16, false);
056:
057: assertFalse(Conversion.getFlag(flags, 1));
058: assertTrue(Conversion.getFlag(flags, 2));
059: assertFalse(Conversion.getFlag(flags, 4));
060: assertTrue(Conversion.getFlag(flags, 8));
061: assertFalse(Conversion.getFlag(flags, 16));
062:
063: }
064:
065: public void testuint2Bytes() {
066: byte[] bytes = Conversion.uint2bytes(Conversion.MAX_UINT);
067: long l = Conversion.bytes2uint(bytes, 0, 4);
068: assertTrue(l == Conversion.MAX_UINT);
069: assertTrue(Arrays.equals(MAX_UINT_BYTES, bytes));
070:
071: bytes = Conversion.uint2bytes(1L);
072: l = Conversion.bytes2uint(bytes, 0, 4);
073: assertTrue(l == 1L);
074: assertTrue(Arrays.equals(ONE_UINT_BYTES, bytes));
075:
076: bytes = Conversion.uint2bytes(0L);
077: l = Conversion.bytes2uint(bytes, 0, 4);
078: assertTrue(l == 0L);
079: assertTrue(Arrays.equals(ZERO_UINT_BYTES, bytes));
080:
081: bytes = Conversion.uint2bytes(3427655L);
082: l = Conversion.bytes2uint(bytes, 0, 4);
083: assertTrue(l == 3427655L);
084: assertTrue(Arrays.equals(new byte[] { (byte) 0x00, (byte) 0x34,
085: (byte) 0x4D, (byte) 0x47 }, bytes));
086:
087: try {
088: Conversion.uint2bytes(Conversion.MAX_UINT + 1L);
089: assertTrue(false);
090: } catch (IllegalArgumentException e) {
091: // Exception thrown, we're okay
092: }
093:
094: try {
095: Conversion.uint2bytes(-1L);
096: assertTrue(false);
097: } catch (IllegalArgumentException e) {
098: // Exception thrown, we're okay
099: }
100:
101: }
102:
103: public void testBytes2uint() {
104: long result = Conversion.bytes2uint(MAX_UINT_BYTES, 0, 4);
105: assertTrue(Conversion.MAX_UINT == result);
106:
107: result = Conversion.bytes2uint(new byte[] { (byte) 0xFF,
108: (byte) 0xFF, (byte) 0xFF, (byte) 0xFE }, 0, 4);
109: assertTrue((Conversion.MAX_UINT - 1L) == result);
110:
111: result = Conversion.bytes2uint(new byte[] { (byte) 0xFF,
112: (byte) 0xFF, (byte) 0xFE, (byte) 0xFF }, 0, 4);
113: assertTrue((Conversion.MAX_UINT - 256L) == result);
114:
115: result = Conversion.bytes2uint(new byte[] { (byte) 0xFF,
116: (byte) 0xFE, (byte) 0xFF, (byte) 0xFF }, 0, 4);
117: assertTrue((Conversion.MAX_UINT - 65536L) == result);
118:
119: result = Conversion.bytes2uint(new byte[] { (byte) 0xFE,
120: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, 0, 4);
121: assertTrue((Conversion.MAX_UINT - 16777216L) == result);
122:
123: result = Conversion.bytes2uint(ZERO_UINT_BYTES, 0, 4);
124: assertTrue(0L == result);
125:
126: result = Conversion.bytes2uint(ONE_UINT_BYTES, 0, 4);
127: assertTrue(1L == result);
128:
129: result = Conversion.bytes2uint(new byte[] { 1, 1, 1, 1 }, 0, 4);
130: assertTrue(16843009L == result);
131:
132: result = Conversion.bytes2uint(new byte[] { (byte) 0xFF,
133: (byte) 0xFF, 1, (byte) 0xFF, (byte) 0xFF }, 2, 1);
134: assertTrue(1L == result);
135: }
136:
137: public void testByte2uint() {
138: int i = 128;
139:
140: for (byte b = Byte.MIN_VALUE; b < 0; b++) {
141: int result = Conversion.byte2uint(b);
142: assertTrue(result == i);
143: i++;
144: }
145:
146: i = 0;
147: for (byte b = 0; b < Byte.MAX_VALUE; b++) {
148: int result = Conversion.byte2uint(b);
149: assertTrue(result == i);
150: i++;
151: }
152:
153: }
154:
155: public void testBytes2String() {
156: // TODO: add tests for other character sets
157: String testString = "test string";
158: String convertedString;
159: byte[] bytes = Conversion.string2Bytes(testString);
160: convertedString = Conversion.bytes2String(bytes);
161: assertTrue(testString.equals(convertedString));
162: }
163:
164: public void testString2Bytes() {
165: // testBytes2String();
166: }
167:
168: public void testBytes2Boolean() {
169: boolean testBoolean = true;
170: boolean convertedBoolean = false;
171:
172: byte[] bytes = Conversion.boolean2Bytes(testBoolean);
173: convertedBoolean = Conversion.bytes2Boolean(bytes);
174: assertTrue(testBoolean == convertedBoolean);
175: }
176:
177: public void testBoolean2Bytes() {
178: //
179: }
180:
181: public void testByte2Bytes() {
182: byte testByte = 18;
183: byte[] convertedBytes = Conversion.byte2Bytes(testByte);
184: assertTrue(convertedBytes != null && convertedBytes.length == 1
185: && convertedBytes[0] == testByte);
186: }
187:
188: public void testBytes2Char() {
189: char testChar = 'c';
190: byte[] convertedBytes = Conversion.char2Bytes(testChar);
191: assertTrue(testChar == Conversion.bytes2Char(convertedBytes));
192: }
193:
194: public void testChar2Bytes() {
195: //
196: }
197:
198: public void testBytes2Double() {
199: double[] testVals = new double[] { Double.MIN_VALUE, -1.1, 0,
200: 1.1, Double.MAX_VALUE };
201:
202: for (int i = 0; i < testVals.length; i++) {
203: byte[] convertedBytes = Conversion
204: .double2Bytes(testVals[i]);
205: double convertedDouble = Conversion
206: .bytes2Double(convertedBytes);
207: assertTrue(testVals[i] == convertedDouble);
208: }
209: }
210:
211: public void testDouble2Bytes() {
212: //
213: }
214:
215: public void testBytes2Float() {
216: float[] testVals = new float[] { Float.MIN_VALUE, -1.1f, 0,
217: 1.1f, Float.MAX_VALUE };
218: for (int i = 0; i < testVals.length; i++) {
219: byte[] convertedBytes = Conversion.float2Bytes(testVals[i]);
220: assertTrue(testVals[i] == Conversion
221: .bytes2Float(convertedBytes));
222: }
223: }
224:
225: public void testFloat2Bytes() {
226: //
227: }
228:
229: public void testBytes2Int() {
230: int[] testVals = new int[] { Integer.MIN_VALUE, -1, 0, 1,
231: Integer.MAX_VALUE };
232: for (int i = 0; i < testVals.length; i++) {
233: try {
234: byte[] convertedBytes = Conversion
235: .int2Bytes(testVals[i]);
236: int convertedInt = Conversion.bytes2Int(convertedBytes);
237: assertEquals(testVals[i], convertedInt);
238: } catch (RuntimeException e) {
239: e.printStackTrace();
240: System.out.println("Failed to convert: " + testVals[i]);
241: fail("Failed to convert: " + testVals[i]);
242: }
243: }
244: }
245:
246: public void testInt2Bytes() {
247: //
248: }
249:
250: public void testBytes2Long() {
251: long[] testVals = new long[] { Long.MIN_VALUE, -1, 0, 1,
252: Long.MAX_VALUE };
253: for (int i = 0; i < testVals.length; i++) {
254: byte[] convertedBytes = Conversion.long2Bytes(testVals[i]);
255: assertTrue(testVals[i] == Conversion
256: .bytes2Long(convertedBytes));
257: }
258: }
259:
260: public void testLong2Bytes() {
261: //
262: }
263:
264: public void testBytes2Short() {
265: short[] testVals = new short[] { Short.MIN_VALUE, -1, 0, 1,
266: Short.MIN_VALUE };
267: for (int i = 0; i < testVals.length; i++) {
268: byte[] convertedBytes = Conversion.short2Bytes(testVals[i]);
269: assertTrue(testVals[i] == Conversion
270: .bytes2Short(convertedBytes));
271: }
272: }
273:
274: public void testShort2Bytes() {
275: //
276: }
277:
278: }
|