Skip to content

Commit

Permalink
first round of tree tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Apr 25, 2023
1 parent f076d8a commit 5c18ea9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 72 deletions.
5 changes: 2 additions & 3 deletions cmd/golem/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func main() {

func motd() {
fmt.Printf(`
$$\
$$ |
$$\
$$ |
$$$$$$\ $$$$$$\ $$ | $$$$$$\ $$$$$$\$$$$\
$$ __$$\ $$ __$$\ $$ |$$ __$$\ $$ _$$ _$$\
$$ / $$ |$$ / $$ |$$ |$$$$$$$$ |$$ / $$ / $$ |
Expand All @@ -77,6 +77,5 @@ func motd() {
$$\ $$ |
\$$$$$$ |
\______/
`)
}
17 changes: 1 addition & 16 deletions pkg/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,8 @@ func Start(
// TODO: drain channels and cleanup
return
case r := <-reads:
if r.OrderID != "" {
// TODO binary traversal to find order by ID
}
if r.Side == "buy" {
// found, err := book.buy.Find(r.Price)
// if err != nil {
// errs <- err
// r.Result <- ReadResult{
// Order: Order{},
// Err: err,
// }
// continue
// }
r.Result <- ReadResult{
Order: Order{},
Err: nil,
}
panic("not impl")
}
case w := <-writes:
if w.Side == "buy" {
Expand Down
2 changes: 0 additions & 2 deletions pkg/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
var numOps int = 10_000
var bufferSize int = 1000

var statsTotal int = 0

func TestRun(t *testing.T) {
ctx := context.Background()
wg := &sync.WaitGroup{}
Expand Down
27 changes: 20 additions & 7 deletions pkg/orderbook/tree.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package orderbook

import "fmt"

type Node struct {
Price uint64
Orders []*Order
Expand All @@ -14,14 +16,10 @@ func NewNode(price uint64) *Node {
}
}

func (n *Node) AddOrder(order *Order) {
n.Orders = append(n.Orders, order)
}

func (n *Node) Insert(order *Order) *Node {
if n == nil {
node := NewNode(order.Price)
return node.Insert(order)
n = NewNode(order.Price)
return n.Insert(order)
}

switch {
Expand All @@ -47,9 +45,15 @@ func (n *Node) RemoveOrder(orderID string) bool {
return false
}

func (n *Node) AddOrder(order *Order) {
n.Orders = append(n.Orders, order)
}

// Find returns the node for a given price.
func (n *Node) Find(price uint64) *Node {
if n == nil {
return nil
n := NewNode(price)
return n
}
if price == n.Price {
return n
Expand All @@ -59,3 +63,12 @@ func (n *Node) Find(price uint64) *Node {
return n.Right.Find(price)
}
}

func (n *Node) Print() {
if n == nil {
return
}
n.Left.Print()
fmt.Printf("Price: %d, Orders: %v\n", n.Price, n.Orders)
n.Right.Print()
}
87 changes: 43 additions & 44 deletions pkg/orderbook/tree_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
package orderbook

import "testing"
import (
"testing"

"github.com/matryer/is"
)

func TestInsert(t *testing.T) {
is := is.New(t)
root := NewNode(10)

order1 := &Order{ID: "1", Price: 5}
order2 := &Order{ID: "2", Price: 15}
order3 := &Order{ID: "3", Price: 8}
order4 := &Order{ID: "4", Price: 12}
order5 := &Order{ID: "5", Price: 12}
order6 := &Order{ID: "6", Price: 12}

root.Insert(order1)
root.Insert(order2)
root.Insert(order3)
root.Insert(order4)
root.Insert(order5)
root.Insert(order6)

root.Print()

ten := root.Find(10)
is.Equal(len(ten.Orders), 0)
}

func TestFind(t *testing.T) {
is := is.New(t)
root := NewNode(10)

order1 := &Order{ID: "1", Price: 5}
order2 := &Order{ID: "2", Price: 15}
order3 := &Order{ID: "3", Price: 8}
order4 := &Order{ID: "4", Price: 12}
order5 := &Order{ID: "5", Price: 12}
order6 := &Order{ID: "6", Price: 12}

root.Insert(order1)
root.Insert(order2)
root.Insert(order3)
root.Insert(order4)
root.Insert(order5)
root.Insert(order6)

n := root.Find(12)
is.Equal(len(n.Orders), 3)

if root.Price != 10 {
t.Errorf("Expected root price to be 10, got %v", root.Price)
}

if len(root.Orders) != 0 {
t.Errorf("Expected root orders to be empty, got %v", root.Orders)
}

left := root.Left
if left == nil || left.Price != 5 {
t.Errorf("Expected node with price 5 to exist on the left of root")
}

if len(left.Orders) != 1 || left.Orders[0].ID != "1" {
t.Errorf("Expected node with price 5 to have order with ID 1")
}

right := root.Right
if right == nil || right.Price != 15 {
t.Errorf("Expected node with price 15 to exist on the right of root")
}

if len(right.Orders) != 0 {
t.Errorf("Expected node with price 15 to have no orders")
}

rightLeft := right.Left
if rightLeft == nil || rightLeft.Price != 12 {
t.Errorf("Expected node with price 12 to exist on the left of node with price 15")
}

if len(rightLeft.Orders) != 1 || rightLeft.Orders[0].ID != "4" {
t.Errorf("Expected node with price 12 to have order with ID 4")
}

rightRight := right.Right
if rightRight == nil || rightRight.Price != 8 {
t.Errorf("Expected node with price 8 to exist on the right of node with price 15")
}

if len(rightRight.Orders) != 1 || rightRight.Orders[0].ID != "3" {
t.Errorf("Expected node with price 8 to have order with ID 3")
}
// Finding a root that doesn't exist should create it and return the root.
n = root.Find(9)
n.Print()
is.Equal(n.Price, uint64(9))
is.Equal(len(n.Orders), 0)
}

0 comments on commit 5c18ea9

Please sign in to comment.