core

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TestCaseSkipped indicates the testcase is skipped
	TestCaseSkipped = iota

	// TestCaseFailed indicates the testcase is failed
	TestCaseFailed = iota

	// TestCaseSuccess indicates the testcase is success
	TestCaseSuccess = iota
)

Variables

View Source
var ErrInvalidLanguageConfigurationFile = errors.New("Invalid language configuration file")

ErrInvalidLanguageConfigurationFile indicates language configuration file has invalid format.

View Source
var ErrInvalidLanguageDirectory = errors.New("Invalid language directory")

ErrInvalidLanguageDirectory indicates directory is not a valid language definition.

View Source
var ErrLanguageNotDebuggable = errors.New("Language is not debuggable")

ErrLanguageNotDebuggable indicates that the language is not debuggable. This happens at compilation process when the language definition doesn't have debugcompile script to compile the solution with debug mode.

View Source
var ErrNoSuchLanguage = errors.New("No such language")

ErrNoSuchLanguage indicates no language found.

View Source
var ErrNoSuchSolution = errors.New("No such solution exists")

ErrNoSuchSolution indicates that no solution exists

View Source
var ErrNoSuchTestCase = errors.New("No such testcase")

ErrNoSuchTestCase indicates testcase not found

Functions

This section is empty.

Types

type CPTool

type CPTool struct {
	// contains filtered or unexported fields
}

CPTool stores information about this tool. The information includes version, all known languages, and configuration directories.

func New added in v1.0.9

func New(exec executioner.Exec, log *logger.Logger) (*CPTool, error)

New create new cptool instance. This instance contains working directory, cptool home directory, user home directory, and logger

func (*CPTool) Bootstrap

func (cptool *CPTool) Bootstrap() error

Bootstrap will bootstrap cptool. The bootstrap process will load all language from known directories.

func (*CPTool) CleanCacheDirectory added in v1.0.5

func (cptool *CPTool) CleanCacheDirectory() error

CleanCacheDirectory clean cptool cache directory that contains compiled solution and output.

func (*CPTool) Compile

func (cptool *CPTool) Compile(ctx context.Context, solution Solution, debug bool) (CompilationResult, error)

Compile will compile solution if not yet compiled. The compilation prosess will execute compile script of the language. It will use debugcompile script when debug parameter is true. When debug is true, but the language is not debuggable (doesn't contain debugcompile script), an ErrLanguageNotDebuggable error will returned. This function will execute the compilation script (could be compile/debugcompile) that defined in language definition. This execution could be skipped when the solution already compiled before.

func (*CPTool) CompileByName

func (cptool *CPTool) CompileByName(ctx context.Context, languageName string, solutionName string, debug bool) (CompilationResult, error)

CompileByName will compile solution if not yet compiled. This method will search the language and solution by its name and then call Compile method. This method will return an error if the language or solution with it's name doesn't exist.

func (*CPTool) GetAllLanguages

func (cptool *CPTool) GetAllLanguages() ([]Language, map[string]Language)

GetAllLanguages returns all known language as a pair of []Language, map[string]Language. The first element of pair contains array of all known languages. The second element of pair contains map of Language of string that map between language's name and itself.

func (*CPTool) GetCompilationRootDir added in v1.0.5

func (cptool *CPTool) GetCompilationRootDir() string

GetCompilationRootDir returns directory of all compiled solutions.

func (*CPTool) GetConfigurationPaths

func (cptool *CPTool) GetConfigurationPaths() []string

GetConfigurationPaths returns all paths to the directory that considered contain cptool configuration. This directory contains language definition, and some configuration like default language. The paths that considered are "/etc/cptool", "~/.cptool", your $CPTOOL_HOME directory and .cptool directory in your current working directory.

func (*CPTool) GetDefaultLanguage

func (cptool *CPTool) GetDefaultLanguage() (Language, error)

GetDefaultLanguage returns default language. The default language is defined in "config" file in some of configuration path. Can be "/etc/cptool/config", "~/.cptool/config", "$CPTOOL_HOME/config" or ".cptool/config" in your current working directory. Below is example of config file that defines default language as C Plus Plus ("cpp" is the language's name, while "C Plus Plus" is the language verbose name).

default_language=cpp

When there is no valid config file, the default language is choosen between all known languages. When there is no known language, then ErrNoSuchLanguage error returned.

func (*CPTool) GetLanguageByName

func (cptool *CPTool) GetLanguageByName(name string) (Language, error)

GetLanguageByName returns language that has specific name. ErrNoSuchLanguage will returned when no such language exists.

func (*CPTool) GetOutputRootDir added in v1.0.5

func (cptool *CPTool) GetOutputRootDir() string

GetOutputRootDir returns directory of all tested solution's output.

func (*CPTool) GetSolution

func (cptool *CPTool) GetSolution(name string, language Language) (Solution, error)

GetSolution returns solution object with specific name and language. Solution is a single file that contains your source code that written in known programming language. A file named "example.cpp" in current working directory can be considered as a solution named "example", with language "cpp" (because cpp's extension is "cpp"). If no solution with the specified name and language exists, then this method will return an ErrNoSuchSolution error.

func (*CPTool) Run

func (cptool *CPTool) Run(
	ctx context.Context,
	solution Solution,
	stdin io.Reader,
	stdout io.Writer,
	stderr io.Writer,
) (ExecutionResult, error)

Run will run solution. This method will execute the solution using the run script that defined in language. Before the execution begin, this method will compile the solution first by calling Compile method. When there is no error occured, this method return ExecutionResult that contains CompilationResult and execution duration.

func (*CPTool) RunByName

func (cptool *CPTool) RunByName(
	ctx context.Context,
	languageName string,
	solutionName string,
	stdin io.Reader,
	stdout io.Writer,
	stderr io.Writer,
) (ExecutionResult, error)

RunByName will run solution. This method will search the language and solution by its name and then call Run method. This method will return an error if the language or solution with it's name doesn't exist.

func (*CPTool) Test

func (cptool *CPTool) Test(
	ctx context.Context,
	solution Solution,
	testPrefix string,
) (TestResult, error)

Test will run solution using some testcases. A test case is a pair of text file that defines input and expected output of a test case. A file named "example.in" and "example.out" in current working directory considered as a test case named "example". This method will tests the given solution using all test cases with Name attribute that stars with `testPrefix`.

func (*CPTool) TestByName

func (cptool *CPTool) TestByName(
	ctx context.Context,
	languageName string,
	solutionName string,
	testPrefix string,
) (TestResult, error)

TestByName will test solution using some test cases. This method will search the language and solution by its name and then call Test method. This method will return an error if the language or solution with it's name doesn't exist.

type CompilationResult

type CompilationResult struct {
	Skipped      bool
	TargetPath   string
	Duration     time.Duration
	ErrorMessage string
}

CompilationResult store the result of compiling solution. The Skipped property indicates whether the compilation process is skipped. The compilation can be skipped when the most up to date solution is already compiled. TargetPath property contain the path to compiled program. Duration property indicates the duration of compilation process.

type ExecutionResult

type ExecutionResult struct {
	CompilationResult
	Duration time.Duration
}

ExecutionResult stores execution result

type Language

type Language struct {
	Name        string
	Extension   string
	VerboseName string

	CompileScript string
	RunScript     string
	DebugScript   string
	Debuggable    bool
}

Language defines programming language used for competitive programming like c, c++, java, etc. VerboseName contains string that will displayed to the user. Name contains string that unique for every language, this identifies the language. Extension is the extension of language, every solution that written using this language must have same extension as this (like .pas for Pascal language).

Every programming language must have compile script and run script, these script contains how to compile and run the solution. Bash script can be used to compile the solution and run the compiled solution. CompileScript contains path to the compile script location. RunScript contains path to the script for running the compiled solution. Some programming languages are debuggable. When the language is debuggable, it Debuggable property will set to true and DebugScript contains path to compile script in debug mode.

In CPTool, A language is defined using a folder, the folder name identifies the language name. Below are example of folder structure of C language

c/
├── compile
├── debugcompile
├── lang.conf
└── run

compile file contains script for compiling program. When the script executed, it will receive two arguments. The first one is the path to the solution's source code and the second one is the path where the compiled program should be located. Below is the example of compile script for c language

#!/bin/bash
SOURCE=$1
DEST=$2
gcc -x c -Wall -O2 -static -pipe -o "$DEST" "$SOURCE" -lm
exit $?

File debugcompile contains script for compiling program in debug mode. It receive the same arguments as compile script when executed Below is the example of compile script for c language in debug mode.

#!/bin/bash
SOURCE=$1
DEST=$2
gcc -x c -Wall -O2 -static -pipe -o "$DEST" "$SOURCE" -lm -g
exit $?

File lang.conf contains other information about language (VerboseName and Extension). Below the example for c language:

verbose_name=C
extension=c

File run contains script for executing compiled program. When the script is executes, it will receive one argument that defines the location of compiled program. Below is example for c language:

#!/bin/bash
PROGRAM=$1
./$PROGRAM
exit $?

type Solution

type Solution struct {
	Name        string
	Language    Language
	Path        string
	LastUpdated time.Time
}

Solution store information about solution includes solution's name, language, location, and last updated.

type TestCase

type TestCase struct {
	Name       string
	InputPath  string
	OutputPath string
}

TestCase represent testcase. A test case is a pair of text file that defines input and expected output of a test case. A file named "example.in" and "example.out" in current working directory considered as a test case named "example". Name property store test case's name. InputPath contain path to test case's input file. OutputPath contains path to test case's expected output file.

type TestCaseResult

type TestCaseResult struct {
	Testcase TestCase
	Duration time.Duration
	Status   int
	Err      error
}

TestCaseResult stores the result of testing a single test case. This contains information about the result of a test, the duration, and the test case. This also contains error if there is an error when testing the test case. The result of testing a test case can be classified into three category: "TestCaseSkipped", "TestCaseFailed", "TestCaseSuccess". Skipped means that there is an error (maybe IO error or something), that made the test skipped. When skipped, the Err property will set to error that made the test skipped. Failed means that the test run successfully but the solution's output is differ with expected output. Success means that test run successfully and gives output as expected.

type TestResult

type TestResult struct {
	TestCaseResults         []TestCaseResult
	Duration                time.Duration
	UnsuccessfullTestsCount uint
}

TestResult store test results of several test case. When testing using many testcase, this struct will returned after all test have been done. TestCaseResults's contain result of every single test case that tested. Duration contains durations of testing all test cases. UnsuccessfullTestsCount contains the number of unsuccessfull test case

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL