質問編集履歴

1

テストがPASSしたコードを記載

2021/03/17 14:02

投稿

ikutyan46
ikutyan46

スコア18

test CHANGED
File without changes
test CHANGED
@@ -129,3 +129,109 @@
129
129
  ・・・以後も取得するのは「0」
130
130
 
131
131
  ```
132
+
133
+
134
+
135
+ # 実際にPASSしたコード
136
+
137
+
138
+
139
+ ### main.go
140
+
141
+
142
+
143
+ ```go
144
+
145
+ package main
146
+
147
+
148
+
149
+ import "fmt"
150
+
151
+
152
+
153
+ func hoge(a, b, x int, f func(int, int, int) int) int {
154
+
155
+ return f(a, b, x)
156
+
157
+ }
158
+
159
+
160
+
161
+ func fuga(a, b, x int) int {
162
+
163
+ return a*x + b
164
+
165
+ }
166
+
167
+ // 一例
168
+
169
+ func main() {
170
+
171
+ ret := hoge(1,2,3, fuga)
172
+
173
+ fmt.Println(ret)
174
+
175
+ }
176
+
177
+
178
+
179
+ ```
180
+
181
+
182
+
183
+ ### main_test.go
184
+
185
+
186
+
187
+ ```go
188
+
189
+ package main
190
+
191
+
192
+
193
+ import (
194
+
195
+ "testing"
196
+
197
+ )
198
+
199
+
200
+
201
+ func TestLinearFunction(t *testing.T) {
202
+
203
+ tests := []struct {
204
+
205
+ a int
206
+
207
+ b int
208
+
209
+ x int
210
+
211
+ expected int
212
+
213
+ }{
214
+
215
+ {1,1,1,2},{2,1,1,3},{1,2,1,3},{1,1,2,3},
216
+
217
+ {2,2,1,4},{2,1,2,5},{1,2,2,4},{2,2,2,6},
218
+
219
+ }
220
+
221
+
222
+
223
+ for _, test := range tests {
224
+
225
+ result := hoge(test.a,test.b,test.x,fuga)
226
+
227
+ if result != test.expected {
228
+
229
+ t.Errorf("input: a=%d b=%d x=%d, expected:%d, got:%d",test.a,test.b,test.x,test.expected,result)
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+ ```