aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/subsystem/linux/coincidence_test.go
blob: 8aaf5ed0a2ff1e6d96bcb2fbf56b10394388fdae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package linux

import (
	"testing"

	"github.com/google/syzkaller/pkg/subsystem"
	"github.com/stretchr/testify/assert"
)

func TestCoincidenceMatrix(t *testing.T) {
	cm := MakeCoincidenceMatrix()
	a, b, c := &subsystem.Subsystem{}, &subsystem.Subsystem{}, &subsystem.Subsystem{}
	cm.Record(a, b)
	cm.Record(b, c)

	// Test counts.
	assert.Equal(t, cm.Count(a), 1)
	assert.Equal(t, cm.Count(b), 2)
	assert.Equal(t, cm.Count(c), 1)

	// Test pairwise counts.
	assert.Equal(t, cm.Get(a, b), 1)
	assert.Equal(t, cm.Get(b, c), 1)
	assert.Equal(t, cm.Get(a, c), 0)

	// Test the iterator.
	type pair struct {
		a *subsystem.Subsystem
		b *subsystem.Subsystem
	}
	expected := []pair{{a, b}, {b, a}, {b, c}, {c, b}}
	got := []pair{}
	cm.NonEmptyPairs(func(a, b *subsystem.Subsystem, _ int) {
		got = append(got, pair{a, b})
	})
	assert.ElementsMatch(t, expected, got)
}