zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

blob 4e7c94f2 (6206B) - Raw


      1 const assert = @import("std").debug.assert;
      2 
      3 fn compileTimeRecursion() {
      4     @setFnTest(this);
      5 
      6     assert(some_data.len == 21);
      7 }
      8 var some_data: [usize(fibonacci(7))]u8 = undefined;
      9 fn fibonacci(x: i32) -> i32 {
     10     if (x <= 1) return 1;
     11     return fibonacci(x - 1) + fibonacci(x - 2);
     12 }
     13 
     14 
     15 
     16 fn unwrapAndAddOne(blah: ?i32) -> i32 {
     17     return ??blah + 1;
     18 }
     19 const should_be_1235 = unwrapAndAddOne(1234);
     20 fn testStaticAddOne() {
     21     @setFnTest(this);
     22     assert(should_be_1235 == 1235);
     23 }
     24 
     25 fn inlinedLoop() {
     26     @setFnTest(this);
     27 
     28     comptime var i = 0;
     29     comptime var sum = 0;
     30     inline while (i <= 5; i += 1)
     31         sum += i;
     32     assert(sum == 15);
     33 }
     34 
     35 fn gimme1or2(comptime a: bool) -> i32 {
     36     const x: i32 = 1;
     37     const y: i32 = 2;
     38     comptime var z: i32 = if (a) x else y;
     39     return z;
     40 }
     41 fn inlineVariableGetsResultOfConstIf() {
     42     @setFnTest(this);
     43     assert(gimme1or2(true) == 1);
     44     assert(gimme1or2(false) == 2);
     45 }
     46 
     47 
     48 fn staticFunctionEvaluation() {
     49     @setFnTest(this);
     50 
     51     assert(statically_added_number == 3);
     52 }
     53 const statically_added_number = staticAdd(1, 2);
     54 fn staticAdd(a: i32, b: i32) -> i32 { a + b }
     55 
     56 
     57 fn constExprEvalOnSingleExprBlocks() {
     58     @setFnTest(this);
     59 
     60     assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
     61 }
     62 
     63 fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 {
     64     const literal = 3;
     65 
     66     const result = if (b) {
     67         literal
     68     } else {
     69         x
     70     };
     71 
     72     return result;
     73 }
     74 
     75 
     76 
     77 
     78 fn staticallyInitalizedList() {
     79     @setFnTest(this);
     80 
     81     assert(static_point_list[0].x == 1);
     82     assert(static_point_list[0].y == 2);
     83     assert(static_point_list[1].x == 3);
     84     assert(static_point_list[1].y == 4);
     85 }
     86 const Point = struct {
     87     x: i32,
     88     y: i32,
     89 };
     90 const static_point_list = []Point { makePoint(1, 2), makePoint(3, 4) };
     91 fn makePoint(x: i32, y: i32) -> Point {
     92     return Point {
     93         .x = x,
     94         .y = y,
     95     };
     96 }
     97 
     98 
     99 fn staticEvalListInit() {
    100     @setFnTest(this);
    101 
    102     assert(static_vec3.data[2] == 1.0);
    103     assert(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
    104 }
    105 const static_vec3 = vec3(0.0, 0.0, 1.0);
    106 pub const Vec3 = struct {
    107     data: [3]f32,
    108 };
    109 pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
    110     Vec3 {
    111         .data = []f32 { x, y, z, },
    112     }
    113 }
    114 
    115 
    116 fn constantExpressions() {
    117     @setFnTest(this);
    118 
    119     var array : [array_size]u8 = undefined;
    120     assert(@sizeOf(@typeOf(array)) == 20);
    121 }
    122 const array_size : u8 = 20;
    123 
    124 
    125 fn constantStructWithNegation() {
    126     @setFnTest(this);
    127 
    128     assert(vertices[0].x == -0.6);
    129 }
    130 const Vertex = struct {
    131     x: f32,
    132     y: f32,
    133     r: f32,
    134     g: f32,
    135     b: f32,
    136 };
    137 const vertices = []Vertex {
    138     Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
    139     Vertex { .x =  0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
    140     Vertex { .x =  0.0, .y =  0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
    141 };
    142 
    143 
    144 fn staticallyInitalizedStruct() {
    145     @setFnTest(this);
    146 
    147     st_init_str_foo.x += 1;
    148     assert(st_init_str_foo.x == 14);
    149 }
    150 const StInitStrFoo = struct {
    151     x: i32,
    152     y: bool,
    153 };
    154 var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
    155 
    156 
    157 fn staticallyInitializedArrayLiteral() {
    158     @setFnTest(this);
    159 
    160     const y : [4]u8 = st_init_arr_lit_x;
    161     assert(y[3] == 4);
    162 }
    163 const st_init_arr_lit_x = []u8{1,2,3,4};
    164 
    165 
    166 fn constSlice() {
    167     @setFnTest(this);
    168 
    169     comptime {
    170         const a = "1234567890";
    171         assert(a.len == 10);
    172         const b = a[1...2];
    173         assert(b.len == 1);
    174         assert(b[0] == '2');
    175     }
    176 }
    177 
    178 fn tryToTrickEvalWithRuntimeIf() {
    179     @setFnTest(this);
    180 
    181     assert(testTryToTrickEvalWithRuntimeIf(true) == 10);
    182 }
    183 
    184 fn testTryToTrickEvalWithRuntimeIf(b: bool) -> usize {
    185     comptime var i: usize = 0;
    186     inline while (i < 10; i += 1) {
    187         const result = if (b) false else true;
    188     }
    189     comptime {
    190         return i;
    191     }
    192 }
    193 
    194 fn max(comptime T: type, a: T, b: T) -> T {
    195     if (T == bool) {
    196         return a || b;
    197     } else if (a > b) {
    198         return a;
    199     } else {
    200         return b;
    201     }
    202 }
    203 fn letsTryToCompareBools(a: bool, b: bool) -> bool {
    204     max(bool, a, b)
    205 }
    206 fn inlinedBlockAndRuntimeBlockPhi() {
    207     @setFnTest(this);
    208 
    209     assert(letsTryToCompareBools(true, true));
    210     assert(letsTryToCompareBools(true, false));
    211     assert(letsTryToCompareBools(false, true));
    212     assert(!letsTryToCompareBools(false, false));
    213 
    214     comptime {
    215         assert(letsTryToCompareBools(true, true));
    216         assert(letsTryToCompareBools(true, false));
    217         assert(letsTryToCompareBools(false, true));
    218         assert(!letsTryToCompareBools(false, false));
    219     }
    220 }
    221 
    222 const CmdFn = struct {
    223     name: []const u8,
    224     func: fn(i32) -> i32,
    225 };
    226 
    227 const cmd_fns = []CmdFn{
    228     CmdFn {.name = "one", .func = one},
    229     CmdFn {.name = "two", .func = two},
    230     CmdFn {.name = "three", .func = three},
    231 };
    232 fn one(value: i32) -> i32 { value + 1 }
    233 fn two(value: i32) -> i32 { value + 2 }
    234 fn three(value: i32) -> i32 { value + 3 }
    235 
    236 fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 {
    237     var result: i32 = start_value;
    238     comptime var i = 0;
    239     inline while (i < cmd_fns.len; i += 1) {
    240         if (cmd_fns[i].name[0] == prefix_char) {
    241             result = cmd_fns[i].func(result);
    242         }
    243     }
    244     return result;
    245 }
    246 
    247 fn comptimeIterateOverFnPtrList() {
    248     @setFnTest(this);
    249 
    250     assert(performFn('t', 1) == 6);
    251     assert(performFn('o', 0) == 1);
    252     assert(performFn('w', 99) == 99);
    253 }
    254 
    255 fn evalSetDebugSafetyAtCompileTime() {
    256     @setFnTest(this);
    257 
    258     const result = comptime fnWithSetDebugSafety();
    259     assert(result == 1234);
    260 }
    261 
    262 fn fnWithSetDebugSafety() -> i32{
    263     @setDebugSafety(this, true);
    264     return 1234;
    265 }
    266 
    267 
    268 
    269 const SimpleStruct = struct {
    270     field: i32,
    271 
    272     fn method(self: &const SimpleStruct) -> i32 {
    273         return self.field + 3;
    274     }
    275 };
    276 
    277 var simple_struct = SimpleStruct{ .field = 1234, };
    278 
    279 const bound_fn = simple_struct.method;
    280 
    281 fn callMethodOnBoundFnReferringToVarInstance() {
    282     @setFnTest(this);
    283 
    284     assert(bound_fn() == 1237);
    285 }
    286 
    287 
    288 
    289 fn ptrToLocalArrayArgumentAtComptime() {
    290     @setFnTest(this);
    291 
    292     comptime {
    293         var bytes: [10]u8 = undefined;
    294         modifySomeBytes(bytes[0...]);
    295         assert(bytes[0] == 'a');
    296         assert(bytes[9] == 'b');
    297     }
    298 }
    299 
    300 fn modifySomeBytes(bytes: []u8) {
    301     bytes[0] = 'a';
    302     bytes[9] = 'b';
    303 }