diff options
| author | Guillermo Ramos <gramos@gramos.me> | 2021-03-31 13:05:25 +0200 |
|---|---|---|
| committer | Guillermo Ramos <gramos@gramos.me> | 2021-03-31 13:10:49 +0200 |
| commit | 100676d6d3968544f5a9bdfaedb6c687e11aefb1 (patch) | |
| tree | 534efb910dc47724e9012cf40cabd1969c9bbb18 /challenge-106 | |
| parent | b680e5bcef16416d5bd6de874efcd5a3987fe1b9 (diff) | |
| download | perlweeklychallenge-club-100676d6d3968544f5a9bdfaedb6c687e11aefb1.tar.gz perlweeklychallenge-club-100676d6d3968544f5a9bdfaedb6c687e11aefb1.tar.bz2 perlweeklychallenge-club-100676d6d3968544f5a9bdfaedb6c687e11aefb1.zip | |
Add tests
Diffstat (limited to 'challenge-106')
| -rw-r--r-- | challenge-106/guillermo-ramos/go/ch-1.go | 22 | ||||
| -rw-r--r-- | challenge-106/guillermo-ramos/go/ch-2.go | 25 | ||||
| -rw-r--r-- | challenge-106/guillermo-ramos/go/ch106_test.go | 42 | ||||
| -rw-r--r-- | challenge-106/guillermo-ramos/go/go.mod | 3 | ||||
| -rwxr-xr-x | challenge-106/guillermo-ramos/go/run_tests.sh | 3 |
5 files changed, 56 insertions, 39 deletions
diff --git a/challenge-106/guillermo-ramos/go/ch-1.go b/challenge-106/guillermo-ramos/go/ch-1.go index bb9ee58e9d..df60fc25a3 100644 --- a/challenge-106/guillermo-ramos/go/ch-1.go +++ b/challenge-106/guillermo-ramos/go/ch-1.go @@ -1,22 +1,8 @@ -package main +package ch106 -import ( - "fmt" - "os" - "sort" - "strconv" -) +import "sort" -func main() { - numbers := make(sort.IntSlice, 0, len(os.Args)-1) - for _, v := range os.Args[1:] { - n, err := strconv.Atoi(v) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - numbers = append(numbers, n) - } +func MaximumGap(numbers sort.IntSlice) int { sort.Sort(numbers) var maxGap int for i := 0; i < len(numbers)-1; i++ { @@ -25,5 +11,5 @@ func main() { maxGap = diff } } - fmt.Println(maxGap) + return maxGap } diff --git a/challenge-106/guillermo-ramos/go/ch-2.go b/challenge-106/guillermo-ramos/go/ch-2.go index 4450841303..14a16a677d 100644 --- a/challenge-106/guillermo-ramos/go/ch-2.go +++ b/challenge-106/guillermo-ramos/go/ch-2.go @@ -1,26 +1,12 @@ -package main +package ch106 import ( "fmt" - "os" "strconv" ) -func getIntArg(i int) int { - if i >= len(os.Args) { - fmt.Println("Not enough arguments") - os.Exit(1) - } - v, err := strconv.Atoi(os.Args[i]) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - return v -} - type PerioDec struct { - digits []int + digits []int periodicIdx int } @@ -40,10 +26,7 @@ func (d PerioDec) String() (s string) { return } -func main() { - n := getIntArg(1) - d := getIntArg(2) - +func DecimalString(n, d int) string { dec := PerioDec{periodicIdx: -1} numeratorIdxs := make(map[int]int) @@ -72,5 +55,5 @@ func main() { } - fmt.Println(dec) + return dec.String() } diff --git a/challenge-106/guillermo-ramos/go/ch106_test.go b/challenge-106/guillermo-ramos/go/ch106_test.go new file mode 100644 index 0000000000..ca80e137b7 --- /dev/null +++ b/challenge-106/guillermo-ramos/go/ch106_test.go @@ -0,0 +1,42 @@ +package ch106 + +import "testing" + +type MaxGapCase struct { + input []int + wants int +} + +func TestMaximumGap(t *testing.T) { + expected := []MaxGapCase{ + {[]int{2, 9, 3, 5}, 4}, + {[]int{1, 3, 8, 2, 0}, 5}, + {[]int{5}, 0}, + } + for _, c := range expected { + res := MaximumGap(c.input) + if res != c.wants { + t.Errorf("MaximumGap(%v) returned '%d' but expected '%d'", + c.input, res, c.wants) + } + } +} + +type DecStringInput struct { + n, d int +} + +func TestDecimalString(t *testing.T) { + expected := map[DecStringInput]string{ + {n: 1, d: 3}: "0.(3)", + {n: 1, d: 2}: "0.5", + {n: 5, d: 66}: "0.0(75)", + } + for input, wants := range expected { + res := DecimalString(input.n, input.d) + if res != wants { + t.Errorf("DecimalString(%d, %d) returned '%s' but expected '%s'", + input.n, input.d, res, wants) + } + } +} diff --git a/challenge-106/guillermo-ramos/go/go.mod b/challenge-106/guillermo-ramos/go/go.mod new file mode 100644 index 0000000000..dd59d04ad4 --- /dev/null +++ b/challenge-106/guillermo-ramos/go/go.mod @@ -0,0 +1,3 @@ +module example.com/ch106 + +go 1.16 diff --git a/challenge-106/guillermo-ramos/go/run_tests.sh b/challenge-106/guillermo-ramos/go/run_tests.sh new file mode 100755 index 0000000000..20e7774e09 --- /dev/null +++ b/challenge-106/guillermo-ramos/go/run_tests.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +go test -v |
