github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/list/classic_skip_list_test.go (about) 1 package list 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "math" 6 "testing" 7 ) 8 9 func strCompare(a, b string) int { 10 if a == b { 11 return 0 12 } 13 if a < b { 14 return -1 15 } 16 return 1 17 } 18 19 func TestClassicSkipList(t *testing.T) { 20 words := []string{ 21 "foo", "bar", "zap", "pomo", "pera", "arancio", "limone", 22 } 23 sl := NewClassicSkipList[string](strCompare) 24 for _, word := range words { 25 sl.Insert(word) 26 } 27 expected := []string{ 28 "arancio", "bar", "foo", "limone", "pera", "pomo", "zap", 29 } 30 actual := make([]string, 0, len(words)) 31 sl.ForEach(func(idx int64, v string) { 32 actual = append(actual, v) 33 t.Logf("idx: %d, v: %s", idx, v) 34 }) 35 assert.Equal(t, expected, actual) 36 37 e := sl.Find("ben") 38 assert.Nil(t, e) 39 40 e = sl.Find("limone") 41 assert.Equal(t, "limone", e.GetObject()) 42 43 sl.PopHead() 44 expected = []string{ 45 "bar", "foo", "limone", "pera", "pomo", "zap", 46 } 47 actual = make([]string, 0, len(words)) 48 sl.ForEach(func(idx int64, v string) { 49 actual = append(actual, v) 50 t.Logf("idx: %d, v: %s", idx, v) 51 }) 52 assert.Equal(t, expected, actual) 53 54 sl.Remove("pera") 55 expected = []string{ 56 "bar", "foo", "limone", "pomo", "zap", 57 } 58 actual = make([]string, 0, len(words)) 59 sl.ForEach(func(idx int64, v string) { 60 actual = append(actual, v) 61 t.Logf("idx: %d, v: %s", idx, v) 62 }) 63 assert.Equal(t, expected, actual) 64 } 65 66 func TestMaxLevel(t *testing.T) { 67 maxLevels := MaxLevels(math.MaxInt32, 0.25) 68 assert.GreaterOrEqual(t, 32, maxLevels) 69 }