| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package treapstore | 5 package treapstore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "strconv" | 9 "strconv" |
| 10 "strings" | 10 "strings" |
| 11 "testing" | 11 "testing" |
| 12 | 12 |
| 13 "github.com/luci/gtreap" | 13 "github.com/luci/gtreap" |
| 14 | 14 |
| 15 . "github.com/smartystreets/goconvey/convey" | 15 . "github.com/smartystreets/goconvey/convey" |
| 16 ) | 16 ) |
| 17 | 17 |
| 18 func stringCompare(a, b interface{}) int { | 18 func stringCompare(a, b interface{}) int { |
| 19 return strings.Compare(a.(string), b.(string)) | 19 return strings.Compare(a.(string), b.(string)) |
| 20 } | 20 } |
| 21 | 21 |
| 22 func putMulti(c *Collection, vs ...string) { | 22 func putMulti(c *Collection, vs ...string) { |
| 23 for _, v := range vs { | 23 for _, v := range vs { |
| 24 c.Put(v) | 24 c.Put(v) |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 func visitAll(c *Collection, pivot string) []string { |
| 29 res := []string{} |
| 30 c.VisitAscend(pivot, func(v gtreap.Item) bool { |
| 31 res = append(res, v.(string)) |
| 32 return true |
| 33 }) |
| 34 return res |
| 35 } |
| 36 |
| 28 func iterAll(it *gtreap.Iterator) []string { | 37 func iterAll(it *gtreap.Iterator) []string { |
| 29 all := []string{} | 38 all := []string{} |
| 30 for { | 39 for { |
| 31 v, ok := it.Next() | 40 v, ok := it.Next() |
| 32 if !ok { | 41 if !ok { |
| 33 return all | 42 return all |
| 34 } | 43 } |
| 35 all = append(all, v.(string)) | 44 all = append(all, v.(string)) |
| 36 } | 45 } |
| 37 } | 46 } |
| 38 | 47 |
| 48 func shouldHaveKeys(actual interface{}, expected ...interface{}) string { |
| 49 c := actual.(*Collection) |
| 50 |
| 51 // expected can either be a single []string or a series of strings. |
| 52 var keys []string |
| 53 var ok bool |
| 54 if len(expected) == 1 { |
| 55 keys, ok = expected[0].([]string) |
| 56 } |
| 57 if !ok { |
| 58 keys = make([]string, len(expected)) |
| 59 for i, v := range expected { |
| 60 keys[i] = v.(string) |
| 61 } |
| 62 } |
| 63 |
| 64 if err := ShouldResemble(iterAll(c.Iterator("")), keys); err != "" { |
| 65 return fmt.Sprintf("failed via iterator: %s", err) |
| 66 } |
| 67 if err := ShouldResemble(visitAll(c, ""), keys); err != "" { |
| 68 return fmt.Sprintf("failed via visit: %s", err) |
| 69 } |
| 70 return "" |
| 71 } |
| 72 |
| 39 func TestStore(t *testing.T) { | 73 func TestStore(t *testing.T) { |
| 40 t.Parallel() | 74 t.Parallel() |
| 41 | 75 |
| 42 Convey(`Testing a string Store`, t, func() { | 76 Convey(`Testing a string Store`, t, func() { |
| 43 st := New() | 77 st := New() |
| 44 coll := st.CreateCollection("test", stringCompare) | 78 coll := st.CreateCollection("test", stringCompare) |
| 45 | 79 |
| 46 Convey(`When empty`, func() { | 80 Convey(`When empty`, func() { |
| 47 checkEmpty := func(c *Collection) { | 81 checkEmpty := func(c *Collection) { |
| 48 So(c.Get("foo"), ShouldBeNil) | 82 So(c.Get("foo"), ShouldBeNil) |
| 49 » » » » So(iterAll(c.Iterator("")), ShouldResemble, []st
ring{}) | 83 » » » » So(c, shouldHaveKeys) |
| 50 } | 84 } |
| 51 | 85 |
| 52 // Check the basic Store. | 86 // Check the basic Store. |
| 53 checkEmpty(coll) | 87 checkEmpty(coll) |
| 54 | 88 |
| 55 // Take a snapshot, then mutate the base Store. Assert t
hat the snapshot | 89 // Take a snapshot, then mutate the base Store. Assert t
hat the snapshot |
| 56 // is still empty. | 90 // is still empty. |
| 57 snap := st.Snapshot() | 91 snap := st.Snapshot() |
| 58 coll.Put("foo") | 92 coll.Put("foo") |
| 59 checkEmpty(snap.GetCollection("test")) | 93 checkEmpty(snap.GetCollection("test")) |
| 60 }) | 94 }) |
| 61 | 95 |
| 62 Convey(`With keys`, func() { | 96 Convey(`With keys`, func() { |
| 63 putMulti(coll, "x", "w", "b", "a") | 97 putMulti(coll, "x", "w", "b", "a") |
| 64 | 98 |
| 65 Convey(`Can iterate`, func() { | 99 Convey(`Can iterate`, func() { |
| 66 checkKeys := func(coll *Collection, keys ...stri
ng) { | 100 checkKeys := func(coll *Collection, keys ...stri
ng) { |
| 67 for _, k := range keys { | 101 for _, k := range keys { |
| 68 So(coll.Get(k), ShouldEqual, k) | 102 So(coll.Get(k), ShouldEqual, k) |
| 69 } | 103 } |
| 70 | 104 |
| 71 » » » » » So(iterAll(coll.Iterator("")), ShouldRes
emble, keys) | 105 » » » » » So(coll, shouldHaveKeys, keys) |
| 72 for i, k := range keys { | 106 for i, k := range keys { |
| 73 So(iterAll(coll.Iterator(k)), Sh
ouldResemble, keys[i:]) | 107 So(iterAll(coll.Iterator(k)), Sh
ouldResemble, keys[i:]) |
| 74 So(iterAll(coll.Iterator(k+"1"))
, ShouldResemble, keys[i+1:]) | 108 So(iterAll(coll.Iterator(k+"1"))
, ShouldResemble, keys[i+1:]) |
| 75 } | 109 } |
| 76 } | 110 } |
| 77 checkKeys(coll, "a", "b", "w", "x") | 111 checkKeys(coll, "a", "b", "w", "x") |
| 78 | 112 |
| 79 // Take a snapshot, then mutate the base Store.
Assert that the snapshot | 113 // Take a snapshot, then mutate the base Store.
Assert that the snapshot |
| 80 // is still empty. | 114 // is still empty. |
| 81 snap := st.Snapshot() | 115 snap := st.Snapshot() |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // The first time around, we *could* read before
anything has been | 300 // The first time around, we *could* read before
anything has been |
| 267 // written. Every other time, something from the
previous round will | 301 // written. Every other time, something from the
previous round will |
| 268 // have been written. | 302 // have been written. |
| 269 check = ShouldBeGreaterThanOrEqualTo | 303 check = ShouldBeGreaterThanOrEqualTo |
| 270 } | 304 } |
| 271 So(<-readDoneC, check, 0) | 305 So(<-readDoneC, check, 0) |
| 272 snaps = append(snaps, head.Snapshot()) | 306 snaps = append(snaps, head.Snapshot()) |
| 273 } | 307 } |
| 274 }) | 308 }) |
| 275 } | 309 } |
| OLD | NEW |