Skip to content

Commit 31ddff3

Browse files
committed
完成测试代码
1 parent e2307cd commit 31ddff3

File tree

7 files changed

+297
-2
lines changed

7 files changed

+297
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: "\U0001F91D Bug Report"
3+
about: As a User, I want to report a Bug.
4+
labels: type/bug
5+
---
6+
7+
## Bug Report
8+
9+
Please answer these questions before submitting your issue. Thanks!
10+
11+
### 1. Minimal reproduce step (Required)
12+
13+
<!-- a step by step guide for reproducing the bug. -->
14+
15+
### 2. What did you expect to see? (Required)
16+
17+
### 3. What did you see instead (Required)
18+
19+
### 4. What is your gstl version? (Required)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: "\U0001F44F Feature Request"
3+
about: As a user, I want to request a New Feature on the product.
4+
labels: type/feature-request
5+
---
6+
7+
## Feature Request
8+
9+
**Is your feature request related to a problem? Please describe:**
10+
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->
11+
12+
**Describe the feature you'd like:**
13+
<!-- A clear and concise description of what you want to happen. -->
14+
15+
**Describe alternatives you've considered:**
16+
<!-- A clear and concise description of any alternative solutions or features you've considered. -->
17+
18+
**Teachability, Documentation, Adoption, Migration Strategy:**
19+
<!-- If you can, explain some scenarios how users might use this, situations it would be helpful in. Any API designs, mockups, or diagrams are also helpful. -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: "\U0001F600 Ask a Question"
3+
about: I want to ask a question.
4+
labels: type/question
5+
---
6+
7+
## General Question
8+
9+
<!--
10+
11+
Before asking a question, make sure you have:
12+
13+
- Searched existing Stack Overflow questions.
14+
- Googled your question.
15+
- Searched open and closed [GitHub issues](https://github.com/antlabs/gstl/issues)
16+
- Read the documentation:
17+
- [gstl Readme](https://github.com/antlabs/gstl/blob/master/README.md)
18+
19+
-->

.github/workflows/go.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
9+
build:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
go: ['1.19']
14+
name: Go ${{ matrix.go }} sample
15+
16+
steps:
17+
18+
- name: Set up Go 1.19
19+
uses: actions/setup-go@v1
20+
with:
21+
go-version: ${{ matrix.go }}
22+
id: go
23+
24+
- name: Check out code into the Go module directory
25+
uses: actions/checkout@v1
26+
27+
- name: Get dependencies
28+
run: |
29+
go get -v -t -d ./...
30+
if [ -f Gopkg.toml ]; then
31+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
32+
dep ensure
33+
fi
34+
35+
- name: Build
36+
run: go build -v .
37+
38+
- name: Test
39+
run: go test -v -coverprofile='coverage.out' -covermode=count ./...
40+
41+
- name: Upload Coverage report
42+
uses: codecov/codecov-action@v1
43+
with:
44+
token: ${{secrets.CODECOV_TOKEN}}
45+
file: ./coverage.out

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
cover.cov

rwmap/rwmap.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,40 @@ type RWMap[K comparable, V any] struct {
99
m map[K]V
1010
}
1111

12+
type Pair[K comparable, V any] struct {
13+
Key K
14+
Val V
15+
}
16+
17+
// 通过new函数分配可以指定map的长度
18+
func New[K comparable, V any](l int) *RWMap[K, V] {
19+
return &RWMap[K, V]{
20+
m: make(map[K]V, l),
21+
}
22+
}
23+
24+
// 删除
1225
func (r *RWMap[K, V]) Delete(key K) {
1326
r.rw.Lock()
1427
delete(r.m, key)
1528
r.rw.Unlock()
1629
}
1730

18-
func (r *RWMap[K, V]) Load(key K) (value any, ok bool) {
31+
// 加载
32+
func (r *RWMap[K, V]) Load(key K) (value V, ok bool) {
1933
r.rw.RLock()
2034
value, ok = r.m[key]
2135
r.rw.RUnlock()
2236
return
2337

2438
}
2539

40+
// 获取值,然后并删除
2641
func (r *RWMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
2742
r.rw.Lock()
2843
if r.m == nil {
29-
r.m = make(map[K]V)
44+
r.rw.Unlock()
45+
return
3046
}
3147
value, loaded = r.m[key]
3248
delete(r.m, key)
@@ -38,6 +54,9 @@ func (r *RWMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
3854
// 不存在就保存现在的值,loaded为false
3955
func (r *RWMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
4056
r.rw.Lock()
57+
if r.m == nil {
58+
r.m = make(map[K]V)
59+
}
4160
actual, loaded = r.m[key]
4261
if !loaded {
4362
actual = value
@@ -57,12 +76,27 @@ func (r *RWMap[K, V]) Range(f func(key K, value V) bool) {
5776
r.rw.RUnlock()
5877
}
5978

79+
func (r *RWMap[K, V]) Iter() <-chan Pair[K, V] {
80+
p := make(chan Pair[K, V])
81+
go func() {
82+
r.rw.RLock()
83+
for k, v := range r.m {
84+
p <- Pair[K, V]{Key: k, Val: v}
85+
}
86+
close(p)
87+
r.rw.RUnlock()
88+
89+
}()
90+
return p
91+
}
92+
6093
// 保存值
6194
func (r *RWMap[K, V]) Store(key K, value V) {
6295
r.rw.Lock()
6396
if r.m == nil {
6497
r.m = make(map[K]V)
6598
}
99+
r.m[key] = value
66100
r.rw.Unlock()
67101
}
68102

rwmap/rwmap_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package rwmap
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"sync"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
// Store And Load
13+
func Test_StoreAndLoad(t *testing.T) {
14+
var m RWMap[string, string]
15+
m.Store("hello", "1")
16+
m.Store("world", "2")
17+
v1, ok1 := m.Load("hello")
18+
assert.Equal(t, v1, "1")
19+
assert.True(t, ok1)
20+
21+
v1, ok1 = m.Load("world")
22+
assert.Equal(t, v1, "2")
23+
assert.True(t, ok1)
24+
}
25+
26+
// Store And Load
27+
func Test_StoreDeleteLoad(t *testing.T) {
28+
var m RWMap[string, string]
29+
m.Store("hello", "1")
30+
m.Store("world", "2")
31+
32+
m.Delete("hello")
33+
m.Delete("world")
34+
35+
v1, ok1 := m.Load("hello")
36+
assert.Equal(t, v1, "")
37+
assert.False(t, ok1)
38+
39+
v1, ok1 = m.Load("world")
40+
assert.Equal(t, v1, "")
41+
assert.False(t, ok1)
42+
}
43+
44+
func Test_LoadAndDelete(t *testing.T) {
45+
46+
var m RWMap[string, string]
47+
v1, ok1 := m.LoadAndDelete("hello")
48+
49+
assert.Equal(t, v1, "")
50+
assert.False(t, ok1)
51+
52+
m.Store("hello", "world")
53+
v1, ok1 = m.Load("hello")
54+
55+
assert.Equal(t, v1, "world")
56+
57+
v1, ok1 = m.LoadAndDelete("hello")
58+
assert.Equal(t, v1, "world")
59+
assert.True(t, ok1)
60+
}
61+
62+
func Test_loadOrStore(t *testing.T) {
63+
var m RWMap[string, string]
64+
var m2 sync.Map
65+
v1, ok1 := m.LoadOrStore("hello", "world")
66+
v2, ok2 := m2.LoadOrStore("hello", "world")
67+
68+
assert.Equal(t, ok1, ok2)
69+
assert.Equal(t, v1, v2.(string))
70+
}
71+
72+
func Test_RangeBreak(t *testing.T) {
73+
var m RWMap[string, string]
74+
m.Store("1", "1")
75+
m.Store("2", "2")
76+
77+
count := 0
78+
m.Range(func(key, val string) bool {
79+
count++
80+
return false
81+
})
82+
83+
assert.Equal(t, count, 1)
84+
}
85+
86+
func Test_Range(t *testing.T) {
87+
var m RWMap[string, string]
88+
max := 5
89+
keyAll := []string{}
90+
valAll := []string{}
91+
92+
for i := 1; i < max; i++ {
93+
key := fmt.Sprintf("%dk", i)
94+
val := fmt.Sprintf("%dv", i)
95+
keyAll = append(keyAll, key)
96+
valAll = append(valAll, val)
97+
m.Store(key, val)
98+
}
99+
100+
gotKey := []string{}
101+
gotVal := []string{}
102+
m.Range(func(key, val string) bool {
103+
gotKey = append(gotKey, key)
104+
gotVal = append(gotVal, val)
105+
return true
106+
})
107+
108+
sort.Strings(gotKey)
109+
sort.Strings(gotVal)
110+
111+
assert.Equal(t, keyAll, gotKey)
112+
assert.Equal(t, valAll, gotVal)
113+
}
114+
115+
func Test_Iter(t *testing.T) {
116+
var m RWMap[string, string]
117+
max := 5
118+
keyAll := []string{}
119+
valAll := []string{}
120+
121+
for i := 1; i < max; i++ {
122+
key := fmt.Sprintf("%dk", i)
123+
val := fmt.Sprintf("%dv", i)
124+
keyAll = append(keyAll, key)
125+
valAll = append(valAll, val)
126+
m.Store(key, val)
127+
}
128+
129+
gotKey := []string{}
130+
gotVal := []string{}
131+
for pair := range m.Iter() {
132+
133+
gotKey = append(gotKey, pair.Key)
134+
gotVal = append(gotVal, pair.Val)
135+
}
136+
137+
sort.Strings(gotKey)
138+
sort.Strings(gotVal)
139+
140+
assert.Equal(t, keyAll, gotKey)
141+
assert.Equal(t, valAll, gotVal)
142+
}
143+
144+
func Test_Len(t *testing.T) {
145+
var m RWMap[string, string]
146+
m.Store("1", "1")
147+
m.Store("2", "2")
148+
m.Store("3", "3")
149+
assert.Equal(t, m.Len(), 3)
150+
}
151+
152+
func Test_New(t *testing.T) {
153+
m := New[string, string](3)
154+
m.Store("1", "1")
155+
m.Store("2", "2")
156+
m.Store("3", "3")
157+
assert.Equal(t, m.Len(), 3)
158+
}

0 commit comments

Comments
 (0)