Warning, /frameworks/syntax-highlighting/autotests/input/test.zig is written in an unsupported language. File is not indexed.

0001 const std = @import("std");
0002 
0003 /// This is a doc comment
0004 // This is a comment
0005 fn getData() !u32 {
0006     return 666;
0007 }
0008 
0009 // multiline string
0010 const hello_world_in_c =
0011     \\#include <stdio.h>
0012     \\
0013     \\int main(int argc, char **argv) {
0014     \\    printf("hello world\n");
0015     \\    return 0;
0016     \\}
0017 ;
0018 
0019 // Top-level declarations are order-independent:
0020 const print = @import("std").debug.print;
0021 
0022 pub fn main() !void {
0023     inline for (values) |v, i| {
0024         if (i != 2) continue;
0025         try expect(v);
0026     }
0027 
0028     // error union
0029     var number_or_error: anyerror!i32 = error.ArgNotFound;
0030 
0031     print("\nerror union 2\ntype: {}\nvalue: {!}\n", .{
0032         @TypeOf(number_or_error), number_or_error,
0033     });
0034 
0035     const stdout = std.io.getStdOut().writer();
0036     try stdout.print("Hello, {s}!\n", .{"world"});
0037     const bytes = "hello";
0038     print("{}\n", .{@TypeOf(bytes)}); // *const [5:0]u8
0039     print("{d}\n", .{bytes[5]}); // 0
0040     print("{}\n", .{'e' == '\x65'}); // true
0041     print("{d}\n", .{'\u{1f4a9}'});                     // 128169
0042     print("{d}\n", .{'💯'});                            // 128175
0043     print("{}\n", .{mem.eql(u8, "hello", "h\x65llo")}); // true
0044     print("0x{x}\n", .{"\xff"[0]}); // non-UTF-8 strings are possible with \xNN notation.
0045     print("{u}\n", .{'âš¡'});
0046 
0047     _ = @import("introducing_zig_test.zig");
0048 }
0049 
0050 // Declare an enum.
0051 const Type = enum {
0052     ok,
0053     not_ok,
0054 };
0055 
0056 test "async skip test" {
0057     var frame = async func();
0058     const result = await frame;
0059     try std.testing.expect(result == 1);
0060 }
0061 
0062 fn func() i32 {
0063     suspend {
0064         resume @frame();
0065     }
0066     return 1;
0067 }
0068 
0069 pub extern "c" fn @"error"() void;
0070 pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int;
0071 
0072 const Color = enum {
0073   red,
0074   @"really red",
0075 };
0076 const color: Color = .@"really red";
0077 
0078 var y: i32 = add(10, x);
0079 const x: i32 = add(12, 34);
0080 
0081 test "comptime vars" {
0082     var x: i32 = 1;
0083     comptime var y: i32 = 1;
0084 
0085     x += 1;
0086     y += 1;
0087 
0088     try expect(x == 2);
0089     try expect(y == 2);
0090 
0091     if (y != 2) {
0092         // This compile error never triggers because y is a comptime variable,
0093         // and so `y != 2` is a comptime value, and this if is statically evaluated.
0094         @compileError("wrong y value");
0095     }
0096 }
0097 
0098 const decimal_int = 98222;
0099 const hex_int = 0xff;
0100 const another_hex_int = 0xFF;
0101 const octal_int = 0o755;
0102 const binary_int = 0b11110000;
0103 
0104 // underscores may be placed between two digits as a visual separator
0105 const one_billion = 1_000_000_000;
0106 const binary_mask = 0b1_1111_1111;
0107 const permissions = 0o7_5_5;
0108 const big_address = 0xFF80_0000_0000_0000;
0109 
0110 const floating_point = 123.0E+77;
0111 const another_float = 123.0;
0112 const yet_another = 123.0e+77;
0113 
0114 const hex_floating_point = 0x103.70p-5;
0115 const another_hex_float = 0x103.70;
0116 const yet_another_hex_float = 0x103.70P-5;
0117 
0118 // underscores may be placed between two digits as a visual separator
0119 const lightspeed = 299_792_458.000_000;
0120 const nanosecond = 0.000_000_001;
0121 const more_hex = 0x1234_5678.9ABC_CDEFp-10;
0122 
0123 const A = error{One};
0124 const B = error{Two};
0125 (A || B) == error{One, Two}
0126 
0127 const x: u32 = 1234;
0128 const ptr = &x;
0129 ptr.* == 1234
0130 
0131 // get the size of an array
0132 comptime {
0133     assert(message.len == 5);
0134 }
0135 
0136 test "iterate over an array" {
0137     var sum: usize = 0;
0138     for (message) |byte| {
0139         sum += byte;
0140     }
0141     try expect(sum == 'h' + 'e' + 'l' * 2 + 'o');
0142 }
0143 
0144 // use compile-time code to initialize an array
0145 var fancy_array = init: {
0146     var initial_value: [10]Point = undefined;
0147     for (initial_value) |*pt, i| {
0148         pt.* = Point{
0149             .x = @intCast(i32, i),
0150             .y = @intCast(i32, i) * 2,
0151         };
0152     }
0153     break :init initial_value;
0154 };
0155 
0156 test "switch on non-exhaustive enum" {
0157     try expect(result);
0158     const is_one = switch (number) {
0159         .one => true,
0160         else => false,
0161     };
0162     try expect(is_one);
0163 
0164     if (a != b) {
0165         try expect(true);
0166     } else if (a == 9) {
0167         unreachable;
0168     } else {
0169         unreachable;
0170     }
0171 }
0172 
0173 fn deferErrorExample(is_error: bool) !void {
0174     print("\nstart of function\n", .{});
0175 
0176     // This will always be executed on exit
0177     defer {
0178         print("end of function\n", .{});
0179     }
0180 
0181     errdefer {
0182         print("encountered an error!\n", .{});
0183     }
0184 }
0185 
0186 pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
0187     return asm volatile ("syscall"
0188         : [ret] "={rax}" (-> usize)
0189         : [number] "{rax}" (number),
0190           [arg1] "{rdi}" (arg1),
0191           [arg2] "{rsi}" (arg2),
0192           [arg3] "{rdx}" (arg3)
0193         : "rcx", "r11"
0194     );
0195 }