From 0096847232325af49de62c317e0736d3bbd08d2f Mon Sep 17 00:00:00 2001 From: ghosind Date: Sun, 4 Jun 2023 03:11:00 +0800 Subject: [PATCH] test: update map test cases. --- map_test.go | 100 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 17 deletions(-) diff --git a/map_test.go b/map_test.go index d304f47..1f4cee2 100644 --- a/map_test.go +++ b/map_test.go @@ -10,7 +10,7 @@ func testMapPut(t *testing.T, m Map[int, int], data []int) { m.Put(i, data[i]) } if m.Size() != len(data)/2 { - t.Errorf("HashMap.Size() returns %d, expect %d", m.Size(), len(data)/2) + t.Errorf("Map.Size() returns %d, expect %d", m.Size(), len(data)/2) } for i, v := range data { @@ -20,11 +20,11 @@ func testMapPut(t *testing.T, m Map[int, int], data []int) { expect = data[i] } if old != expect { - t.Errorf("HashMap.Put returns %d, expect %d", old, expect) + t.Errorf("Map.Put returns %d, expect %d", old, expect) } } if m.Size() != len(data) { - t.Errorf("HashMap.Size() returns %d, expect %d", m.Size(), len(data)) + t.Errorf("Map.Size() returns %d, expect %d", m.Size(), len(data)) } } @@ -33,14 +33,14 @@ func testMapGet(t *testing.T, m Map[int, int], data []int) { v, ok := m.Get(i) if i < len(data) { if !ok { - t.Errorf("HashMap not contains key %d, expect contains", i) + t.Errorf("Map not contains key %d, expect contains", i) } if v != data[i] { - t.Errorf("HashMap.Get(%d) returns %d, expect %d", i, v, data[i]) + t.Errorf("Map.Get(%d) returns %d, expect %d", i, v, data[i]) } } else { if ok { - t.Errorf("HashMap contains key %d, expect not", i) + t.Errorf("Map contains key %d, expect not", i) } } } @@ -49,11 +49,11 @@ func testMapGet(t *testing.T, m Map[int, int], data []int) { v := m.GetDefault(i, i+1) if i < len(data) { if v != data[i] { - t.Errorf("HashMap.GetDefault(%d) returns %d, expect %d", i, v, data[i]) + t.Errorf("Map.GetDefault(%d) returns %d, expect %d", i, v, data[i]) } } else { if v != i+1 { - t.Errorf("HashMap.GetDefault(%d) returns %d, expect %d", i, v, i+1) + t.Errorf("Map.GetDefault(%d) returns %d, expect %d", i, v, i+1) } } } @@ -64,11 +64,11 @@ func testMapContains(t *testing.T, m Map[int, int], data []int) { isContains := m.ContainsKey(i) if i < len(data) { if !isContains { - t.Errorf("HashMap.Contains(%d) return false, expect true", i) + t.Errorf("Map.Contains(%d) return false, expect true", i) } } else { if isContains { - t.Errorf("HashMap.Contains(%d) return true, expect false", i) + t.Errorf("Map.Contains(%d) return true, expect false", i) } } } @@ -85,11 +85,11 @@ func testMapForEach(t *testing.T, m Map[int, int], data []int) { return nil }) if err != nil { - t.Errorf("HashMap.ForEach returns %v, expect nil", err) + t.Errorf("Map.ForEach returns %v, expect nil", err) } if n != m.Size() { - t.Errorf("HashMap.ForEach run handler %d times, expect %d", n, m.Size()) + t.Errorf("Map.ForEach run handler %d times, expect %d", n, m.Size()) } } @@ -98,23 +98,83 @@ func testMapRemove(t *testing.T, m Map[int, int], data []int) { old := m.Remove(i) if i < len(data) { if old != data[i] { - t.Errorf("HashMap.Remove(%d) returns %d, expect %d", i, old, data[i]) + t.Errorf("Map.Remove(%d) returns %d, expect %d", i, old, data[i]+1) } } else if old != 0 { - t.Errorf("HashMap.Remove(%d) returns %d, expect 0", i, old) + t.Errorf("Map.Remove(%d) returns %d, expect 0", i, old) } } } func testMapClear(t *testing.T, m Map[int, int]) { if m.IsEmpty() { - t.Error("HashMap.IsEmpty() return true, expect false") + t.Error("Map.IsEmpty() return true, expect false") } m.Clear() if !m.IsEmpty() { - t.Error("HashMap.IsEmpty() return false, expect true") + t.Error("Map.IsEmpty() return false, expect true") + } +} + +func testMapKeys(t *testing.T, m Map[int, int]) { + keys := m.Keys() + if len(keys) != m.Size() { + t.Errorf("Map.Keys() return an array contains %d element, expect %d", len(keys), m.Size()) + } + + for _, k := range keys { + if !m.ContainsKey(k) { + t.Errorf("key %d not in map", k) + } + } +} + +func testMapValues(t *testing.T, m Map[int, int]) { + vals := m.Values() + if len(vals) != m.Size() { + t.Errorf("Map.Values() return an array contains %d element, expect %d", len(vals), m.Size()) + } + + valSet := NewHashSet[int]() + valSet.AddAll(vals...) + + m.ForEach(func(_, v int) error { + if !valSet.Contains(v) { + t.Errorf("Map.Values() not contains %dd", v) + } + return nil + }) +} + +func testMapReplace(t *testing.T, m Map[int, int], data []int) { + for i := 0; i < len(data)/2; i++ { + m.Put(i, data[i]) + } + + for i := 0; i < len(data); i++ { + old, ok := m.Replace(i, data[i]+1) + if i < len(data)/2 { + if !ok { + t.Errorf("Map.Replace(%d, ?) no old value found, expect return old value", i) + } else if old != data[i] { + t.Errorf("Map.Replace(%d, ?) return old value %d, expect %d", i, old, data[i]) + } + } else if ok { + t.Errorf("Map.Replace(%d, ?) found old value, expect no old value", i) + } + } + + for i := 0; i < len(data); i++ { + v := m.GetDefault(i, 0) + if i < len(data)/2 { + if v != data[i]+1 { + t.Errorf("Map.Get(%d) return %d, expect %d", i, v, data[i]+1) + } + } else if v != 0 { + t.Errorf("Map.Get(%d) return %d, expect 0", i, v) + } } } @@ -122,7 +182,7 @@ func testMap(t *testing.T, m Map[int, int]) { data := rand.Perm(10) if !m.IsEmpty() { - t.Error("HashMap.IsEmpty() return false, expect true") + t.Error("Map.IsEmpty() return false, expect true") } testMapPut(t, m, data) @@ -133,7 +193,13 @@ func testMap(t *testing.T, m Map[int, int]) { testMapForEach(t, m, data) + testMapKeys(t, m) + + testMapValues(t, m) + testMapRemove(t, m, data) testMapClear(t, m) + + testMapReplace(t, m, data) }