Commit 68416710 authored by Dong-hee Na's avatar Dong-hee Na Committed by Dylan Trotter

Implement complex hash. (#293)

parent 0def2c25
...@@ -72,6 +72,7 @@ func initComplexType(dict map[string]*Object) { ...@@ -72,6 +72,7 @@ func initComplexType(dict map[string]*Object) {
ComplexType.slots.Eq = &binaryOpSlot{complexEq} ComplexType.slots.Eq = &binaryOpSlot{complexEq}
ComplexType.slots.GE = &binaryOpSlot{complexCompareNotSupported} ComplexType.slots.GE = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.GT = &binaryOpSlot{complexCompareNotSupported} ComplexType.slots.GT = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.Hash = &unaryOpSlot{complexHash}
ComplexType.slots.LE = &binaryOpSlot{complexCompareNotSupported} ComplexType.slots.LE = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.LT = &binaryOpSlot{complexCompareNotSupported} ComplexType.slots.LT = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.NE = &binaryOpSlot{complexNE} ComplexType.slots.NE = &binaryOpSlot{complexNE}
...@@ -130,3 +131,12 @@ func complexCoerce(o *Object) (complex128, bool) { ...@@ -130,3 +131,12 @@ func complexCoerce(o *Object) (complex128, bool) {
} }
return complex(floatO, 0.0), true return complex(floatO, 0.0), true
} }
func complexHash(f *Frame, o *Object) (*Object, *BaseException) {
v := toComplexUnsafe(o).Value()
hashCombined := hashFloat(real(v)) + 1000003*hashFloat(imag(v))
if hashCombined == -1 {
hashCombined = -2
}
return NewInt(hashCombined).ToObject(), nil
}
...@@ -93,3 +93,18 @@ func TestComplexRepr(t *testing.T) { ...@@ -93,3 +93,18 @@ func TestComplexRepr(t *testing.T) {
} }
} }
} }
func TestComplexHash(t *testing.T) {
cases := []invokeTestCase{
{args: wrapArgs(complex(0.0, 0.0)), want: NewInt(0).ToObject()},
{args: wrapArgs(complex(0.0, 1.0)), want: NewInt(1000003).ToObject()},
{args: wrapArgs(complex(1.0, 0.0)), want: NewInt(1).ToObject()},
{args: wrapArgs(complex(3.1, -4.2)), want: NewInt(-1556830019620134).ToObject()},
{args: wrapArgs(complex(3.1, 4.2)), want: NewInt(1557030815934348).ToObject()},
}
for _, cas := range cases {
if err := runInvokeTestCase(wrapFuncForTest(complexHash), &cas); err != "" {
t.Error(err)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment