~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/backwards/src/test/org/apache/lucene/util/TestSmallFloat.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.util;
2
 
 
3
 
/**
4
 
 * Copyright 2005 The Apache Software Foundation
5
 
 *
6
 
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 
 * you may not use this file except in compliance with the License.
8
 
 * You may obtain a copy of the License at
9
 
 *
10
 
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 
 *
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
 
 */
18
 
 
19
 
public class TestSmallFloat extends LuceneTestCase {
20
 
 
21
 
  // original lucene byteToFloat
22
 
  static float orig_byteToFloat(byte b) {
23
 
    if (b == 0)                                   // zero is a special case
24
 
      return 0.0f;
25
 
    int mantissa = b & 7;
26
 
    int exponent = (b >> 3) & 31;
27
 
    int bits = ((exponent+(63-15)) << 24) | (mantissa << 21);
28
 
    return Float.intBitsToFloat(bits);
29
 
  }
30
 
 
31
 
  // original lucene floatToByte (since lucene 1.3)
32
 
  static byte orig_floatToByte_v13(float f) {
33
 
    if (f < 0.0f)                                 // round negatives up to zero
34
 
      f = 0.0f;
35
 
 
36
 
    if (f == 0.0f)                                // zero is a special case
37
 
      return 0;
38
 
 
39
 
    int bits = Float.floatToIntBits(f);           // parse float into parts
40
 
    int mantissa = (bits & 0xffffff) >> 21;
41
 
    int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
42
 
 
43
 
    if (exponent > 31) {                          // overflow: use max value
44
 
      exponent = 31;
45
 
      mantissa = 7;
46
 
    }
47
 
 
48
 
    if (exponent < 0) {                           // underflow: use min value
49
 
      exponent = 0;
50
 
      mantissa = 1;
51
 
    }
52
 
 
53
 
    return (byte)((exponent << 3) | mantissa);    // pack into a byte
54
 
  }
55
 
 
56
 
  // This is the original lucene floatToBytes (from v1.3)
57
 
  // except with the underflow detection bug fixed for values like 5.8123817E-10f
58
 
  static byte orig_floatToByte(float f) {
59
 
    if (f < 0.0f)                                 // round negatives up to zero
60
 
      f = 0.0f;
61
 
 
62
 
    if (f == 0.0f)                                // zero is a special case
63
 
      return 0;
64
 
 
65
 
    int bits = Float.floatToIntBits(f);           // parse float into parts
66
 
    int mantissa = (bits & 0xffffff) >> 21;
67
 
    int exponent = (((bits >> 24) & 0x7f) - 63) + 15;
68
 
 
69
 
    if (exponent > 31) {                          // overflow: use max value
70
 
      exponent = 31;
71
 
      mantissa = 7;
72
 
    }
73
 
 
74
 
    if (exponent < 0 || exponent == 0 && mantissa == 0) { // underflow: use min value
75
 
      exponent = 0;
76
 
      mantissa = 1;
77
 
    }
78
 
 
79
 
    return (byte)((exponent << 3) | mantissa);    // pack into a byte
80
 
  }
81
 
 
82
 
 
83
 
  public void testByteToFloat() {
84
 
    for (int i=0; i<256; i++) {
85
 
      float f1 = orig_byteToFloat((byte)i);
86
 
      float f2 = SmallFloat.byteToFloat((byte)i, 3,15);
87
 
      float f3 = SmallFloat.byte315ToFloat((byte)i);
88
 
      assertEquals(f1,f2,0.0);
89
 
      assertEquals(f2,f3,0.0);
90
 
 
91
 
      float f4 = SmallFloat.byteToFloat((byte)i,5,2);
92
 
      float f5 = SmallFloat.byte52ToFloat((byte)i);
93
 
      assertEquals(f4,f5,0.0);
94
 
    }
95
 
  }
96
 
 
97
 
  public void testFloatToByte() {
98
 
    assertEquals(0, orig_floatToByte_v13(5.8123817E-10f));       // verify the old bug (see LUCENE-2937)
99
 
    assertEquals(1, orig_floatToByte(5.8123817E-10f));           // verify it's fixed in this test code
100
 
    assertEquals(1, SmallFloat.floatToByte315(5.8123817E-10f));  // verify it's fixed
101
 
 
102
 
    // test some constants
103
 
    assertEquals(0, SmallFloat.floatToByte315(0));
104
 
    assertEquals(1, SmallFloat.floatToByte315(Float.MIN_VALUE));             // underflow rounds up to smallest positive
105
 
    assertEquals(255, SmallFloat.floatToByte315(Float.MAX_VALUE) & 0xff);    // overflow rounds down to largest positive
106
 
    assertEquals(255, SmallFloat.floatToByte315(Float.POSITIVE_INFINITY) & 0xff);
107
 
 
108
 
    // all negatives map to 0
109
 
    assertEquals(0, SmallFloat.floatToByte315(-Float.MIN_VALUE));
110
 
    assertEquals(0, SmallFloat.floatToByte315(-Float.MAX_VALUE));
111
 
    assertEquals(0, SmallFloat.floatToByte315(Float.NEGATIVE_INFINITY));
112
 
 
113
 
 
114
 
    // up iterations for more exhaustive test after changing something
115
 
    int num = atLeast(100000);
116
 
    for (int i = 0; i < num; i++) {
117
 
      float f = Float.intBitsToFloat(random.nextInt());
118
 
      if (Float.isNaN(f)) continue;    // skip NaN
119
 
      byte b1 = orig_floatToByte(f);
120
 
      byte b2 = SmallFloat.floatToByte(f,3,15);
121
 
      byte b3 = SmallFloat.floatToByte315(f);
122
 
      assertEquals(b1,b2);
123
 
      assertEquals(b2,b3);
124
 
 
125
 
      byte b4 = SmallFloat.floatToByte(f,5,2);
126
 
      byte b5 = SmallFloat.floatToByte52(f);
127
 
      assertEquals(b4,b5);
128
 
    }
129
 
  }
130
 
 
131
 
  /***
132
 
  // Do an exhaustive test of all possible floating point values
133
 
  // for the 315 float against the original norm encoding in Similarity.
134
 
  // Takes 75 seconds on my Pentium4 3GHz, with Java5 -server
135
 
  public void testAllFloats() {
136
 
    for(int i = Integer.MIN_VALUE;;i++) {
137
 
      float f = Float.intBitsToFloat(i);
138
 
      if (f==f) { // skip non-numbers
139
 
        byte b1 = orig_floatToByte(f);
140
 
        byte b2 = SmallFloat.floatToByte315(f);
141
 
        if (b1!=b2 || b2==0 && f>0) {
142
 
          fail("Failed floatToByte315 for float " + f + " source bits="+Integer.toHexString(i) + " float raw bits=" + Integer.toHexString(Float.floatToRawIntBits(i)));
143
 
        }
144
 
      }
145
 
      if (i==Integer.MAX_VALUE) break;
146
 
    }
147
 
  }
148
 
  ***/
149
 
 
150
 
}