001: package org.apache.lucene.util;
002:
003: /**
004: * Copyright 2005 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.lucene.util.LuceneTestCase;
020: import java.util.Random;
021:
022: /**
023: * @author yonik
024: * @version $Id$
025: */
026: public class TestSmallFloat extends LuceneTestCase {
027:
028: // original lucene byteToFloat
029: static float orig_byteToFloat(byte b) {
030: if (b == 0) // zero is a special case
031: return 0.0f;
032: int mantissa = b & 7;
033: int exponent = (b >> 3) & 31;
034: int bits = ((exponent + (63 - 15)) << 24) | (mantissa << 21);
035: return Float.intBitsToFloat(bits);
036: }
037:
038: // original lucene floatToByte
039: static byte orig_floatToByte(float f) {
040: if (f < 0.0f) // round negatives up to zero
041: f = 0.0f;
042:
043: if (f == 0.0f) // zero is a special case
044: return 0;
045:
046: int bits = Float.floatToIntBits(f); // parse float into parts
047: int mantissa = (bits & 0xffffff) >> 21;
048: int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
049:
050: if (exponent > 31) { // overflow: use max value
051: exponent = 31;
052: mantissa = 7;
053: }
054:
055: if (exponent < 0) { // underflow: use min value
056: exponent = 0;
057: mantissa = 1;
058: }
059:
060: return (byte) ((exponent << 3) | mantissa); // pack into a byte
061: }
062:
063: public void testByteToFloat() {
064: for (int i = 0; i < 256; i++) {
065: float f1 = orig_byteToFloat((byte) i);
066: float f2 = SmallFloat.byteToFloat((byte) i, 3, 15);
067: float f3 = SmallFloat.byte315ToFloat((byte) i);
068: assertEquals(f1, f2, 0.0);
069: assertEquals(f2, f3, 0.0);
070:
071: float f4 = SmallFloat.byteToFloat((byte) i, 5, 2);
072: float f5 = SmallFloat.byte52ToFloat((byte) i);
073: assertEquals(f4, f5, 0.0);
074: }
075: }
076:
077: public void testFloatToByte() {
078: Random rand = new Random(0);
079: // up iterations for more exhaustive test after changing something
080: for (int i = 0; i < 100000; i++) {
081: float f = Float.intBitsToFloat(rand.nextInt());
082: if (f != f)
083: continue; // skip NaN
084: byte b1 = orig_floatToByte(f);
085: byte b2 = SmallFloat.floatToByte(f, 3, 15);
086: byte b3 = SmallFloat.floatToByte315(f);
087: assertEquals(b1, b2);
088: assertEquals(b2, b3);
089:
090: byte b4 = SmallFloat.floatToByte(f, 5, 2);
091: byte b5 = SmallFloat.floatToByte52(f);
092: assertEquals(b4, b5);
093: }
094: }
095:
096: /***
097: // Do an exhaustive test of all possible floating point values
098: // for the 315 float against the original norm encoding in Similarity.
099: // Takes 75 seconds on my Pentium4 3GHz, with Java5 -server
100: public void testAllFloats() {
101: for(int i = Integer.MIN_VALUE;;i++) {
102: float f = Float.intBitsToFloat(i);
103: if (f==f) { // skip non-numbers
104: byte b1 = orig_floatToByte(f);
105: byte b2 = SmallFloat.floatToByte315(f);
106: if (b1!=b2) {
107: TestCase.fail("Failed floatToByte315 for float " + f);
108: }
109: }
110: if (i==Integer.MAX_VALUE) break;
111: }
112: }
113: ***/
114:
115: }
|