@@ -3,16 +3,16 @@ package descriptor_test
3
3
import (
4
4
"testing"
5
5
6
+ "github.com/tetratelabs/wazero/internal/descriptor"
6
7
"github.com/tetratelabs/wazero/internal/sys"
7
8
"github.com/tetratelabs/wazero/internal/testing/require"
8
9
)
9
10
10
11
func TestFileTable (t * testing.T ) {
11
12
table := new (sys.FileTable )
12
13
13
- if n := table .Len (); n != 0 {
14
- t .Errorf ("new table is not empty: length=%d" , n )
15
- }
14
+ n := table .Len ()
15
+ require .Equal (t , 0 , n , "new table is not empty: length=%d" , n )
16
16
17
17
// The id field is used as a sentinel value.
18
18
v0 := & sys.FileEntry {Name : "1" }
@@ -38,16 +38,12 @@ func TestFileTable(t *testing.T) {
38
38
{key : k1 , val : v1 },
39
39
{key : k2 , val : v2 },
40
40
} {
41
- if v , ok := table .Lookup (lookup .key ); ! ok {
42
- t .Errorf ("value not found for key '%v'" , lookup .key )
43
- } else if v .Name != lookup .val .Name {
44
- t .Errorf ("wrong value returned for key '%v': want=%v got=%v" , lookup .key , lookup .val .Name , v .Name )
45
- }
41
+ v , ok := table .Lookup (lookup .key )
42
+ require .True (t , ok , "value not found for key '%v'" , lookup .key )
43
+ require .Equal (t , lookup .val .Name , v .Name , "wrong value returned for key '%v'" , lookup .key )
46
44
}
47
45
48
- if n := table .Len (); n != 3 {
49
- t .Errorf ("wrong table length: want=3 got=%d" , n )
50
- }
46
+ require .Equal (t , 3 , table .Len (), "wrong table length: want=3 got=%d" , table .Len ())
51
47
52
48
k0Found := false
53
49
k1Found := false
@@ -62,9 +58,7 @@ func TestFileTable(t *testing.T) {
62
58
case k2 :
63
59
k2Found , want = true , v2
64
60
}
65
- if v .Name != want .Name {
66
- t .Errorf ("wrong value found ranging over '%v': want=%v got=%v" , k , want .Name , v .Name )
67
- }
61
+ require .Equal (t , want .Name , v .Name , "wrong value found ranging over table" )
68
62
return true
69
63
})
70
64
@@ -76,9 +70,7 @@ func TestFileTable(t *testing.T) {
76
70
{key : k1 , ok : k1Found },
77
71
{key : k2 , ok : k2Found },
78
72
} {
79
- if ! found .ok {
80
- t .Errorf ("key not found while ranging over table: %v" , found .key )
81
- }
73
+ require .True (t , found .ok , "key not found while ranging over table: %v" , found .key )
82
74
}
83
75
84
76
for i , deletion := range []struct {
@@ -89,12 +81,10 @@ func TestFileTable(t *testing.T) {
89
81
{key : k2 },
90
82
} {
91
83
table .Delete (deletion .key )
92
- if _ , ok := table .Lookup (deletion .key ); ok {
93
- t .Errorf ("item found after deletion of '%v'" , deletion .key )
94
- }
95
- if n , want := table .Len (), 3 - (i + 1 ); n != want {
96
- t .Errorf ("wrong table length after deletion: want=%d got=%d" , want , n )
97
- }
84
+ _ , ok := table .Lookup (deletion .key )
85
+ require .False (t , ok , "item found after deletion of '%v'" , deletion .key )
86
+ n , want := table .Len (), 3 - (i + 1 )
87
+ require .Equal (t , want , n , "wrong table length after deletion: want=%d got=%d" , want , n )
98
88
}
99
89
}
100
90
@@ -134,3 +124,82 @@ func BenchmarkFileTableLookup(b *testing.B) {
134
124
b .Error ("wrong file returned by lookup" )
135
125
}
136
126
}
127
+
128
+ func Test_sizeOfTable (t * testing.T ) {
129
+ tests := []struct {
130
+ name string
131
+ operation func (* descriptor.Table [int32 , string ])
132
+ expectedSize int
133
+ }{
134
+ {
135
+ name : "empty table" ,
136
+ operation : func (table * descriptor.Table [int32 , string ]) {},
137
+ expectedSize : 0 ,
138
+ },
139
+ {
140
+ name : "1 insert" ,
141
+ operation : func (table * descriptor.Table [int32 , string ]) {
142
+ table .Insert ("a" )
143
+ },
144
+ expectedSize : 1 ,
145
+ },
146
+ {
147
+ name : "32 inserts" ,
148
+ operation : func (table * descriptor.Table [int32 , string ]) {
149
+ for i := 0 ; i < 32 ; i ++ {
150
+ table .Insert ("a" )
151
+ }
152
+ },
153
+ expectedSize : 1 ,
154
+ },
155
+ {
156
+ name : "257 inserts" ,
157
+ operation : func (table * descriptor.Table [int32 , string ]) {
158
+ for i := 0 ; i < 257 ; i ++ {
159
+ table .Insert ("a" )
160
+ }
161
+ },
162
+ expectedSize : 5 ,
163
+ },
164
+ {
165
+ name : "1 insert at 63" ,
166
+ operation : func (table * descriptor.Table [int32 , string ]) {
167
+ table .InsertAt ("a" , 63 )
168
+ },
169
+ expectedSize : 1 ,
170
+ },
171
+ {
172
+ name : "1 insert at 64" ,
173
+ operation : func (table * descriptor.Table [int32 , string ]) {
174
+ table .InsertAt ("a" , 64 )
175
+ },
176
+ expectedSize : 2 ,
177
+ },
178
+ {
179
+ name : "1 insert at 257" ,
180
+ operation : func (table * descriptor.Table [int32 , string ]) {
181
+ table .InsertAt ("a" , 257 )
182
+ },
183
+ expectedSize : 5 ,
184
+ },
185
+ {
186
+ name : "insert at until 320" ,
187
+ operation : func (table * descriptor.Table [int32 , string ]) {
188
+ for i := int32 (0 ); i < 320 ; i ++ {
189
+ table .InsertAt ("a" , i )
190
+ }
191
+ },
192
+ expectedSize : 5 ,
193
+ },
194
+ }
195
+ for _ , tt := range tests {
196
+ tc := tt
197
+
198
+ t .Run (tc .name , func (t * testing.T ) {
199
+ table := new (descriptor.Table [int32 , string ])
200
+ tc .operation (table )
201
+ require .Equal (t , tc .expectedSize , len (descriptor .Masks (table )))
202
+ require .Equal (t , tc .expectedSize * 64 , len (descriptor .Items (table )))
203
+ })
204
+ }
205
+ }
0 commit comments