zig

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

blob 69e8fc65 (2846B) - Raw


      1 const builtin = @import("builtin");
      2 const std = @import("std");
      3 const expect = std.testing.expect;
      4 const expectEqual = std.testing.expectEqual;
      5 
      6 test "basic invocations" {
      7     if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
      8 
      9     const foo = struct {
     10         fn foo() i32 {
     11             return 1234;
     12         }
     13     }.foo;
     14     try expect(@call(.{}, foo, .{}) == 1234);
     15     comptime {
     16         // modifiers that allow comptime calls
     17         try expect(@call(.{}, foo, .{}) == 1234);
     18         try expect(@call(.{ .modifier = .no_async }, foo, .{}) == 1234);
     19         try expect(@call(.{ .modifier = .always_tail }, foo, .{}) == 1234);
     20         try expect(@call(.{ .modifier = .always_inline }, foo, .{}) == 1234);
     21     }
     22     {
     23         // comptime call without comptime keyword
     24         const result = @call(.{ .modifier = .compile_time }, foo, .{}) == 1234;
     25         comptime try expect(result);
     26     }
     27     {
     28         // call of non comptime-known function
     29         var alias_foo = foo;
     30         try expect(@call(.{ .modifier = .no_async }, alias_foo, .{}) == 1234);
     31         try expect(@call(.{ .modifier = .never_tail }, alias_foo, .{}) == 1234);
     32         try expect(@call(.{ .modifier = .never_inline }, alias_foo, .{}) == 1234);
     33     }
     34 }
     35 
     36 test "tuple parameters" {
     37     if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
     38 
     39     const add = struct {
     40         fn add(a: i32, b: i32) i32 {
     41             return a + b;
     42         }
     43     }.add;
     44     var a: i32 = 12;
     45     var b: i32 = 34;
     46     try expect(@call(.{}, add, .{ a, 34 }) == 46);
     47     try expect(@call(.{}, add, .{ 12, b }) == 46);
     48     try expect(@call(.{}, add, .{ a, b }) == 46);
     49     try expect(@call(.{}, add, .{ 12, 34 }) == 46);
     50     comptime try expect(@call(.{}, add, .{ 12, 34 }) == 46);
     51     {
     52         const separate_args0 = .{ a, b };
     53         const separate_args1 = .{ a, 34 };
     54         const separate_args2 = .{ 12, 34 };
     55         const separate_args3 = .{ 12, b };
     56         try expect(@call(.{ .modifier = .always_inline }, add, separate_args0) == 46);
     57         try expect(@call(.{ .modifier = .always_inline }, add, separate_args1) == 46);
     58         try expect(@call(.{ .modifier = .always_inline }, add, separate_args2) == 46);
     59         try expect(@call(.{ .modifier = .always_inline }, add, separate_args3) == 46);
     60     }
     61 }
     62 
     63 test "comptime call with bound function as parameter" {
     64     if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
     65 
     66     const S = struct {
     67         fn ReturnType(func: anytype) type {
     68             return switch (@typeInfo(@TypeOf(func))) {
     69                 .BoundFn => |info| info,
     70                 else => unreachable,
     71             }.return_type orelse void;
     72         }
     73 
     74         fn call_me_maybe() ?i32 {
     75             return 123;
     76         }
     77     };
     78 
     79     var inst: S = undefined;
     80     try expectEqual(?i32, S.ReturnType(inst.call_me_maybe));
     81 }