1 17 package org.alfresco.repo.search.impl.lucene.analysis; 18 19 import junit.framework.TestCase; 20 21 public class NumericEncodingTest extends TestCase 22 { 23 24 public NumericEncodingTest() 25 { 26 super(); 27 } 28 29 public NumericEncodingTest(String arg0) 30 { 31 super(arg0); 32 } 33 34 38 public void xtestAllIntegerEncodings() 39 { 40 String lastString = null; 41 String nextString = null; 42 for (long i = Integer.MIN_VALUE; i <= Integer.MAX_VALUE; i++) 43 { 44 nextString = NumericEncoder.encode((int) i); 45 if (lastString != null) 46 { 47 assertFalse(lastString.compareTo(nextString) > 0); 48 } 49 lastString = nextString; 50 } 51 } 52 53 57 public void xtestAllFloatEncodings() 58 { 59 Float last = null; 60 Float next = null; 61 String lastString = null; 62 String nextString = null; 63 64 for (int sign = 1; sign >= 0; sign--) 65 { 66 if (sign == 0) 67 { 68 for (int exponent = 0; exponent <= 0xFF; exponent++) 69 { 70 for (int mantissa = 0; mantissa <= 0x007FFFFF; mantissa++) 71 { 72 int bitPattern = sign << 31 | exponent << 23 | mantissa; 73 next = Float.intBitsToFloat(bitPattern); 74 75 if (!next.equals(Float.NaN) && (last != null) && (last.compareTo(next) > 0)) 76 { 77 System.err.println(last + " > " + next); 78 } 79 if (!next.equals(Float.NaN)) 80 { 81 nextString = NumericEncoder.encode(next); 82 if ((lastString != null) && (lastString.compareTo(nextString) > 0)) 83 { 84 System.err.println(lastString + " > " + nextString); 85 } 86 lastString = nextString; 87 } 88 last = next; 89 90 } 91 } 92 } 93 else 94 { 95 for (int exponent = 0xFF; exponent >= 0; exponent--) 96 { 97 for (int mantissa = 0x007FFFFF; mantissa >= 0; mantissa--) 98 { 99 int bitPattern = sign << 31 | exponent << 23 | mantissa; 100 next = Float.intBitsToFloat(bitPattern); 101 if (!next.equals(Float.NaN) && (last != null) && (last.compareTo(next) > 0)) 102 { 103 System.err.println(last + " > " + next); 104 } 105 if (!next.equals(Float.NaN)) 106 { 107 nextString = NumericEncoder.encode(next); 108 if ((lastString != null) && (lastString.compareTo(nextString) > 0)) 109 { 110 System.err.println(lastString + " > " + nextString); 111 } 112 lastString = nextString; 113 } 114 last = next; 115 } 116 } 117 } 118 } 119 } 120 121 124 125 public void testIntegerEncoding() 126 { 127 assertEquals("00000000", NumericEncoder.encode(Integer.MIN_VALUE)); 128 assertEquals("00000001", NumericEncoder.encode(Integer.MIN_VALUE + 1)); 129 assertEquals("7fffffff", NumericEncoder.encode(-1)); 130 assertEquals("80000000", NumericEncoder.encode(0)); 131 assertEquals("80000001", NumericEncoder.encode(1)); 132 assertEquals("fffffffe", NumericEncoder.encode(Integer.MAX_VALUE - 1)); 133 assertEquals("ffffffff", NumericEncoder.encode(Integer.MAX_VALUE)); 134 } 135 136 139 140 public void testLongEncoding() 141 { 142 assertEquals("0000000000000000", NumericEncoder.encode(Long.MIN_VALUE)); 143 assertEquals("0000000000000001", NumericEncoder.encode(Long.MIN_VALUE + 1)); 144 assertEquals("7fffffffffffffff", NumericEncoder.encode(-1L)); 145 assertEquals("8000000000000000", NumericEncoder.encode(0L)); 146 assertEquals("8000000000000001", NumericEncoder.encode(1L)); 147 assertEquals("fffffffffffffffe", NumericEncoder.encode(Long.MAX_VALUE - 1)); 148 assertEquals("ffffffffffffffff", NumericEncoder.encode(Long.MAX_VALUE)); 149 } 150 151 154 155 public void testFloatEncoding() 156 { 157 assertEquals("007fffff", NumericEncoder.encode(Float.NEGATIVE_INFINITY)); 158 assertEquals("00800000", NumericEncoder.encode(-Float.MAX_VALUE)); 159 assertEquals("7ffffffe", NumericEncoder.encode(-Float.MIN_VALUE)); 160 assertEquals("7fffffff", NumericEncoder.encode(-0f)); 161 assertEquals("80000000", NumericEncoder.encode(0f)); 162 assertEquals("80000001", NumericEncoder.encode(Float.MIN_VALUE)); 163 assertEquals("ff7fffff", NumericEncoder.encode(Float.MAX_VALUE)); 164 assertEquals("ff800000", NumericEncoder.encode(Float.POSITIVE_INFINITY)); 165 assertEquals("ffc00000", NumericEncoder.encode(Float.NaN)); 166 167 } 168 169 172 173 public void testDoubleEncoding() 174 { 175 assertEquals("000fffffffffffff", NumericEncoder.encode(Double.NEGATIVE_INFINITY)); 176 assertEquals("0010000000000000", NumericEncoder.encode(-Double.MAX_VALUE)); 177 assertEquals("7ffffffffffffffe", NumericEncoder.encode(-Double.MIN_VALUE)); 178 assertEquals("7fffffffffffffff", NumericEncoder.encode(-0d)); 179 assertEquals("8000000000000000", NumericEncoder.encode(0d)); 180 assertEquals("8000000000000001", NumericEncoder.encode(Double.MIN_VALUE)); 181 assertEquals("ffefffffffffffff", NumericEncoder.encode(Double.MAX_VALUE)); 182 assertEquals("fff0000000000000", NumericEncoder.encode(Double.POSITIVE_INFINITY)); 183 assertEquals("fff8000000000000", NumericEncoder.encode(Double.NaN)); 184 185 } 186 } 187 | Popular Tags |