github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/signal/signal_test.go (about)

     1  // Copyright 2024 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 signal
     5  
     6  import (
     7  	"math/rand"
     8  	"testing"
     9  
    10  	"github.com/google/syzkaller/pkg/testutil"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRandomSubset(t *testing.T) {
    15  	r := rand.New(testutil.RandSource(t))
    16  	base := FromRaw([]uint32{0, 1, 2, 3, 4}, 0)
    17  	var s Signal
    18  	for i := 0; i < 1000 && s.Len() < base.Len(); i++ {
    19  		delta := base.RandomSubset(r, 1)
    20  		assert.Equal(t, 1, delta.Len())
    21  		s.Merge(delta)
    22  	}
    23  	assert.Equal(t, base.Len(), s.Len())
    24  }
    25  
    26  func TestSubtract(t *testing.T) {
    27  	base := FromRaw([]uint32{0, 1, 2, 3, 4}, 0)
    28  	assert.Equal(t, 5, base.Len())
    29  	base.Subtract(FromRaw([]uint32{0}, 0))
    30  	assert.Equal(t, 4, base.Len())
    31  	base.Subtract(FromRaw([]uint32{1}, 0))
    32  	assert.Equal(t, 3, base.Len())
    33  }
    34  
    35  func TestIntersectsWith(t *testing.T) {
    36  	base := FromRaw([]uint32{0, 1, 2, 3, 4}, 1)
    37  	assert.True(t, base.IntersectsWith(FromRaw([]uint32{0, 5, 10}, 1)))
    38  	assert.False(t, base.IntersectsWith(FromRaw([]uint32{5, 10, 15}, 1)))
    39  	// The other signal has a lower priority.
    40  	assert.False(t, base.IntersectsWith(FromRaw([]uint32{0, 1, 2}, 0)))
    41  }