github.com/kaydxh/golang@v0.0.131/go/container/set/set_test.go (about) 1 package set_test 2 3 import ( 4 "testing" 5 6 set_ "github.com/kaydxh/golang/go/container/set" 7 ) 8 9 func TestGenericSetNew(t *testing.T) { 10 s := set_.New[int]() 11 s.Insert(2) 12 t.Logf("s: %v", s) 13 } 14 15 func TestGenericSetInsert(t *testing.T) { 16 s := set_.New("10", "2", "5") 17 s.Insert("a", "d", "e") 18 if len(s) != 5 { 19 t.Errorf("Expected len=5: %d", len(s)) 20 } 21 22 if !s.Has("a") || !s.Has("b") || !s.Has("c") || !s.Has("d") || !s.Has("e") { 23 t.Errorf("UnExpected contents: %#v", s) 24 } 25 26 //%v output value 27 //map[a:{} b:{} c:{} d:{} e:{}] 28 t.Logf("s: %v", s) 29 30 //%+v output field name + value 31 //map[a:{} b:{} c:{} d:{} e:{}] 32 t.Logf("s: %+v", s) 33 34 //%#v output struct name + field name + value 35 //set.Object{"a":set.Empty{}, "b":set.Empty{}, "c":set.Empty{}, "d":set.Empty{}, "e":set.Empty{}} 36 t.Logf("s: %#v", s) 37 38 //[a b c d e] 39 t.Logf("s: %v", s.List()) 40 41 } 42 43 func TestGenericSetEquals(t *testing.T) { 44 45 a := set_.New("1", "2") 46 b := set_.New("2", "1") 47 48 if !a.Equal(b) { 49 t.Errorf("Expected to be equal: %v vs %v", a, b) 50 } 51 52 //It is a set; duplicates are ignored 53 b = set_.New("2", "1", "1") 54 if !a.Equal(b) { 55 t.Errorf("Expected to be equal: %v vs %v", a, b) 56 } 57 } 58 59 func TestGenericSetUnion(t *testing.T) { 60 tests := []struct { 61 s1 set_.Set[string] 62 s2 set_.Set[string] 63 expected set_.Set[string] 64 }{ 65 { 66 set_.New("1", "2", "3", "4"), 67 set_.New("3", "4", "5", "6"), 68 set_.New("1", "2", "3", "4", "5", "6"), 69 }, 70 { 71 set_.New("1", "2", "3", "4"), 72 set_.New[string](), 73 set_.New("1", "2", "3", "4"), 74 }, 75 { 76 set_.New[string](), 77 set_.New("1", "2", "3", "4"), 78 set_.New("1", "2", "3", "4"), 79 }, 80 { 81 set_.New[string](), 82 set_.New[string](), 83 set_.New[string](), 84 }, 85 } 86 87 for _, test := range tests { 88 union := test.s1.Union(test.s2) 89 if union.Len() != test.expected.Len() { 90 t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len()) 91 } 92 93 if !union.Equal(test.expected) { 94 t.Errorf( 95 "Expected union.Equal(expected) but not true. union:%v expected:%v", 96 union.List(), 97 test.expected.List(), 98 ) 99 } 100 } 101 102 }