-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcount_atomic_test.go
112 lines (91 loc) · 1.97 KB
/
bitcount_atomic_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
// Copyright (c) 2020 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package infounit_test
import (
"sync"
"testing"
"github.com/tunabay/go-infounit"
)
//
func TestAtomicAddBitCount_1(t *testing.T) {
t.Parallel()
var wg sync.WaitGroup
bc := infounit.Megabit
for i := 0; i < 10000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
infounit.AtomicAddBitCount(&bc, 100)
}()
}
wg.Wait()
exbc := infounit.Megabit + infounit.BitCount(100*10000)
if bc != exbc {
t.Errorf(`want: %s, got: %s`, exbc, bc)
}
}
//
func TestAtomicAddBitCount_2(t *testing.T) {
t.Parallel()
bc := infounit.Bit * 30000
v := infounit.AtomicAddBitCount(&bc, 999)
exbc := infounit.Bit * 30999
if bc != exbc {
t.Errorf(`want: %s, got: %s`, exbc, bc)
}
if v != exbc {
t.Errorf(`want: %s, got: %s`, exbc, v)
}
}
//
func TestAtomicSubBitCount_1(t *testing.T) {
t.Parallel()
var wg sync.WaitGroup
bc := infounit.Megabit
for i := 0; i < 10000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
infounit.AtomicSubBitCount(&bc, 10)
}()
}
wg.Wait()
exbc := infounit.Megabit - infounit.BitCount(10*10000)
if bc != exbc {
t.Errorf(`want: %s, got: %s`, exbc, bc)
}
}
//
func TestAtomicSubBitCount_2(t *testing.T) {
t.Parallel()
bc := infounit.Bit * 30000
v := infounit.AtomicSubBitCount(&bc, 999)
exbc := infounit.Bit * 29001
if bc != exbc {
t.Errorf(`want: %s, got: %s`, exbc, bc)
}
if v != exbc {
t.Errorf(`want: %s, got: %s`, exbc, v)
}
}
//
func TestAtomicLoadBitCount_1(t *testing.T) {
t.Parallel()
bc := infounit.Bit * 30000
v := infounit.AtomicLoadBitCount(&bc)
exbc := infounit.Bit * 30000
if v != exbc {
t.Errorf(`want: %s, got: %s`, exbc, bc)
}
}
//
func TestAtomicStoreBitCount_1(t *testing.T) {
t.Parallel()
bc := infounit.Bit * 30000
infounit.AtomicStoreBitCount(&bc, 12345)
exbc := infounit.Bit * 12345
if bc != exbc {
t.Errorf(`want: %s, got: %s`, exbc, bc)
}
}