Skip to content

Commit

Permalink
Merge pull request #5 from paul1r/paul1r/handle_32bit_system
Browse files Browse the repository at this point in the history
test: Enable test on 32bit system
  • Loading branch information
kamstrup authored Dec 18, 2024
2 parents 881e963 + 5734b6c commit cfd57b2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions map64.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type pair[K IntKey, V any] struct {
const fillFactor64 = 0.7

func phiMix64(x int) int {
h := x * 0x9E3779B9
return h ^ (h >> 16)
h := int64(x) * int64(0x9E3779B9)
return int(h ^ (h >> 16))
}

// Map is a hashmap where the keys are some any integer type.
Expand Down Expand Up @@ -251,7 +251,7 @@ func (m *Map[K, V]) Keys() iter.Seq[K] {
if m.hasZeroKey && !yield(K(0)) {
return
}

for _, p := range m.data {
if p.K != K(0) && !yield(p.K) {
return
Expand Down
50 changes: 50 additions & 0 deletions map64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,53 @@ func TestMap64Iterators(t *testing.T) {
t.Fatalf("unexpected sum when iterating over values: %d, want %d", sum, sumTo99*2)
}
}

func TestPhiMix64(t *testing.T) {
cases := []struct {
name string
input int
expected func(int) int
}{
{
name: "zero",
input: 0,
expected: func(x int) int {
return 0
},
},
{
name: "one",
input: 1,
expected: func(x int) int {
h := int64(x) * 0x9E3779B9
return int(h ^ (h >> 16))
},
},
{
name: "negative_one",
input: -1,
expected: func(x int) int {
h := int64(x) * 0x9E3779B9
return int(h ^ (h >> 16))
},
},
{
name: "large_number",
input: 1234567,
expected: func(x int) int {
h := int64(x) * 0x9E3779B9
return int(h ^ (h >> 16))
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result := phiMix64(tc.input)
expected := tc.expected(tc.input)
if result != expected {
t.Errorf("phiMix64(%d) = %d; want %d", tc.input, result, expected)
}
})
}
}

0 comments on commit cfd57b2

Please sign in to comment.