Fix hash code for doubles, add unit test to compare with integer hash codes.

This commit is contained in:
James Roseborough
2009-05-22 16:41:28 +00:00
parent c4b2ab86f7
commit 623db25250
3 changed files with 23 additions and 1 deletions

BIN
bin/LuaJit0.class Normal file

Binary file not shown.

View File

@@ -47,7 +47,7 @@ public class LDouble extends LNumber {
int iz = (int) m_value; int iz = (int) m_value;
if ( iz == m_value ) return iz; if ( iz == m_value ) return iz;
long bits = Double.doubleToLongBits( m_value ); long bits = Double.doubleToLongBits( m_value );
return ( (int) bits >> 32 ) + ( (int) bits ); return ((int) (bits >> 32)) + ((int)bits);
} }
} }

View File

@@ -8,6 +8,8 @@ import junit.framework.TestCase;
import org.luaj.TestPlatform; import org.luaj.TestPlatform;
import org.luaj.lib.BaseLib; import org.luaj.lib.BaseLib;
import org.luaj.vm.LClosure; import org.luaj.vm.LClosure;
import org.luaj.vm.LDouble;
import org.luaj.vm.LInteger;
import org.luaj.vm.LPrototype; import org.luaj.vm.LPrototype;
import org.luaj.vm.LValue; import org.luaj.vm.LValue;
import org.luaj.vm.LuaState; import org.luaj.vm.LuaState;
@@ -85,4 +87,24 @@ public class SimpleTests extends TestCase {
String s = "print( 1 == b and b )\n"; String s = "print( 1 == b and b )\n";
doTest( s ); doTest( s );
} }
private static final int [] samehash = { 0, 1, -1, 2, -2, 4, 8, 16, 32, Integer.MAX_VALUE, Integer.MIN_VALUE };
private static final double [] diffhash = { .5, 1, 1.5, 1, .5, 1.5, 1.25, 2.5 };
public void testDoubleHashCode() {
for ( int i=0; i<samehash.length; i++ ) {
LInteger j = LInteger.valueOf(samehash[i]);
LDouble d = new LDouble(samehash[i]);
int hj = j.hashCode();
int hd = d.hashCode();
assertEquals(hj, hd);
}
for ( int i=0; i<diffhash.length; i+=2 ) {
LDouble c = new LDouble(diffhash[i+0]);
LDouble d = new LDouble(diffhash[i+1]);
int hc = c.hashCode();
int hd = d.hashCode();
assertTrue("hash codes are same: "+hc,hc!=hd);
}
}
} }