forked from wasilibs/nottinygc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitmap_test.go
102 lines (94 loc) · 1.88 KB
/
bitmap_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
// Copyright wasilibs authors
// SPDX-License-Identifier: MIT
package nottinygc
import (
"fmt"
"testing"
)
func TestBitmap32Bits(t *testing.T) {
tests := []uintptr{
0,
0b1,
0b101,
0b111,
0b0001,
0b1000001,
0xFFFFFFFF,
0x11111111,
0x01010101,
0x0F0F0F0F,
}
for _, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) {
bm := newBitmap(32)
if len(bm.words) != 1 {
t.Fatalf("expected 1 word, got %v", len(bm.words))
}
for i := 0; i < 32; i++ {
if tc&(1<<i) != 0 {
bm.set(uintptr(i))
}
}
for i := 0; i < 32; i++ {
got := bm.get(uintptr(i))
if tc&(1<<i) != 0 {
if got == 0 {
t.Fatalf("expected bit %v to be set", i)
}
} else {
if got != 0 {
t.Fatalf("expected bit %v to be unset", i)
}
}
}
})
}
}
// Test for multiple words, we pick larger than 64-bits to have more than one word on Go
// as well. We don't actually run CI with Go but it can be helpful for development.
func TestBitmap128Bits(t *testing.T) {
// We'll just repeat these.
tests := []uintptr{
0,
0b1,
0b101,
0b111,
0b0001,
0b1000001,
0xFFFFFFFF,
0x11111111,
0x01010101,
0x0F0F0F0F,
}
for _, tc := range tests {
tc := tc
t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) {
bm := newBitmap(128)
if cppWordsz == 32 && len(bm.words) != 4 || cppWordsz == 64 && len(bm.words) != 2 {
t.Fatalf("got %v words", len(bm.words))
}
for j := 0; j < 4; j++ {
for i := 0; i < 32; i++ {
if tc&(1<<(32*j+i)) != 0 {
bm.set(uintptr(i))
}
}
}
for j := 0; j < 4; j++ {
for i := 0; i < 32; i++ {
got := bm.get(uintptr(32*j + i))
if tc&(1<<(32*j+i)) != 0 {
if got == 0 {
t.Fatalf("expected bit %v to be set", i)
}
} else {
if got != 0 {
t.Fatalf("expected bit %v to be unset", i)
}
}
}
}
})
}
}