2020-01-03 19:11:19 -08:00
|
|
|
const std = @import("std");
|
2020-01-02 19:45:48 -08:00
|
|
|
const tests = @import("tests.zig");
|
2020-01-03 19:11:19 -08:00
|
|
|
const nl = std.cstr.line_sep;
|
2020-01-02 19:45:48 -08:00
|
|
|
|
|
|
|
pub fn addCases(cases: *tests.RunTranslatedCContext) void {
|
2020-01-05 15:18:26 -08:00
|
|
|
cases.add("boolean values and expressions",
|
|
|
|
\\#include <stdlib.h>
|
|
|
|
\\static const _Bool false_val = 0;
|
|
|
|
\\static const _Bool true_val = 1;
|
|
|
|
\\void foo(int x, int y) {
|
|
|
|
\\ _Bool r = x < y;
|
|
|
|
\\ if (!r) abort();
|
|
|
|
\\ _Bool self = foo;
|
|
|
|
\\ if (self == false_val) abort();
|
|
|
|
\\}
|
|
|
|
\\int main(int argc, char **argv) {
|
|
|
|
\\ foo(2, 5);
|
|
|
|
\\ if (false_val == true_val) abort();
|
|
|
|
\\ return 0;
|
|
|
|
\\}
|
|
|
|
, "");
|
|
|
|
|
2020-01-02 19:45:48 -08:00
|
|
|
cases.add("hello world",
|
|
|
|
\\#define _NO_CRT_STDIO_INLINE 1
|
|
|
|
\\#include <stdio.h>
|
|
|
|
\\int main(int argc, char **argv) {
|
|
|
|
\\ printf("hello, world!\n");
|
|
|
|
\\ return 0;
|
|
|
|
\\}
|
2020-01-03 19:11:19 -08:00
|
|
|
, "hello, world!" ++ nl);
|
2020-01-02 19:45:48 -08:00
|
|
|
|
|
|
|
cases.add("anon struct init",
|
|
|
|
\\#include <stdlib.h>
|
|
|
|
\\struct {int a; int b;} x = {1, 2};
|
|
|
|
\\int main(int argc, char **argv) {
|
|
|
|
\\ x.a += 2;
|
|
|
|
\\ x.b += 1;
|
|
|
|
\\ if (x.a != 3) abort();
|
|
|
|
\\ if (x.b != 3) abort();
|
|
|
|
\\ return 0;
|
|
|
|
\\}
|
|
|
|
, "");
|
2020-01-02 18:36:36 -08:00
|
|
|
|
|
|
|
cases.add("casting away const and volatile",
|
|
|
|
\\void foo(int *a) {}
|
|
|
|
\\void bar(const int *a) {
|
|
|
|
\\ foo((int *)a);
|
|
|
|
\\}
|
|
|
|
\\void baz(volatile int *a) {
|
|
|
|
\\ foo((int *)a);
|
|
|
|
\\}
|
|
|
|
\\int main(int argc, char **argv) {
|
|
|
|
\\ int a = 0;
|
|
|
|
\\ bar((const int *)&a);
|
|
|
|
\\ baz((volatile int *)&a);
|
|
|
|
\\ return 0;
|
|
|
|
\\}
|
|
|
|
, "");
|
2020-01-05 14:56:27 -08:00
|
|
|
|
|
|
|
cases.add("anonymous struct & unions",
|
|
|
|
\\#include <stdlib.h>
|
|
|
|
\\#include <stdint.h>
|
|
|
|
\\static struct { struct { uint16_t x, y; }; } x = { 1 };
|
|
|
|
\\static struct { union { uint32_t x; uint8_t y; }; } y = { 0x55AA55AA };
|
|
|
|
\\int main(int argc, char **argv) {
|
|
|
|
\\ if (x.x != 1) abort();
|
|
|
|
\\ if (x.y != 0) abort();
|
|
|
|
\\ if (y.x != 0x55AA55AA) abort();
|
|
|
|
\\ if (y.y != 0xAA) abort();
|
|
|
|
\\ return 0;
|
|
|
|
\\}
|
|
|
|
, "");
|
2020-01-06 08:11:10 -08:00
|
|
|
|
|
|
|
cases.add("array to pointer decay",
|
|
|
|
\\#include <stdlib.h>
|
|
|
|
\\int main(int argc, char **argv) {
|
|
|
|
\\ char data[3] = {'a','b','c'};
|
|
|
|
\\ if (2[data] != data[2]) abort();
|
|
|
|
\\ if ("abc"[1] != data[1]) abort();
|
|
|
|
\\ char *as_ptr = data;
|
|
|
|
\\ if (2[as_ptr] != as_ptr[2]) abort();
|
|
|
|
\\ if ("abc"[1] != as_ptr[1]) abort();
|
|
|
|
\\ return 0;
|
|
|
|
\\}
|
|
|
|
, "");
|
2020-01-02 19:45:48 -08:00
|
|
|
}
|