github.com/songzhibin97/gkit@v1.2.13/structure/hashset/hashset_test.go (about) 1 // Copyright 2021 ByteDance 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 hashset 16 17 import ( 18 "fmt" 19 "testing" 20 ) 21 22 func Example() { 23 l := NewInt() 24 25 for _, v := range []int{10, 12, 15} { 26 l.Add(v) 27 } 28 29 if l.Contains(10) { 30 fmt.Println("hashset contains 10") 31 } 32 33 l.Range(func(value int) bool { 34 fmt.Println("hashset range found ", value) 35 return true 36 }) 37 38 l.Remove(15) 39 fmt.Printf("hashset contains %d items\r\n", l.Len()) 40 } 41 42 func TestIntSet(t *testing.T) { 43 // Correctness. 44 l := NewInt64() 45 if l.Len() != 0 { 46 t.Fatal("invalid length") 47 } 48 if l.Contains(0) { 49 t.Fatal("invalid contains") 50 } 51 52 if l.Add(0); l.Len() != 1 { 53 t.Fatal("invalid add") 54 } 55 if !l.Contains(0) { 56 t.Fatal("invalid contains") 57 } 58 if l.Remove(0); l.Len() != 0 { 59 t.Fatal("invalid remove") 60 } 61 62 if l.Add(20); l.Len() != 1 { 63 t.Fatal("invalid add") 64 } 65 if l.Add(22); l.Len() != 2 { 66 t.Fatal("invalid add") 67 } 68 if l.Add(21); l.Len() != 3 { 69 t.Fatal("invalid add") 70 } 71 if l.Add(21); l.Len() != 3 { 72 t.Fatal(l.Len(), " invalid add") 73 } 74 }