|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "../../../../gen" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "text/template" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + t, err := template.New("").Parse(tmpl) |
| 12 | + if err != nil { |
| 13 | + log.Fatal(err) |
| 14 | + } |
| 15 | + j := map[string]any{ |
| 16 | + "abs": &[]testCaseUnaryRationalToRational{}, |
| 17 | + "add": &[]testCaseBinaryRationalToRational{}, |
| 18 | + "div": &[]testCaseBinaryRationalToRational{}, |
| 19 | + "mul": &[]testCaseBinaryRationalToRational{}, |
| 20 | + "sub": &[]testCaseBinaryRationalToRational{}, |
| 21 | + "exprational": &[]testCaseExprational{}, |
| 22 | + "expreal": &[]testCaseExpreal{}, |
| 23 | + "reduce": &[]testCaseUnaryRationalToRational{}, |
| 24 | + } |
| 25 | + if err := gen.Gen("rational-numbers", j, t); err != nil { |
| 26 | + log.Fatal(err) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +type Rational [2]int |
| 31 | + |
| 32 | +func (r Rational) String() string { |
| 33 | + return fmt.Sprintf("Rational{%d, %d}", r[0], r[1]) |
| 34 | +} |
| 35 | + |
| 36 | +type RationalPair struct { |
| 37 | + R1 Rational `json:"r1"` |
| 38 | + R2 Rational `json:"r2"` |
| 39 | +} |
| 40 | + |
| 41 | +func (r RationalPair) String() string { |
| 42 | + return fmt.Sprintf("[2]Rational{{%d, %d}, {%d, %d}}", r.R1[0], r.R1[1], r.R2[0], r.R2[1]) |
| 43 | +} |
| 44 | + |
| 45 | +type testCaseUnaryRationalToRational struct { |
| 46 | + Description string `json:"description"` |
| 47 | + Input struct { |
| 48 | + R Rational `json:"r"` |
| 49 | + } `json:"input"` |
| 50 | + Expected Rational `json:"expected"` |
| 51 | +} |
| 52 | + |
| 53 | +type testCaseBinaryRationalToRational struct { |
| 54 | + Description string `json:"description"` |
| 55 | + Input RationalPair `json:"input"` |
| 56 | + Expected Rational `json:"expected"` |
| 57 | +} |
| 58 | + |
| 59 | +type testCaseExprational struct { |
| 60 | + Description string `json:"description"` |
| 61 | + Input struct { |
| 62 | + R Rational `json:"r"` |
| 63 | + N int `json:"n"` |
| 64 | + } `json:"input"` |
| 65 | + Expected Rational `json:"expected"` |
| 66 | +} |
| 67 | + |
| 68 | +type testCaseExpreal struct { |
| 69 | + Description string `json:"description"` |
| 70 | + Input struct { |
| 71 | + X int `json:"x"` |
| 72 | + R Rational `json:"r"` |
| 73 | + } `json:"input"` |
| 74 | + Expected float64 `json:"expected"` |
| 75 | +} |
| 76 | + |
| 77 | +var tmpl = `{{.Header}} |
| 78 | +
|
| 79 | +var testCasesAbs = []struct { |
| 80 | + description string |
| 81 | + input Rational |
| 82 | + expected Rational |
| 83 | +}{ |
| 84 | +{{range .J.abs}}{ |
| 85 | + description: {{printf "%q" .Description}}, |
| 86 | + input: {{.Input.R}}, |
| 87 | + expected: {{.Expected}}, |
| 88 | +}, |
| 89 | +{{end}} |
| 90 | +} |
| 91 | +
|
| 92 | +var testCasesAdd = []struct { |
| 93 | + description string |
| 94 | + input [2]Rational |
| 95 | + expected Rational |
| 96 | +}{ |
| 97 | +{{range .J.add}}{ |
| 98 | + description: {{printf "%q" .Description}}, |
| 99 | + input: {{.Input}}, |
| 100 | + expected: {{.Expected}}, |
| 101 | +}, |
| 102 | +{{end}} |
| 103 | +} |
| 104 | +
|
| 105 | +var testCasesSub = []struct { |
| 106 | + description string |
| 107 | + input [2]Rational |
| 108 | + expected Rational |
| 109 | +}{ |
| 110 | +{{range .J.sub}}{ |
| 111 | + description: {{printf "%q" .Description}}, |
| 112 | + input: {{.Input}}, |
| 113 | + expected: {{.Expected}}, |
| 114 | +}, |
| 115 | +{{end}} |
| 116 | +} |
| 117 | +
|
| 118 | +var testCasesMul = []struct { |
| 119 | + description string |
| 120 | + input [2]Rational |
| 121 | + expected Rational |
| 122 | +}{ |
| 123 | +{{range .J.mul}}{ |
| 124 | + description: {{printf "%q" .Description}}, |
| 125 | + input: {{.Input}}, |
| 126 | + expected: {{.Expected}}, |
| 127 | +}, |
| 128 | +{{end}} |
| 129 | +} |
| 130 | +
|
| 131 | +var testCasesDiv = []struct { |
| 132 | + description string |
| 133 | + input [2]Rational |
| 134 | + expected Rational |
| 135 | +}{ |
| 136 | +{{range .J.div}}{ |
| 137 | + description: {{printf "%q" .Description}}, |
| 138 | + input: {{.Input}}, |
| 139 | + expected: {{.Expected}}, |
| 140 | +}, |
| 141 | +{{end}} |
| 142 | +} |
| 143 | +
|
| 144 | +var testCasesExprational = []struct { |
| 145 | + description string |
| 146 | + inputR Rational |
| 147 | + inputInt int |
| 148 | + expected Rational |
| 149 | +}{ |
| 150 | +{{range .J.exprational}}{ |
| 151 | + description: {{printf "%q" .Description}}, |
| 152 | + inputR: {{.Input.R}}, |
| 153 | + inputInt: {{.Input.N}}, |
| 154 | + expected: {{.Expected}}, |
| 155 | +}, |
| 156 | +{{end}} |
| 157 | +} |
| 158 | +
|
| 159 | +var testCasesExpreal = []struct { |
| 160 | + description string |
| 161 | + inputInt int |
| 162 | + inputR Rational |
| 163 | + expected float64 |
| 164 | +}{ |
| 165 | +{{range .J.expreal}}{ |
| 166 | + description: {{printf "%q" .Description}}, |
| 167 | + inputInt: {{.Input.X}}, |
| 168 | + inputR: {{.Input.R}}, |
| 169 | + expected: {{.Expected}}, |
| 170 | +}, |
| 171 | +{{end}} |
| 172 | +} |
| 173 | +
|
| 174 | +var testCasesReduce = []struct { |
| 175 | + description string |
| 176 | + input Rational |
| 177 | + expected Rational |
| 178 | +}{ |
| 179 | +{{range .J.reduce}}{ |
| 180 | + description: {{printf "%q" .Description}}, |
| 181 | + input: {{.Input.R}}, |
| 182 | + expected: {{.Expected}}, |
| 183 | +}, |
| 184 | +{{end}} |
| 185 | +} |
| 186 | +` |
0 commit comments