package tool import ( "fmt" "math/rand" "testing" "github.com/bits-and-blooms/bitset" "github.com/gammazero/deque" "github.com/huandu/skiplist" ) func TestSkipList(t *testing.T) { // Create a skip list with int key. list := skiplist.New(skiplist.Int) // Add some values. Value can be anything. list.Set(12, "hello world") list.Set(34, 56) list.Set(78, 90.12) // Get element by index. elem := list.Get(34) // Value is stored in elem.Value. fmt.Println(elem.Value) // Output: 56 next := elem.Next() // Get next element. prev := next.Prev() // Get previous element. fmt.Println(next.Value, prev.Value) // Output: 90.12 56 // Or, directly get value just like a map val, ok := list.GetValue(34) fmt.Println(val, ok) // Output: 56 true // Find first elements with score greater or equal to key foundElem := list.Find(30) fmt.Println(foundElem.Key(), foundElem.Value) // Output: 34 56 // Remove an element for key. list.Remove(34) } // Queue using PushBack and PopFront // Stack using PushBack and PopBack func TestDeque(t *testing.T) { var q deque.Deque q.PushBack("foo") q.PushBack("bar") q.PushBack("baz") fmt.Println(q.Len()) // Prints: 3 fmt.Println(q.Front()) // Prints: foo fmt.Println(q.Back()) // Prints: baz q.PopFront() // remove "foo" q.PopBack() // remove "baz" q.PushFront("hello") q.PushBack("world") // Consume deque and print elements. for q.Len() != 0 { fmt.Println(q.PopFront()) } } // bitsets func TestBitsets(t *testing.T) { fmt.Printf("Hello from BitSet!\n") var b bitset.BitSet // play some Go Fish for i := 0; i < 100; i++ { card1 := uint(rand.Intn(52)) card2 := uint(rand.Intn(52)) b.Set(card1) if b.Test(card2) { fmt.Println("Go Fish!") } b.Clear(card1) } // Chaining b.Set(10).Set(11) for i, e := b.NextSet(0); e; i, e = b.NextSet(i + 1) { fmt.Println("The following bit is set:", i) } if b.Intersection(bitset.New(100).Set(10)).Count() == 1 { fmt.Println("Intersection works.") } else { fmt.Println("Intersection doesn't work???") } }