github.com/kaydxh/golang@v0.0.131/go/container/set/set_string_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package set_test
    23  
    24  import (
    25  	"testing"
    26  
    27  	set_ "github.com/kaydxh/golang/go/container/set"
    28  )
    29  
    30  func TestStringSetInsert(t *testing.T) {
    31  	s := set_.NewString("a", "b", "c")
    32  	s.Insert("a", "d", "e")
    33  	if len(s) != 5 {
    34  		t.Errorf("Expected  len=5: %d", len(s))
    35  	}
    36  
    37  	if !s.Has("a") || !s.Has("b") || !s.Has("c") || !s.Has("d") || !s.Has("e") {
    38  		t.Errorf("UnExpected  contents: %#v", s)
    39  	}
    40  
    41  	//%v output value
    42  	//map[a:{} b:{} c:{} d:{} e:{}]
    43  	t.Logf("s: %v", s)
    44  
    45  	//%+v output field name + value
    46  	//map[a:{} b:{} c:{} d:{} e:{}]
    47  	t.Logf("s: %+v", s)
    48  
    49  	//%#v output struct name + field name + value
    50  	//set.String{"a":set.Empty{}, "b":set.Empty{}, "c":set.Empty{}, "d":set.Empty{}, "e":set.Empty{}}
    51  	t.Logf("s: %#v", s)
    52  
    53  	//[a b c d e]
    54  	t.Logf("s: %v", s.List())
    55  
    56  }
    57  
    58  func TestStringSetEquals(t *testing.T) {
    59  
    60  	a := set_.NewString("1", "2")
    61  	b := set_.NewString("2", "1")
    62  
    63  	if !a.Equal(b) {
    64  		t.Errorf("Expected to be equal: %v vs %v", a, b)
    65  	}
    66  
    67  	//It is a set; duplicates are ignored
    68  	b = set_.NewString("2", "1", "1")
    69  	if !a.Equal(b) {
    70  		t.Errorf("Expected to be equal: %v vs %v", a, b)
    71  	}
    72  }
    73  
    74  func TestStringSetUnion(t *testing.T) {
    75  	tests := []struct {
    76  		s1       set_.String
    77  		s2       set_.String
    78  		expected set_.String
    79  	}{
    80  		{
    81  			set_.NewString("1", "2", "3", "4"),
    82  			set_.NewString("3", "4", "5", "6"),
    83  			set_.NewString("1", "2", "3", "4", "5", "6"),
    84  		},
    85  		{
    86  			set_.NewString("1", "2", "3", "4"),
    87  			set_.NewString(),
    88  			set_.NewString("1", "2", "3", "4"),
    89  		},
    90  		{
    91  			set_.NewString(),
    92  			set_.NewString("1", "2", "3", "4"),
    93  			set_.NewString("1", "2", "3", "4"),
    94  		},
    95  		{
    96  			set_.NewString(),
    97  			set_.NewString(),
    98  			set_.NewString(),
    99  		},
   100  	}
   101  
   102  	for _, test := range tests {
   103  		union := test.s1.Union(test.s2)
   104  		if union.Len() != test.expected.Len() {
   105  			t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
   106  		}
   107  
   108  		if !union.Equal(test.expected) {
   109  			t.Errorf(
   110  				"Expected union.Equal(expected) but not true.  union:%v expected:%v",
   111  				union.List(),
   112  				test.expected.List(),
   113  			)
   114  		}
   115  	}
   116  
   117  }
   118  
   119  func TestStringSetIntersection(t *testing.T) {
   120  	tests := []struct {
   121  		s1       set_.String
   122  		s2       set_.String
   123  		expected set_.String
   124  	}{
   125  		{
   126  			set_.NewString("1", "2", "3", "4"),
   127  			set_.NewString("3", "4", "5", "6"),
   128  			set_.NewString("3", "4"),
   129  		},
   130  		{
   131  			set_.NewString("1", "2", "3", "4"),
   132  			set_.NewString("1", "2", "3", "4"),
   133  			set_.NewString("1", "2", "3", "4"),
   134  		},
   135  		{
   136  			set_.NewString("1", "2", "3", "4"),
   137  			set_.NewString(),
   138  			set_.NewString(),
   139  		},
   140  		{
   141  			set_.NewString(),
   142  			set_.NewString("1", "2", "3", "4"),
   143  			set_.NewString(),
   144  		},
   145  		{
   146  			set_.NewString(),
   147  			set_.NewString(),
   148  			set_.NewString(),
   149  		},
   150  	}
   151  
   152  	for _, test := range tests {
   153  		intersection := test.s1.Intersection(test.s2)
   154  		if intersection.Len() != test.expected.Len() {
   155  			t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
   156  		}
   157  
   158  		if !intersection.Equal(test.expected) {
   159  			t.Errorf(
   160  				"Expected intersection.Equal(expected) but not true.  intersection:%v expected:%v",
   161  				intersection.List(),
   162  				test.expected.List(),
   163  			)
   164  		}
   165  	}
   166  
   167  }