Move the FormulaEvaluator code out of scratchpad
[poi.git] / src / testcases / org / apache / poi / util / TestShortField.java
blob999a735f881e5c41e6306a605bb07f340ce7976e
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
20 package org.apache.poi.util;
22 import junit.framework.*;
24 import java.io.*;
26 /**
27 * Test ShortField code
29 * @author Marc Johnson (mjohnson at apache dot org)
32 public class TestShortField
33 extends TestCase
36 /**
37 * Constructor
39 * @param name
42 public TestShortField(String name)
44 super(name);
47 static private final short[] _test_array =
49 Short.MIN_VALUE, ( short ) -1, ( short ) 0, ( short ) 1,
50 Short.MAX_VALUE
53 /**
54 * Test constructors.
57 public void testConstructors()
59 try
61 new ShortField(-1);
62 fail("Should have caught ArrayIndexOutOfBoundsException");
64 catch (ArrayIndexOutOfBoundsException ignored_e)
67 // as expected
69 ShortField field = new ShortField(2);
71 assertEquals(0, field.get());
72 try
74 new ShortField(-1, ( short ) 1);
75 fail("Should have caught ArrayIndexOutOfBoundsException");
77 catch (ArrayIndexOutOfBoundsException ignored_e)
80 // as expected
82 field = new ShortField(2, ( short ) 0x1234);
83 assertEquals(0x1234, field.get());
84 byte[] array = new byte[ 4 ];
86 try
88 new ShortField(-1, ( short ) 1, array);
89 fail("Should have caught ArrayIndexOutOfBoundsException");
91 catch (ArrayIndexOutOfBoundsException ignored_e)
94 // as expected
96 field = new ShortField(2, ( short ) 0x1234, array);
97 assertEquals(( short ) 0x1234, field.get());
98 assertEquals(( byte ) 0x34, array[ 2 ]);
99 assertEquals(( byte ) 0x12, array[ 3 ]);
100 array = new byte[ 3 ];
103 new ShortField(2, ( short ) 5, array);
104 fail("should have gotten ArrayIndexOutOfBoundsException");
106 catch (ArrayIndexOutOfBoundsException ignored_e)
109 // as expected
111 for (int j = 0; j < _test_array.length; j++)
113 array = new byte[ 2 ];
114 new ShortField(0, _test_array[ j ], array);
115 assertEquals(_test_array[ j ], new ShortField(0, array).get());
120 * Test set() methods
123 public void testSet()
125 ShortField field = new ShortField(0);
126 byte[] array = new byte[ 2 ];
128 for (int j = 0; j < _test_array.length; j++)
130 field.set(_test_array[ j ]);
131 assertEquals("testing _1 " + j, _test_array[ j ], field.get());
132 field = new ShortField(0);
133 field.set(_test_array[ j ], array);
134 assertEquals("testing _2 ", _test_array[ j ], field.get());
135 assertEquals("testing _3.0 " + _test_array[ j ],
136 ( byte ) (_test_array[ j ] % 256), array[ 0 ]);
137 assertEquals("testing _3.1 " + _test_array[ j ],
138 ( byte ) ((_test_array[ j ] >> 8) % 256),
139 array[ 1 ]);
144 * Test readFromBytes
147 public void testReadFromBytes()
149 ShortField field = new ShortField(1);
150 byte[] array = new byte[ 2 ];
154 field.readFromBytes(array);
155 fail("should have caught ArrayIndexOutOfBoundsException");
157 catch (ArrayIndexOutOfBoundsException ignored_e)
160 // as expected
162 field = new ShortField(0);
163 for (int j = 0; j < _test_array.length; j++)
165 array[ 0 ] = ( byte ) (_test_array[ j ] % 256);
166 array[ 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
167 field.readFromBytes(array);
168 assertEquals("testing " + j, _test_array[ j ], field.get());
173 * Test readFromStream
175 * @exception IOException
178 public void testReadFromStream()
179 throws IOException
181 ShortField field = new ShortField(0);
182 byte[] buffer = new byte[ _test_array.length * 2 ];
184 for (int j = 0; j < _test_array.length; j++)
186 buffer[ (j * 2) + 0 ] = ( byte ) (_test_array[ j ] % 256);
187 buffer[ (j * 2) + 1 ] = ( byte ) ((_test_array[ j ] >> 8) % 256);
189 ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
191 for (int j = 0; j < buffer.length / 2; j++)
193 field.readFromStream(stream);
194 assertEquals("Testing " + j, _test_array[ j ], field.get());
199 * test writeToBytes
202 public void testWriteToBytes()
204 ShortField field = new ShortField(0);
205 byte[] array = new byte[ 2 ];
207 for (int j = 0; j < _test_array.length; j++)
209 field.set(_test_array[ j ]);
210 field.writeToBytes(array);
211 short val = ( short ) (array[ 1 ] << 8);
213 val &= ( short ) 0xFF00;
214 val += ( short ) (array[ 0 ] & 0x00FF);
215 assertEquals("testing ", _test_array[ j ], val);
220 * Main
222 * @param args
225 public static void main(String [] args)
227 System.out.println("Testing util.ShortField functionality");
228 junit.textui.TestRunner.run(TestShortField.class);