mirror of
https://github.com/ziglang/zig.git
synced 2024-12-03 18:38:45 +00:00
5619ce2406
Conflicts: * doc/langref.html.in * lib/std/enums.zig * lib/std/fmt.zig * lib/std/hash/auto_hash.zig * lib/std/math.zig * lib/std/mem.zig * lib/std/meta.zig * test/behavior/alignof.zig * test/behavior/bitcast.zig * test/behavior/bugs/1421.zig * test/behavior/cast.zig * test/behavior/ptrcast.zig * test/behavior/type_info.zig * test/behavior/vector.zig Master branch added `try` to a bunch of testing function calls, and some lines also had changed how to refer to the native architecture and other `@import("builtin")` stuff.
48 lines
1.1 KiB
Zig
48 lines
1.1 KiB
Zig
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" {
|
|
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 {
|
|
const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
|
|
defer testing.allocator.free(args);
|
|
for (args) |_, 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) |item, index| {
|
|
if (b[index] != item) return false;
|
|
}
|
|
return true;
|
|
}
|