Breaking things out, adding test, all that good crap

This commit is contained in:
Dan Buch 2012-11-18 08:06:50 -05:00
parent f3712555d5
commit 631472da41
4 changed files with 33 additions and 10 deletions

View File

@ -1,6 +1,8 @@
CLEAN_GOPATH := $(shell echo $(GOPATH) | tr ":" "\n" | grep -v '^$$' | grep -v $(PWD) | tr "\n" ":") CLEAN_GOPATH := $(shell echo $(GOPATH) | tr ":" "\n" | grep -v '^$$' | grep -v $(PWD) | tr "\n" ":")
GOPATH := $(PWD):$(CLEAN_GOPATH) GOPATH := $(PWD):$(CLEAN_GOPATH)
PACKAGES := meatballhat.com/algs4-gcd PACKAGES := \
meatballhat.com/algs4 \
meatballhat.com/algs4-gcd
test: build test: build
go test $(PACKAGES) go test $(PACKAGES)

View File

@ -6,15 +6,11 @@ import (
"strconv" "strconv"
) )
const USAGE string = `Usage: algs4-gcd <int> <int>` import (
"meatballhat.com/algs4"
)
func gcd(p, q uint64) uint64 { const USAGE string = `Usage: algs4-gcd <uint> <uint>`
if q == 0 {
return p
}
r := p % q
return gcd(q, r)
}
func main() { func main() {
if len(os.Args) < 3 { if len(os.Args) < 3 {
@ -34,5 +30,5 @@ func main() {
os.Exit(1) os.Exit(1)
} }
fmt.Println(gcd(p, q)) fmt.Println(algs4.Gcd(p, q))
} }

View File

@ -0,0 +1,9 @@
package algs4
func Gcd(p, q uint64) uint64 {
if q == 0 {
return p
}
r := p % q
return Gcd(q, r)
}

View File

@ -0,0 +1,16 @@
package algs4_test
import (
"testing"
)
import (
"meatballhat.com/algs4"
)
func TestGcd(t *testing.T) {
d := algs4.Gcd(uint64(81), uint64(72))
if d != 9 {
t.Error("WRONG")
}
}