MCPcopy Create free account
hub / github.com/benbjohnson/immutable / TestMap_LimitedHash

Function TestMap_LimitedHash

immutable_test.go:1193–1298  ·  view source on GitHub ↗

Ensure map works even with hash conflicts.

(t *testing.T)

Source from the content-addressed store, hash-verified

1191
1192// Ensure map works even with hash conflicts.
1193func TestMap_LimitedHash(t *testing.T) {
1194 if testing.Short() {
1195 t.Skip("skipping: short")
1196 }
1197
1198 t.Run("Immutable", func(t *testing.T) {
1199 h := mockHasher[int]{
1200 hash: func(value int) uint32 { return hashUint64(uint64(value)) % 0xFF },
1201 equal: func(a, b int) bool { return a == b },
1202 }
1203 m := NewMap[int, int](&h)
1204
1205 rand := rand.New(rand.NewSource(0))
1206 keys := rand.Perm(100000)
1207 for _, i := range keys {
1208 m = m.Set(i, i) // initial set
1209 }
1210 for i := range keys {
1211 m = m.Set(i, i*2) // overwrite
1212 }
1213 if m.Len() != len(keys) {
1214 t.Fatalf("unexpected len: %d", m.Len())
1215 }
1216
1217 // Verify all key/value pairs in map.
1218 for i := 0; i < m.Len(); i++ {
1219 if v, ok := m.Get(i); !ok || v != i*2 {
1220 t.Fatalf("Get(%d)=<%v,%v>", i, v, ok)
1221 }
1222 }
1223
1224 // Verify iteration.
1225 itr := m.Iterator()
1226 for !itr.Done() {
1227 if k, v, ok := itr.Next(); !ok || v != k*2 {
1228 t.Fatalf("MapIterator.Next()=<%v,%v>, expected value %v", k, v, k*2)
1229 }
1230 }
1231
1232 // Verify not found works.
1233 if _, ok := m.Get(10000000); ok {
1234 t.Fatal("expected no value")
1235 }
1236
1237 // Verify delete non-existent key works.
1238 if other := m.Delete(10000000 + 1); m != other {
1239 t.Fatal("expected no change")
1240 }
1241
1242 // Remove all keys.
1243 for _, key := range keys {
1244 m = m.Delete(key)
1245 }
1246 if m.Len() != 0 {
1247 t.Fatalf("unexpected size: %d", m.Len())
1248 }
1249 })
1250

Callers

nothing calls this directly

Calls 8

hashUint64Function · 0.85
SetMethod · 0.45
LenMethod · 0.45
GetMethod · 0.45
IteratorMethod · 0.45
DoneMethod · 0.45
NextMethod · 0.45
DeleteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…