mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 23:52:31 +00:00
1f4f8d34a1
See the new test case - this fails in the previous implementation. See #13303
31 lines
897 B
Zig
31 lines
897 B
Zig
const std = @import("std");
|
|
const common = @import("./common.zig");
|
|
|
|
comptime {
|
|
@export(memcmp, .{ .name = "memcmp", .linkage = common.linkage });
|
|
}
|
|
|
|
pub fn memcmp(vl: [*]const u8, vr: [*]const u8, n: usize) callconv(.C) c_int {
|
|
var i: usize = 0;
|
|
while (i < n) : (i += 1) {
|
|
const compared = @as(c_int, vl[i]) -% @as(c_int, vr[i]);
|
|
if (compared != 0) return compared;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
test "memcmp" {
|
|
const arr0 = &[_]u8{ 1, 1, 1 };
|
|
const arr1 = &[_]u8{ 1, 1, 1 };
|
|
const arr2 = &[_]u8{ 1, 0, 1 };
|
|
const arr3 = &[_]u8{ 1, 2, 1 };
|
|
const arr4 = &[_]u8{ 1, 0xff, 1 };
|
|
|
|
try std.testing.expect(memcmp(arr0, arr1, 3) == 0);
|
|
try std.testing.expect(memcmp(arr0, arr2, 3) > 0);
|
|
try std.testing.expect(memcmp(arr0, arr3, 3) < 0);
|
|
|
|
try std.testing.expect(memcmp(arr0, arr4, 3) < 0);
|
|
try std.testing.expect(memcmp(arr4, arr0, 3) > 0);
|
|
}
|