factor_test.gno
1.48 Kb · 77 lines
1package factor
2
3import "testing"
4
5func TestInputsInRange(t *testing.T) {
6 ins := Inputs(1)
7 if len(ins) != 6 {
8 t.Fatalf("want 6 inputs, got %d", len(ins))
9 }
10 for _, in := range ins {
11 if !Check(in, factorize(in)) {
12 t.Fatalf("own factorization of %q rejected", in)
13 }
14 }
15 if Inputs(2)[0] == ins[0] {
16 t.Fatal("different seeds must give different inputs")
17 }
18}
19
20func TestCheck(t *testing.T) {
21 cases := []struct {
22 in, out string
23 want bool
24 }{
25 {"84", "2*2*3*7", true},
26 {"13", "13", true},
27 {"84", "7*3*2*2", false}, // not ascending
28 {"84", "2*2*21", false}, // 21 is not prime
29 {"84", "2*2*3", false}, // product too small
30 {"84", "2*2*3*7*1", false}, // 1 is not prime
31 {"84", "84", false}, // 84 is not prime
32 {"84", "", false},
33 {"1", "1", false}, // out of range
34 }
35 for _, c := range cases {
36 if got := Check(c.in, c.out); got != c.want {
37 t.Fatalf("Check(%q, %q) = %v, want %v", c.in, c.out, got, c.want)
38 }
39 }
40}
41
42// factorize is only used by the test, to prove generated inputs are solvable.
43func factorize(in string) string {
44 n := uint64(0)
45 for i := 0; i < len(in); i++ {
46 n = n*10 + uint64(in[i]-'0')
47 }
48 out := ""
49 for d := uint64(2); d*d <= n; d++ {
50 for n%d == 0 {
51 if out != "" {
52 out += "*"
53 }
54 out += utoa(d)
55 n /= d
56 }
57 }
58 if n > 1 {
59 if out != "" {
60 out += "*"
61 }
62 out += utoa(n)
63 }
64 return out
65}
66
67func utoa(n uint64) string {
68 if n == 0 {
69 return "0"
70 }
71 s := ""
72 for n > 0 {
73 s = string(rune('0'+n%10)) + s
74 n /= 10
75 }
76 return s
77}