Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map tests queues and refactor #1559

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
smagnani96 marked this conversation as resolved.
Show resolved Hide resolved
if err := hash.Put(k, uint32(i)); err != nil {
t.Fatal(err)
}
}

var key string
smagnani96 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -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) {
smagnani96 marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand All @@ -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())
}
})

Expand Down
Loading