Browse Source

Refactor raid3 test

content-update
Xavier Petit 4 years ago committed by xpetit
parent
commit
c343099722
  1. 112
      tests/go/raid3_test.go

112
tests/go/raid3_test.go

@ -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)
if err != nil {
for i := 0; i < len(names); i++ { t.Fatal(s)
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)
} }
return s
} }
if err != nil {
return "", err
}
return
}
func chall(t *testing.T) { // executes the test and compares student with solutions
_, err := mainOut("./solutions/raid3") executeTest := func(args string, x, y int) {
if err != nil { var output, correct string
t.Error(err)
}
_, err = z01.ExecOut("go", "build", "-o", "raid3_student", "./student/raid3/main.go")
}
// executes the test and compares student with solutions
func executeTest(t *testing.T, args string, x, y int) {
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) } else {
} output = execFatal("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3_student")
correct, err = z01.ExecOut("sh", "-c", args+" | ./raid3") correct = execFatal("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3")
if err != nil {
t.Error(err)
}
} else {
output, err = z01.ExecOut("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3_student")
if err != nil {
t.Error(err)
} }
correct, err = z01.ExecOut("sh", "-c", "./"+args+" "+strconv.Itoa(x)+" "+strconv.Itoa(y)+" | ./raid3") if output != correct {
if err != nil { t.Fatalf("./%s %d %d | ./raid3 prints %q instead of %q\n",
t.Error(err) args, x, y, output, correct)
} }
} }
if output != correct {
t.Errorf("./%s %d %d | ./raid3 prints %q instead of %q\n",
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…
Cancel
Save