github.com/hashicorp/packer@v1.14.3/internal/dag/set_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package dag
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  func TestSetDifference(t *testing.T) {
    12  	cases := []struct {
    13  		Name     string
    14  		A, B     []interface{}
    15  		Expected []interface{}
    16  	}{
    17  		{
    18  			"same",
    19  			[]interface{}{1, 2, 3},
    20  			[]interface{}{3, 1, 2},
    21  			[]interface{}{},
    22  		},
    23  
    24  		{
    25  			"A has extra elements",
    26  			[]interface{}{1, 2, 3},
    27  			[]interface{}{3, 2},
    28  			[]interface{}{1},
    29  		},
    30  
    31  		{
    32  			"B has extra elements",
    33  			[]interface{}{1, 2, 3},
    34  			[]interface{}{3, 2, 1, 4},
    35  			[]interface{}{},
    36  		},
    37  		{
    38  			"B is nil",
    39  			[]interface{}{1, 2, 3},
    40  			nil,
    41  			[]interface{}{1, 2, 3},
    42  		},
    43  	}
    44  
    45  	for i, tc := range cases {
    46  		t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
    47  			one := make(Set)
    48  			two := make(Set)
    49  			expected := make(Set)
    50  			for _, v := range tc.A {
    51  				one.Add(v)
    52  			}
    53  			for _, v := range tc.B {
    54  				two.Add(v)
    55  			}
    56  			if tc.B == nil {
    57  				two = nil
    58  			}
    59  			for _, v := range tc.Expected {
    60  				expected.Add(v)
    61  			}
    62  
    63  			actual := one.Difference(two)
    64  			match := actual.Intersection(expected)
    65  			if match.Len() != expected.Len() {
    66  				t.Fatalf("bad: %#v", actual.List())
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestSetFilter(t *testing.T) {
    73  	cases := []struct {
    74  		Input    []interface{}
    75  		Expected []interface{}
    76  	}{
    77  		{
    78  			[]interface{}{1, 2, 3},
    79  			[]interface{}{1, 2, 3},
    80  		},
    81  
    82  		{
    83  			[]interface{}{4, 5, 6},
    84  			[]interface{}{4},
    85  		},
    86  
    87  		{
    88  			[]interface{}{7, 8, 9},
    89  			[]interface{}{},
    90  		},
    91  	}
    92  
    93  	for i, tc := range cases {
    94  		t.Run(fmt.Sprintf("%d-%#v", i, tc.Input), func(t *testing.T) {
    95  			input := make(Set)
    96  			expected := make(Set)
    97  			for _, v := range tc.Input {
    98  				input.Add(v)
    99  			}
   100  			for _, v := range tc.Expected {
   101  				expected.Add(v)
   102  			}
   103  
   104  			actual := input.Filter(func(v interface{}) bool {
   105  				return v.(int) < 5
   106  			})
   107  			match := actual.Intersection(expected)
   108  			if match.Len() != expected.Len() {
   109  				t.Fatalf("bad: %#v", actual.List())
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestSetCopy(t *testing.T) {
   116  	a := make(Set)
   117  	a.Add(1)
   118  	a.Add(2)
   119  
   120  	b := a.Copy()
   121  	b.Add(3)
   122  
   123  	diff := b.Difference(a)
   124  
   125  	if diff.Len() != 1 {
   126  		t.Fatalf("expected single diff value, got %#v", diff)
   127  	}
   128  
   129  	if !diff.Include(3) {
   130  		t.Fatalf("diff does not contain 3, got %#v", diff)
   131  	}
   132  
   133  }
   134  
   135  func makeSet(n int) Set {
   136  	ret := make(Set, n)
   137  	for i := 0; i < n; i++ {
   138  		ret.Add(i)
   139  	}
   140  	return ret
   141  }
   142  
   143  func BenchmarkSetIntersection_100_100000(b *testing.B) {
   144  	small := makeSet(100)
   145  	large := makeSet(100000)
   146  
   147  	b.ResetTimer()
   148  	for n := 0; n < b.N; n++ {
   149  		small.Intersection(large)
   150  	}
   151  }
   152  
   153  func BenchmarkSetIntersection_100000_100(b *testing.B) {
   154  	small := makeSet(100)
   155  	large := makeSet(100000)
   156  
   157  	b.ResetTimer()
   158  	for n := 0; n < b.N; n++ {
   159  		large.Intersection(small)
   160  	}
   161  }