Skip to content

Commit 5578df7

Browse files
GradeUserCode
1 parent 5a47882 commit 5578df7

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

backend/cmd/exec_user_code.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ func ExecUserCode(ctx context.Context, gr types.GradeRequest, tmpDir string) (*E
7878
}
7979

8080
defer func() {
81-
err := cli.ContainerRemove(ctxdl, resp.ID, dockertypes.ContainerRemoveOptions{
81+
err := cli.ContainerRemove(ctx, resp.ID, dockertypes.ContainerRemoveOptions{
8282
Force: true,
8383
})
8484
if err != nil {
8585
log.Panicln("cannot kill and remove container")
86-
panic(err)
8786
}
8887
}()
8988

backend/cmd/grade_user_code.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func GradeUserCode(ctx context.Context, gr types.GradeRequest) (*types.GradeResu
1919
return nil, errors.Wrap(err, "cannot create tmpDir")
2020
}
2121
defer os.RemoveAll(tmpDir)
22+
os.Chmod(tmpDir, 0777)
2223

2324
cr, err := CompileUserCode(ctx, gr, tmpDir)
2425
if err != nil {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/markhuang1212/code-grader/backend/cmd"
8+
"github.com/markhuang1212/code-grader/backend/types"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGradeUserCode1(t *testing.T) {
13+
ctx := context.Background()
14+
gr := types.GradeRequest{
15+
TestCaseName: "example-1",
16+
UserCode: "int main() { cout << \"Hello\" << endl; }",
17+
}
18+
result, err := cmd.GradeUserCode(ctx, gr)
19+
assert.Nil(t, err)
20+
assert.Equal(t, result.Status, types.GradeResultSuccess)
21+
}
22+
23+
func TestGradeUserCode2(t *testing.T) {
24+
ctx := context.Background()
25+
gr := types.GradeRequest{
26+
TestCaseName: "example-1",
27+
UserCode: "int main() { while (1) { } }",
28+
}
29+
result, err := cmd.GradeUserCode(ctx, gr)
30+
assert.Nil(t, err)
31+
assert.Equal(t, result.Status, types.GradeResultTimeLimitExceed)
32+
}

testcases/example-1/answer.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ int main() {
33
cin >> w;
44
if (w == "Hello")
55
{
6-
cout << "World" << endl;
6+
cout << "Hello" << endl;
77
}
88
return 0;
99
}

testcases/test.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
TEST_CASE_DIR=$1
4+
5+
cd ${TEST_CASE_DIR}
6+
cat prepend.txt > /tmp/main.cpp
7+
cat answer.txt >> /tmp/main.cpp
8+
cat append.txt >> /tmp/main.cpp
9+
10+
g++ -std=c++11 -o /tmp/a.out /tmp/main.cpp
11+
/tmp/a.out < input.txt > /tmp/output.txt
12+
diff output.txt /tmp/output.txt
13+
14+
if [ $? -eq 0 ]
15+
then
16+
echo "SUCCESS"
17+
fi

0 commit comments

Comments
 (0)