|
| 1 | +// Copyright 2019 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package runtime_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "runtime" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +var spanDesc = map[uintptr]uintptr{ |
| 13 | + 0xc0000000: 2, |
| 14 | + 0xc0006000: 1, |
| 15 | + 0xc0010000: 8, |
| 16 | + 0xc0022000: 7, |
| 17 | + 0xc0034000: 4, |
| 18 | + 0xc0040000: 5, |
| 19 | + 0xc0050000: 5, |
| 20 | + 0xc0060000: 5000, |
| 21 | +} |
| 22 | + |
| 23 | +// Wrap the Treap one more time because go:notinheap doesn't |
| 24 | +// actually follow a structure across package boundaries. |
| 25 | +// |
| 26 | +//go:notinheap |
| 27 | +type treap struct { |
| 28 | + runtime.Treap |
| 29 | +} |
| 30 | + |
| 31 | +// This test ensures that the treap implementation in the runtime |
| 32 | +// maintains all stated invariants after different sequences of |
| 33 | +// insert, removeSpan, find, and erase. Invariants specific to the |
| 34 | +// treap data structure are checked implicitly: after each mutating |
| 35 | +// operation, treap-related invariants are checked for the entire |
| 36 | +// treap. |
| 37 | +func TestTreap(t *testing.T) { |
| 38 | + // Set up a bunch of spans allocated into mheap_. |
| 39 | + spans := make([]runtime.Span, 0, len(spanDesc)) |
| 40 | + for base, pages := range spanDesc { |
| 41 | + s := runtime.AllocSpan(base, pages) |
| 42 | + defer s.Free() |
| 43 | + spans = append(spans, s) |
| 44 | + } |
| 45 | + t.Run("Insert", func(t *testing.T) { |
| 46 | + tr := treap{} |
| 47 | + // Test just a very basic insert/remove for sanity. |
| 48 | + tr.Insert(spans[0]) |
| 49 | + tr.RemoveSpan(spans[0]) |
| 50 | + }) |
| 51 | + t.Run("FindTrivial", func(t *testing.T) { |
| 52 | + tr := treap{} |
| 53 | + // Test just a very basic find operation for sanity. |
| 54 | + tr.Insert(spans[0]) |
| 55 | + i := tr.Find(1) |
| 56 | + if i.Span() != spans[0] { |
| 57 | + t.Fatal("found unknown span in treap") |
| 58 | + } |
| 59 | + tr.RemoveSpan(spans[0]) |
| 60 | + }) |
| 61 | + t.Run("Find", func(t *testing.T) { |
| 62 | + // Note that Find doesn't actually find the best-fit |
| 63 | + // element, so just make sure it always returns an element |
| 64 | + // that is at least large enough to satisfy the request. |
| 65 | + // |
| 66 | + // Run this 10 times, recreating the treap each time. |
| 67 | + // Because of the non-deterministic structure of a treap, |
| 68 | + // we'll be able to test different structures this way. |
| 69 | + for i := 0; i < 10; i++ { |
| 70 | + tr := treap{} |
| 71 | + for _, s := range spans { |
| 72 | + tr.Insert(s) |
| 73 | + } |
| 74 | + i := tr.Find(5) |
| 75 | + if i.Span().Pages() < 5 { |
| 76 | + t.Fatalf("expected span of size at least 5, got size %d", i.Span().Pages()) |
| 77 | + } |
| 78 | + for _, s := range spans { |
| 79 | + tr.RemoveSpan(s) |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + t.Run("Iterate", func(t *testing.T) { |
| 84 | + t.Run("StartToEnd", func(t *testing.T) { |
| 85 | + // Ensure progressing an iterator actually goes over the whole treap |
| 86 | + // from the start and that it iterates over the elements in order. |
| 87 | + // Also ensures that Start returns a valid iterator. |
| 88 | + tr := treap{} |
| 89 | + for _, s := range spans { |
| 90 | + tr.Insert(s) |
| 91 | + } |
| 92 | + nspans := 0 |
| 93 | + lastSize := uintptr(0) |
| 94 | + for i := tr.Start(); i.Valid(); i = i.Next() { |
| 95 | + nspans++ |
| 96 | + if lastSize > i.Span().Pages() { |
| 97 | + t.Fatalf("not iterating in correct order: encountered size %d before %d", lastSize, i.Span().Pages()) |
| 98 | + } |
| 99 | + lastSize = i.Span().Pages() |
| 100 | + } |
| 101 | + if nspans != len(spans) { |
| 102 | + t.Fatal("failed to iterate forwards over full treap") |
| 103 | + } |
| 104 | + for _, s := range spans { |
| 105 | + tr.RemoveSpan(s) |
| 106 | + } |
| 107 | + }) |
| 108 | + t.Run("EndToStart", func(t *testing.T) { |
| 109 | + // Ensure progressing an iterator actually goes over the whole treap |
| 110 | + // from the end and that it iterates over the elements in reverse |
| 111 | + // order. Also ensures that End returns a valid iterator. |
| 112 | + tr := treap{} |
| 113 | + for _, s := range spans { |
| 114 | + tr.Insert(s) |
| 115 | + } |
| 116 | + nspans := 0 |
| 117 | + lastSize := ^uintptr(0) |
| 118 | + for i := tr.End(); i.Valid(); i = i.Prev() { |
| 119 | + nspans++ |
| 120 | + if lastSize < i.Span().Pages() { |
| 121 | + t.Fatalf("not iterating in correct order: encountered size %d before %d", lastSize, i.Span().Pages()) |
| 122 | + } |
| 123 | + lastSize = i.Span().Pages() |
| 124 | + } |
| 125 | + if nspans != len(spans) { |
| 126 | + t.Fatal("failed to iterate backwards over full treap") |
| 127 | + } |
| 128 | + for _, s := range spans { |
| 129 | + tr.RemoveSpan(s) |
| 130 | + } |
| 131 | + }) |
| 132 | + t.Run("Prev", func(t *testing.T) { |
| 133 | + // Test the iterator invariant that i.prev().next() == i. |
| 134 | + tr := treap{} |
| 135 | + for _, s := range spans { |
| 136 | + tr.Insert(s) |
| 137 | + } |
| 138 | + i := tr.Start().Next().Next() |
| 139 | + p := i.Prev() |
| 140 | + if !p.Valid() { |
| 141 | + t.Fatal("i.prev() is invalid") |
| 142 | + } |
| 143 | + if p.Next().Span() != i.Span() { |
| 144 | + t.Fatal("i.prev().next() != i") |
| 145 | + } |
| 146 | + for _, s := range spans { |
| 147 | + tr.RemoveSpan(s) |
| 148 | + } |
| 149 | + }) |
| 150 | + t.Run("Next", func(t *testing.T) { |
| 151 | + // Test the iterator invariant that i.next().prev() == i. |
| 152 | + tr := treap{} |
| 153 | + for _, s := range spans { |
| 154 | + tr.Insert(s) |
| 155 | + } |
| 156 | + i := tr.Start().Next().Next() |
| 157 | + n := i.Next() |
| 158 | + if !n.Valid() { |
| 159 | + t.Fatal("i.next() is invalid") |
| 160 | + } |
| 161 | + if n.Prev().Span() != i.Span() { |
| 162 | + t.Fatal("i.next().prev() != i") |
| 163 | + } |
| 164 | + for _, s := range spans { |
| 165 | + tr.RemoveSpan(s) |
| 166 | + } |
| 167 | + }) |
| 168 | + }) |
| 169 | + t.Run("EraseOne", func(t *testing.T) { |
| 170 | + // Test that erasing one iterator correctly retains |
| 171 | + // all relationships between elements. |
| 172 | + tr := treap{} |
| 173 | + for _, s := range spans { |
| 174 | + tr.Insert(s) |
| 175 | + } |
| 176 | + i := tr.Start().Next().Next().Next() |
| 177 | + s := i.Span() |
| 178 | + n := i.Next() |
| 179 | + p := i.Prev() |
| 180 | + tr.Erase(i) |
| 181 | + if n.Prev().Span() != p.Span() { |
| 182 | + t.Fatal("p, n := i.Prev(), i.Next(); n.prev() != p after i was erased") |
| 183 | + } |
| 184 | + if p.Next().Span() != n.Span() { |
| 185 | + t.Fatal("p, n := i.Prev(), i.Next(); p.next() != n after i was erased") |
| 186 | + } |
| 187 | + tr.Insert(s) |
| 188 | + for _, s := range spans { |
| 189 | + tr.RemoveSpan(s) |
| 190 | + } |
| 191 | + }) |
| 192 | + t.Run("EraseAll", func(t *testing.T) { |
| 193 | + // Test that erasing iterators actually removes nodes from the treap. |
| 194 | + tr := treap{} |
| 195 | + for _, s := range spans { |
| 196 | + tr.Insert(s) |
| 197 | + } |
| 198 | + for i := tr.Start(); i.Valid(); { |
| 199 | + n := i.Next() |
| 200 | + tr.Erase(i) |
| 201 | + i = n |
| 202 | + } |
| 203 | + if size := tr.Size(); size != 0 { |
| 204 | + t.Fatalf("should have emptied out treap, %d spans left", size) |
| 205 | + } |
| 206 | + }) |
| 207 | +} |
0 commit comments