pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / document / TestNumberTools.java
1 package org.apache.lucene.document;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.util.LuceneTestCase;
21
22 public class TestNumberTools extends LuceneTestCase {
23     public void testNearZero() {
24         for (int i = -100; i <= 100; i++) {
25             for (int j = -100; j <= 100; j++) {
26                 subtestTwoLongs(i, j);
27             }
28         }
29     }
30
31     public void testMax() {
32         // make sure the constants convert to their equivalents
33         assertEquals(Long.MAX_VALUE, NumberTools
34                 .stringToLong(NumberTools.MAX_STRING_VALUE));
35         assertEquals(NumberTools.MAX_STRING_VALUE, NumberTools
36                 .longToString(Long.MAX_VALUE));
37
38         // test near MAX, too
39         for (long l = Long.MAX_VALUE; l > Long.MAX_VALUE - 10000; l--) {
40             subtestTwoLongs(l, l - 1);
41         }
42     }
43
44     public void testMin() {
45         // make sure the constants convert to their equivelents
46         assertEquals(Long.MIN_VALUE, NumberTools
47                 .stringToLong(NumberTools.MIN_STRING_VALUE));
48         assertEquals(NumberTools.MIN_STRING_VALUE, NumberTools
49                 .longToString(Long.MIN_VALUE));
50
51         // test near MIN, too
52         for (long l = Long.MIN_VALUE; l < Long.MIN_VALUE + 10000; l++) {
53             subtestTwoLongs(l, l + 1);
54         }
55     }
56
57     private static void subtestTwoLongs(long i, long j) {
58         // convert to strings
59         String a = NumberTools.longToString(i);
60         String b = NumberTools.longToString(j);
61
62         // are they the right length?
63         assertEquals(NumberTools.STR_SIZE, a.length());
64         assertEquals(NumberTools.STR_SIZE, b.length());
65
66         // are they the right order?
67         if (i < j) {
68             assertTrue(a.compareTo(b) < 0);
69         } else if (i > j) {
70             assertTrue(a.compareTo(b) > 0);
71         } else {
72             assertEquals(a, b);
73         }
74
75         // can we convert them back to longs?
76         long i2 = NumberTools.stringToLong(a);
77         long j2 = NumberTools.stringToLong(b);
78
79         assertEquals(i, i2);
80         assertEquals(j, j2);
81     }
82 }