aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/const_slice_child.zig
blob: 68c6863bcd8129afb625a0f7a3747f90e58da88e (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
const builtin = @import("builtin");
const std = @import("std");
const debug = std.debug;
const testing = std.testing;
const expect = testing.expect;

var argv: [*]const [*]const u8 = undefined;

test "const slice child" {
    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
    if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;

    const strs = [_][*]const u8{ "one", "two", "three" };
    argv = &strs;
    try bar(strs.len);
}

fn foo(args: [][]const u8) !void {
    try expect(args.len == 3);
    try expect(streql(args[0], "one"));
    try expect(streql(args[1], "two"));
    try expect(streql(args[2], "three"));
}

fn bar(argc: usize) !void {
    var args_buffer: [10][]const u8 = undefined;
    const args = args_buffer[0..argc];
    for (args, 0..) |_, i| {
        const ptr = argv[i];
        args[i] = ptr[0..strlen(ptr)];
    }
    try foo(args);
}

fn strlen(ptr: [*]const u8) usize {
    var count: usize = 0;
    while (ptr[count] != 0) : (count += 1) {}
    return count;
}

fn streql(a: []const u8, b: []const u8) bool {
    if (a.len != b.len) return false;
    for (a, 0..) |item, index| {
        if (b[index] != item) return false;
    }
    return true;
}