aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/import_c_keywords.zig
blob: 3e17e30b37a7382924095ca8b175843bfc115c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const builtin = @import("builtin");
const Id = @import("export_c_keywords.zig").Id;
const std = @import("std");

extern var int: Id;
extern var long: Id;
extern var an_alias_of_int: Id;

extern var some_non_c_keyword_variable: Id;
extern var @"void": Id;
extern var an_alias_of_some_non_c_keyword_variable: Id;

extern const @"if": Id;
extern const @"else": Id;
extern const an_alias_of_if: Id;

extern const some_non_c_keyword_constant: Id;
extern const @"switch": Id;
extern const an_alias_of_some_non_c_keyword_constant: Id;

extern fn float() Id;
extern fn double() Id;
extern fn an_alias_of_float() Id;

extern fn some_non_c_keyword_function() Id;
extern fn @"break"() Id;
extern fn an_alias_of_some_non_c_keyword_function() Id;

test "import c keywords" {
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;

    try std.testing.expect(int == .c_keyword_variable);
    try std.testing.expect(long == .c_keyword_variable);
    try std.testing.expect(an_alias_of_int == .c_keyword_variable);

    try std.testing.expect(some_non_c_keyword_variable == .non_c_keyword_variable);
    try std.testing.expect(@"void" == .non_c_keyword_variable);
    try std.testing.expect(an_alias_of_some_non_c_keyword_variable == .non_c_keyword_variable);

    try std.testing.expect(@"if" == .c_keyword_constant);
    try std.testing.expect(@"else" == .c_keyword_constant);
    try std.testing.expect(an_alias_of_if == .c_keyword_constant);

    try std.testing.expect(some_non_c_keyword_constant == .non_c_keyword_constant);
    try std.testing.expect(@"switch" == .non_c_keyword_constant);
    try std.testing.expect(an_alias_of_some_non_c_keyword_constant == .non_c_keyword_constant);

    try std.testing.expect(float() == .c_keyword_function);
    try std.testing.expect(double() == .c_keyword_function);
    try std.testing.expect(an_alias_of_float() == .c_keyword_function);

    try std.testing.expect(some_non_c_keyword_function() == .non_c_keyword_function);
    try std.testing.expect(@"break"() == .non_c_keyword_function);
    try std.testing.expect(an_alias_of_some_non_c_keyword_function() == .non_c_keyword_function);

    var ptr_id: *const Id = &long;
    try std.testing.expect(ptr_id == &int);
    ptr_id = &an_alias_of_int;
    try std.testing.expect(ptr_id == &int);

    ptr_id = &@"void";
    try std.testing.expect(ptr_id == &some_non_c_keyword_variable);
    ptr_id = &an_alias_of_some_non_c_keyword_variable;
    try std.testing.expect(ptr_id == &some_non_c_keyword_variable);

    ptr_id = &@"else";
    try std.testing.expect(ptr_id == &@"if");
    ptr_id = &an_alias_of_if;
    try std.testing.expect(ptr_id == &@"if");

    ptr_id = &@"switch";
    try std.testing.expect(ptr_id == &some_non_c_keyword_constant);
    ptr_id = &an_alias_of_some_non_c_keyword_constant;
    try std.testing.expect(ptr_id == &some_non_c_keyword_constant);

    if (builtin.target.ofmt != .coff and builtin.target.os.tag != .windows) {
        var ptr_fn: *const fn () callconv(.c) Id = &double;
        try std.testing.expect(ptr_fn == &float);
        ptr_fn = &an_alias_of_float;
        try std.testing.expect(ptr_fn == &float);

        ptr_fn = &@"break";
        try std.testing.expect(ptr_fn == &some_non_c_keyword_function);
        ptr_fn = &an_alias_of_some_non_c_keyword_function;
        try std.testing.expect(ptr_fn == &some_non_c_keyword_function);
    }
}