-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,142 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/mraron/njudge/internal/judge" | ||
"github.com/mraron/njudge/pkg/language" | ||
"github.com/mraron/njudge/pkg/language/sandbox" | ||
"github.com/mraron/njudge/pkg/problems" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"log/slog" | ||
"os" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
var TestProblemArgs struct { | ||
type TestProblemConfig struct { | ||
ProblemDir string | ||
Language string | ||
SolutionPath string | ||
Verbose int | ||
Verbose bool | ||
|
||
IsolateSandboxRange []int | ||
Isolate bool | ||
Concurrency int | ||
} | ||
|
||
var DefaultTestProblemConfig = TestProblemConfig{ | ||
ProblemDir: ".", | ||
Language: "cpp17", | ||
SolutionPath: "./sol/solution.cpp", | ||
Verbose: false, | ||
|
||
IsolateSandboxRange: []int{10, 99}, | ||
Isolate: true, | ||
Concurrency: 4, | ||
} | ||
|
||
type ProblemStore struct { | ||
problem problems.Problem | ||
} | ||
|
||
func (p ProblemStore) GetProblem(s string) (problems.Problem, error) { | ||
return p.problem, nil | ||
} | ||
|
||
var TestProblemCmd = &cobra.Command{ | ||
Use: "testproblem", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
//todo | ||
/*sp := sandbox.NewProvider() | ||
s1, _ := sandbox.NewIsolate(50) | ||
sp.Put(s1) | ||
s2, _ := sandbox.NewIsolate(51) | ||
sp.Put(s2) | ||
w := judge.NewWorker(1, sp) | ||
logger, err := zap.NewDevelopment() | ||
if err != nil { | ||
return err | ||
} | ||
p, err := problems.Parse(TestProblemArgs.ProblemDir) | ||
if err != nil { | ||
return err | ||
} | ||
l, _ := language.DefaultStore.Get(TestProblemArgs.Language) | ||
src, err := os.ReadFile(TestProblemArgs.SolutionPath) | ||
if err != nil { | ||
return err | ||
} | ||
st, err := w.Judge(context.Background(), logger, p, src, l, judge.NewWriterCallback(io.Discard, func() {})) | ||
for _, test := range st.Feedback[0].Testcases() { | ||
fmt.Println(test.Group, test.Index, test.VerdictName, test.Score, test.MaxScore) | ||
} | ||
fmt.Println(st.Feedback[0].Verdict(), st.Feedback[0].Score(), "/", st.Feedback[0].MaxScore()) | ||
fmt.Println(st.FeedbackType) | ||
if TestProblemArgs.Verbose > 0 { | ||
res, err := json.Marshal(st) | ||
func NewTestProblemCmd() *cobra.Command { | ||
cfg := TestProblemConfig{} | ||
cmd := &cobra.Command{ | ||
Use: "test_problem", | ||
Short: "Test a problem with a solution", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
log := slog.Default() | ||
if !cfg.Verbose { | ||
log = slog.New(slog.NewJSONHandler(io.Discard, nil)) | ||
} | ||
|
||
p, err := problems.Parse(cfg.ProblemDir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(string(res)) | ||
} | ||
provider := sandbox.NewProvider() | ||
sandboxCount := 0 | ||
if cfg.Isolate { | ||
for i := cfg.IsolateSandboxRange[0]; i <= cfg.IsolateSandboxRange[1]; i++ { | ||
s, err := sandbox.NewIsolate(i, sandbox.IsolateOptionUseLogger(log)) | ||
if err != nil { | ||
return err | ||
} | ||
provider.Put(s) | ||
sandboxCount++ | ||
} | ||
} else { | ||
for i := 0; i < 10; i++ { | ||
s, err := sandbox.NewDummy(sandbox.DummyWithLogger(log)) | ||
if err != nil { | ||
return err | ||
} | ||
provider.Put(s) | ||
sandboxCount++ | ||
} | ||
} | ||
|
||
return err*/ | ||
return nil | ||
}, | ||
} | ||
if 2*cfg.Concurrency > sandboxCount { | ||
log.Warn("sandbox count is low for concurrency") | ||
} | ||
if cfg.Concurrency > runtime.GOMAXPROCS(0) { | ||
log.Warn("concurrency is higher than GOMAXPROCS") | ||
} | ||
|
||
func init() { | ||
TestProblemCmd.Flags().StringVar(&TestProblemArgs.ProblemDir, "problem", "", "problemdir") | ||
TestProblemCmd.Flags().StringVar(&TestProblemArgs.Language, "lang", "", "language") | ||
TestProblemCmd.Flags().StringVar(&TestProblemArgs.SolutionPath, "sol", "", "solutionpath") | ||
TestProblemCmd.Flags().CountVarP(&TestProblemArgs.Verbose, "verbose", "v", "verbose") | ||
tokens := make(chan struct{}, cfg.Concurrency) | ||
for range cfg.Concurrency { | ||
tokens <- struct{}{} | ||
} | ||
|
||
TestProblemCmd.MarkFlagFilename("sol") | ||
TestProblemCmd.MarkFlagDirname("problem") | ||
j := &judge.Judge{ | ||
SandboxProvider: provider, | ||
ProblemStore: ProblemStore{problem: p}, | ||
LanguageStore: language.DefaultStore, | ||
RateLimit: 200 * time.Millisecond, | ||
Tokens: tokens, | ||
Logger: log, | ||
} | ||
src, err := os.ReadFile(cfg.SolutionPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
TestProblemCmd.MarkFlagRequired("problem") | ||
TestProblemCmd.MarkFlagRequired("lang") | ||
TestProblemCmd.MarkFlagRequired("sol") | ||
res, err := j.Judge(context.Background(), judge.Submission{ | ||
Source: src, | ||
Language: cfg.Language, | ||
}, func(result judge.Result) error { | ||
fmt.Println(result.Status.Feedback[0].Verdict(), result.Status.Feedback[0].Score(), "/", result.Status.Feedback[0].MaxScore()) | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
for _, test := range res.Feedback[0].Testcases() { | ||
fmt.Println(test.Group, test.Index, test.VerdictName, test.Score, test.MaxScore) | ||
} | ||
fmt.Println(res.Feedback[0].Verdict(), res.Feedback[0].Score(), "/", res.Feedback[0].MaxScore()) | ||
fmt.Println(res.FeedbackType) | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVar(&cfg.ProblemDir, "problem_dir", DefaultTestProblemConfig.ProblemDir, "problem directory") | ||
cmd.Flags().StringVar(&cfg.Language, "lang", DefaultTestProblemConfig.Language, "language") | ||
cmd.Flags().StringVar(&cfg.SolutionPath, "sol", DefaultTestProblemConfig.SolutionPath, "sol") | ||
cmd.Flags().BoolVar(&cfg.Verbose, "verbose", DefaultTestProblemConfig.Verbose, "verbose") | ||
cmd.Flags().BoolVar(&cfg.Isolate, "isolate", DefaultTestProblemConfig.Isolate, "isolate") | ||
cmd.Flags().IntSliceVar(&cfg.IsolateSandboxRange, "isolate_sandbox_range", DefaultTestProblemConfig.IsolateSandboxRange, "inclusive interval of isolate sandbox IDs") | ||
cmd.Flags().IntVar(&cfg.Concurrency, "concurrency", DefaultTestProblemConfig.Concurrency, "the maximum number of concurrently executed testcase") | ||
|
||
RootCmd.AddCommand(TestProblemCmd) | ||
return cmd | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(NewTestProblemCmd()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters