diff --git a/map_test.go b/map_test.go index 93dd764d7..f70e6e627 100644 --- a/map_test.go +++ b/map_test.go @@ -838,6 +838,14 @@ func TestMapQueue(t *testing.T) { } var v uint32 + if err := m.Lookup(nil, &v); err != nil { + t.Fatal("Lookup (Peek) on Queue:", err) + } + if v != 42 { + t.Error("Want value 42, got", v) + } + v = 0 + if err := m.LookupAndDelete(nil, &v); err != nil { t.Fatal("Can't lookup and delete element:", err) } @@ -856,6 +864,10 @@ func TestMapQueue(t *testing.T) { if err := m.LookupAndDelete(nil, &v); !errors.Is(err, ErrKeyNotExist) { t.Fatal("Lookup and delete on empty Queue:", err) } + + if err := m.Lookup(nil, &v); !errors.Is(err, ErrKeyNotExist) { + t.Fatal("Lookup (Peek) on empty Queue:", err) + } } func TestMapInMap(t *testing.T) { @@ -1050,7 +1062,7 @@ func TestIterateEmptyMap(t *testing.T) { var key string var value uint64 - if entries.Next(&key, &value) != false { + if entries.Next(&key, &value) { t.Error("Empty hash should not be iterable") } if err := entries.Err(); err != nil { @@ -1090,12 +1102,11 @@ func TestMapIterate(t *testing.T) { } defer hash.Close() - if err := hash.Put("hello", uint32(21)); err != nil { - t.Fatal(err) - } - - if err := hash.Put("world", uint32(42)); err != nil { - t.Fatal(err) + data := []string{"hello", "world"} + for i, k := range data { + if err := hash.Put(k, uint32(i)); err != nil { + t.Fatal(err) + } } var key string @@ -1106,22 +1117,32 @@ func TestMapIterate(t *testing.T) { for entries.Next(&key, &value) { keys = append(keys, key) } - - if err := entries.Err(); err != nil { - t.Fatal(err) - } + qt.Assert(t, qt.IsNil(entries.Err())) sort.Strings(keys) + qt.Assert(t, qt.DeepEquals(keys, data)) +} - if n := len(keys); n != 2 { - t.Fatal("Expected to get 2 keys, have", n) - } - if keys[0] != "hello" { - t.Error("Expected index 0 to be hello, got", keys[0]) - } - if keys[1] != "world" { - t.Error("Expected index 1 to be hello, got", keys[1]) - } +func TestIterateWrongMap(t *testing.T) { + testutils.SkipOnOldKernel(t, "4.20", "map type queue") + + m, err := NewMap(&MapSpec{ + Type: Queue, + ValueSize: 4, + MaxEntries: 2, + Contents: []MapKV{ + {nil, uint32(0)}, + {nil, uint32(1)}, + }, + }) + qt.Assert(t, qt.IsNil(err)) + defer m.Close() + + var value uint32 + entries := m.Iterate() + + qt.Assert(t, qt.IsFalse(entries.Next(nil, &value))) + qt.Assert(t, qt.IsNotNil(entries.Err())) } func TestMapIteratorAllocations(t *testing.T) { @@ -1142,7 +1163,7 @@ func TestMapIteratorAllocations(t *testing.T) { // AllocsPerRun warms up the function for us. allocs := testing.AllocsPerRun(int(arr.MaxEntries()-1), func() { if !iter.Next(&k, &v) { - t.Fatal("Next failed") + t.Fatal("Next failed while iterating: %w", iter.Err()) } })