Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
chaocai committed Mar 11, 2019
1 parent 0d7941b commit 1920f44
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions code/ch7/map/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package my_map

import "testing"

func TestInitMap(t *testing.T) {
m1 := map[int]int{1: 1, 2: 4, 3: 9}
t.Log(m1[2])
t.Logf("len m1=%d", len(m1))
m2 := map[int]int{}
m2[4] = 16
t.Logf("len m2=%d", len(m2))
m3 := make(map[int]int, 10)
t.Logf("len m3=%d", len(m3))
}

func TestAccessNotExistingKey(t *testing.T) {
m1 := map[int]int{}
t.Log(m1[1])
m1[2] = 0
t.Log(m1[2])
m1[3] = 0
if v, ok := m1[3]; ok {
t.Logf("Key 3's value is %d", v)
} else {
t.Log("key 3 is not existing.")
}
}

func TestTravelMap(t *testing.T) {
m1 := map[int]int{1: 1, 2: 4, 3: 9}
for k, v := range m1 {
t.Log(k, v)
}
}
31 changes: 31 additions & 0 deletions code/ch8/map/map_ext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package map_ext

import "testing"

func TestMapWithFunValue(t *testing.T) {
m := map[int]func(op int) int{}
m[1] = func(op int) int { return op }
m[2] = func(op int) int { return op * op }
m[3] = func(op int) int { return op * op * op }
t.Log(m[1](2), m[2](2), m[3](2))
}

func TestMapForSet(t *testing.T) {
mySet := map[int]bool{}
mySet[1] = true
n := 3
if mySet[n] {
t.Logf("%d is existing", n)
} else {
t.Logf("%d is not existing", n)
}
mySet[3] = true
t.Log(len(mySet))
delete(mySet, 1)
n = 1
if mySet[n] {
t.Logf("%d is existing", n)
} else {
t.Logf("%d is not existing", n)
}
}
Binary file not shown.

0 comments on commit 1920f44

Please sign in to comment.