diff options
| author | Chris Lu <chris.lu@gmail.com> | 2021-10-02 14:15:49 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2021-10-02 14:15:49 -0700 |
| commit | 69b84bb771e816e2914daee8635379b1150e0de7 (patch) | |
| tree | e7fd4b488321c700543ff19fb231b62dc75c3d49 | |
| parent | 57e2fd3f9bb3c094f897daaaed3851f5b5af0ed2 (diff) | |
| download | seaweedfs-69b84bb771e816e2914daee8635379b1150e0de7.tar.xz seaweedfs-69b84bb771e816e2914daee8635379b1150e0de7.zip | |
TestFindGreaterOrEqual
| -rw-r--r-- | weed/util/skiplist/skiplist_test.go | 45 |
1 files changed, 45 insertions, 0 deletions
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 |
