-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathz_test.go
120 lines (109 loc) · 2.43 KB
/
z_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2018 Massimiliano Ghilardi. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gls
import (
"testing"
)
var verbose bool = false
func AsyncGoID() <-chan uintptr {
ch := make(chan uintptr)
go func() {
ch <- GoID()
}()
return ch
}
func TestGoID(t *testing.T) {
id1 := GoID()
id2 := GoID()
if id1 == id2 {
if verbose {
t.Logf("TestGoID: 0x%x == 0x%x", id1, id2)
}
} else {
t.Errorf("TestGoID: 0x%x != 0x%x", id1, id2)
}
}
func TestAsyncGoID1(t *testing.T) {
id1 := GoID()
id2 := <-AsyncGoID()
if id1 != id2 {
if verbose {
t.Logf("TestAsyncGoID1: 0x%x != 0x%x", id1, id2)
}
} else {
t.Errorf("TestAsyncGoID1: 0x%x == 0x%x", id1, id2)
}
}
func TestAsyncGoID2(t *testing.T) {
ch1 := AsyncGoID()
ch2 := AsyncGoID()
id1 := <-ch1
id2 := <-ch2
if id1 != id2 {
if verbose {
t.Logf("TestAsyncGoID2: 0x%x != 0x%x", id1, id2)
}
} else {
t.Errorf("TestAsyncGoID2: 0x%x == 0x%x", id1, id2)
}
}
// check that Get() returns repeteable results
func TestMap1(t *testing.T) {
Set(0, '#')
v, ok := Get(0)
if ok && v == '#' {
if verbose {
t.Logf("TestMap1: expecting (%v, %v) and found (%v, %v)", '#', true, v, ok)
}
} else {
t.Errorf("TestMap1: expecting (%v, %v) but found (%v, %v)", '#', true, v, ok)
}
}
// check that changes to the map returned by GetAll()
// are visible in subsequent calls to Get() and GetAll()
func TestMap2(t *testing.T) {
m := GetAll()
m[1] = 2
v, ok := Get(1)
if ok && v == 2 {
if verbose {
t.Logf("TestMap2: expecting (%v, %v) and found (%v, %v)", 2, true, v, ok)
}
} else {
t.Errorf("TestMap2: expecting (%v, %v) but found (%v, %v)", 2, true, v, ok)
}
}
// check that changes to the map passed to SetAll()
// are visible in subsequent Get() and GetAll()
func TestMap3(t *testing.T) {
m := make(Map)
SetAll(m)
m["a"] = "b"
v, ok := Get("a")
if ok && v == "b" {
if verbose {
t.Logf("TestMap3: expecting (%v, %v) and found (%v, %v)", "b", true, v, ok)
}
} else {
t.Errorf("TestMap3: expecting (%v, %v) but found (%v, %v)", "b", true, v, ok)
}
}
// check that different goroutines get independent maps
func TestMap4(t *testing.T) {
Set(1.0, 2.0)
m1 := GetAll()
ch := make(chan Map)
go func() {
ch <- GetAll()
DelAll()
}()
m2 := <-ch
if len(m1) != len(m2) {
if verbose {
t.Logf("TestMap4: len(m1) != len(m2)")
}
} else {
t.Errorf("TestMap4: len(m1) == len(m2)")
}
}