github.com/seeker-insurance/kit@v0.0.13/set/int_test.go (about)

     1  package set
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  const (
    12  	_int_foo = iota
    13  	_int_bar
    14  	_int_baz
    15  	_int_moo
    16  )
    17  
    18  var (
    19  	_int_fooBar    = FromInts(_int_foo, _int_bar)
    20  	_int_barBaz    = FromInts(_int_bar, _int_baz)
    21  	_int_fooBaz    = FromInts(_int_foo, _int_baz)
    22  	_int_fooBarBaz = FromInts(_int_foo, _int_bar, _int_baz)
    23  )
    24  
    25  func TestInt_Union(t *testing.T) {
    26  	type args struct {
    27  		strings []Int
    28  	}
    29  
    30  	tests := []struct {
    31  		name      string
    32  		s         Int
    33  		args      args
    34  		wantUnion Int
    35  	}{
    36  		{
    37  			name:      "ok",
    38  			s:         _int_fooBar,
    39  			args:      args{[]Int{_int_barBaz}},
    40  			wantUnion: _int_fooBarBaz,
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			if gotUnion := tt.s.Union(tt.args.strings...); !reflect.DeepEqual(gotUnion, tt.wantUnion) {
    46  				t.Errorf("Int.Union() = %v, want %v", gotUnion, tt.wantUnion)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestInt_Difference(t *testing.T) {
    53  	type args struct {
    54  		strings []Int
    55  	}
    56  	tests := []struct {
    57  		name           string
    58  		s              Int
    59  		args           args
    60  		wantDifference Int
    61  	}{
    62  		{
    63  			name:           "ok",
    64  			s:              _int_fooBarBaz,
    65  			args:           args{[]Int{_int_fooBar}},
    66  			wantDifference: FromInts(_int_baz),
    67  		},
    68  		{
    69  			name:           "return self",
    70  			s:              _int_fooBarBaz,
    71  			wantDifference: _int_fooBarBaz,
    72  		},
    73  		{
    74  			name: "multiples ok",
    75  			s:    _int_fooBarBaz,
    76  			args: args{[]Int{_int_fooBar, _int_barBaz}},
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			got := tt.s.Difference(tt.args.strings...)
    82  			if !got.Equal(tt.wantDifference) {
    83  				t.Errorf("got %v, but want %v", got, tt.wantDifference)
    84  			}
    85  		})
    86  	}
    87  }
    88  
    89  func TestInt_Add(t *testing.T) {
    90  	type args struct {
    91  		keys []int
    92  	}
    93  	tests := []struct {
    94  		name string
    95  		s    Int
    96  		args args
    97  		want Int
    98  	}{
    99  		{
   100  			name: "add",
   101  			s:    Int{_int_foo: yes},
   102  			args: args{[]int{_int_foo, _int_foo, _int_bar, _int_baz}},
   103  			want: _int_fooBarBaz,
   104  		},
   105  	}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			tt.s.Add(tt.args.keys...)
   109  			if !tt.s.Equal(tt.want) {
   110  				t.Errorf("reciever of s.Add() is %v, but should be %v", tt.s, tt.want)
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestInt_ToSlice(t *testing.T) {
   117  	tests := []struct {
   118  		name     string
   119  		s        Int
   120  		want     []int
   121  		wantFail bool
   122  	}{
   123  		{
   124  			name: "ok",
   125  			s:    _int_fooBar,
   126  			want: []int{_int_foo, _int_bar},
   127  		},
   128  	}
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			got := tt.s.Sorted()
   132  			if !reflect.DeepEqual(got, tt.want) && !tt.wantFail {
   133  				t.Errorf("Int.ToSlice() = %v, want %v", got, tt.want)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestInt_Intersection(t *testing.T) {
   140  	type args struct {
   141  		strings []Int
   142  	}
   143  	tests := []struct {
   144  		name             string
   145  		s                Int
   146  		args             args
   147  		wantIntersection Int
   148  	}{
   149  		{"ok", _int_fooBar, args{[]Int{_int_barBaz}}, FromInts(_int_bar)},
   150  		{"multiples", _int_fooBar, args{[]Int{_int_fooBarBaz, _int_barBaz}}, FromInts(_int_bar)},
   151  	}
   152  	for _, tt := range tests {
   153  		t.Run(tt.name, func(t *testing.T) {
   154  			if got := tt.s.Intersection(tt.args.strings...); !got.Equal(tt.wantIntersection) {
   155  				t.Errorf("Int.Intersection() = %v, want %v", got, tt.wantIntersection)
   156  			}
   157  		})
   158  	}
   159  }
   160  func TestInt_Delete(t *testing.T) {
   161  	var set = make(Int)
   162  	set.Add(_int_foo, _int_bar, _int_baz)
   163  	assert.Equal(t, 3, len(set))
   164  	set.Delete(_int_foo, _int_bar)
   165  	assert.Equal(t, 1, len(set))
   166  	set.Delete(_int_foo)
   167  	assert.Equal(t, 1, len(set))
   168  }
   169  
   170  func TestInt_Equal(t *testing.T) {
   171  	type args struct {
   172  		other Int
   173  	}
   174  	tests := []struct {
   175  		name string
   176  		s    Int
   177  		args args
   178  		want bool
   179  	}{
   180  		{
   181  			"yes",
   182  			Int{_int_foo: yes, _int_bar: yes},
   183  			args{Int{_int_foo: yes, _int_bar: yes}},
   184  			true,
   185  		},
   186  		{
   187  			"no",
   188  			Int{_int_foo: yes, _int_bar: yes},
   189  			args{Int{}},
   190  			false,
   191  		}, {
   192  			"no - nonoverlapping keys",
   193  			FromInts(_int_foo, _int_bar),
   194  			args{other: FromInts(_int_baz, _int_moo)},
   195  			false,
   196  		},
   197  	}
   198  	for _, tt := range tests {
   199  		t.Run(tt.name, func(t *testing.T) {
   200  			if got := tt.s.Equal(tt.args.other); got != tt.want {
   201  				t.Errorf("Int.Equal() = %v, want %v", got, tt.want)
   202  			}
   203  		})
   204  	}
   205  }
   206  
   207  func TestInt_IUnion(t *testing.T) {
   208  	set := make(Int)
   209  	a := FromInts(_int_foo, _int_bar)
   210  	b := FromInts(_int_baz)
   211  
   212  	set.IUnion(a)
   213  	assert.Equal(t, set, a)
   214  	set.IUnion(b)
   215  	b.IUnion(a)
   216  	assert.Equal(t, set, b)
   217  }
   218  func TestInt_XOR(t *testing.T) {
   219  	type args struct {
   220  		a Int
   221  		b Int
   222  	}
   223  	tests := []struct {
   224  		name string
   225  		args args
   226  		want Int
   227  	}{
   228  		{
   229  			"ok",
   230  			args{Int{_int_foo: yes, _int_bar: yes}, Int{_int_foo: yes, _int_baz: yes}},
   231  			Int{_int_bar: yes, _int_baz: yes},
   232  		},
   233  	}
   234  	for _, tt := range tests {
   235  		t.Run(tt.name, func(t *testing.T) {
   236  			if got := tt.args.a.XOR(tt.args.b); !reflect.DeepEqual(got, tt.want) {
   237  				t.Errorf("XOR() = %v, want %v", got, tt.want)
   238  			}
   239  		})
   240  	}
   241  }
   242  
   243  func TestInt_Map(t *testing.T) {
   244  	double := func(n int) int {
   245  		return 2 * n
   246  	}
   247  	set := FromInts(1, 2, 4, 8)
   248  	want := FromInts(2, 4, 8, 16)
   249  	got := set.Map(double)
   250  	if !got.Equal(want) {
   251  		t.Errorf("map broken: %v", got.XOR(want))
   252  	}
   253  }
   254  
   255  func TestInt_Reduce(t *testing.T) {
   256  	min := func(a, b int) int {
   257  		if a < b {
   258  			return a
   259  		}
   260  		return b
   261  	}
   262  
   263  	set := FromInts(-1, -5, 2, 8)
   264  	want := -5
   265  	got, _ := set.Reduce(min)
   266  	if want != got {
   267  		t.Errorf("reduce broken: should get %d, but got %d", want, got)
   268  	}
   269  	var emptySet Int
   270  	if _, ok := emptySet.Reduce(min); ok {
   271  		t.Errorf("should not get OK on empty set")
   272  	}
   273  }
   274  
   275  func TestInt_Filter(t *testing.T) {
   276  	nonneg := func(n int) bool {
   277  		return n >= 0
   278  	}
   279  	set := FromInts(-1, 0, 2, 3)
   280  	want := FromInts(0, 2, 3)
   281  	got := set.Filter(nonneg)
   282  	if !want.Equal(got) {
   283  		t.Errorf("should get %v, but got %v", want, got)
   284  	}
   285  }
   286  
   287  func TestInt_Contains(t *testing.T) {
   288  	type args struct {
   289  		key int
   290  	}
   291  	tests := []struct {
   292  		name string
   293  		s    Int
   294  		args args
   295  		want bool
   296  	}{
   297  	// TODO: Add test cases.
   298  	}
   299  	for _, tt := range tests {
   300  		t.Run(tt.name, func(t *testing.T) {
   301  			if got := tt.s.Contains(tt.args.key); got != tt.want {
   302  				t.Errorf("Int.Contains() = %v, want %v", got, tt.want)
   303  			}
   304  		})
   305  	}
   306  }
   307  
   308  func TestInt_Copy(t *testing.T) {
   309  	tests := []struct {
   310  		name string
   311  		s    Int
   312  		want Int
   313  	}{
   314  	// TODO: Add test cases.
   315  	}
   316  	for _, tt := range tests {
   317  		t.Run(tt.name, func(t *testing.T) {
   318  			if got := tt.s.Copy(); !reflect.DeepEqual(got, tt.want) {
   319  				t.Errorf("Int.Copy() = %v, want %v", got, tt.want)
   320  			}
   321  		})
   322  	}
   323  }
   324  
   325  func TestFromInts(t *testing.T) {
   326  	type args struct {
   327  		ints []int
   328  	}
   329  	tests := []struct {
   330  		name string
   331  		args args
   332  		want Int
   333  	}{
   334  	// TODO: Add test cases.
   335  	}
   336  	for _, tt := range tests {
   337  		t.Run(tt.name, func(t *testing.T) {
   338  			if got := FromInts(tt.args.ints...); !reflect.DeepEqual(got, tt.want) {
   339  				t.Errorf("FromInts() = %v, want %v", got, tt.want)
   340  			}
   341  		})
   342  	}
   343  }
   344  
   345  func TestInt_Sorted(t *testing.T) {
   346  	tests := []struct {
   347  		name string
   348  		s    Int
   349  		want []int
   350  	}{
   351  	// TODO: Add test cases.
   352  	}
   353  	for _, tt := range tests {
   354  		t.Run(tt.name, func(t *testing.T) {
   355  			if got := tt.s.Sorted(); !reflect.DeepEqual(got, tt.want) {
   356  				t.Errorf("Int.Sorted() = %v, want %v", got, tt.want)
   357  			}
   358  		})
   359  	}
   360  }
   361  
   362  func TestInt_IsSubset(t *testing.T) {
   363  	var (
   364  		s = FromInts(2, 3)
   365  		q = FromInts(2, 5)
   366  		r = FromInts(2)
   367  		w = FromInts(2, 5, 8, 9)
   368  	)
   369  
   370  	assert.True(t, r.IsSubset(s))
   371  	assert.False(t, s.IsSubset(q))
   372  	assert.False(t, w.IsSubset(s))
   373  }
   374  
   375  func TestInt_Pop(t *testing.T) {
   376  	s := FromInts(5, 2, 3)
   377  	b, _ := s.Pop()
   378  	c, _ := s.Pop()
   379  	d, _ := s.Pop()
   380  	got := []int{b, c, d}
   381  	sort.Ints(got)
   382  	assert.Equal(t, []int{2, 3, 5}, got)
   383  }
   384  
   385  func TestInt_IsDisjoint(t *testing.T) {
   386  	s, q := FromInts(2, 8), FromInts(3, 7)
   387  	assert.True(t, s.IsDisjoint(q))
   388  	r := FromInts(2)
   389  	assert.False(t, s.IsDisjoint(r))
   390  }
   391  
   392  func TestInt_IsProperSubset(t *testing.T) {
   393  	s, q := FromInts(2, 3), FromInts(2, 3, 5)
   394  	assert.True(t, s.IsProperSubset(q))
   395  	assert.False(t, s.IsProperSubset(s))
   396  }
   397  
   398  func TestInt_IsSuperset(t *testing.T) {
   399  
   400  }
   401  
   402  func TestInt_IsProperSuperset(t *testing.T) {
   403  
   404  }