github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/subsystem/linux/coincidence_test.go (about)

     1  // Copyright 2023 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package linux
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/syzkaller/pkg/subsystem"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCoincidenceMatrix(t *testing.T) {
    14  	cm := MakeCoincidenceMatrix()
    15  	a, b, c := &subsystem.Subsystem{}, &subsystem.Subsystem{}, &subsystem.Subsystem{}
    16  	cm.Record(a, b)
    17  	cm.Record(b, c)
    18  
    19  	// Test counts.
    20  	assert.Equal(t, cm.Count(a), 1)
    21  	assert.Equal(t, cm.Count(b), 2)
    22  	assert.Equal(t, cm.Count(c), 1)
    23  
    24  	// Test pairwise counts.
    25  	assert.Equal(t, cm.Get(a, b), 1)
    26  	assert.Equal(t, cm.Get(b, c), 1)
    27  	assert.Equal(t, cm.Get(a, c), 0)
    28  
    29  	// Test the iterator.
    30  	type pair struct {
    31  		a *subsystem.Subsystem
    32  		b *subsystem.Subsystem
    33  	}
    34  	expected := []pair{{a, b}, {b, a}, {b, c}, {c, b}}
    35  	got := []pair{}
    36  	cm.NonEmptyPairs(func(a, b *subsystem.Subsystem, _ int) {
    37  		got = append(got, pair{a, b})
    38  	})
    39  	assert.ElementsMatch(t, expected, got)
    40  }