| 1 |
package txn |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"gopkg.in/mgo.v2/bson" |
| 6 |
. "gopkg.in/check.v1" |
| 7 |
) |
| 8 |
|
| 9 |
type TarjanSuite struct{} |
| 10 |
|
| 11 |
var _ = Suite(TarjanSuite{}) |
| 12 |
|
| 13 |
func bid(n int) bson.ObjectId { |
| 14 |
return bson.ObjectId(fmt.Sprintf("%024d", n)) |
| 15 |
} |
| 16 |
|
| 17 |
func bids(ns ...int) (ids []bson.ObjectId) { |
| 18 |
for _, n := range ns { |
| 19 |
ids = append(ids, bid(n)) |
| 20 |
} |
| 21 |
return |
| 22 |
} |
| 23 |
|
| 24 |
func (TarjanSuite) TestExample(c *C) { |
| 25 |
successors := map[bson.ObjectId][]bson.ObjectId{ |
| 26 |
bid(1): bids(2, 3), |
| 27 |
bid(2): bids(1, 5), |
| 28 |
bid(3): bids(4), |
| 29 |
bid(4): bids(3, 5), |
| 30 |
bid(5): bids(6), |
| 31 |
bid(6): bids(7), |
| 32 |
bid(7): bids(8), |
| 33 |
bid(8): bids(6, 9), |
| 34 |
bid(9): bids(), |
| 35 |
} |
| 36 |
|
| 37 |
c.Assert(tarjanSort(successors), DeepEquals, [][]bson.ObjectId{ |
| 38 |
bids(9), |
| 39 |
bids(6, 7, 8), |
| 40 |
bids(5), |
| 41 |
bids(3, 4), |
| 42 |
bids(1, 2), |
| 43 |
}) |
| 44 |
} |