From a89d5d52ecdc78efefaa4a9dbd2ccd9a369ff02b Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Mon, 3 Sep 2012 21:34:18 -0600 Subject: [PATCH] moved walk/same into a package, and added a true test file --- ebt/ebt.go | 38 ++++++++++++++++++++++++++++++++++++++ ebt/ebt_test.go | 18 ++++++++++++++++++ exercises/08-ebt/go.go | 40 ++++------------------------------------ 3 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 ebt/ebt.go create mode 100644 ebt/ebt_test.go diff --git a/ebt/ebt.go b/ebt/ebt.go new file mode 100644 index 0000000..36139a4 --- /dev/null +++ b/ebt/ebt.go @@ -0,0 +1,38 @@ +package ebt + +import ( + "code.google.com/p/go-tour/tree" +) + +// Walk walks the tree t sending all values +// from the tree to the channel ch. +func Walk(t *tree.Tree, ch chan int) { + // do it as a nested closure so that I can control the call to close so + // that I can use the range-based forloop in main.main + var walk func(t *tree.Tree) + walk = func(t *tree.Tree) { + if t != nil { + walk(t.Left) + ch <- t.Value + walk(t.Right) + } + } + walk(t) + close(ch) +} + +// Same determines whether the trees +// t1 and t2 contain the same values. +func Same(t1, t2 *tree.Tree) bool { + ch1 := make(chan int) + ch2 := make(chan int) + go Walk(t1, ch1) + go Walk(t2, ch2) + for i := range ch1 { + if i != <-ch2 { + return false + } + } + return true +} + diff --git a/ebt/ebt_test.go b/ebt/ebt_test.go new file mode 100644 index 0000000..2584610 --- /dev/null +++ b/ebt/ebt_test.go @@ -0,0 +1,18 @@ +package ebt + +import ( + "code.google.com/p/go-tour/tree" + "testing" +) + +func TestSameTrees(t *testing.T) { + if !Same(tree.New(1), tree.New(1)) { + t.Error("Trees should have been the same ...") + } +} + +func TestDifferentTrees(t *testing.T) { + if Same(tree.New(1), tree.New(2)) { + t.Error("Trees should have been the different ...") + } +} diff --git a/exercises/08-ebt/go.go b/exercises/08-ebt/go.go index cb48d62..feff354 100644 --- a/exercises/08-ebt/go.go +++ b/exercises/08-ebt/go.go @@ -2,49 +2,17 @@ package main import ( "code.google.com/p/go-tour/tree" + "gotour-notes/ebt" "fmt" ) - -// Walk walks the tree t sending all values -// from the tree to the channel ch. -func Walk(t *tree.Tree, ch chan int) { - // do it as a nested closure so that I can control the call to close so - // that I can use the range-based forloop in main.main - var walk func(t *tree.Tree) - walk = func(t *tree.Tree) { - if t != nil { - walk(t.Left) - ch <- t.Value - walk(t.Right) - } - } - walk(t) - close(ch) -} - -// Same determines whether the trees -// t1 and t2 contain the same values. -func Same(t1, t2 *tree.Tree) bool { - ch1 := make(chan int) - ch2 := make(chan int) - go Walk(t1, ch1) - go Walk(t2, ch2) - for i := range ch1 { - if i != <-ch2 { - return false - } - } - return true -} - func main() { t := tree.New(1) ch := make(chan int) - go Walk(t, ch) + go ebt.Walk(t, ch) for i := range ch { fmt.Printf("%d, ", i) } fmt.Println() - fmt.Println(Same(tree.New(1), tree.New(1))) - fmt.Println(Same(tree.New(1), tree.New(2))) + fmt.Println(ebt.Same(tree.New(1), tree.New(1))) + fmt.Println(ebt.Same(tree.New(1), tree.New(2))) }