authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 13:03:52-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-16 13:03:52-04:00
log140d9df99b08d0a08e91019fa64e4a452b363101
tree165a0c6993f5a223f26bd74e4eae0c6ecd841dc1
parent5edabb3990c9a1dce61e1c3957a218d635e27210
parent2ebd6bd7060dab3347a736ffd8a0ca5914624dc4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8506 from LemonBoy/test-c-file

build: Test the c.zig file too

3 files changed, 55 insertions(+), 26 deletions(-)

build.zig+1
...@@ -264,6 +264,7 @@ pub fn build(b: *Builder) !void {...@@ -264,6 +264,7 @@ pub fn build(b: *Builder) !void {
264 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/std.zig", "std", "Run the standard library tests", modes, false, skip_non_native, skip_libc, is_wine_enabled, is_qemu_enabled, is_wasmtime_enabled, glibc_multi_dir));264 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/std.zig", "std", "Run the standard library tests", modes, false, skip_non_native, skip_libc, is_wine_enabled, is_qemu_enabled, is_wasmtime_enabled, glibc_multi_dir));
265265
266 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, true, skip_non_native, true, is_wine_enabled, is_qemu_enabled, is_wasmtime_enabled, glibc_multi_dir));266 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, true, skip_non_native, true, is_wine_enabled, is_qemu_enabled, is_wasmtime_enabled, glibc_multi_dir));
267 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/special/c.zig", "minilibc", "Run the mini libc tests", modes, true, skip_non_native, true, is_wine_enabled, is_qemu_enabled, is_wasmtime_enabled, glibc_multi_dir));
267268
268 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));269 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
269 test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));270 test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));
lib/std/math/sqrt.zig+17-3
...@@ -39,7 +39,13 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {...@@ -39,7 +39,13 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {
39 }39 }
40}40}
4141
42fn sqrt_int(comptime T: type, value: T) std.meta.Int(.unsigned, @typeInfo(T).Int.bits / 2) {42fn sqrt_int(comptime T: type, value: T) Sqrt(T) {
43 switch (T) {
44 u0 => return 0,
45 u1 => return value,
46 else => {},
47 }
48
43 var op = value;49 var op = value;
44 var res: T = 0;50 var res: T = 0;
45 var one: T = 1 << (@typeInfo(T).Int.bits - 2);51 var one: T = 1 << (@typeInfo(T).Int.bits - 2);
...@@ -58,11 +64,13 @@ fn sqrt_int(comptime T: type, value: T) std.meta.Int(.unsigned, @typeInfo(T).Int...@@ -58,11 +64,13 @@ fn sqrt_int(comptime T: type, value: T) std.meta.Int(.unsigned, @typeInfo(T).Int
58 one >>= 2;64 one >>= 2;
59 }65 }
6066
61 const ResultType = std.meta.Int(.unsigned, @typeInfo(T).Int.bits / 2);67 const ResultType = Sqrt(T);
62 return @intCast(ResultType, res);68 return @intCast(ResultType, res);
63}69}
6470
65test "math.sqrt_int" {71test "math.sqrt_int" {
72 expect(sqrt_int(u0, 0) == 0);
73 expect(sqrt_int(u1, 1) == 1);
66 expect(sqrt_int(u32, 3) == 1);74 expect(sqrt_int(u32, 3) == 1);
67 expect(sqrt_int(u32, 4) == 2);75 expect(sqrt_int(u32, 4) == 2);
68 expect(sqrt_int(u32, 5) == 2);76 expect(sqrt_int(u32, 5) == 2);
...@@ -74,7 +82,13 @@ test "math.sqrt_int" {...@@ -74,7 +82,13 @@ test "math.sqrt_int" {
74/// Returns the return type `sqrt` will return given an operand of type `T`.82/// Returns the return type `sqrt` will return given an operand of type `T`.
75pub fn Sqrt(comptime T: type) type {83pub fn Sqrt(comptime T: type) type {
76 return switch (@typeInfo(T)) {84 return switch (@typeInfo(T)) {
77 .Int => |int| std.meta.Int(.unsigned, int.bits / 2),85 .Int => |int| {
86 return switch (int.bits) {
87 0 => u0,
88 1 => u1,
89 else => std.meta.Int(.unsigned, int.bits / 2),
90 };
91 },
78 else => T,92 else => T,
79 };93 };
80}94}
lib/std/special/c.zig+37-23
...@@ -85,7 +85,7 @@ test "strncpy" {...@@ -85,7 +85,7 @@ test "strncpy" {
85 var s1: [9:0]u8 = undefined;85 var s1: [9:0]u8 = undefined;
8686
87 s1[0] = 0;87 s1[0] = 0;
88 _ = strncpy(&s1, "foobarbaz", 9);88 _ = strncpy(&s1, "foobarbaz", @sizeOf(@TypeOf(s1)));
89 std.testing.expectEqualSlices(u8, "foobarbaz", std.mem.spanZ(&s1));89 std.testing.expectEqualSlices(u8, "foobarbaz", std.mem.spanZ(&s1));
90}90}
9191
...@@ -993,17 +993,24 @@ export fn sqrt(x: f64) f64 {...@@ -993,17 +993,24 @@ export fn sqrt(x: f64) f64 {
993}993}
994994
995test "sqrt" {995test "sqrt" {
996 const epsilon = 0.000001;996 const V = [_]f64{
997997 0.0,
998 std.testing.expect(sqrt(0.0) == 0.0);998 4.089288054930154,
999 std.testing.expect(std.math.approxEqAbs(f64, sqrt(2.0), 1.414214, epsilon));999 7.538757127071935,
1000 std.testing.expect(std.math.approxEqAbs(f64, sqrt(3.6), 1.897367, epsilon));1000 8.97780793672623,
1001 std.testing.expect(sqrt(4.0) == 2.0);1001 5.304443821913729,
1002 std.testing.expect(std.math.approxEqAbs(f64, sqrt(7.539840), 2.745877, epsilon));1002 5.682408965311888,
1003 std.testing.expect(std.math.approxEqAbs(f64, sqrt(19.230934), 4.385309, epsilon));1003 0.5846878579110049,
1004 std.testing.expect(sqrt(64.0) == 8.0);1004 3.650338664297043,
1005 std.testing.expect(std.math.approxEqAbs(f64, sqrt(64.1), 8.006248, epsilon));1005 0.3178091951800732,
1006 std.testing.expect(std.math.approxEqAbs(f64, sqrt(8942.230469), 94.563367, epsilon));1006 7.1505232436382835,
1007 3.6589165881946464,
1008 };
1009
1010 // Note that @sqrt will either generate the sqrt opcode (if supported by the
1011 // target ISA) or a call to `sqrtf` otherwise.
1012 for (V) |val|
1013 std.testing.expectEqual(@sqrt(val), sqrt(val));
1007}1014}
10081015
1009test "sqrt special" {1016test "sqrt special" {
...@@ -1091,17 +1098,24 @@ export fn sqrtf(x: f32) f32 {...@@ -1091,17 +1098,24 @@ export fn sqrtf(x: f32) f32 {
1091}1098}
10921099
1093test "sqrtf" {1100test "sqrtf" {
1094 const epsilon = 0.000001;1101 const V = [_]f32{
10951102 0.0,
1096 std.testing.expect(sqrtf(0.0) == 0.0);1103 4.089288054930154,
1097 std.testing.expect(std.math.approxEqAbs(f32, sqrtf(2.0), 1.414214, epsilon));1104 7.538757127071935,
1098 std.testing.expect(std.math.approxEqAbs(f32, sqrtf(3.6), 1.897367, epsilon));1105 8.97780793672623,
1099 std.testing.expect(sqrtf(4.0) == 2.0);1106 5.304443821913729,
1100 std.testing.expect(std.math.approxEqAbs(f32, sqrtf(7.539840), 2.745877, epsilon));1107 5.682408965311888,
1101 std.testing.expect(std.math.approxEqAbs(f32, sqrtf(19.230934), 4.385309, epsilon));1108 0.5846878579110049,
1102 std.testing.expect(sqrtf(64.0) == 8.0);1109 3.650338664297043,
1103 std.testing.expect(std.math.approxEqAbs(f32, sqrtf(64.1), 8.006248, epsilon));1110 0.3178091951800732,
1104 std.testing.expect(std.math.approxEqAbs(f32, sqrtf(8942.230469), 94.563370, epsilon));1111 7.1505232436382835,
1112 3.6589165881946464,
1113 };
1114
1115 // Note that @sqrt will either generate the sqrt opcode (if supported by the
1116 // target ISA) or a call to `sqrtf` otherwise.
1117 for (V) |val|
1118 std.testing.expectEqual(@sqrt(val), sqrtf(val));
1105}1119}
11061120
1107test "sqrtf special" {1121test "sqrtf special" {