001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import junit.framework.*;
021:
022: import java.io.*;
023:
024: /**
025: * Test LongField code
026: *
027: * @author Marc Johnson (mjohnson at apache dot org)
028: */
029:
030: public class TestLongField extends TestCase {
031:
032: /**
033: * Constructor
034: *
035: * @param name
036: */
037:
038: public TestLongField(String name) {
039: super (name);
040: }
041:
042: static private final long[] _test_array = { Long.MIN_VALUE, -1L,
043: 0L, 1L, Long.MAX_VALUE };
044:
045: /**
046: * Test constructors.
047: */
048:
049: public void testConstructors() {
050: try {
051: new LongField(-1);
052: fail("Should have caught ArrayIndexOutOfBoundsException");
053: } catch (ArrayIndexOutOfBoundsException ignored_e) {
054:
055: // as expected
056: }
057: LongField field = new LongField(2);
058:
059: assertEquals(0L, field.get());
060: try {
061: new LongField(-1, 1L);
062: fail("Should have caught ArrayIndexOutOfBoundsException");
063: } catch (ArrayIndexOutOfBoundsException ignored_e) {
064:
065: // as expected
066: }
067: field = new LongField(2, 0x123456789ABCDEF0L);
068: assertEquals(0x123456789ABCDEF0L, field.get());
069: byte[] array = new byte[10];
070:
071: try {
072: new LongField(-1, 1L, array);
073: fail("Should have caught ArrayIndexOutOfBoundsException");
074: } catch (ArrayIndexOutOfBoundsException ignored_e) {
075:
076: // as expected
077: }
078: field = new LongField(2, 0x123456789ABCDEF0L, array);
079: assertEquals(0x123456789ABCDEF0L, field.get());
080: assertEquals((byte) 0xF0, array[2]);
081: assertEquals((byte) 0xDE, array[3]);
082: assertEquals((byte) 0xBC, array[4]);
083: assertEquals((byte) 0x9A, array[5]);
084: assertEquals((byte) 0x78, array[6]);
085: assertEquals((byte) 0x56, array[7]);
086: assertEquals((byte) 0x34, array[8]);
087: assertEquals((byte) 0x12, array[9]);
088: array = new byte[9];
089: try {
090: new LongField(2, 5L, array);
091: fail("should have gotten ArrayIndexOutOfBoundsException");
092: } catch (ArrayIndexOutOfBoundsException ignored_e) {
093:
094: // as expected
095: }
096: for (int j = 0; j < _test_array.length; j++) {
097: array = new byte[8];
098: new LongField(0, _test_array[j], array);
099: assertEquals(_test_array[j], new LongField(0, array).get());
100: }
101: }
102:
103: /**
104: * Test set() methods
105: */
106:
107: public void testSet() {
108: LongField field = new LongField(0);
109: byte[] array = new byte[8];
110:
111: for (int j = 0; j < _test_array.length; j++) {
112: field.set(_test_array[j]);
113: assertEquals("testing _1 " + j, _test_array[j], field.get());
114: field = new LongField(0);
115: field.set(_test_array[j], array);
116: assertEquals("testing _2 ", _test_array[j], field.get());
117: assertEquals("testing _3.0 " + _test_array[j],
118: (byte) (_test_array[j] % 256), array[0]);
119: assertEquals("testing _3.1 " + _test_array[j],
120: (byte) ((_test_array[j] >> 8) % 256), array[1]);
121: assertEquals("testing _3.2 " + _test_array[j],
122: (byte) ((_test_array[j] >> 16) % 256), array[2]);
123: assertEquals("testing _3.3 " + _test_array[j],
124: (byte) ((_test_array[j] >> 24) % 256), array[3]);
125: assertEquals("testing _3.4 " + _test_array[j],
126: (byte) ((_test_array[j] >> 32) % 256), array[4]);
127: assertEquals("testing _3.5 " + _test_array[j],
128: (byte) ((_test_array[j] >> 40) % 256), array[5]);
129: assertEquals("testing _3.6 " + _test_array[j],
130: (byte) ((_test_array[j] >> 48) % 256), array[6]);
131: assertEquals("testing _3.7 " + _test_array[j],
132: (byte) ((_test_array[j] >> 56) % 256), array[7]);
133: }
134: }
135:
136: /**
137: * Test readFromBytes
138: */
139:
140: public void testReadFromBytes() {
141: LongField field = new LongField(1);
142: byte[] array = new byte[8];
143:
144: try {
145: field.readFromBytes(array);
146: fail("should have caught ArrayIndexOutOfBoundsException");
147: } catch (ArrayIndexOutOfBoundsException ignored_e) {
148:
149: // as expected
150: }
151: field = new LongField(0);
152: for (int j = 0; j < _test_array.length; j++) {
153: array[0] = (byte) (_test_array[j] % 256);
154: array[1] = (byte) ((_test_array[j] >> 8) % 256);
155: array[2] = (byte) ((_test_array[j] >> 16) % 256);
156: array[3] = (byte) ((_test_array[j] >> 24) % 256);
157: array[4] = (byte) ((_test_array[j] >> 32) % 256);
158: array[5] = (byte) ((_test_array[j] >> 40) % 256);
159: array[6] = (byte) ((_test_array[j] >> 48) % 256);
160: array[7] = (byte) ((_test_array[j] >> 56) % 256);
161: field.readFromBytes(array);
162: assertEquals("testing " + j, _test_array[j], field.get());
163: }
164: }
165:
166: /**
167: * Test readFromStream
168: *
169: * @exception IOException
170: */
171:
172: public void testReadFromStream() throws IOException {
173: LongField field = new LongField(0);
174: byte[] buffer = new byte[_test_array.length * 8];
175:
176: for (int j = 0; j < _test_array.length; j++) {
177: buffer[(j * 8) + 0] = (byte) (_test_array[j] % 256);
178: buffer[(j * 8) + 1] = (byte) ((_test_array[j] >> 8) % 256);
179: buffer[(j * 8) + 2] = (byte) ((_test_array[j] >> 16) % 256);
180: buffer[(j * 8) + 3] = (byte) ((_test_array[j] >> 24) % 256);
181: buffer[(j * 8) + 4] = (byte) ((_test_array[j] >> 32) % 256);
182: buffer[(j * 8) + 5] = (byte) ((_test_array[j] >> 40) % 256);
183: buffer[(j * 8) + 6] = (byte) ((_test_array[j] >> 48) % 256);
184: buffer[(j * 8) + 7] = (byte) ((_test_array[j] >> 56) % 256);
185: }
186: ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
187:
188: for (int j = 0; j < buffer.length / 8; j++) {
189: field.readFromStream(stream);
190: assertEquals("Testing " + j, _test_array[j], field.get());
191: }
192: }
193:
194: /**
195: * test writeToBytes
196: */
197:
198: public void testWriteToBytes() {
199: LongField field = new LongField(0);
200: byte[] array = new byte[8];
201:
202: for (int j = 0; j < _test_array.length; j++) {
203: field.set(_test_array[j]);
204: field.writeToBytes(array);
205: long val = ((long) array[7]) << 56;
206:
207: val &= 0xFF00000000000000L;
208: val += (((long) array[6]) << 48) & 0x00FF000000000000L;
209: val += (((long) array[5]) << 40) & 0x0000FF0000000000L;
210: val += (((long) array[4]) << 32) & 0x000000FF00000000L;
211: val += (((long) array[3]) << 24) & 0x00000000FF000000L;
212: val += (((long) array[2]) << 16) & 0x0000000000FF0000L;
213: val += (((long) array[1]) << 8) & 0x000000000000FF00L;
214: val += (array[0] & 0x00000000000000FFL);
215: assertEquals("testing ", _test_array[j], val);
216: }
217: }
218:
219: /**
220: * Main
221: *
222: * @param args
223: */
224:
225: public static void main(String[] args) {
226: System.out.println("Testing util.LongField functionality");
227: junit.textui.TestRunner.run(TestLongField.class);
228: }
229: }
|