diff options
author | Russ Cox <rsc@golang.org> | 2012-01-26 16:25:07 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2012-01-26 16:25:07 -0500 |
commit | 408f0b1f7459ebcbc74ad5936950072796fe449a (patch) | |
tree | 8b13422bc016157ca6a612913f638ff9bfc93446 /test/map.go | |
parent | 109a9763550aac3071e30f6e13cb5ec1172aa017 (diff) | |
download | go-408f0b1f7459ebcbc74ad5936950072796fe449a.tar.gz go-408f0b1f7459ebcbc74ad5936950072796fe449a.zip |
gc, runtime: handle floating point map keys
Fixes #2609.
R=ken2
CC=golang-dev
https://golang.org/cl/5572069
Diffstat (limited to 'test/map.go')
-rw-r--r-- | test/map.go | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/test/map.go b/test/map.go index c3963499bc..1c66986299 100644 --- a/test/map.go +++ b/test/map.go @@ -8,6 +8,7 @@ package main import ( "fmt" + "math" "strconv" ) @@ -488,4 +489,160 @@ func main() { for _, _ = range mnil { panic("range mnil") } + + testfloat() +} + +func testfloat() { + // Test floating point numbers in maps. + // Two map keys refer to the same entry if the keys are ==. + // The special cases, then, are that +0 == -0 and that NaN != NaN. + + { + var ( + pz = float32(0) + nz = math.Float32frombits(1 << 31) + nana = float32(math.NaN()) + nanb = math.Float32frombits(math.Float32bits(nana) ^ 2) + ) + + m := map[float32]string{ + pz: "+0", + nana: "NaN", + nanb: "NaN", + } + if m[pz] != "+0" { + fmt.Println("float32 map cannot read back m[+0]:", m[pz]) + } + if m[nz] != "+0" { + fmt.Println("float32 map does not treat", pz, "and", nz, "as equal for read") + fmt.Println("float32 map does not treat -0 and +0 as equal for read") + } + m[nz] = "-0" + if m[pz] != "-0" { + fmt.Println("float32 map does not treat -0 and +0 as equal for write") + } + if _, ok := m[nana]; ok { + fmt.Println("float32 map allows NaN lookup (a)") + } + if _, ok := m[nanb]; ok { + fmt.Println("float32 map allows NaN lookup (b)") + } + if len(m) != 3 { + fmt.Println("float32 map should have 3 entries:", m) + } + m[nana] = "NaN" + m[nanb] = "NaN" + if len(m) != 5 { + fmt.Println("float32 map should have 5 entries:", m) + } + } + + { + var ( + pz = float64(0) + nz = math.Float64frombits(1 << 63) + nana = float64(math.NaN()) + nanb = math.Float64frombits(math.Float64bits(nana) ^ 2) + ) + + m := map[float64]string{ + pz: "+0", + nana: "NaN", + nanb: "NaN", + } + if m[nz] != "+0" { + fmt.Println("float64 map does not treat -0 and +0 as equal for read") + } + m[nz] = "-0" + if m[pz] != "-0" { + fmt.Println("float64 map does not treat -0 and +0 as equal for write") + } + if _, ok := m[nana]; ok { + fmt.Println("float64 map allows NaN lookup (a)") + } + if _, ok := m[nanb]; ok { + fmt.Println("float64 map allows NaN lookup (b)") + } + if len(m) != 3 { + fmt.Println("float64 map should have 3 entries:", m) + } + m[nana] = "NaN" + m[nanb] = "NaN" + if len(m) != 5 { + fmt.Println("float64 map should have 5 entries:", m) + } + } + + { + var ( + pz = complex64(0) + nz = complex(0, math.Float32frombits(1<<31)) + nana = complex(5, float32(math.NaN())) + nanb = complex(5, math.Float32frombits(math.Float32bits(float32(math.NaN()))^2)) + ) + + m := map[complex64]string{ + pz: "+0", + nana: "NaN", + nanb: "NaN", + } + if m[nz] != "+0" { + fmt.Println("complex64 map does not treat -0 and +0 as equal for read") + } + m[nz] = "-0" + if m[pz] != "-0" { + fmt.Println("complex64 map does not treat -0 and +0 as equal for write") + } + if _, ok := m[nana]; ok { + fmt.Println("complex64 map allows NaN lookup (a)") + } + if _, ok := m[nanb]; ok { + fmt.Println("complex64 map allows NaN lookup (b)") + } + if len(m) != 3 { + fmt.Println("complex64 map should have 3 entries:", m) + } + m[nana] = "NaN" + m[nanb] = "NaN" + if len(m) != 5 { + fmt.Println("complex64 map should have 5 entries:", m) + } + } + + { + var ( + pz = complex128(0) + nz = complex(0, math.Float64frombits(1<<63)) + nana = complex(5, float64(math.NaN())) + nanb = complex(5, math.Float64frombits(math.Float64bits(float64(math.NaN()))^2)) + ) + + m := map[complex128]string{ + pz: "+0", + nana: "NaN", + nanb: "NaN", + } + if m[nz] != "+0" { + fmt.Println("complex128 map does not treat -0 and +0 as equal for read") + } + m[nz] = "-0" + if m[pz] != "-0" { + fmt.Println("complex128 map does not treat -0 and +0 as equal for write") + } + if _, ok := m[nana]; ok { + fmt.Println("complex128 map allows NaN lookup (a)") + } + if _, ok := m[nanb]; ok { + fmt.Println("complex128 map allows NaN lookup (b)") + } + if len(m) != 3 { + fmt.Println("complex128 map should have 3 entries:", m) + } + m[nana] = "NaN" + m[nanb] = "NaN" + if len(m) != 5 { + fmt.Println("complex128 map should have 5 entries:", m) + } + } } |