github.com/searKing/golang/go@v1.2.74/container/set/set_test.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package set
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func Test(t *testing.T) {
    12  	s := New()
    13  
    14  	if s.Count() != 0 {
    15  		t.Errorf("Length should be 0")
    16  	}
    17  	s.Remove(0)
    18  	s.Add(5)
    19  
    20  	if s.Count() != 1 {
    21  		t.Errorf("Length should be 1")
    22  	}
    23  
    24  	if !s.Contains(5) {
    25  		t.Errorf("Membership test failed")
    26  	}
    27  
    28  	s.Remove(5)
    29  
    30  	if s.Count() != 0 {
    31  		t.Errorf("Length should be 0")
    32  	}
    33  
    34  	if s.Contains(5) {
    35  		t.Errorf("The set should be empty")
    36  	}
    37  
    38  	// Difference
    39  	s1 := Of(1, 2, 3, 4, 5, 6)
    40  	s2 := Of(4, 5, 6)
    41  	s3 := s1.RemoveAll(s2.ToStream())
    42  
    43  	if s3.Length() != 3 {
    44  		t.Errorf("Length should be 3")
    45  	}
    46  
    47  	if !(s3.Contains(1) && s3.Contains(2) && s3.Contains(3)) {
    48  		t.Errorf("Set should only contain 1, 2, 3")
    49  	}
    50  
    51  	s1 = Of(1, 2, 3, 4, 5, 6)
    52  	// Intersection
    53  	s3 = s1.RetainAll(s2.ToStream())
    54  	if s3.Count() != 3 {
    55  		t.Errorf("Length should be 3 after RetainAll")
    56  	}
    57  
    58  	if !(s3.Contains(4) && s3.Contains(5) && s3.Contains(6)) {
    59  		t.Errorf("Set should contain 4, 5, 6")
    60  	}
    61  
    62  	// Union
    63  	s4 := Of(7, 8, 9)
    64  	s3 = s2.AddAll(s4.ToStream())
    65  
    66  	if s3.Length() != 6 {
    67  		t.Errorf("Length should be 6 after union")
    68  	}
    69  
    70  	if !(s3.Contains(7)) {
    71  		t.Errorf("Set should contain 4, 5, 6, 7, 8, 9")
    72  	}
    73  
    74  	// Subset
    75  	if !s1.ContainsAll(s1.ToStream()) {
    76  		t.Errorf("set should be a subset of itself")
    77  	}
    78  
    79  }