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

     1  // Copyright 2020 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 cover
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  	"github.com/google/syzkaller/pkg/cover/backend"
    13  )
    14  
    15  func TestMergeDiff(t *testing.T) {
    16  	type Test struct {
    17  		init   []uint32
    18  		merge  []uint32
    19  		diff   []uint32
    20  		result []uint32
    21  	}
    22  	tests := []Test{
    23  		{
    24  			init:   nil,
    25  			merge:  nil,
    26  			diff:   nil,
    27  			result: []uint32{},
    28  		},
    29  		{
    30  			init:   []uint32{0, 1, 3, 4},
    31  			merge:  nil,
    32  			diff:   nil,
    33  			result: []uint32{0, 1, 3, 4},
    34  		},
    35  		{
    36  			init:   nil,
    37  			merge:  []uint32{0, 1, 3, 4},
    38  			diff:   []uint32{0, 1, 3, 4},
    39  			result: []uint32{0, 1, 3, 4},
    40  		},
    41  		{
    42  			init:   []uint32{0, 1, 3, 4},
    43  			merge:  []uint32{4, 7, 1, 9},
    44  			diff:   []uint32{7, 9},
    45  			result: []uint32{0, 1, 3, 4, 7, 9},
    46  		},
    47  	}
    48  	for i, test := range tests {
    49  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    50  			var cov Cover
    51  			cov.Merge(test.init)
    52  			diff := cov.MergeDiff(test.merge)
    53  			if res := cmp.Diff(test.diff, diff); res != "" {
    54  				t.Fatalf("result is wrong: %v", res)
    55  			}
    56  			result := cov.Serialize()
    57  			sort.Slice(result, func(i, j int) bool {
    58  				return result[i] < result[j]
    59  			})
    60  			if res := cmp.Diff(test.result, result); res != "" {
    61  				t.Fatalf("resulting coverage is wrong: %v", res)
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestPerLineCoverage(t *testing.T) {
    68  	const End = backend.LineEnd
    69  	// Start line:col - end line:col.
    70  	// nolint
    71  	covered := []backend.Range{
    72  		// Just covered.
    73  		{1, 2, 1, 10},
    74  		{2, 1, 4, 10},
    75  		// Both covered and uncovered.
    76  		{10, 0, 10, 10},
    77  		{11, 20, 11, 30},
    78  		{12, 0, 12, End},
    79  		// Broken debug data.
    80  		{30, 10, 29, 20},
    81  		{31, 20, 30, 10},
    82  		{32, 10, 32, 5},
    83  		// Double covered.
    84  		{40, 10, 40, 20},
    85  		{40, 12, 40, 18},
    86  		{41, 10, 41, 20},
    87  		{41, 15, 41, 30},
    88  		{42, 20, 42, 30},
    89  		{42, 10, 42, 25},
    90  	}
    91  	// nolint
    92  	uncovered := []backend.Range{
    93  		{10, 20, 10, 30},
    94  		{11, 0, 11, 20},
    95  		{12, 0, 12, End},
    96  		// Only uncovered.
    97  		{20, 20, 21, 10},
    98  	}
    99  	want := map[int][]lineCoverChunk{
   100  		1:  {{2, false, false}, {10, true, false}, {End, false, false}},
   101  		2:  {{1, false, false}, {End, true, false}},
   102  		3:  {{End, true, false}},
   103  		4:  {{10, true, false}, {End, false, false}},
   104  		10: {{10, true, false}, {20, false, false}, {30, false, true}, {End, false, false}},
   105  		11: {{20, false, true}, {30, true, false}, {End, false, false}},
   106  		12: {{End, true, true}},
   107  		20: {{20, false, false}, {End, false, true}},
   108  		21: {{10, false, true}, {End, false, false}},
   109  		30: {{10, false, false}, {20, true, false}, {End, false, false}},
   110  		31: {{20, false, false}, {End, true, false}},
   111  		32: {{10, false, false}, {End, true, false}},
   112  		40: {{10, false, false}, {20, true, false}, {End, false, false}},
   113  		41: {{10, false, false}, {20, true, false}, {30, true, false}, {End, false, false}},
   114  		42: {{10, false, false}, {20, true, false}, {30, true, false}, {End, false, false}},
   115  	}
   116  	got := perLineCoverage(covered, uncovered)
   117  	if diff := cmp.Diff(want, got); diff != "" {
   118  		t.Fatal(diff)
   119  	}
   120  }