github.com/aacfactory/rings@v1.1.2/hashed_test.go (about) 1 package rings_test 2 3 import ( 4 "fmt" 5 "github.com/aacfactory/rings" 6 "math/rand" 7 "testing" 8 ) 9 10 func TestHashed(t *testing.T) { 11 hashed := rings.NewHashed( 12 &Item{ 13 key: "1", 14 value: 1, 15 }, 16 &Item{ 17 key: "2", 18 value: 2, 19 }, 20 &Item{ 21 key: "3", 22 value: 3, 23 }, 24 ) 25 fmt.Println(hashed) 26 for i := 0; i < 5; i++ { 27 key := []byte(fmt.Sprintf("%d", i*rand.Intn(9999999))) 28 fmt.Print(hashed.Get(key)) 29 fmt.Print(", ") 30 } 31 fmt.Println() 32 prev, cLow, cHigh, active, cancel, ok := hashed.Add(&Item{ 33 key: "4", 34 value: 4, 35 }) 36 if !ok { 37 fmt.Println(ok) 38 return 39 } 40 fmt.Println("prev:", prev, cLow, cHigh) 41 fmt.Println(hashed) 42 active() 43 fmt.Println(hashed) 44 cancel() 45 fmt.Println(hashed) 46 fmt.Println("-----") 47 for i := 0; i < 5; i++ { 48 _, cLow, cHigh, active, _, _ = hashed.Add(&Item{ 49 key: fmt.Sprintf("%d", 4+i), 50 value: 4 + i, 51 }) 52 active() 53 fmt.Println(hashed, cLow, cHigh) 54 } 55 }