Skip to content

Commit

Permalink
Port Card Number and Pangram to test{}
Browse files Browse the repository at this point in the history
This required a small tweak to outputTests() which I think is valid, we
skip over any empty strings in expected output. This allows these two
holes to have arguments that produce no output. I don't think any
current holes support empty string as an expected output for an arg.

The stutter in Card Number of repeating the valid hardcoded test cases
is a little unforunate but it's better than having yet another struct we
turn into test{}. Maybe it points to having a simple builder func that
takes an arg and a bool.

We now unconditionally print the arg separator in outputTests() which is
fine since we merged code-golf#1333 which unconditionally strips trailing
whitespace from every hole's expected output.
  • Loading branch information
JRaspass committed Sep 22, 2024
1 parent 4d21049 commit d2a2ae4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
46 changes: 15 additions & 31 deletions hole/card-number-validation.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package hole

import (
"strconv"
"strings"
)
import "strconv"

func cardNumberValidation() []Run {
type Case struct {
arg string
valid bool
}

digitSum := func(n int) int {
return n - n/10*9
}
Expand All @@ -34,13 +26,13 @@ func cardNumberValidation() []Run {
return result
}

cases := []Case{
{"4242 4242 4242 4242", true},
{"4242 4244 4242 4242", false},
{"5555 5555 5555 4444", true},
{"5555 5555 5555 5444", false},
{"3566 0020 2036 0505", true},
{"3656 0020 2036 0505", false},
tests := []test{
{"4242 4242 4242 4242", "4242 4242 4242 4242"},
{"4242 4244 4242 4242", ""},
{"5555 5555 5555 4444", "5555 5555 5555 4444"},
{"5555 5555 5555 5444", ""},
{"3566 0020 2036 0505", "3566 0020 2036 0505"},
{"3656 0020 2036 0505", ""},
}

for i := range 100 {
Expand All @@ -53,23 +45,15 @@ func cardNumberValidation() []Run {
if i < 50 {
lastDigit = randInt(0, 9)
}
cases = append(cases, Case{formatDigits(append(digits, lastDigit)), lastDigit == checkDigit})
}

shuffle(cases)
args := make([]string, len(cases))
var answer strings.Builder

for i, c := range cases {
args[i] = c.arg

if c.valid {
if answer.Len() > 0 {
answer.WriteByte('\n')
}
answer.WriteString(c.arg)
cardNumber := formatDigits(append(digits, lastDigit))
t := test{in: cardNumber}
if lastDigit == checkDigit {
t.out = cardNumber
}

tests = append(tests, t)
}

return []Run{{Args: args, Answer: answer.String()}}
return outputTests(shuffle(tests))
}
4 changes: 2 additions & 2 deletions hole/hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func outputTestsWithSep(sep string, testRuns ...[]test) []Run {
for i, t := range tests {
args[i] = t.in

if i > 0 {
if t.out != "" {
answer.WriteString(t.out)
answer.WriteString(sep)
}
answer.WriteString(t.out)
}

runs[i] = Run{Args: args, Answer: answer.String()}
Expand Down
20 changes: 8 additions & 12 deletions hole/pangram-grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

func pangramGrep() []Run {
return []Run{
return outputTests(
pangramGrepTests(2, 5),
pangramGrepTests(0, 0),
}
)
}

func pangramGrepTests(l, r int) Run {
func pangramGrepTests(l, r int) []test {
// They all start lowercase and valid.
pangrams := shuffle([][]byte{
[]byte("6>_4\"gv9lb?2!ic7}=-m'fd30ph].o%@w+[8unk&t1es<az(x;${^y#)q,rj\\5/*:"),
Expand Down Expand Up @@ -102,25 +102,21 @@ func pangramGrepTests(l, r int) Run {
}
}

var args []string
var out string
tests := make([]test, len(pangrams))

outer:
for _, pangram := range shuffle(pangrams) {
for i, pangram := range shuffle(pangrams) {
str := string(pangram)
args = append(args, str)
tests[i].in = str

for c := 'a'; c <= 'z'; c++ {
if !strings.ContainsRune(str, c) && !strings.ContainsRune(str, c-32) {
continue outer
}
}

out += str + "\n"
tests[i].out = str
}

// Drop the trailing newline.
out = out[:len(out)-1]

return Run{Args: args, Answer: out}
return tests
}

0 comments on commit d2a2ae4

Please sign in to comment.