| 1 | package llrb |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestAscendGreaterOrEqual(t *testing.T) { |
| 9 | tree := New() |
| 10 | tree.InsertNoReplace(Int(4)) |
| 11 | tree.InsertNoReplace(Int(6)) |
| 12 | tree.InsertNoReplace(Int(1)) |
| 13 | tree.InsertNoReplace(Int(3)) |
| 14 | var ary []Item |
| 15 | tree.AscendGreaterOrEqual(Int(-1), func(i Item) bool { |
| 16 | ary = append(ary, i) |
| 17 | return true |
| 18 | }) |
| 19 | expected := []Item{Int(1), Int(3), Int(4), Int(6)} |
| 20 | if !reflect.DeepEqual(ary, expected) { |
| 21 | t.Errorf("expected %v but got %v", expected, ary) |
| 22 | } |
| 23 | ary = nil |
| 24 | tree.AscendGreaterOrEqual(Int(3), func(i Item) bool { |
| 25 | ary = append(ary, i) |
| 26 | return true |
| 27 | }) |
| 28 | expected = []Item{Int(3), Int(4), Int(6)} |
| 29 | if !reflect.DeepEqual(ary, expected) { |
| 30 | t.Errorf("expected %v but got %v", expected, ary) |
| 31 | } |
| 32 | ary = nil |
| 33 | tree.AscendGreaterOrEqual(Int(2), func(i Item) bool { |
| 34 | ary = append(ary, i) |
| 35 | return true |
| 36 | }) |
| 37 | expected = []Item{Int(3), Int(4), Int(6)} |
| 38 | if !reflect.DeepEqual(ary, expected) { |
| 39 | t.Errorf("expected %v but got %v", expected, ary) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestDescendLessOrEqual(t *testing.T) { |
| 44 | tree := New() |
| 45 | tree.InsertNoReplace(Int(4)) |
| 46 | tree.InsertNoReplace(Int(6)) |
| 47 | tree.InsertNoReplace(Int(1)) |
| 48 | tree.InsertNoReplace(Int(3)) |
| 49 | var ary []Item |
| 50 | tree.DescendLessOrEqual(Int(10), func(i Item) bool { |
| 51 | ary = append(ary, i) |
| 52 | return true |
| 53 | }) |
| 54 | expected := []Item{Int(6), Int(4), Int(3), Int(1)} |
| 55 | if !reflect.DeepEqual(ary, expected) { |
| 56 | t.Errorf("expected %v but got %v", expected, ary) |
| 57 | } |
| 58 | ary = nil |
| 59 | tree.DescendLessOrEqual(Int(4), func(i Item) bool { |
| 60 | ary = append(ary, i) |
| 61 | return true |
| 62 | }) |
| 63 | expected = []Item{Int(4), Int(3), Int(1)} |
| 64 | if !reflect.DeepEqual(ary, expected) { |
| 65 | t.Errorf("expected %v but got %v", expected, ary) |
| 66 | } |
| 67 | ary = nil |
| 68 | tree.DescendLessOrEqual(Int(5), func(i Item) bool { |
| 69 | ary = append(ary, i) |
| 70 | return true |
| 71 | }) |
| 72 | expected = []Item{Int(4), Int(3), Int(1)} |
| 73 | if !reflect.DeepEqual(ary, expected) { |
| 74 | t.Errorf("expected %v but got %v", expected, ary) |
| 75 | } |
| 76 | } |