Move the FormulaEvaluator code out of scratchpad
[poi.git] / src / testcases / org / apache / poi / util / TestStringUtil.java
blobb22439cd09526ac13e1aa7ae288142c398367b09
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 ==================================================================== */
19 package org.apache.poi.util;
21 import junit.framework.*;
23 import java.text.NumberFormat;
25 /**
26 * Unit test for StringUtil
28 * @author Marc Johnson (mjohnson at apache dot org
29 * @author Glen Stampoultzis (glens at apache.org)
30 * @author Sergei Kozello (sergeikozello at mail.ru)
32 public class TestStringUtil
33 extends TestCase
35 /**
36 * Creates new TestStringUtil
38 * @param name
40 public TestStringUtil( String name )
42 super( name );
45 /**
46 * test simple form of getFromUnicode
48 public void testSimpleGetFromUnicode()
50 byte[] test_data = new byte[32];
51 int index = 0;
53 for ( int k = 0; k < 16; k++ )
55 test_data[index++] = (byte) 0;
56 test_data[index++] = (byte) ( 'a' + k );
59 assertEquals( "abcdefghijklmnop",
60 StringUtil.getFromUnicodeBE( test_data ) );
63 /**
64 * test simple form of getFromUnicode with symbols with code below and more 127
66 public void testGetFromUnicodeSymbolsWithCodesMoreThan127()
68 byte[] test_data = new byte[]{0x04, 0x22,
69 0x04, 0x35,
70 0x04, 0x41,
71 0x04, 0x42,
72 0x00, 0x20,
73 0x00, 0x74,
74 0x00, 0x65,
75 0x00, 0x73,
76 0x00, 0x74,
79 assertEquals( "\u0422\u0435\u0441\u0442 test",
80 StringUtil.getFromUnicodeBE( test_data ) );
83 /**
84 * test getFromUnicodeHigh for symbols with code below and more 127
86 public void testGetFromUnicodeHighSymbolsWithCodesMoreThan127()
88 byte[] test_data = new byte[]{0x22, 0x04,
89 0x35, 0x04,
90 0x41, 0x04,
91 0x42, 0x04,
92 0x20, 0x00,
93 0x74, 0x00,
94 0x65, 0x00,
95 0x73, 0x00,
96 0x74, 0x00,
100 assertEquals( "\u0422\u0435\u0441\u0442 test",
101 StringUtil.getFromUnicodeLE( test_data ) );
105 * Test more complex form of getFromUnicode
107 public void testComplexGetFromUnicode()
109 byte[] test_data = new byte[32];
110 int index = 0;
111 for ( int k = 0; k < 16; k++ )
113 test_data[index++] = (byte) 0;
114 test_data[index++] = (byte) ( 'a' + k );
116 assertEquals( "abcdefghijklmno",
117 StringUtil.getFromUnicodeBE( test_data, 0, 15 ) );
118 assertEquals( "bcdefghijklmnop",
119 StringUtil.getFromUnicodeBE( test_data, 2, 15 ) );
122 StringUtil.getFromUnicodeBE( test_data, -1, 16 );
123 fail( "Should have caught ArrayIndexOutOfBoundsException" );
125 catch ( ArrayIndexOutOfBoundsException ignored )
127 // as expected
132 StringUtil.getFromUnicodeBE( test_data, 32, 16 );
133 fail( "Should have caught ArrayIndexOutOfBoundsException" );
135 catch ( ArrayIndexOutOfBoundsException ignored )
137 // as expected
142 StringUtil.getFromUnicodeBE( test_data, 1, 16 );
143 fail( "Should have caught IllegalArgumentException" );
145 catch ( IllegalArgumentException ignored )
147 // as expected
152 StringUtil.getFromUnicodeBE( test_data, 1, -1 );
153 fail( "Should have caught IllegalArgumentException" );
155 catch ( IllegalArgumentException ignored )
157 // as expected
162 * Test putCompressedUnicode
164 public void testPutCompressedUnicode() throws Exception
166 byte[] output = new byte[100];
167 byte[] expected_output =
169 (byte) 'H', (byte) 'e', (byte) 'l', (byte) 'l',
170 (byte) 'o', (byte) ' ', (byte) 'W', (byte) 'o',
171 (byte) 'r', (byte) 'l', (byte) 'd', (byte) 0xAE
173 String input = new String( expected_output, StringUtil.getPreferredEncoding() );
175 StringUtil.putCompressedUnicode( input, output, 0 );
176 for ( int j = 0; j < expected_output.length; j++ )
178 assertEquals( "testing offset " + j, expected_output[j],
179 output[j] );
181 StringUtil.putCompressedUnicode( input, output,
182 100 - expected_output.length );
183 for ( int j = 0; j < expected_output.length; j++ )
185 assertEquals( "testing offset " + j, expected_output[j],
186 output[100 + j - expected_output.length] );
190 StringUtil.putCompressedUnicode( input, output,
191 101 - expected_output.length );
192 fail( "Should have caught ArrayIndexOutOfBoundsException" );
194 catch ( ArrayIndexOutOfBoundsException ignored )
196 // as expected
201 * Test putUncompressedUnicode
203 public void testPutUncompressedUnicode()
205 byte[] output = new byte[100];
206 String input = "Hello World";
207 byte[] expected_output =
209 (byte) 'H', (byte) 0, (byte) 'e', (byte) 0, (byte) 'l',
210 (byte) 0, (byte) 'l', (byte) 0, (byte) 'o', (byte) 0,
211 (byte) ' ', (byte) 0, (byte) 'W', (byte) 0, (byte) 'o',
212 (byte) 0, (byte) 'r', (byte) 0, (byte) 'l', (byte) 0,
213 (byte) 'd', (byte) 0
216 StringUtil.putUnicodeLE( input, output, 0 );
217 for ( int j = 0; j < expected_output.length; j++ )
219 assertEquals( "testing offset " + j, expected_output[j],
220 output[j] );
222 StringUtil.putUnicodeLE( input, output,
223 100 - expected_output.length );
224 for ( int j = 0; j < expected_output.length; j++ )
226 assertEquals( "testing offset " + j, expected_output[j],
227 output[100 + j - expected_output.length] );
231 StringUtil.putUnicodeLE( input, output,
232 101 - expected_output.length );
233 fail( "Should have caught ArrayIndexOutOfBoundsException" );
235 catch ( ArrayIndexOutOfBoundsException ignored )
237 // as expected
242 public void testFormat()
243 throws Exception
245 assertEquals( "This is a test " + fmt( 1.2345, 2, 2 ),
246 StringUtil.format( "This is a test %2.2", new Object[]
248 new Double( 1.2345 )
249 } ) );
250 assertEquals( "This is a test " + fmt( 1.2345, -1, 3 ),
251 StringUtil.format( "This is a test %.3", new Object[]
253 new Double( 1.2345 )
254 } ) );
255 assertEquals( "This is a great test " + fmt( 1.2345, -1, 3 ),
256 StringUtil.format( "This is a % test %.3", new Object[]
258 "great", new Double( 1.2345 )
259 } ) );
260 assertEquals( "This is a test 1",
261 StringUtil.format( "This is a test %", new Object[]
263 new Integer( 1 )
264 } ) );
265 assertEquals( "This is a test 1",
266 StringUtil.format( "This is a test %", new Object[]
268 new Integer( 1 ), new Integer( 1 )
269 } ) );
270 assertEquals( "This is a test 1.x",
271 StringUtil.format( "This is a test %1.x", new Object[]
273 new Integer( 1 )
274 } ) );
275 assertEquals( "This is a test ?missing data?1.x",
276 StringUtil.format( "This is a test %1.x", new Object[]
278 } ) );
279 assertEquals( "This is a test %1.x",
280 StringUtil.format( "This is a test \\%1.x", new Object[]
282 } ) );
286 private String fmt( double num, int minIntDigits, int maxFracDigitis )
288 NumberFormat nf = NumberFormat.getInstance();
290 if ( minIntDigits != -1 )
292 nf.setMinimumIntegerDigits( minIntDigits );
294 if ( maxFracDigitis != -1 )
296 nf.setMaximumFractionDigits( maxFracDigitis );
299 return nf.format( num );
304 * main
306 * @param ignored_args
308 public static void main( String[] ignored_args )
310 System.out.println( "Testing util.StringUtil functionality" );
311 junit.textui.TestRunner.run( TestStringUtil.class );
315 * @see junit.framework.TestCase#setUp()
317 protected void setUp() throws Exception
319 super.setUp();
321 // System.setProperty()