dump
This commit is contained in:
34
tools/sextest/README.org
Normal file
34
tools/sextest/README.org
Normal file
@@ -0,0 +1,34 @@
|
||||
* Sextest
|
||||
|
||||
A tool for testing sex compilator by using test programs.
|
||||
The tools compiles them using the compiler, then runs with provided
|
||||
input, then checks that the output and return code matches the test
|
||||
specification.
|
||||
|
||||
* Test format
|
||||
The test program consists of two sections: prelude and the test
|
||||
program itself. Prelude contains four sections: compilation
|
||||
parameters, such as flags to compiler; string to be
|
||||
passed to program's stdin; output to match against
|
||||
program's stdout; return code to match against what
|
||||
program returns.
|
||||
All sections start with respective name: :compilation, :stdout,
|
||||
:stdin, :return.
|
||||
Section names must be present, but all can be empty. Return code
|
||||
defaults to 0 in such case.
|
||||
|
||||
* Example
|
||||
some-test.sex:
|
||||
#+begin_src
|
||||
; :compilation
|
||||
; :stdin
|
||||
; :stdout Hello world!
|
||||
; :return
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
(pub fn main () int
|
||||
(puts "Hello World!")
|
||||
(printf "Hello, %s!\n" name)
|
||||
(return 0))
|
||||
#+end_src
|
||||
73
tools/sextest/sextest.sex
Normal file
73
tools/sextest/sextest.sex
Normal file
@@ -0,0 +1,73 @@
|
||||
(include stdio.h)
|
||||
(include unistd.h)
|
||||
(include string.h)
|
||||
|
||||
(var COMP (* const char) ":compilation")
|
||||
(var STDIN (* const char) ":stdin")
|
||||
(var STDOUT (* const char) ":stdout")
|
||||
(var RET (* const char) ":return")
|
||||
|
||||
(var test-file-path (* const char) NULL)
|
||||
|
||||
(var prelude-comp [const char 1024])
|
||||
(var prelude-stdin [const char 1024])
|
||||
(var prelude-stdout [const char 1024])
|
||||
(var prelude-ret [const char 1024])
|
||||
|
||||
(fn print-usage () void
|
||||
(printf "Usage: sextest ./path/to/test-file.sex\n"))
|
||||
|
||||
(fn read-file ((file-path (* const char))) void
|
||||
(var f (* FILE) (fopen file-path "r"))
|
||||
(if (== f NULL)
|
||||
(begin
|
||||
(printf "Failed to read file %s\n" file-path)
|
||||
(return)))
|
||||
;; should be more than enough
|
||||
(var buffer [const char 1024])
|
||||
(var i int 0)
|
||||
|
||||
(var c char)
|
||||
|
||||
(var section (* const char))
|
||||
(while (!= EOF (= c (getc f)))
|
||||
(switch c
|
||||
(case (#\space #\newline #\tab #\return)
|
||||
(= [buffer i] #\null)
|
||||
|
||||
(if (!= NULL section)
|
||||
(begin
|
||||
(memcpy i section buffer)))
|
||||
|
||||
|
||||
(if (== 0 (strncmp buffer COMP 8ul)))
|
||||
(= i 0))
|
||||
(default
|
||||
(if (== i (sizeof buffer))
|
||||
(printf "Blast! The section is longer than %d, not going to work with this" (++/post i)))
|
||||
|
||||
(if (< i (sizeof buffer))
|
||||
(= [buffer (++/post i)] c))))))
|
||||
|
||||
(fn get-section ((section-name (* const char))) (* const char)
|
||||
;; Get section from test file by name.
|
||||
;; Name must be one of: :compilation, :stdin, :stdout, :return
|
||||
(if (== 0 (strncmp section-name COMP 8ul))
|
||||
(return prelude-comp))
|
||||
(if (== 0 (strncmp section-name STDIN 6ul))
|
||||
(return prelude-stdin))
|
||||
(if (== 0 (strncmp section-name STDOUT 7ul))
|
||||
(return prelude-stdout))
|
||||
(if (== 0 (strncmp section-name RET 7ul))
|
||||
(return prelude-ret))
|
||||
(printf "Expected section name, got %s\n" section-name)
|
||||
(return NULL))
|
||||
|
||||
(pub fn main ((argc int) (argv [* const char])) int
|
||||
(if (!= argc 2)
|
||||
(begin
|
||||
(print-usage)
|
||||
(return 1)))
|
||||
(var test-file-path (* const char) [argv 1])
|
||||
(read-file test-file-path)
|
||||
(return 0))
|
||||
Reference in New Issue
Block a user