31
31
}
32
32
)
33
33
34
+ // newHash returns a new Map of type Hash. Cleanup is handled automatically.
35
+ func newHash (t * testing.T ) * Map {
36
+ hash , err := NewMap (& MapSpec {
37
+ Type : Hash ,
38
+ KeySize : 5 ,
39
+ ValueSize : 4 ,
40
+ MaxEntries : 10 ,
41
+ })
42
+ if err != nil {
43
+ t .Fatal (err )
44
+ }
45
+ t .Cleanup (func () { hash .Close () })
46
+ return hash
47
+ }
48
+
34
49
func TestMap (t * testing.T ) {
35
50
m := createArray (t )
36
51
defer m .Close ()
@@ -1214,8 +1229,7 @@ func TestMapGuessNonExistentKey(t *testing.T) {
1214
1229
}
1215
1230
1216
1231
func TestNotExist (t * testing.T ) {
1217
- hash := createHash ()
1218
- defer hash .Close ()
1232
+ hash := newHash (t )
1219
1233
1220
1234
var tmp uint32
1221
1235
err := hash .Lookup ("hello" , & tmp )
@@ -1246,8 +1260,7 @@ func TestNotExist(t *testing.T) {
1246
1260
}
1247
1261
1248
1262
func TestExist (t * testing.T ) {
1249
- hash := createHash ()
1250
- defer hash .Close ()
1263
+ hash := newHash (t )
1251
1264
1252
1265
if err := hash .Put ("hello" , uint32 (21 )); err != nil {
1253
1266
t .Errorf ("Failed to put key/value pair into hash: %v" , err )
@@ -1611,8 +1624,8 @@ func TestMapGetNextID(t *testing.T) {
1611
1624
var next MapID
1612
1625
var err error
1613
1626
1614
- hash := createHash ()
1615
- defer hash . Close ( )
1627
+ // Ensure there is at least one map on the system.
1628
+ _ = newHash ( t )
1616
1629
1617
1630
if next , err = MapGetNextID (MapID (0 )); err != nil {
1618
1631
t .Fatal ("Can't get next ID:" , err )
@@ -1638,8 +1651,7 @@ func TestMapGetNextID(t *testing.T) {
1638
1651
}
1639
1652
1640
1653
func TestNewMapFromID (t * testing.T ) {
1641
- hash := createHash ()
1642
- defer hash .Close ()
1654
+ hash := newHash (t )
1643
1655
1644
1656
info , err := hash .Info ()
1645
1657
testutils .SkipIfNotSupported (t , err )
@@ -1985,7 +1997,15 @@ func ExampleMap_perCPU() {
1985
1997
// Note that using unsafe.Pointer is only marginally faster than
1986
1998
// implementing Marshaler on the type.
1987
1999
func ExampleMap_zeroCopy () {
1988
- hash := createHash ()
2000
+ hash , err := NewMap (& MapSpec {
2001
+ Type : Hash ,
2002
+ KeySize : 5 ,
2003
+ ValueSize : 4 ,
2004
+ MaxEntries : 10 ,
2005
+ })
2006
+ if err != nil {
2007
+ panic (err )
2008
+ }
1989
2009
defer hash .Close ()
1990
2010
1991
2011
key := [5 ]byte {'h' , 'e' , 'l' , 'l' , 'o' }
@@ -2004,7 +2024,7 @@ func ExampleMap_zeroCopy() {
2004
2024
// Output: The value is: 23
2005
2025
}
2006
2026
2007
- func createHash () * Map {
2027
+ func ExampleMap_NextKey () {
2008
2028
hash , err := NewMap (& MapSpec {
2009
2029
Type : Hash ,
2010
2030
KeySize : 5 ,
@@ -2014,11 +2034,6 @@ func createHash() *Map {
2014
2034
if err != nil {
2015
2035
panic (err )
2016
2036
}
2017
- return hash
2018
- }
2019
-
2020
- func ExampleMap_NextKey () {
2021
- hash := createHash ()
2022
2037
defer hash .Close ()
2023
2038
2024
2039
if err := hash .Put ("hello" , uint32 (21 )); err != nil {
@@ -2045,7 +2060,15 @@ func ExampleMap_NextKey() {
2045
2060
// ExampleMap_Iterate demonstrates how to iterate over all entries
2046
2061
// in a map.
2047
2062
func ExampleMap_Iterate () {
2048
- hash := createHash ()
2063
+ hash , err := NewMap (& MapSpec {
2064
+ Type : Hash ,
2065
+ KeySize : 5 ,
2066
+ ValueSize : 4 ,
2067
+ MaxEntries : 10 ,
2068
+ })
2069
+ if err != nil {
2070
+ panic (err )
2071
+ }
2049
2072
defer hash .Close ()
2050
2073
2051
2074
if err := hash .Put ("hello" , uint32 (21 )); err != nil {
0 commit comments