MCPcopy Index your code
hub / github.com/EndlessCheng/codeforces-go / RunLeetCodeFuncWithExamples

Function RunLeetCodeFuncWithExamples

leetcode/testutil/leetcode.go:234–314  ·  view source on GitHub ↗

rawExamples[i] = 输入+输出 若反射出来的函数或 rawExamples 数据不合法,则会返回一个非空的 error,否则返回 nil

(t *testing.T, f interface{}, rawExamples [][]string, targetCaseNum int)

Source from the content-addressed store, hash-verified

232// rawExamples[i] = 输入+输出
233// 若反射出来的函数或 rawExamples 数据不合法,则会返回一个非空的 error,否则返回 nil
234func RunLeetCodeFuncWithExamples(t *testing.T, f interface{}, rawExamples [][]string, targetCaseNum int) (err error) {
235 fType := reflect.TypeOf(f)
236 if fType.Kind() != reflect.Func {
237 return fmt.Errorf("f must be a function")
238 }
239
240 // 例如,-1 表示最后一个测试用例
241 if targetCaseNum < 0 {
242 targetCaseNum += len(rawExamples) + 1
243 }
244
245 allCasesOk := true
246 fValue := reflect.ValueOf(f)
247 for curCaseNum, example := range rawExamples {
248 if targetCaseNum > 0 && curCaseNum+1 != targetCaseNum {
249 continue
250 }
251
252 if len(example) != fType.NumIn()+fType.NumOut() {
253 return fmt.Errorf("len(example) = %d, but we need %d+%d", len(example), fType.NumIn(), fType.NumOut())
254 }
255
256 rawIn := example[:fType.NumIn()]
257 ins := make([]reflect.Value, len(rawIn))
258 for i, rawArg := range rawIn {
259 rawArg = trimSpaceAndNewLine(rawArg)
260 ins[i], err = parseRawArg(fType.In(i), rawArg)
261 if err != nil {
262 return
263 }
264 }
265 // just check rawExpectedOuts is valid or not
266 rawExpectedOuts := example[fType.NumIn():]
267 for i := range rawExpectedOuts {
268 rawExpectedOuts[i] = trimSpaceAndNewLine(rawExpectedOuts[i])
269 if _, err = parseRawArg(fType.Out(i), rawExpectedOuts[i]); err != nil {
270 return
271 }
272 }
273
274 const maxInputSize = 150
275 inputInfo := strings.Join(rawIn, "\n")
276 if len(inputInfo) > maxInputSize { // 截断过长的输入
277 inputInfo = inputInfo[:maxInputSize] + "..."
278 }
279
280 var outs []reflect.Value
281 _f := func() { outs = fValue.Call(ins) }
282 if targetCaseNum == 0 && isTLE(_f) {
283 allCasesOk = false
284 t.Errorf("Time Limit Exceeded %d\nInput:\n%s", curCaseNum+1, inputInfo)
285 continue
286 } else if targetCaseNum != 0 {
287 _f()
288 }
289
290 for i, out := range outs {
291 rawActualOut, er := toRawString(out)

Callers 15

TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92

Calls 4

trimSpaceAndNewLineFunction · 0.85
parseRawArgFunction · 0.85
toRawStringFunction · 0.85
isTLEFunction · 0.70

Tested by 15

TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74