mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
3.8 KiB
167 lines
3.8 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"path/filepath"
|
||
|
"sort"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
5 years ago
|
|
||
5 years ago
|
"../lib"
|
||
5 years ago
|
)
|
||
|
|
||
|
type (
|
||
|
vect struct{ x, y int }
|
||
|
|
||
|
// the tetromino is described with three vectors
|
||
|
// example :
|
||
|
//
|
||
|
// [#][#][#]
|
||
|
// [#]
|
||
|
//
|
||
|
// tetromino{{1, 0}, {2, 0}, {1, 1}}
|
||
|
tetromino [3]vect
|
||
|
)
|
||
|
|
||
5 years ago
|
// load a tetromino composed of the rune r in the map s, example :
|
||
5 years ago
|
//
|
||
|
// read(`......
|
||
|
// ......
|
||
|
// ......
|
||
|
// ....X.
|
||
|
// ..XXX.
|
||
|
// ......
|
||
|
// `,
|
||
|
// 'X',
|
||
|
// ) == tetromino{{-2, 1}, {-1, 1}, {0, 1}}
|
||
|
func read(s string, r rune) (t tetromino) {
|
||
|
var origin vect
|
||
|
i := 0
|
||
|
first := true
|
||
|
lines := strings.Split(s, "\n")
|
||
|
for y, line := range lines {
|
||
5 years ago
|
for x, rr := range line {
|
||
|
if rr == r {
|
||
5 years ago
|
if first {
|
||
|
first = false
|
||
|
origin = vect{x, y}
|
||
|
} else {
|
||
|
t[i] = vect{x - origin.x, y - origin.y}
|
||
|
i++
|
||
|
if i == 3 {
|
||
|
return tetromino{}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
var (
|
||
|
timeout = 30 * time.Second
|
||
|
samples = "./solutions/tetrisoptimizer/samples"
|
||
|
student = "./student/tetrisoptimizer"
|
||
|
exe = filepath.Join(student, "a.out")
|
||
|
)
|
||
|
// load samples
|
||
|
f, err := os.Open(samples)
|
||
|
if err != nil {
|
||
5 years ago
|
lib.Fatal("Cannot open directory", err)
|
||
5 years ago
|
}
|
||
|
defer f.Close()
|
||
|
filenames, err := f.Readdirnames(0)
|
||
|
if err != nil {
|
||
5 years ago
|
lib.Fatal("Cannot read directory", err)
|
||
5 years ago
|
}
|
||
|
|
||
|
// separate samples into good (valid) and bad (invalid) files
|
||
|
var goodFiles, badFiles []string
|
||
|
for _, filename := range filenames {
|
||
|
if strings.HasPrefix(filename, "good") {
|
||
|
goodFiles = append(goodFiles, filepath.Join(samples, filename))
|
||
|
} else if strings.HasPrefix(filename, "bad") {
|
||
|
badFiles = append(badFiles, filepath.Join(samples, filename))
|
||
|
}
|
||
|
}
|
||
|
sort.Strings(badFiles)
|
||
|
sort.Strings(goodFiles)
|
||
|
|
||
|
// compile student code
|
||
|
cmd := exec.Command("go", "build", "-o", exe, "-trimpath", "-ldflags", "-s -w", student)
|
||
|
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOARCH=amd64")
|
||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||
5 years ago
|
lib.Fatal("Cannot compile :", string(out))
|
||
5 years ago
|
}
|
||
|
|
||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||
|
defer cancel()
|
||
|
|
||
|
// test invalid cases
|
||
|
for _, badFile := range badFiles {
|
||
|
b, _ := exec.CommandContext(ctx, exe, badFile).CombinedOutput()
|
||
|
if string(b) != "ERROR\n" {
|
||
5 years ago
|
lib.Fatal(`Failed to handle bad format, should output : "ERROR\n"`)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// test valid cases
|
||
|
score := .0
|
||
|
defer func() {
|
||
|
fmt.Println("score =", score)
|
||
|
}()
|
||
|
for _, goodFile := range goodFiles {
|
||
|
size, _ := strconv.Atoi(strings.Split(goodFile, "-")[2])
|
||
|
b, err := exec.CommandContext(ctx, exe, goodFile).Output()
|
||
|
if ctx.Err() == context.DeadlineExceeded {
|
||
|
return
|
||
|
}
|
||
|
if err != nil {
|
||
5 years ago
|
lib.Fatal("Failed to process a valid map : execution failed")
|
||
5 years ago
|
}
|
||
|
s := string(b)
|
||
|
lines := strings.Split(s, "\n")
|
||
|
if lines[len(lines)-1] != "" {
|
||
5 years ago
|
lib.Fatal(`Failed to process a valid map : missing final '\n'`)
|
||
5 years ago
|
}
|
||
|
lines = lines[:len(lines)-1]
|
||
|
for _, line := range lines {
|
||
|
if len(line) != len(lines) {
|
||
5 years ago
|
lib.Fatal("Failed to process a valid map : invalid square, it is expected as many lines as characters")
|
||
5 years ago
|
}
|
||
|
}
|
||
|
if len(lines) < size {
|
||
5 years ago
|
lib.Fatal("Failed to process a valid map : the square cannot be that small")
|
||
5 years ago
|
}
|
||
|
b, err = ioutil.ReadFile(goodFile)
|
||
|
if err != nil {
|
||
5 years ago
|
lib.Fatal("Failed to read a valid map")
|
||
5 years ago
|
}
|
||
|
pieces := strings.Split(string(b), "\n\n")
|
||
|
surface := len(lines) * len(lines)
|
||
|
if strings.Count(s, ".") != surface-len(pieces)*4 {
|
||
5 years ago
|
lib.Fatal("Failed to process a valid map : the number of holes (character '.') is not correct")
|
||
5 years ago
|
}
|
||
|
letter := 'A'
|
||
|
for _, piece := range pieces {
|
||
|
if read(s, letter) != read(piece, '#') {
|
||
5 years ago
|
lib.Fatal("Failed to process a valid map : a tetromino is missing")
|
||
5 years ago
|
}
|
||
|
letter += 1
|
||
|
}
|
||
|
score += float64(size*size) / float64(surface)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// TODO:
|
||
5 years ago
|
// Fix path
|
||
5 years ago
|
// Add error cases :
|
||
|
// wrong arguments
|
||
|
// bad file types (links, folders)
|