mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
57 lines
1.6 KiB
Zig
57 lines
1.6 KiB
Zig
const std = @import("std");
|
|
const tests = @import("tests.zig");
|
|
const nl = std.cstr.line_sep;
|
|
|
|
pub fn addCases(cases: *tests.RunTranslatedCContext) void {
|
|
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;
|
|
\\}
|
|
, "hello, world!" ++ nl);
|
|
|
|
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;
|
|
\\}
|
|
, "");
|
|
|
|
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;
|
|
\\}
|
|
, "");
|
|
|
|
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;
|
|
\\}
|
|
, "");
|
|
}
|