1const builtin = @import("builtin");
2const std = @import("std");
3
4const c = std.c;
5const mem = std.mem;
6const testing = std.testing;
7
8test "bzero" {
9 if (builtin.target.os.tag == .windows) return; // no bzero
10
11 var array: [10]u8 = [_]u8{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
12 var a = mem.zeroes([array.len]u8);
13 a[9] = '0';
14 c.bzero(&array[0], 9);
15 try testing.expect(mem.eql(u8, &array, &a));
16}
17
18fn testFfs(comptime T: type) !void {
19 const ffs = switch (T) {
20 c_int => c.ffs,
21 c_long => c.ffsl,
22 c_longlong => c.ffsll,
23 else => unreachable,
24 };
25
26 try testing.expectEqual(0, ffs(0));
27
28 for (0..@bitSizeOf(T)) |i| {
29 const bit = @as(T, 1) << @intCast(i);
30
31 try testing.expectEqual(@as(T, @intCast(i + 1)), ffs(bit));
32 }
33}
34
35test "ffs" {
36 if (builtin.target.os.tag == .openbsd) return; // no ffsl/ffsll
37 if (builtin.target.os.tag == .windows) return; // no ffs
38
39 try testFfs(c_int);
40
41 if (builtin.target.os.tag == .netbsd) return; // no ffsl/ffsll until 11
42
43 try testFfs(c_long);
44
45 if (@sizeOf(usize) == 4) return error.SkipZigTest; // TODO
46
47 try testFfs(c_longlong);
48}
49
50test "strcasecmp" {
51 if (builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // mingw-only
52
53 try testing.expect(c.strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0);
54 try testing.expect(c.strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0);
55 try testing.expect(c.strcasecmp(@ptrCast("A"), @ptrCast("b")) < 0);
56 try testing.expect(c.strcasecmp(@ptrCast("b"), @ptrCast("A")) > 0);
57 try testing.expect(c.strcasecmp(@ptrCast("A"), @ptrCast("A")) == 0);
58 try testing.expect(c.strcasecmp(@ptrCast("B"), @ptrCast("b")) == 0);
59 try testing.expect(c.strcasecmp(@ptrCast("bb"), @ptrCast("AA")) > 0);
60}
61
62test "strncasecmp" {
63 if (builtin.target.os.tag == .windows and builtin.target.abi != .gnu) return; // mingw-only
64
65 try testing.expect(c.strncasecmp(@ptrCast("a"), @ptrCast("b"), 1) < 0);
66 try testing.expect(c.strncasecmp(@ptrCast("b"), @ptrCast("a"), 1) > 0);
67 try testing.expect(c.strncasecmp(@ptrCast("A"), @ptrCast("b"), 1) < 0);
68 try testing.expect(c.strncasecmp(@ptrCast("b"), @ptrCast("A"), 1) > 0);
69 try testing.expect(c.strncasecmp(@ptrCast("A"), @ptrCast("A"), 1) == 0);
70 try testing.expect(c.strncasecmp(@ptrCast("B"), @ptrCast("b"), 1) == 0);
71 try testing.expect(c.strncasecmp(@ptrCast("bb"), @ptrCast("AA"), 2) > 0);
72}