test_lang.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. // Test from http://www.unixwiz.net/techtips/reading-cdecl.html
  15. typedef char *(*(**foo [][2*3+1])())[];
  16. int;
  17. struct OtherStruct {
  18. int x, y, z;
  19. };
  20. int init1 = 10 + 20;
  21. struct OtherStruct init2 = { 1000, 2 * 5, 200 / 120};
  22. typedef struct Test {
  23. int x:5, y:6;
  24. struct OtherStruct s;
  25. char z:0;
  26. struct Test *ptr;
  27. } Test2, *TestPtr, **TestPtrPtr;
  28. typedef union Test {
  29. int x, y;
  30. struct OtherStruct s;
  31. char z;
  32. struct Test *ptr;
  33. } Test3;
  34. enum Enum {
  35. ZERO,
  36. ONE,
  37. TWO,
  38. THREE,
  39. FOUR,
  40. TEN = 5+3+2,
  41. ELEVEN
  42. };
  43. int test_false(void) {
  44. return 0;
  45. }
  46. int test_true(void) {
  47. return 1;
  48. }
  49. int test_octal(void) {
  50. if (*"" != 0) return 0;
  51. if (*"\0" != 0) return 0;
  52. if (*"\177" != 127) return 0;
  53. if (*"\305" != 197-256) return 0;
  54. return 1;
  55. }
  56. int do_sum(int x, char y) {
  57. return x+y;
  58. }
  59. int test_while() {
  60. int i;
  61. int sum;
  62. i = 0;
  63. sum = 0;
  64. while (i < 200) {
  65. i = i + 1;
  66. if (i == 10) continue;
  67. sum = do_sum(sum, i);
  68. if (i == 100) break;
  69. }
  70. return sum;
  71. }
  72. int test_do_while() {
  73. int i;
  74. int sum;
  75. i = 0;
  76. sum = 0;
  77. do {
  78. i = i + 1;
  79. if (i == 10) continue;
  80. sum = do_sum(sum, i);
  81. if (i == 100) break;
  82. } while (i < 200 && i != 0);
  83. return sum;
  84. }
  85. int test_for() {
  86. int i;
  87. int sum;
  88. sum = 0;
  89. for (i = 0; i < 200; i = i + 1) {
  90. if (i == 10) continue;
  91. sum = do_sum(sum, i);
  92. if (i == 100) break;
  93. }
  94. for (;;) {
  95. break;
  96. }
  97. return sum;
  98. }
  99. int test_array() {
  100. short x[3];
  101. *(x+1) = *x = 200;
  102. *x = 100;
  103. //*(x+1) = 200;
  104. *(x+2) = 300;
  105. x[3] = 400;
  106. if (3[x] != 400) return 0;
  107. //return (x+2)-x;
  108. return x[1];
  109. }
  110. int test_struct() {
  111. Test2 t;
  112. t.x = 10;
  113. t.y = 20;
  114. t.s.x = 30;
  115. t.s.y = 40;
  116. t.s.z = 50;
  117. Test2 t2;
  118. t2 = t;
  119. Test2 *ptr;
  120. ptr = &t2;
  121. return ptr->s.y;
  122. }
  123. extern int glob2;
  124. // C++ comment
  125. int glob; /* C comment */
  126. int glob2; \
  127. Test2 glob3;
  128. Test3 glob4;
  129. int test_enum() {
  130. return ELEVEN;
  131. }
  132. char *global_str = "global test string\n" "other piece\n";
  133. int test_strings() {
  134. if (*global_str != 'g') return 0;
  135. if (*"local test string\n" != 'l') return 0;
  136. return 1;
  137. }
  138. // Test compile-time evaluation
  139. #if 0
  140. type_does_not_exist;
  141. #endif
  142. #if 1
  143. typedef int type_must_exist1;
  144. #endif
  145. #if 1 ? 1 : 0
  146. typedef int type_must_exist2;
  147. #endif
  148. #if 1 ? 0 : 1
  149. type_does_not_exist;
  150. #endif
  151. #if 0 ? 1 : 0
  152. type_does_not_exist;
  153. #endif
  154. #if 0 ? 0 : 1
  155. typedef int type_must_exist3;
  156. #endif
  157. type_must_exist1;
  158. type_must_exist2;
  159. type_must_exist3;
  160. #define YES
  161. #undef NO
  162. #define TEST 1 < 2
  163. int test_define() {
  164. int i = 0;
  165. #if defined(NO)
  166. return 0;
  167. #ifdef YES
  168. return 0;
  169. #endif
  170. #endif
  171. i = i + 1;
  172. #if !defined(YES)
  173. return 0;
  174. #else
  175. i = i + 1;
  176. #ifndef NO
  177. i = i + 1;
  178. #else
  179. return 0;
  180. #endif
  181. #endif
  182. #if defined(YES)
  183. i = i + 1;
  184. #else
  185. return 0;
  186. #endif
  187. #if TEST
  188. i = i + 1;
  189. #endif
  190. #if !TEST
  191. return 0;
  192. #endif
  193. return i;
  194. }
  195. #undef YES
  196. #undef NO
  197. #undef TEST
  198. int test_extension() {
  199. char c = -1;
  200. return c;
  201. }
  202. int test_unary() {
  203. if (+0 != 0) return 0;
  204. if (+1 != 1) return 0;
  205. if (+2 != 2) return 0;
  206. if (-0 != 0) return 0;
  207. if (-1 != 0xffffffff) return 0;
  208. if (-2 != 0xfffffffe) return 0;
  209. if (~0 != 0xffffffff) return 0;
  210. if (~1 != 0xfffffffe) return 0;
  211. if (~2 != 0xfffffffd) return 0;
  212. if (-0 != 0-0) return 0;
  213. if (-1 != 0-1) return 0;
  214. if (-2 != 0-2) return 0;
  215. if (!0 != 1) return 0;
  216. if (!1 != 0) return 0;
  217. if (!2 != 0) return 0;
  218. return 1;
  219. }
  220. int test_shifts() {
  221. int s1 = 128;
  222. unsigned int u1 = 128;
  223. int s2 = -128;
  224. if (s1 << 2 != 512) return 0;
  225. if (s1 >> 2 != 32) return 0;
  226. if (u1 << 2 != 512) return 0;
  227. if (u1 >> 2 != 32) return 0;
  228. if (s2 << 2 != -512) return 0;
  229. if (s2 >> 2 != -32) return 0;
  230. return 1;
  231. }
  232. int side_effect(int *val, int ret) {
  233. *val = 2;
  234. return ret;
  235. }
  236. int test_logic() {
  237. int val = 1;
  238. if ((0 && side_effect(&val, 0)) != 0) return 0;
  239. if ((1 && 0) != 0) return 0;
  240. if ((0 && side_effect(&val, 2)) != 0) return 0;
  241. if ((2 && 3) != 1) return 0;
  242. if ((0 || 0) != 0) return 0;
  243. if ((1 || side_effect(&val, 0)) != 1) return 0;
  244. if ((0 || 2) != 1) return 0;
  245. if ((2 || side_effect(&val, 3)) != 1) return 0;
  246. return val;
  247. }
  248. int test_switch() {
  249. int i;
  250. int j = 0;
  251. for (i = 0; i < 10; i++) {
  252. switch (i) {
  253. return 0;
  254. case 1:
  255. j++;
  256. break;
  257. return 0;
  258. case THREE:
  259. case FOUR:
  260. case 5:
  261. j += 2;
  262. case 7:
  263. j++;
  264. break;
  265. return 0;
  266. case 1:
  267. case 6:
  268. break;
  269. case 100:
  270. return 0;
  271. default:
  272. j += 10;
  273. }
  274. }
  275. return j == 51;
  276. }
  277. int test_goto() {
  278. int x = 0;
  279. // Check that empty statements are accepted
  280. ;
  281. goto test_lab;
  282. return 0;
  283. test_lab2:
  284. if (x != 1) return 0;
  285. goto finish;
  286. int y = 13;
  287. test_lab:
  288. x = 1;
  289. if (0) {
  290. goto test_lab2;
  291. } else {
  292. goto test_lab2;
  293. }
  294. finish:
  295. return 1;
  296. }
  297. struct struct_size {
  298. int x;
  299. char y;
  300. };
  301. int test_sizeof() {
  302. int s = sizeof(int);
  303. if (s != 4) return 0;
  304. int x = 10;
  305. // Check that side effect do not happen (i.e., expression is not
  306. // evaluated)
  307. s = sizeof --x;
  308. if (s != 4) return 0;
  309. if (x != 10) return 0;
  310. s = sizeof(test_sizeof());
  311. if (s != 4) return 0;
  312. if (sizeof(char) != 1) return 0;
  313. if (sizeof(char*) != 4) return 0;
  314. if (sizeof(char[10]) != 10) return 0;
  315. char x[10];
  316. if (sizeof(x) != 10) return 0;
  317. if (sizeof(x[0]) != 1) return 0;
  318. if (sizeof(struct struct_size) != 8) return 0;
  319. short static_sizeof[sizeof s];
  320. if (sizeof static_sizeof != 8) return 0;
  321. if (sizeof "1234" != 5) return 0;
  322. if (sizeof(float) != 4) return 0;
  323. if (sizeof(double) != 8) return 0;
  324. if (sizeof(long double) != 12) return 0;
  325. return 1;
  326. }
  327. int test_comma() {
  328. int x = 2;
  329. int y = (x++, 10);
  330. if (x != 3) return 0;
  331. if (y != 10) return 0;
  332. return 1;
  333. }
  334. int array_with_length[4] = {1, 2, 3, ' '};
  335. int array_without_length[] = {1, 2, 3, ' '};
  336. int array_with_length_and_comma[4] = {1, 2, 3, ' ', };
  337. int array_without_length_with_comma[] = {1, 2, 3, ' ', };
  338. char string_without_length[] = "test\n\f\r";
  339. char string_in_array_form[] = {1, 2, 3};
  340. char strings[][6] = { "one", "two", "three" };
  341. static int st1;
  342. static int st1;
  343. static int st2 = 1;
  344. static int st2;
  345. static int st3;
  346. static int st3 = 1;
  347. int test_initializers() {
  348. if (sizeof(array_with_length) != 4*4) return 0;
  349. if (sizeof(array_without_length) != 4*4) return 0;
  350. if (sizeof(array_with_length_and_comma) != 4*4) return 0;
  351. if (sizeof(array_without_length_with_comma) != 4*4) return 0;
  352. if (sizeof(string_without_length) != 8) return 0;
  353. if (sizeof(string_in_array_form) != 3) return 0;
  354. if (string_without_length[0] != 't') return 0;
  355. if (string_without_length[4] != '\n') return 0;
  356. if (string_without_length[6] != '\r') return 0;
  357. if (string_without_length[7] != 0) return 0;
  358. if (st1 != 0) return 0;
  359. if (st2 != 1) return 0;
  360. if (st3 != 1) return 0;
  361. return 1;
  362. }