diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index 5df7346..eb4b9b3 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -19,11 +19,7 @@ (export emit-c - sex-line-directives - - ;; for the tests - atom-to-fmt-c unkebabify type-convert-to-c - process-toplevel-form walk-expr walk-type walk-var walk-fn-def walk-struct) + sex-line-directives) ;;; egg `tree' not ported to CHICKEN 6 yet (define (tree-map f tree) diff --git a/tests/basic.scm b/tests/basic.scm deleted file mode 100644 index f03157d..0000000 --- a/tests/basic.scm +++ /dev/null @@ -1,58 +0,0 @@ -(import fmt-c-writer) - -(test-group "basic" - - ;; unkebabify - (test '- (unkebabify '-)) - (test '-- (unkebabify '--)) - (test '-> (unkebabify '->)) - (test '-= (unkebabify '-=)) - (test 'kebab_case (unkebabify 'kebab-case)) - (test '_what_ (unkebabify '-what-)) - (test 'this->member (unkebabify 'this->member)) - (test '_this_->_member_ (unkebabify '-this-->-member-)) - (test '__->>> (unkebabify '--->>>)) - - ;; A Lisp predicate and mutator, in letters C takes. The operators - ;; spelled with the same characters are not names - (test 'ident_startʔ (unkebabify 'ident-start?)) - (test 'resetꜝ (unkebabify 'reset!)) - (test '! (unkebabify '!)) - (test '!= (unkebabify '!=)) - - ;; Non-ASCII identifiers must survive intact. The `regex' egg's - ;; string-substitute drops one trailing character per multi-byte - ;; character, which renames things silently -- the C still compiles, - ;; just under a different name than was written. - (test 'naïve_count (unkebabify 'naïve-count)) - (test 'aï_b (unkebabify 'aï-b)) - (test 'ïï (unkebabify 'ïï)) - - ;; atom-to-fmt-c - (test '%fun (atom-to-fmt-c 'fn)) - (test '%prototype (atom-to-fmt-c 'prototype)) - (test '%block-begin (atom-to-fmt-c 'do)) - (test '%define (atom-to-fmt-c 'define)) - (test '%pointer (atom-to-fmt-c 'pointer)) - (test '%array (atom-to-fmt-c 'array)) - (test 'vector-ref (atom-to-fmt-c '¤)) - (test '%include (atom-to-fmt-c 'include)) - - ;; C99 onwards, true C spellings for bool - (test 'bool (atom-to-fmt-c 'bool)) - (test 'true (atom-to-fmt-c 'true)) - (test 'false (atom-to-fmt-c 'false)) - - ;; dot-access -> %. member-access directive (kebab-converted operands) - (test '(%. a b) (walk-expr '(dot-access a b))) - (test '(%. a b c) (walk-expr '(dot-access a b c))) - (test '(%. a_b c) (walk-expr '(dot-access a-b c))) - - ;; comment -> %comment directive (rendered as /* ... */) - ;; The reader eats only the `;' that introduced the line, so ";;; Foo" - ;; arrives as ";; Foo"; and c-comment puts nothing between /* */ and - ;; the text. Both are handled on the way out. - (test '(%comment " hi ") (walk-expr '(comment " hi"))) - (test '(%comment " hi ") (process-toplevel-form '(comment " hi"))) - (test '(%comment " Foo ") (walk-expr '(comment ";; Foo"))) - (test '(%comment " Foo ") (walk-expr '(comment ";;; Foo ")))) diff --git a/tests/codegen.scm b/tests/codegen.scm index eff2537..9da8790 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -1,10 +1,6 @@ -;;; Codegen details that are easy to get subtly wrong, and that the -;;; walk-* unit tests cannot see: they check the intermediate form we -;;; hand to fmt-c, not the C that fmt-c renders from it. -;;; -;;; Both cases below were found by writing an OpenGL example, not by -;;; the existing suite, because both need an operand shape that no -;;; earlier test program happened to use. +;;; Codegen details that are easy to get subtly wrong: precedence, +;;; scopes, comments in odd places, and the shapes no earlier test +;;; program happened to use. (import srfi-13) diff --git a/tests/fmt-c-writer.scm b/tests/fmt-c-writer.scm index 152662a..992c5f3 100644 --- a/tests/fmt-c-writer.scm +++ b/tests/fmt-c-writer.scm @@ -1,192 +1,132 @@ -;;; Types -(test-group "fmt-writer" +;;; The C each declaration comes out as: names, types, variables, +;;; initializers, functions and structs. Non-pub ones are static, which +;;; the fragments leave out. - (test - '(const int) - (walk-type '(const int))) +(test-group "fmt-c-writer" - (test - '(%array (const char) 512) - (walk-type '(¤ const char 512))) + (test-group "names" + (test-assert "kebab case is snake case" + (emits? "(var kebab-case int)" "int kebab_case;")) + ;; A Lisp predicate and mutator, in letters C takes + (test-assert "a predicate" + (emits? "(var ident-start? int)" "int ident_startʔ;")) + (test-assert "a mutator" + (emits? "(var reset! int)" "int resetꜝ;")) + ;; The `regex' egg's string-substitute dropped one trailing character + ;; per multi-byte one, and the C still compiled under the new name + (test-assert "a non-ASCII name survives" + (emits? "(var naïve-count int)" "int naïve_count;")) + (test-assert "wherever the dash is" + (emits? "(var aï-b int)" "int aï_b;")) + (test-assert "or with none" + (emits? "(var ïï int)" "int ïï;")) + (test-assert "an arrow inside a name is C's" + (emits? "(fn f () void (= this->member 1))" "this->member = 1;"))) - (test - '(%array (float) 512) - (walk-type '(¤ (float) 512))) + ;; The operators spelled with the characters names are converted from + (test-group "operators" + (test-assert "-=" + (emits? "(fn f ((a int) (b int)) void (-= a b))" "a -= b;")) + (test-assert "!" + (emits? "(fn f ((a int)) void (if (! a) (return)))" "if (!a)")) + (test-assert "!=" + (emits? "(fn f ((a int) (b int)) void (if (!= a b) (return)))" "if (a != b)"))) - (test - '(%array (const char) 512) - (walk-type '(¤ (const char) 512))) + (test-group "toplevel comments" + (test-assert "are C comments" + (emits? ";; hi" "/* hi */")) + (test-assert "without the markers or the padding" + (emits? ";;; Foo " "/* Foo */"))) - (test - '(%array (const char)) - (walk-type '(¤ const char))) + (test-group "types" + (test-assert "an array" + (emits? "(var a [const char 512])" "const char a[512];")) + (test-assert "whose element type is grouped" + (emits? "(var c [(const char) 512])" "const char c[512];")) + (test-assert "an unsized one" + (emits? "(extern var d [const char])" "extern const char d[];")) + (test-assert "a pointer to const" + (emits? "(var p (* const char))" "const char * p;")) + (test-assert "a const pointer to const" + (emits? "(var q (const * const char))" "const char * const q;")) + (test-assert "a function pointer" + (emits? "(var fp (fn ((int) (float) [const * struct what]) void))" + "void (*fp)(int, float, struct what* const []);")) + (test-assert "taking an array" + (emits? "(var fq (fn ((int) [float]) void))" + "void (*fq)(int, float []);")) + (test-assert "an array of them" + (emits? "(extern var fa [(fn ((int) [float]) void)])" + "extern void (*fa[])(int, float []);"))) - (test - '(%array (const char)) - (walk-type '(¤ (const char)))) + (test-group "variables" + (test-assert "a local array" + (emits? "(fn f () void (var a [float 8]))" "float a[8];")) + (test-assert "a pointer, initialized" + (emits? "(fn f () void (var n int) (var b (* int) (& n)))" "int * b = &n;")) + (test-assert "a pointer to const" + (emits? "(fn f () void (var n int) (var c (* const int) (& n)))" + "const int * c = &n;")) + (test-assert "a struct" + (emits? "(fn f () void (var s (struct suc)))" "struct suc s;")) + (test-assert "a pointer to a const struct" + (emits? "(fn f ((s1 (* const struct suc))) void (var t (* const struct suc) s1))" + "const struct suc * t = s1;")) + (test-assert "grouped" + (emits? "(fn f ((s1 (* const struct suc))) void (var u (* (const struct suc)) s1))" + "const struct suc * u = s1;")) + (test-assert "a struct from a call" + (emits? "(fn f () void (var v (struct suc) (hoge piyo)))" + "struct suc v = hoge(piyo);")) + (test-assert "a pointer to one" + (emits? "(fn f () void (var w (* struct suc) (hoge piyo)))" + "struct suc* w = hoge(piyo);"))) - (test - '(%array float 8) - (walk-type '(¤ float 8))) + (test-group "initializers" + (test-assert "a bare one is braces" + (emits? "(fn f () void (var a [int 2] #(1 2)))" "int a[2] = {1, 2};")) + (test-assert "`:' ends the type and makes it a compound literal" + (emits? "(fn f () void (var p (struct point) #(struct point : 3 4)))" + "struct point p = (struct point){3, 4};")) + (test-assert "the type is bare words, as everywhere else" + (emits? "(fn f () void (var s (* const char) #(* const char : 65)))" + "const char * s = (const char *){65};")) + (test-assert "and may be an array type" + (emits? "(fn f () void (var t [int 3] #([int 3] : 10 20 30)))" + "int t[3] = (int [3]){10, 20, 30};")) + (test-assert "`.field value' is a designated initializer, kebab and all" + (emits? "(fn f () void (var n (struct named) #(struct named : .n 7 .first-name \"zoe\")))" + "(struct named){.n = 7, .first_name = \"zoe\"}")) + (test-assert "positional and designated may be mixed" + (emits? "(fn f () void (var m (struct point) #(struct point : 1 .y 5)))" + "(struct point){1, .y = 5}")) + (test-assert "designators work in an untyped initializer too" + (emits? "(fn f () void (var o (struct point) #(.y 5)))" + "struct point o = {.y = 5};"))) - (test - "Pointer to const char" - '(const char *) - (walk-type '(* const char))) + (test-group "functions" + (test-assert "a prototype may leave its parameters nameless" + (emits? "(fn puk ((int) [float 8]) void)" "void puk (int, float [8]);")) + (test-assert "an array parameter" + (emits? "(fn g ((argc int) (argv [const char])) int (return 0))" + "int g (int argc, const char argv[]) {")) + (test-assert "a pointer parameter" + (emits? "(fn quxu ((bar (* struct piq))) int (return 0))" + "int quxu (struct piq * bar) {")) + (test-assert "an array of pointers" + (emits? "(fn quxu ((bar (* struct piq)) (ploop [* struct tt])) int (return 0))" + "int quxu (struct piq * bar, struct tt* ploop[]) {"))) - (test - "Const pointer to const char" - '(const char * const) - (walk-type '(const * const char))) - - (test - '(%fun void (int float (%array (struct what * const)))) - (walk-type '(fn ((int) (float) (¤ (const * struct what))) void))) - - (test - '(%fun void (int (%array float) (%array (struct what * const)))) - (walk-type '(fn ((int) (¤ float) (¤ (const * struct what))) void))) - - (test - '(%array (%fun void (int (%array float) (%array (struct what * const))))) - (walk-type '(¤ (fn ((int) (¤ float) (¤ (const * struct what))) void)))) - - ;; Type convert to C - (test - '(int) - (type-convert-to-c '(int))) - - (test - '(* int) - (type-convert-to-c '(int *))) - - (test - '(* const int) - (type-convert-to-c '(const int *))) - - (test - '(const * const char) - (type-convert-to-c '(const char * const))) - -;;; Variable defs - (test - '(%var (%array float 8) a) - (walk-var '(var a (¤ float 8)))) - - (test - '(%var (int *) a (& n)) - (walk-var '(var a (* int) (& n)))) - - (test - '(%var (const int *) a (& n)) - (walk-var '(var a (* const int) (& n)))) - - (test - '(%var (struct suc) s) - (walk-var '(var s (struct suc)))) - - (test - '(%var (const struct suc *) s s1) - (walk-var '(var s (* const struct suc) s1))) - - (test - '(%var (const struct suc *) s s1) - (walk-var '(var s (* (const struct suc)) s1))) - - (test - '(%var (struct suc) s (hoge piyo)) - (walk-var '(var s (struct suc) (hoge piyo)))) - - (test - '(%var (struct suc *) s (hoge piyo)) - (walk-var '(var s (* struct suc) (hoge piyo)))) - -;;; Initializers and compound literals - (test "a bare initializer is unchanged" - '#(1 2) - (walk-expr '#(1 2))) - - (test "`:' ends the type and makes it a compound literal" - '(%compound (struct point) 3 4) - (walk-expr '#(struct point : 3 4))) - - (test "the type is bare words, as everywhere else" - '(%compound (const char *) 65) - (walk-expr '#(* const char : 65))) - - (test "and may be an array type" - '(%compound (%array int 3) 10 20 30) - (walk-expr '#(¤ int 3 : 10 20 30))) - - (test "a grouped type is unwrapped the way a declaration's is" - '(%compound (%array int 3) 10) - (walk-expr '#((¤ int 3) : 10))) - - (test "`.field value' is a designated initializer, kebab and all" - '(%compound (struct named) (%designate n 7) (%designate first_name "zoe")) - (walk-expr '#(struct named : .n 7 .first-name "zoe"))) - - (test "positional and designated may be mixed" - '(%compound (struct point) 1 (%designate y 5)) - (walk-expr '#(struct point : 1 .y 5))) - - (test "designators work in an untyped initializer too" - '#((%designate y 5)) - (walk-expr '#(.y 5))) - -;;; Fn defs - (test - '(%fun void puk ((int #f) ((%array float 8) #f))) - (walk-fn-def '(fn puk ((int) (¤ float 8)) void))) - - (test - '(%fun int main ((int argc) ((%array (const char)) argv)) - (return 0)) - (walk-fn-def - '(fn main ((argc int) (argv (¤ const char))) int - (return 0)))) - (test - '(%fun int quxu (((struct piq *) bar)) - (return 0)) - (walk-fn-def - '(fn quxu ((bar (* struct piq))) int - (return 0)))) - - (test - '(%fun int quxu (((struct piq *) bar) ((%array (struct tt *)) ploop)) - (return 0)) - (walk-fn-def - '(fn quxu ((bar (* struct piq)) (ploop (¤ * struct tt))) int - (return 0)))) - - ;; Structs - (test - '(struct no_kebab ((int a) (float f))) - (walk-struct '(struct no-kebab ((a int) (f float))))) - - (test - '(struct settings ((u32 x y w h) - ((%array (struct ((float r g b a))) 4) colors))) - (walk-struct - '(struct settings - ((x y w h u32) - (colors [¤ struct ((r g b a float)) 4]))))) - - (test - '(struct settings ((u32 x y w h) - ((%array (struct color ((float r g b a))) 4) colors))) - (walk-struct - '(struct settings - ((x y w h u32) - (colors [¤ struct color ((r g b a float)) 4]))))) - - (test - '(struct mega_kebab ((int a) - ((struct ((int year) (int month) (int day))) dob) - ((%fun bool (int (%array int))) min))) - (walk-struct '(struct mega-kebab - ((a int) - (dob (struct ((year int) - (month int) - (day int)))) - (min (fn ((int) (¤ int)) bool))))))) + (test-group "structs" + (test-assert "names are converted" + (emits? "(struct no-kebab ((a int) (f float)))" + "struct no_kebab {\n int a;\n float f;\n};")) + (test-assert "fields sharing a type share a line, and a field may be a struct" + (emits? "(struct settings ((x y w h u32) (colors [struct ((r g b a float)) 4])))" + "struct settings {\n u32 x, y, w, h;\n struct{\n float r, g, b, a;\n } colors[4];\n};")) + (test-assert "a named one" + (emits? "(struct settings ((x y w h u32) (colors [struct color ((r g b a float)) 4])))" + " struct color{\n float r, g, b, a;\n } colors[4];")) + (test-assert "or a function pointer" + (emits? "(struct mega-kebab ((a int) (min (fn ((int) [int]) bool))))" + " bool (*min)(int, int []);")))) diff --git a/tests/run.scm b/tests/run.scm index 20e72f8..32e3068 100644 --- a/tests/run.scm +++ b/tests/run.scm @@ -2,7 +2,6 @@ test) (include-relative "pipeline.scm") -(include-relative "basic.scm") (include-relative "semen.scm") (include-relative "reader.scm") (include-relative "fmt-c-writer.scm")