| 1 |
;;; |
| 2 |
;;; tests for lang.* |
| 3 |
;;; |
| 4 |
;;; For now, these are experimental modules so we only test basic |
| 5 |
;;; stuff. Eventually we need comprehensive tests for each lang.* modules. |
| 6 |
;;; |
| 7 |
|
| 8 |
(use gauche.test) |
| 9 |
(test-start "lang.*") |
| 10 |
(use util.match) |
| 11 |
|
| 12 |
;;---------------------------------------------------------------------- |
| 13 |
(test-section "lang.asm") |
| 14 |
(use lang.asm.regset) |
| 15 |
(test-module 'lang.asm.regset) |
| 16 |
|
| 17 |
(use lang.asm.x86_64) |
| 18 |
(test-module 'lang.asm.x86_64) |
| 19 |
|
| 20 |
;;---------------------------------------------------------------------- |
| 21 |
(test-section "lang.c") |
| 22 |
|
| 23 |
(use lang.c.type) |
| 24 |
(test-module 'lang.c.type) |
| 25 |
|
| 26 |
(use lang.c.lexer) |
| 27 |
(use parser.peg :only (<parse-error>)) |
| 28 |
(test-module 'lang.c.lexer) |
| 29 |
|
| 30 |
(let () |
| 31 |
;; originally contributed from Shigenobu Kimura |
| 32 |
(define (t str expect) |
| 33 |
(test* (write-to-string str) |
| 34 |
(if (regexp? expect) |
| 35 |
(test-error <parse-error> expect) |
| 36 |
expect) |
| 37 |
(c-tokenize (string->list str)))) |
| 38 |
|
| 39 |
(t "forwhile;" '((ident forwhile) |\;|)) |
| 40 |
(t "forx;" '((ident forx) |\;|)) |
| 41 |
(t "foox;" '((ident foox) |\;|)) |
| 42 |
(t "xfor;" '((ident xfor) |\;|)) |
| 43 |
(t "foo;" '((ident foo) |\;|)) |
| 44 |
(t "b>x;" '((ident b) > (ident x) |\;|)) |
| 45 |
(t "b>>x;" '((ident b) >> (ident x) |\;|)) |
| 46 |
(t "b->x;" '((ident b) -> (ident x) |\;|)) |
| 47 |
(t "b--x;" '((ident b) -- (ident x) |\;|)) |
| 48 |
(t "b---x;" '((ident b) -- - (ident x) |\;|)) |
| 49 |
|
| 50 |
(t "0L;" '((const long "0L") |\;|)) |
| 51 |
(t "0l;" '((const long "0l") |\;|)) |
| 52 |
(t "0U;" '((const u-int "0U") |\;|)) |
| 53 |
(t "0u;" '((const u-int "0u") |\;|)) |
| 54 |
(t "0uL;" '((const u-long "0uL") |\;|)) |
| 55 |
(t "0ul;" '((const u-long "0ul") |\;|)) |
| 56 |
(t "0lU;" '((const u-long "0lU") |\;|)) |
| 57 |
(t "0ll;" '((const long-long "0ll") |\;|)) |
| 58 |
(t "0LL;" '((const long-long "0LL") |\;|)) |
| 59 |
(t "0ull;" '((const u-long-long "0ull") |\;|)) |
| 60 |
(t "0uLL;" '((const u-long-long "0uLL") |\;|)) |
| 61 |
(t "0llU;" '((const u-long-long "0llU") |\;|)) |
| 62 |
(t "0LLu;" '((const u-long-long "0LLu") |\;|)) |
| 63 |
(t "1234;" '((const int "1234") |\;|)) |
| 64 |
(t "012;" '((const int "012" ) |\;|)) |
| 65 |
(t "0x12;" '((const int "0x12") |\;|)) |
| 66 |
(t "0xbeef;" '((const int "0xbeef") |\;|)) |
| 67 |
(t "0xallu;" '((const u-long-long "0xallu") |\;|)) |
| 68 |
(t "0in;" #/malformed integer constant suffix: in/) |
| 69 |
|
| 70 |
(t "'a';" '((const char "a") |\;|)) |
| 71 |
(t "'A';" '((const char "A") |\;|)) |
| 72 |
(t "' ';" '((const char " ") |\;|)) |
| 73 |
(t "'?';" '((const char "?") |\;|)) |
| 74 |
(t "'\\r';" '((const char "\r") |\;|)) |
| 75 |
(t "'\\0';" '((const char "\0") |\;|)) |
| 76 |
(t "'\"';" '((const char "\"") |\;|)) |
| 77 |
(t "'\\377';" '((const char "\xff;") |\;|)) |
| 78 |
(t "'%';" '((const char "%") |\;|)) |
| 79 |
(t "'\\23';" '((const char "\x13;") |\;|)) |
| 80 |
(t "'8';" '((const char "8") |\;|)) |
| 81 |
(t "'\\\\';" '((const char "\\") |\;|)) |
| 82 |
(t "'ABCD';" '((const char "ABCD") |\;|)) |
| 83 |
(t "L'8';" '((const wchar "8") |\;|)) |
| 84 |
|
| 85 |
(t "0.;" '((const double "0." ) |\;|)) |
| 86 |
(t "3e1;" '((const double "3e1" ) |\;|)) |
| 87 |
(t "3.14159;" '((const double "3.14159") |\;|)) |
| 88 |
(t ".0;" '((const double ".0" ) |\;|)) |
| 89 |
(t "1.0E-3;" '((const double "1.0E-3" ) |\;|)) |
| 90 |
(t "1e-3;" '((const double "1e-3" ) |\;|)) |
| 91 |
(t "1.0;" '((const double "1.0" ) |\;|)) |
| 92 |
(t "0.00034;" '((const double "0.00034") |\;|)) |
| 93 |
(t "2e+9;" '((const double "2e+9" ) |\;|)) |
| 94 |
(t "1.0f;" '((const float "1.0f" ) |\;|)) |
| 95 |
(t "1.0e67L;" '((const long-double "1.0e67L") |\;|)) |
| 96 |
(t "1.37E+6L;" '((const long-double "1.37E+6L") |\;|)) |
| 97 |
(t "0E1L;" '((const long-double "0E1L" ) |\;|)) |
| 98 |
(t "0x1.0p1;" '((const double "0x1.0p1") |\;|)) |
| 99 |
(t "0x1.0;" '((const double "0x1.0") |\;|)) |
| 100 |
|
| 101 |
(t "\"abra\"" '((string "abra"))) |
| 102 |
(t "\"\";" '((string "") |\;|)) |
| 103 |
(t "\"\\\"\";" '((string "\"") |\;|)) |
| 104 |
(t "\"Copyright 2000 \\nTexas Instruments. \"" |
| 105 |
'((string "Copyright 2000 \nTexas Instruments. "))) |
| 106 |
(t "L\"abc\";" '((wstring "abc") |\;|)) |
| 107 |
|
| 108 |
(t "X++Y;" '((ident X) ++ (ident Y) |\;|)) |
| 109 |
(t "-12ul;" '(- (const u-long "12ul") |\;|)) |
| 110 |
(t "x**2;" '((ident x) * * (const int "2") |\;|)) |
| 111 |
(t "A*=B;" '((ident A) *= (ident B) |\;|)) |
| 112 |
) |
| 113 |
|
| 114 |
(use lang.c.parser) |
| 115 |
(test-module 'lang.c.parser) |
| 116 |
|
| 117 |
;; grokking type |
| 118 |
(let () |
| 119 |
(define (t-type expect code) |
| 120 |
(test* code expect |
| 121 |
(let loop ((decls (c-parse-string code))) |
| 122 |
(match decls |
| 123 |
[(('decl ('x _ type _)) . _) type] |
| 124 |
[(_ . r) (loop r)])))) |
| 125 |
|
| 126 |
(t-type '(int ()) "int x;") |
| 127 |
(t-type '(int (const)) "const int x;") |
| 128 |
(t-type '(int (const)) "int const x;") |
| 129 |
(t-type '(int (volatile restrict const)) "volatile int restrict const x;") |
| 130 |
|
| 131 |
(t-type '(char ()) "char x;") |
| 132 |
(t-type '(u-char ()) "unsigned char x;") |
| 133 |
(t-type '(u-char ()) "char unsigned x;") |
| 134 |
(t-type '(s-char ()) "signed char x;") |
| 135 |
(t-type '(s-char ()) "char signed x;") |
| 136 |
|
| 137 |
(t-type '(short ()) "short x;") |
| 138 |
(t-type '(short ()) "short int x;") |
| 139 |
(t-type '(short ()) "int short x;") |
| 140 |
(t-type '(u-short ()) "unsigned short x;") |
| 141 |
(t-type '(u-short ()) "unsigned int short x;") |
| 142 |
(t-type '(short ()) "signed short x;") |
| 143 |
(t-type '(short ()) "signed int short x;") |
| 144 |
(t-type '(short (volatile const)) "signed volatile int const short x;") |
| 145 |
|
| 146 |
(t-type '(long ()) "long x;") |
| 147 |
(t-type '(long ()) "long int x;") |
| 148 |
(t-type '(long ()) "int long x;") |
| 149 |
(t-type '(u-long ()) "unsigned long x;") |
| 150 |
(t-type '(u-long ()) "unsigned int long x;") |
| 151 |
(t-type '(long ()) "signed long x;") |
| 152 |
(t-type '(long ()) "signed int long x;") |
| 153 |
(t-type '(long-long ()) "long long x;") |
| 154 |
(t-type '(long-long ()) "long int long x;") |
| 155 |
(t-type '(long-long ()) "int long long x;") |
| 156 |
(t-type '(u-long-long ()) "int long unsigned long x;") |
| 157 |
(t-type '(u-long-long ()) "unsigned long long x;") |
| 158 |
(t-type '(long-long ()) "signed int long long x;") |
| 159 |
(t-type '(long-long (restrict const)) "restrict long signed int const long x;") |
| 160 |
|
| 161 |
(t-type '(bool ()) "_Bool x;") |
| 162 |
(t-type '(bool (const volatile)) "const _Bool volatile x;") |
| 163 |
|
| 164 |
(t-type '(float ()) "float x;") |
| 165 |
(t-type '(double ()) "double x;") |
| 166 |
(t-type '(long-double ()) "long double x;") |
| 167 |
(t-type '(long-double ()) "double long x;") |
| 168 |
(t-type '(float-complex ()) "_Complex float x;") |
| 169 |
(t-type '(float-complex ()) "float _Complex x;") |
| 170 |
(t-type '(double-complex ()) "_Complex double x;") |
| 171 |
(t-type '(long-double-complex ()) "long _Complex double x;") |
| 172 |
|
| 173 |
(t-type '(.type T () (int ())) "typedef int T; T x;") |
| 174 |
(t-type '(.type T (const) (int ())) "typedef int T; const T x;") |
| 175 |
(t-type '(.type T (const) (int ())) "typedef int T; T const x;") |
| 176 |
(t-type '(.type T () (int (const))) "typedef const int T; T x;") |
| 177 |
(t-type '(.type T (volatile) (int (const))) "typedef const int T; T volatile x;") |
| 178 |
|
| 179 |
(t-type '(.pointer () (int ())) "int *x;") |
| 180 |
(t-type '(.pointer () (int (const))) "const int *x;") |
| 181 |
(t-type '(.pointer (const) (int ())) "int * const x;") |
| 182 |
(t-type '(.pointer (const) (int (const))) "int const * const x;") |
| 183 |
(t-type '(.pointer () (.pointer () (int ()))) "int **x;") |
| 184 |
(t-type '(.pointer (volatile) (.pointer () (int ()))) "int *volatile*x;") |
| 185 |
|
| 186 |
(t-type '(.array (int ()) () 3) "int x[3];") |
| 187 |
(t-type '(.array (int ()) () 12) "int x[3*4];") |
| 188 |
(t-type '(.array (.array (int ()) () 2) () 3) "int x[3][2];") |
| 189 |
(t-type '(.array (.pointer () (int ())) () (ident N)) "int *x[N];") |
| 190 |
|
| 191 |
(t-type '(.function () (int ()) ()) "int x(void);") |
| 192 |
(t-type '(.function () (void ()) unknown-args) "void x();") |
| 193 |
(t-type '(.function () (int ()) ((#f (int ())) |
| 194 |
(#f (.pointer () (char ()))))) |
| 195 |
"int x(int, char*);") |
| 196 |
(t-type '(.function () (int ()) ((#f (int ())) |
| 197 |
(#f (.pointer () (char ()))) |
| 198 |
...)) |
| 199 |
"int x(int, char*, ...);") |
| 200 |
(t-type '(.function () (int ()) ((x #f) |
| 201 |
(y #f) |
| 202 |
(z #f))) |
| 203 |
"int x(x, y, z);") |
| 204 |
(t-type '(.function () (int ()) ((x (int ())) |
| 205 |
(y (int ())) |
| 206 |
(z (double ())))) |
| 207 |
"int x(int x, int y, double z);") |
| 208 |
(t-type '(.function () (.pointer () (int (const))) ()) |
| 209 |
"const int *x(void);") |
| 210 |
(t-type '(.pointer () (.function () (int (const)) ())) |
| 211 |
"const int (*x)(void);") |
| 212 |
(t-type '(.function (inline) (int ()) ()) "inline int x(void);") |
| 213 |
(t-type '(.function (inline) (.pointer () (int ())) ()) "inline int *x(void);") |
| 214 |
|
| 215 |
(t-type '(.struct foo () ()) "struct foo {} x;") |
| 216 |
(t-type '(.struct foo () ((a (int ())) |
| 217 |
(b (.pointer () (int ()))) |
| 218 |
(c (double ())))) |
| 219 |
"struct foo {int a, *b; double c;} x;") |
| 220 |
(t-type '(.struct #f (const volatile) ()) "const struct {} volatile x;") |
| 221 |
(t-type '(.union foo () ()) "union foo {} x;") |
| 222 |
|
| 223 |
(t-type '(.union #f () ((n (.struct #f () ((a (int ())) |
| 224 |
(b (double ()))))) |
| 225 |
(b (.array (u-char ()) () 16)))) |
| 226 |
"union { struct { int a; double b; } n; unsigned char b[16];} x;") |
| 227 |
|
| 228 |
(t-type '(.enum e () ((X #f) (Y #f) (Z #f))) |
| 229 |
"enum e { X, Y, Z } x;") |
| 230 |
(t-type '(.enum #f (const) ((X 1) (Y 3) (Z #f))) |
| 231 |
"enum { X=1, Y=3, Z } const x;") |
| 232 |
(t-type '(.type T () (.enum e () ())) |
| 233 |
"typedef enum e T; T x;") |
| 234 |
) |
| 235 |
|
| 236 |
;; We haven't fixed the format of the semantic value, so for now we just |
| 237 |
;; see if it parses. |
| 238 |
(let () |
| 239 |
(define (t-succ expect code) |
| 240 |
(test* code expect (boolean (c-parse-string code)))) |
| 241 |
|
| 242 |
;; simple declaration |
| 243 |
(t-succ #t "int x;") |
| 244 |
(t-succ #t "const int x;") |
| 245 |
(t-succ #t "int const x;") |
| 246 |
(t-succ #t "int x, y, z;") |
| 247 |
(t-succ #t "int *x, **y, * const * volatile z;") |
| 248 |
|
| 249 |
;; typedef |
| 250 |
(t-succ #t "typedef int N; N x;") |
| 251 |
|
| 252 |
;; typedef scoped |
| 253 |
(t-succ #t "int N; { typedef double N; (N)3; } N = 3;") |
| 254 |
(t-succ #t "typedef int N; N x; { int N; return N; }") |
| 255 |
(t-succ #t "typedef int N; N x; { int N; return N; } N y;") |
| 256 |
) |
| 257 |
|
| 258 |
(test-end) |