From 69b84bb771e816e2914daee8635379b1150e0de7 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 2 Oct 2021 14:15:49 -0700 Subject: [PATCH] TestFindGreaterOrEqual --- weed/util/skiplist/skiplist_test.go | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/weed/util/skiplist/skiplist_test.go b/weed/util/skiplist/skiplist_test.go index ca41e382a..60bd5f923 100644 --- a/weed/util/skiplist/skiplist_test.go +++ b/weed/util/skiplist/skiplist_test.go @@ -2,6 +2,7 @@ package skiplist import ( "bytes" + "fmt" "math/rand" "strconv" "testing" @@ -210,3 +211,47 @@ func TestGetNodeCount(t *testing.T) { t.Fail() } } + +func TestFindGreaterOrEqual(t *testing.T) { + + maxNumber := maxN * 100 + + var list *SkipList + var listPointer *SkipList + + // Test on empty list. + if _, ok := listPointer.FindGreaterOrEqual(Element(0)); ok { + t.Fail() + } + + list = New() + + for i := 0; i < maxN; i++ { + list.Insert(Element(rand.Intn(maxNumber))) + } + + for i := 0; i < maxN; i++ { + key := Element(rand.Intn(maxNumber)) + if v, ok := list.FindGreaterOrEqual(key); ok { + // if f is v should be bigger than the element before + if bytes.Compare(v.Prev.Key, key) >= 0 { + fmt.Printf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key)) + t.Fail() + } + // v should be bigger or equal to f + // If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails. + if bytes.Compare(v.Values[0], key) < 0 { + fmt.Printf("v: %s\n key: %s\n\n", string(v.Values[0]), string(key)) + t.Fail() + } + } else { + lastV := list.GetLargestNode().GetValue() + // It is OK, to fail, as long as f is bigger than the last element. + if bytes.Compare(key, lastV) <= 0 { + fmt.Printf("lastV: %s\n key: %s\n\n", string(lastV), string(key)) + t.Fail() + } + } + } + +} \ No newline at end of file