@@ -65,6 +65,32 @@ func (c *DiffChecker) Check(got, expected string) error {
65
65
return nil
66
66
}
67
67
68
+ // Implementation of a slightly smarter checker.
69
+ // This checker tokenizes the input and expected output and compares them token
70
+ // by token ignoring spaces and empty lines.
71
+ type TokenChecker struct {
72
+ }
73
+
74
+ func (c * TokenChecker ) Check (got , expected string ) error {
75
+ inputTokens := strings .Fields (got )
76
+ outputTokens := strings .Fields (expected )
77
+ if len (inputTokens ) != len (outputTokens ) {
78
+ return errors .New (fmt .Sprintf ("Checker failed, number of tokens different: expected %d, got %d\n \r " ,
79
+ len (outputTokens ), len (inputTokens )))
80
+ }
81
+ i := 0
82
+ n := len (inputTokens )
83
+ for i < n {
84
+ if inputTokens [i ] != outputTokens [i ] {
85
+ return errors .New (fmt .Sprintf ("Checker failed, token %d does not match: expected %s, got %s\n \r " ,
86
+ i , outputTokens [i ], inputTokens [i ]))
87
+ }
88
+ i = i + 1
89
+ }
90
+ // everything matched.
91
+ return nil
92
+ }
93
+
68
94
// Case description contains minimum information required to run one test case.
69
95
type CaseDescription struct {
70
96
InputFile string
@@ -474,21 +500,29 @@ func (judge *PythonJudge) Cleanup() error {
474
500
}
475
501
476
502
// Creates and returns a Judge implementation corresponding to the given language
477
- func NewJudgeFor (meta config.EgorMeta , configuration * config.Config ) (Judge , error ) {
503
+ func NewJudgeFor (meta config.EgorMeta , configuration * config.Config , checker Checker ) (Judge , error ) {
478
504
switch meta .TaskLang {
479
505
case "java" :
480
- return & JavaJudge {Meta : meta , checker : & DiffChecker {} }, nil
506
+ return & JavaJudge {Meta : meta , checker : checker }, nil
481
507
case "cpp" :
482
- return & CppJudge {Meta : meta , checker : & DiffChecker {} , hasLibrary : configuration .HasCppLibrary (), LibraryLocation : configuration .CppLibraryLocation }, nil
508
+ return & CppJudge {Meta : meta , checker : checker , hasLibrary : configuration .HasCppLibrary (), LibraryLocation : configuration .CppLibraryLocation }, nil
483
509
case "c" :
484
- return & CppJudge {Meta : meta , checker : & DiffChecker {} , hasLibrary : configuration .HasCppLibrary (), LibraryLocation : configuration .CppLibraryLocation }, nil
510
+ return & CppJudge {Meta : meta , checker : checker , hasLibrary : configuration .HasCppLibrary (), LibraryLocation : configuration .CppLibraryLocation }, nil
485
511
case "python" :
486
- return & PythonJudge {Meta : meta , checker : & DiffChecker {} }, nil
512
+ return & PythonJudge {Meta : meta , checker : checker }, nil
487
513
}
488
514
return nil , errors .New (fmt .Sprintf ("Cannot find judge for the given lang %s" , meta .TaskLang ))
489
515
}
490
516
491
- func RunAction (_ * cli.Context ) error {
517
+ // Resolve the checker by name, otherwise fallback to the DiffChecker
518
+ func getChecker (name string ) Checker {
519
+ if name == "tokens" {
520
+ return & TokenChecker {}
521
+ }
522
+ return & DiffChecker {}
523
+ }
524
+
525
+ func RunAction (context * cli.Context ) error {
492
526
configuration , err := config .LoadDefaultConfiguration ()
493
527
if err != nil {
494
528
return err
@@ -503,7 +537,7 @@ func RunAction(_ *cli.Context) error {
503
537
return err
504
538
}
505
539
506
- judge , err := NewJudgeFor (egorMeta , configuration )
540
+ judge , err := NewJudgeFor (egorMeta , configuration , getChecker ( context . String ( "checker" )) )
507
541
if err != nil {
508
542
return err
509
543
}
@@ -524,8 +558,17 @@ func RunAction(_ *cli.Context) error {
524
558
}
525
559
526
560
var TestCommand = cli.Command {
527
- Name : "test" ,
528
- Aliases : []string {"r" },
561
+ Name : "test" ,
562
+ Aliases : []string {"r" },
563
+ Flags : []cli.Flag {
564
+ & cli.StringFlag {
565
+ Name : "checker" ,
566
+ // Add new checker values.
567
+ Usage : "Override the default checker, available values (diff, tokens)" ,
568
+ Aliases : []string {"io" , "fio" },
569
+ Value : "diff" ,
570
+ },
571
+ },
529
572
Usage : "Run test cases using the provided solution" ,
530
573
UsageText : "egor test" ,
531
574
Action : RunAction ,
0 commit comments