Skip to content

Commit

Permalink
map: Make the Examples all testable examples.
Browse files Browse the repository at this point in the history
Signed-off-by: Alun Evans <alun@badgerous.net>
  • Loading branch information
alxn committed Dec 15, 2023
1 parent 14adc78 commit 4576fa0
Showing 1 changed file with 82 additions and 17 deletions.
99 changes: 82 additions & 17 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2186,12 +2186,20 @@ func ExampleMap_perCPU() {
}
defer arr.Close()

first := []uint32{4, 5}
possibleCpus := MustPossibleCPU()

first := make([]uint32, possibleCpus)
for i := range first {
first[i] = uint32(4)
}
if err := arr.Put(uint32(0), first); err != nil {
panic(err)
}

second := []uint32{2, 8}
second := make([]uint32, possibleCpus)
for i := range second {
second[i] = uint32(5)
}
if err := arr.Put(uint32(1), second); err != nil {
panic(err)
}
Expand All @@ -2200,7 +2208,7 @@ func ExampleMap_perCPU() {
if err := arr.Lookup(uint32(0), &values); err != nil {
panic(err)
}
fmt.Println("First two values:", values[:2])
fmt.Println("First-key value[0]:", values[0])

var (
key uint32
Expand All @@ -2213,12 +2221,15 @@ func ExampleMap_perCPU() {
for _, n := range values {
sum += n
}
fmt.Printf("Sum of %d: %d\n", key, sum)
fmt.Printf("Mean of %d: %d\n", key, sum/uint32(possibleCpus))
}

if err := entries.Err(); err != nil {
panic(err)
}
// Output: First-key value[0]: 4
// Mean of 0: 4
// Mean of 1: 5
}

// It is possible to use unsafe.Pointer to avoid marshalling
Expand Down Expand Up @@ -2275,17 +2286,17 @@ func ExampleMap_NextKey() {
panic(err)
}

var firstKey string
if err := hash.NextKey(nil, &firstKey); err != nil {
keys := make([]string, 2)
if err := hash.NextKey(nil, &keys[0]); err != nil {
panic(err)
}

var nextKey string
if err := hash.NextKey(firstKey, &nextKey); err != nil {
if err := hash.NextKey(keys[0], &keys[1]); err != nil {
panic(err)
}

// Order of keys is non-deterministic due to randomized map seed
sort.Strings(keys)
fmt.Printf("Keys are %v\n", keys)
// Output: Keys are [hello world]
}

// ExampleMap_Iterate demonstrates how to iterate over all entries
Expand Down Expand Up @@ -2316,37 +2327,91 @@ func ExampleMap_Iterate() {
entries = hash.Iterate()
)

m := make(map[string]uint32)
for entries.Next(&key, &value) {
// Order of keys is non-deterministic due to randomized map seed
fmt.Printf("key: %s, value: %d\n", key, value)
m[key] = value
}

if err := entries.Err(); err != nil {
panic(fmt.Sprint("Iterator encountered an error:", err))
}

keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
fmt.Printf("key: %s, value: %d\n", k, m[k])
}
// Output: key: hello, value: 21
// key: world, value: 42
}

// It is possible to iterate nested maps and program arrays by
// unmarshaling into a *Map or *Program.
func ExampleMap_Iterate_nestedMapsAndProgramArrays() {
var arrayOfMaps *Map // Set this up somehow

spec := &MapSpec{
Type: ArrayOfMaps,
InnerMap: &MapSpec{
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
},
KeySize: 4,
ValueSize: 4,
MaxEntries: 10,
}
arrayOfMaps, err := NewMap(spec)
if err != nil {
panic(err)
}
defer arrayOfMaps.Close()
inner, err := NewMap(spec.InnerMap)
if err != nil {
panic(err)
}
defer inner.Close()
var (
key uint32
m *Map
entries = arrayOfMaps.Iterate()
)

// Make sure that the iterated map is closed after
// we are done.
defer m.Close()
if err := inner.Put(uint32(1), uint32(2)); err != nil {
panic(err)
}

if err := arrayOfMaps.Put(uint32(0), inner); err != nil {
panic(err)
}

for entries.Next(&key, &m) {
// Make sure that the iterated map is closed after
// we are done.
defer m.Close()

// Order of keys is non-deterministic due to randomized map seed
fmt.Printf("key: %v, map: %v\n", key, m)
fmt.Printf("key: %v\n", key)
items := m.Iterate()
var innerKey, innerValue uint32

for items.Next(&innerKey, &innerValue) {
fmt.Printf("\tinnerKey %v innerValue %v\n", innerKey, innerValue)
}
if err := items.Err(); err != nil {
panic(fmt.Sprint("Inner Iterator encountered an error:", err))
}
}

if err := entries.Err(); err != nil {
panic(fmt.Sprint("Iterator encountered an error:", err))
}
// Output:
// key: 0
// innerKey 0 innerValue 0
// innerKey 1 innerValue 2
}

0 comments on commit 4576fa0

Please sign in to comment.