mirror of https://github.com/01-edu/public.git
Xavier Petit
5 years ago
committed by
xpetit
1 changed files with 37 additions and 75 deletions
@ -1,101 +1,63 @@ |
|||||||
package student_test |
package student_test |
||||||
|
|
||||||
import ( |
import ( |
||||||
"os" |
"os/exec" |
||||||
"path" |
|
||||||
"strconv" |
"strconv" |
||||||
"strings" |
|
||||||
"testing" |
"testing" |
||||||
|
|
||||||
"github.com/01-edu/z01" |
"github.com/01-edu/z01" |
||||||
) |
) |
||||||
|
|
||||||
// builds all the files for testing, student, solution and relevant files
|
func TestRaid3(t *testing.T) { |
||||||
func mainOut(name string) (out string, err error) { |
execFatal := func(name string, arg ...string) string { |
||||||
names := []string{"main.go", "/raid1aProg/raid1a.go", "/raid1bProg/raid1b.go", "/raid1cProg/raid1c.go", "/raid1dProg/raid1d.go", "/raid1eProg/raid1e.go"} |
b, err := exec.Command(name, arg...).CombinedOutput() |
||||||
var pathRaid1 string |
s := string(b) |
||||||
|
|
||||||
for i := 0; i < len(names); i++ { |
|
||||||
pathRaid1 = path.Join(name, names[i]) |
|
||||||
if strings.Contains(pathRaid1, "main.go") { |
|
||||||
_, err = z01.ExecOut("go", "build", "-o", "raid3", pathRaid1) |
|
||||||
if err != nil { |
|
||||||
return "", err |
|
||||||
} |
|
||||||
} else { |
|
||||||
_, err = z01.ExecOut("go", "build", pathRaid1) |
|
||||||
} |
|
||||||
} |
|
||||||
if err != nil { |
|
||||||
return "", err |
|
||||||
} |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
func chall(t *testing.T) { |
|
||||||
_, err := mainOut("./solutions/raid3") |
|
||||||
if err != nil { |
if err != nil { |
||||||
t.Error(err) |
t.Fatal(s) |
||||||
} |
} |
||||||
_, err = z01.ExecOut("go", "build", "-o", "raid3_student", "./student/raid3/main.go") |
return s |
||||||
} |
} |
||||||
|
|
||||||
// executes the test and compares student with solutions
|
// executes the test and compares student with solutions
|
||||||
func executeTest(t *testing.T, args string, x, y int) { |
executeTest := func(args string, x, y int) { |
||||||
var output, correct string |
var output, correct string |
||||||
var err error |
|
||||||
|
|
||||||
if x == 0 && y == 0 { |
if x == 0 && y == 0 { |
||||||
output, err = z01.ExecOut("sh", "-c", args+" | ./raid3_student") |
output = execFatal("sh", "-c", args+" | ./raid3_student") |
||||||
if err != nil { |
correct = execFatal("sh", "-c", args+" | ./raid3") |
||||||
t.Error(err) |
|
||||||
} |
|
||||||
correct, err = z01.ExecOut("sh", "-c", args+" | ./raid3") |
|
||||||
if err != nil { |
|
||||||
t.Error(err) |
|
||||||
} |
|
||||||
} else { |
} else { |
||||||
output, err = z01.ExecOut("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3_student") |
output = execFatal("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3_student") |
||||||
if err != nil { |
correct = execFatal("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3") |
||||||
t.Error(err) |
|
||||||
} |
|
||||||
correct, err = z01.ExecOut("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3") |
|
||||||
if err != nil { |
|
||||||
t.Error(err) |
|
||||||
} |
|
||||||
} |
} |
||||||
if output != correct { |
if output != correct { |
||||||
t.Errorf("./%s %d %d | ./raid3 prints %q instead of %q\n", |
t.Fatalf("./%s %d %d | ./raid3 prints %q instead of %q\n", |
||||||
args, x, y, output, correct) |
args, x, y, output, correct) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
func TestRaid3(t *testing.T) { |
// builds all the files for testing, student, solution and relevant files
|
||||||
table := []string{"raid1a", "raid1b", "raid1c", "raid1d", "raid1e"} |
execFatal("go", "build", "-o", "raid3", "solutions/raid3/main.go") |
||||||
chall(t) |
execFatal("go", "build", "-o", "raid3_student", "student/raid3/main.go") |
||||||
|
execFatal("go", "build", "solutions/raid3/raid1aProg/raid1a.go") |
||||||
|
execFatal("go", "build", "solutions/raid3/raid1bProg/raid1b.go") |
||||||
|
execFatal("go", "build", "solutions/raid3/raid1cProg/raid1c.go") |
||||||
|
execFatal("go", "build", "solutions/raid3/raid1dProg/raid1d.go") |
||||||
|
execFatal("go", "build", "solutions/raid3/raid1eProg/raid1e.go") |
||||||
|
|
||||||
// testing all raids1
|
// testing all raids1
|
||||||
|
table := []string{"raid1a", "raid1b", "raid1c", "raid1d", "raid1e"} |
||||||
for _, s := range table { |
for _, s := range table { |
||||||
x := z01.RandIntBetween(1, 50) |
x := z01.RandIntBetween(1, 50) |
||||||
y := z01.RandIntBetween(1, 50) |
y := z01.RandIntBetween(1, 50) |
||||||
executeTest(t, s, x, y) |
executeTest(s, x, y) |
||||||
} |
} |
||||||
|
|
||||||
// testing special case AA, AC, A, A
|
// testing special case AA, AC, A, A
|
||||||
// A C
|
// A C
|
||||||
executeTest(t, "raid1e", 2, 1) |
executeTest("raid1e", 2, 1) |
||||||
executeTest(t, "raid1c", 2, 1) |
executeTest("raid1c", 2, 1) |
||||||
executeTest(t, "raid1d", 1, 2) |
executeTest("raid1d", 1, 2) |
||||||
executeTest(t, "raid1e", 1, 2) |
executeTest("raid1e", 1, 2) |
||||||
executeTest(t, "raid1e", 1, 1) |
executeTest("raid1e", 1, 1) |
||||||
executeTest(t, "echo", 0, 0) |
executeTest("echo", 0, 0) |
||||||
|
|
||||||
// removing all binary files after finishing the tests
|
|
||||||
table = append(table, "raid3_student") |
|
||||||
table = append(table, "raid3") |
|
||||||
for _, v := range table { |
|
||||||
err := os.Remove(v) |
|
||||||
if err != nil { |
|
||||||
t.Error(err) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue