tests: add test of sumtype-based binary tree (#8121)

pull/8132/head
Ruofan XU 2021-01-15 21:10:30 +08:00 committed by GitHub
parent 8d014d4646
commit 5de287a6e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 0 deletions

View File

@ -539,3 +539,39 @@ fn handle(e Expr) string {
} }
return '' return ''
} }
// for a binary tree
struct Empty {}
struct Node_ {
// TODO: make value generic once it's more robust
value f64
left Tree
right Tree
}
type Tree = Empty | Node_
fn size(tree Tree) int {
return match tree {
// TODO: remove int() here once match gets smarter
Empty { int(0) }
Node_ { 1 + size(tree.left) + size(tree.right) }
}
}
fn sum(tree Tree) f64 {
return match tree {
// TODO: remove f64() here once match gets smarter
Empty { f64(0) }
Node_ { tree.value + sum(tree.left) + sum(tree.right) }
}
}
fn test_binary_tree_operation() {
left := Node_{0.2, Empty{}, Empty{}}
right := Node_{0.3, Empty{}, Node_{0.4, Empty{}, Empty{}}}
tree := Node_{0.5, left, right}
assert size(tree) == 4
assert sum(tree) == 1.4
}