Skip to content

Commit

Permalink
test: update map test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosind committed Jun 3, 2023
1 parent 152444b commit 0096847
Showing 1 changed file with 83 additions and 17 deletions.
100 changes: 83 additions & 17 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}

Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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())
}
}

Expand All @@ -98,31 +98,91 @@ 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)
}
}
}

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)
Expand All @@ -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)
}

0 comments on commit 0096847

Please sign in to comment.