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

Function TestMap_Delete

immutable_test.go:1116–1190  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1114}
1115
1116func TestMap_Delete(t *testing.T) {
1117 t.Run("Empty", func(t *testing.T) {
1118 m := NewMap[string, int](nil)
1119 other := m.Delete("foo")
1120 if m != other {
1121 t.Fatal("expected same map")
1122 }
1123 })
1124
1125 t.Run("Simple", func(t *testing.T) {
1126 m := NewMap[int, string](nil)
1127 m = m.Set(100, "foo")
1128 if v, ok := m.Get(100); !ok || v != "foo" {
1129 t.Fatalf("unexpected value: <%v,%v>", v, ok)
1130 }
1131 })
1132
1133 t.Run("Small", func(t *testing.T) {
1134 const n = 1000
1135 m := NewMap[int, int](nil)
1136 for i := 0; i < n; i++ {
1137 m = m.Set(i, i+1)
1138 }
1139 for i := range rand.New(rand.NewSource(0)).Perm(n) {
1140 m = m.Delete(i)
1141 }
1142 if m.Len() != 0 {
1143 t.Fatalf("expected no elements, got %d", m.Len())
1144 }
1145 })
1146
1147 t.Run("Large", func(t *testing.T) {
1148 if testing.Short() {
1149 t.Skip("skipping: short")
1150 }
1151 const n = 1000000
1152 m := NewMap[int, int](nil)
1153 for i := 0; i < n; i++ {
1154 m = m.Set(i, i+1)
1155 }
1156 for i := range rand.New(rand.NewSource(0)).Perm(n) {
1157 m = m.Delete(i)
1158 }
1159 if m.Len() != 0 {
1160 t.Fatalf("expected no elements, got %d", m.Len())
1161 }
1162 })
1163
1164 RunRandom(t, "Random", func(t *testing.T, rand *rand.Rand) {
1165 m := NewTestMap()
1166 for i := 0; i < 10000; i++ {
1167 switch rand.Intn(8) {
1168 case 0: // overwrite
1169 m.Set(m.ExistingKey(rand), rand.Intn(10000))
1170 case 1: // delete existing key
1171 m.Delete(m.ExistingKey(rand))
1172 case 2: // delete non-existent key.
1173 m.Delete(m.NewKey(rand))

Callers

nothing calls this directly

Calls 9

RunRandomFunction · 0.85
NewTestMapFunction · 0.85
DeleteMethod · 0.45
SetMethod · 0.45
GetMethod · 0.45
LenMethod · 0.45
ExistingKeyMethod · 0.45
NewKeyMethod · 0.45
ValidateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…