github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/set/unsafe_set_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package set 16 17 import "testing" 18 19 func Test_set_Add(t *testing.T) { 20 s := NewSet("test_element") 21 s.Contains("test_element") 22 23 type args struct { 24 element interface{} 25 } 26 tests := []struct { 27 name string 28 set Set 29 args args 30 want bool 31 }{ 32 {"case add success", NewSet(), args{"test_element"}, true}, 33 {"case add failed", NewSet("test_element"), args{"test_element"}, false}, 34 } 35 for _, tt := range tests { 36 t.Run(tt.name, func(t *testing.T) { 37 if got := tt.set.Add(tt.args.element); got != tt.want { 38 t.Errorf("Add() = %v, want %v", got, tt.want) 39 } 40 }) 41 } 42 } 43 44 func Test_set_Contains(t *testing.T) { 45 type args struct { 46 elements []interface{} 47 } 48 tests := []struct { 49 name string 50 set Set 51 args args 52 want bool 53 }{ 54 {"case contains all", NewSet("e1", "e2"), args{elements: []interface{}{"e1", "e2"}}, true}, 55 {"case not contains", NewSet("e1"), args{elements: []interface{}{"e1", "e2"}}, false}, 56 {"case contains one", NewSet("e1", "e2"), args{elements: []interface{}{"e1"}}, true}, 57 } 58 for _, tt := range tests { 59 t.Run(tt.name, func(t *testing.T) { 60 if got := tt.set.Contains(tt.args.elements...); got != tt.want { 61 t.Errorf("Contains() = %v, want %v", got, tt.want) 62 } 63 }) 64 } 65 } 66 67 func Test_set_Clear(t *testing.T) { 68 tests := []struct { 69 name string 70 set Set 71 wantLength int 72 wantErr bool 73 }{ 74 {"case clear", NewSet("e1", "e2"), 0, false}, 75 {"case clear", NewSet("e1", "e2"), 1, true}, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 tt.set.Clear() 80 if got := tt.set.Len(); (got != tt.wantLength) != tt.wantErr { 81 t.Errorf("Len() = %v, want %v", got, tt.wantLength) 82 } 83 }) 84 } 85 } 86 87 func Test_set_Len(t *testing.T) { 88 tests := []struct { 89 name string 90 set Set 91 want int 92 }{ 93 {"case len 0", NewSet(), 0}, 94 {"case len 1", NewSet("e1"), 1}, 95 {"case len 2", NewSet("e1", "e2"), 2}, 96 } 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 if got := tt.set.Len(); got != tt.want { 100 t.Errorf("Len() = %v, want %v", got, tt.want) 101 } 102 }) 103 } 104 } 105 106 func Test_set_Remove(t *testing.T) { 107 type args struct { 108 element interface{} 109 } 110 tests := []struct { 111 name string 112 set Set 113 args args 114 wantErr bool 115 }{ 116 {"case remove 1", NewSet("e1", "e2"), args{"e2"}, false}, 117 {"case remove 2", NewSet("e1", "e3"), args{"e2"}, false}, 118 } 119 for _, tt := range tests { 120 t.Run(tt.name, func(t *testing.T) { 121 tt.set.Remove(tt.args) 122 if got := tt.set.Contains(tt.args); got { 123 t.Errorf("Len() = %v, want %v", got, tt.wantErr) 124 } 125 }) 126 } 127 }