Example #1
0
// ReadFrom reads an evaluator from a directory. It reads a text file
// with name 'solution.*', with extension depending on the programming
// language. Then reads all files 'test.N.<type>', where N is an integer
// using a polymorphic method 'ReadFrom' for each tester.
//
func (E *Evaluator) ReadDir(dir string, prob *eval.Problem) error {
	// Read solution
	matches, err := filepath.Glob(dir + "/solution.*")
	if err != nil {
		return fmt.Errorf("Cannot look for 'solution.*': %s\n")
	}

	var sol string
	for i, m := range matches {
		if i == 0 {
			sol = m
		} else {
			fmt.Fprintf(os.Stderr, "Warning: ignoring solution '%s'\n", m)
		}
	}

	ext := filepath.Ext(sol)
	solstr, err := ioutil.ReadFile(sol)
	if err != nil {
		return fmt.Errorf("Cannot read file '%s': %s\n", sol, err)
	}
	// extension in the first line (to be cut later)
	prob.Solution = fmt.Sprintf("%s\n%s", ext, solstr)

	// Read limits
	E.Limits = readLimits(dir + "/limits")

	// Read Tests
	// path/filepath.glob: "New matches are added in
	//   lexicographical order" (we use that for now)
	matches, err = filepath.Glob(dir + "/test.*.*")
	if err != nil {
		return fmt.Errorf("Cannot look for 'test.*': %s\n")
	}
	E.Tests = []db.Obj{}
	for _, m := range matches {
		typ := getType(m)
		obj := db.ObjFromType("prog.test." + typ)
		tester, ok := obj.(Reader)
		if !ok {
			return fmt.Errorf("Type '%s' is not a programming.Tester", typ)
		}
		if err := tester.ReadFrom(m); err != nil {
			return fmt.Errorf("Couldn't read test '%s': %s\n", m, err)
		}
		E.Tests = append(E.Tests, db.Obj{tester})
	}
	return nil
}
Example #2
0
func IsProblem(abspath string) (bool, Evaluator, error) {
	abspath = filepath.Clean(abspath)
	base := filepath.Base(abspath)
	idx := strings.Index(base, ".")
	if idx == -1 {
		return false, nil, fmt.Errorf("'%s' doesn't match <Name>.<Type>", base)
	}
	typ := base[idx+1:]

	// Lookup <type>.Evaluator
	ev := db.ObjFromType(typ + ".Evaluator")
	if ev == nil {
		return false, nil, fmt.Errorf(`Type '%s.Evaluator' not found`, typ)
	}
	E, ok := ev.(Evaluator)
	if !ok {
		return false, nil, fmt.Errorf("Retrieved object is not an Evaluator")
	}
	return true, E, nil
}