Skip to content

Commit d8bb139

Browse files
authored
Merge pull request ziglang#19390 from ziglang/valgrind
make the behavior tests run almost valgrind clean
2 parents a2651cb + 90c94a2 commit d8bb139

31 files changed

+403
-360
lines changed

lib/std/ascii.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub fn isWhitespace(c: u8) bool {
147147
/// See also: `isWhitespace`
148148
pub const whitespace = [_]u8{ ' ', '\t', '\n', '\r', control_code.vt, control_code.ff };
149149

150-
test "whitespace" {
150+
test whitespace {
151151
for (whitespace) |char| try std.testing.expect(isWhitespace(char));
152152

153153
var i: u8 = 0;
@@ -278,7 +278,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
278278
return output[0..ascii_string.len];
279279
}
280280

281-
test "lowerString" {
281+
test lowerString {
282282
var buf: [1024]u8 = undefined;
283283
const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
284284
try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
@@ -291,7 +291,7 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)
291291
return lowerString(result, ascii_string);
292292
}
293293

294-
test "allocLowerString" {
294+
test allocLowerString {
295295
const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
296296
defer std.testing.allocator.free(result);
297297
try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
@@ -307,7 +307,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
307307
return output[0..ascii_string.len];
308308
}
309309

310-
test "upperString" {
310+
test upperString {
311311
var buf: [1024]u8 = undefined;
312312
const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
313313
try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
@@ -320,7 +320,7 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)
320320
return upperString(result, ascii_string);
321321
}
322322

323-
test "allocUpperString" {
323+
test allocUpperString {
324324
const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
325325
defer std.testing.allocator.free(result);
326326
try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
@@ -335,7 +335,7 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
335335
return true;
336336
}
337337

338-
test "eqlIgnoreCase" {
338+
test eqlIgnoreCase {
339339
try std.testing.expect(eqlIgnoreCase("HEl💩Lo!", "hel💩lo!"));
340340
try std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
341341
try std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
@@ -345,7 +345,7 @@ pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
345345
return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
346346
}
347347

348-
test "startsWithIgnoreCase" {
348+
test startsWithIgnoreCase {
349349
try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
350350
try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
351351
}
@@ -354,7 +354,7 @@ pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
354354
return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
355355
}
356356

357-
test "endsWithIgnoreCase" {
357+
test endsWithIgnoreCase {
358358
try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
359359
try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
360360
}
@@ -409,7 +409,7 @@ fn boyerMooreHorspoolPreprocessIgnoreCase(pattern: []const u8, table: *[256]usiz
409409
}
410410
}
411411

412-
test "indexOfIgnoreCase" {
412+
test indexOfIgnoreCase {
413413
try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
414414
try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
415415
try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);

lib/std/bit_set.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ fn testStaticBitSet(comptime Set: type) !void {
16481648
try testPureBitSet(Set);
16491649
}
16501650

1651-
test "IntegerBitSet" {
1651+
test IntegerBitSet {
16521652
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
16531653

16541654
try testStaticBitSet(IntegerBitSet(0));
@@ -1661,7 +1661,7 @@ test "IntegerBitSet" {
16611661
try testStaticBitSet(IntegerBitSet(127));
16621662
}
16631663

1664-
test "ArrayBitSet" {
1664+
test ArrayBitSet {
16651665
inline for (.{ 0, 1, 2, 31, 32, 33, 63, 64, 65, 254, 500, 3000 }) |size| {
16661666
try testStaticBitSet(ArrayBitSet(u8, size));
16671667
try testStaticBitSet(ArrayBitSet(u16, size));
@@ -1671,7 +1671,7 @@ test "ArrayBitSet" {
16711671
}
16721672
}
16731673

1674-
test "DynamicBitSetUnmanaged" {
1674+
test DynamicBitSetUnmanaged {
16751675
const allocator = std.testing.allocator;
16761676
var a = try DynamicBitSetUnmanaged.initEmpty(allocator, 300);
16771677
try testing.expectEqual(@as(usize, 0), a.count());
@@ -1724,7 +1724,7 @@ test "DynamicBitSetUnmanaged" {
17241724
}
17251725
}
17261726

1727-
test "DynamicBitSet" {
1727+
test DynamicBitSet {
17281728
const allocator = std.testing.allocator;
17291729
var a = try DynamicBitSet.initEmpty(allocator, 300);
17301730
try testing.expectEqual(@as(usize, 0), a.count());
@@ -1765,7 +1765,7 @@ test "DynamicBitSet" {
17651765
}
17661766
}
17671767

1768-
test "StaticBitSet" {
1768+
test StaticBitSet {
17691769
try testing.expectEqual(IntegerBitSet(0), StaticBitSet(0));
17701770
try testing.expectEqual(IntegerBitSet(5), StaticBitSet(5));
17711771
try testing.expectEqual(IntegerBitSet(@bitSizeOf(usize)), StaticBitSet(@bitSizeOf(usize)));

lib/std/bounded_array.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub fn BoundedArrayAligned(
287287
};
288288
}
289289

290-
test "BoundedArray" {
290+
test BoundedArray {
291291
var a = try BoundedArray(u8, 64).init(32);
292292

293293
try testing.expectEqual(a.capacity(), 64);

lib/std/child_process.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ fn windowsCreateProcessSupportsExtension(ext: []const u16) ?CreateProcessSupport
12311231
return null;
12321232
}
12331233

1234-
test "windowsCreateProcessSupportsExtension" {
1234+
test windowsCreateProcessSupportsExtension {
12351235
try std.testing.expectEqual(CreateProcessSupportedExtension.exe, windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e' }).?);
12361236
try std.testing.expect(windowsCreateProcessSupportsExtension(&[_]u16{ '.', 'e', 'X', 'e', 'c' }) == null);
12371237
}
@@ -1322,7 +1322,7 @@ pub fn argvToCommandLineWindows(
13221322
return try unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);
13231323
}
13241324

1325-
test "argvToCommandLineWindows" {
1325+
test argvToCommandLineWindows {
13261326
const t = testArgvToCommandLineWindows;
13271327

13281328
try t(&.{
@@ -1556,7 +1556,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) !
15561556
return envp_buf;
15571557
}
15581558

1559-
test "createNullDelimitedEnvMap" {
1559+
test createNullDelimitedEnvMap {
15601560
const testing = std.testing;
15611561
const allocator = testing.allocator;
15621562
var envmap = EnvMap.init(allocator);

lib/std/crypto/utils.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub inline fn secureZero(comptime T: type, s: []T) void {
138138
@memset(@as([]volatile T, s), 0);
139139
}
140140

141-
test "timingSafeEql" {
141+
test timingSafeEql {
142142
var a: [100]u8 = undefined;
143143
var b: [100]u8 = undefined;
144144
random.bytes(a[0..]);
@@ -162,7 +162,7 @@ test "timingSafeEql (vectors)" {
162162
try testing.expect(timingSafeEql(@Vector(100, u8), v1, v3));
163163
}
164164

165-
test "timingSafeCompare" {
165+
test timingSafeCompare {
166166
var a = [_]u8{10} ** 32;
167167
var b = [_]u8{10} ** 32;
168168
try testing.expectEqual(timingSafeCompare(u8, &a, &b, .big), .eq);
@@ -195,7 +195,7 @@ test "timingSafe{Add,Sub}" {
195195
}
196196
}
197197

198-
test "secureZero" {
198+
test secureZero {
199199
var a = [_]u8{0xfe} ** 8;
200200
var b = [_]u8{0xfe} ** 8;
201201

lib/std/debug.zig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach
905905
return null;
906906
}
907907

908-
test "machoSearchSymbols" {
908+
test machoSearchSymbols {
909909
const symbols = [_]MachoSymbol{
910910
.{ .addr = 100, .strx = undefined, .size = undefined, .ofile = undefined },
911911
.{ .addr = 200, .strx = undefined, .size = undefined, .ofile = undefined },
@@ -1504,7 +1504,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
15041504
}
15051505
}
15061506

1507-
test "printLineFromFileAnyOs" {
1507+
test printLineFromFileAnyOs {
15081508
var output = std.ArrayList(u8).init(std.testing.allocator);
15091509
defer output.deinit();
15101510
const output_stream = output.writer();
@@ -2858,6 +2858,16 @@ pub const SafetyLock = struct {
28582858
}
28592859
};
28602860

2861+
/// Detect whether the program is being executed in the Valgrind virtual machine.
2862+
///
2863+
/// When Valgrind integrations are disabled, this returns comptime-known false.
2864+
/// Otherwise, the result is runtime-known.
2865+
pub inline fn inValgrind() bool {
2866+
if (@inComptime()) return false;
2867+
if (!builtin.valgrind_support) return false;
2868+
return std.valgrind.runningOnValgrind() > 0;
2869+
}
2870+
28612871
test {
28622872
_ = &dump_hex;
28632873
}

lib/std/fifo.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ test "LinearFifo(u8, .Dynamic)" {
507507
}
508508
}
509509

510-
test "LinearFifo" {
510+
test LinearFifo {
511511
inline for ([_]type{ u1, u8, u16, u64 }) |T| {
512512
inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| {
513513
const FifoType = LinearFifo(T, bt);

lib/std/fmt.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ pub fn fmtDuration(ns: u64) Formatter(formatDuration) {
13031303
return .{ .data = data };
13041304
}
13051305

1306-
test "fmtDuration" {
1306+
test fmtDuration {
13071307
var buf: [24]u8 = undefined;
13081308
inline for (.{
13091309
.{ .s = "0ns", .d = 0 },
@@ -1367,7 +1367,7 @@ pub fn fmtDurationSigned(ns: i64) Formatter(formatDurationSigned) {
13671367
return .{ .data = ns };
13681368
}
13691369

1370-
test "fmtDurationSigned" {
1370+
test fmtDurationSigned {
13711371
var buf: [24]u8 = undefined;
13721372
inline for (.{
13731373
.{ .s = "0ns", .d = 0 },
@@ -1497,7 +1497,7 @@ pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
14971497
return parseWithSign(T, buf, base, .pos);
14981498
}
14991499

1500-
test "parseInt" {
1500+
test parseInt {
15011501
try std.testing.expect((try parseInt(i32, "-10", 10)) == -10);
15021502
try std.testing.expect((try parseInt(i32, "+10", 10)) == 10);
15031503
try std.testing.expect((try parseInt(u32, "+10", 10)) == 10);
@@ -1639,7 +1639,7 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!
16391639
return parseWithSign(T, buf, base, .pos);
16401640
}
16411641

1642-
test "parseUnsigned" {
1642+
test parseUnsigned {
16431643
try std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
16441644
try std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
16451645
try std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);
@@ -1713,7 +1713,7 @@ pub fn parseIntSizeSuffix(buf: []const u8, digit_base: u8) ParseIntError!usize {
17131713
return math.mul(usize, number, multiplier);
17141714
}
17151715

1716-
test "parseIntSizeSuffix" {
1716+
test parseIntSizeSuffix {
17171717
try std.testing.expect(try parseIntSizeSuffix("2", 10) == 2);
17181718
try std.testing.expect(try parseIntSizeSuffix("2B", 10) == 2);
17191719
try std.testing.expect(try parseIntSizeSuffix("2kB", 10) == 2000);
@@ -1796,7 +1796,7 @@ pub fn allocPrintZ(allocator: mem.Allocator, comptime fmt: []const u8, args: any
17961796
return result[0 .. result.len - 1 :0];
17971797
}
17981798

1799-
test "bufPrintInt" {
1799+
test bufPrintIntToSlice {
18001800
var buffer: [100]u8 = undefined;
18011801
const buf = buffer[0..];
18021802

@@ -1830,7 +1830,7 @@ pub inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [cou
18301830
}
18311831
}
18321832

1833-
test "comptimePrint" {
1833+
test comptimePrint {
18341834
@setEvalBranchQuota(2000);
18351835
try std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptimePrint("{}", .{100})));
18361836
try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
@@ -2445,14 +2445,14 @@ pub fn hexToBytes(out: []u8, input: []const u8) ![]u8 {
24452445
return out[0 .. in_i / 2];
24462446
}
24472447

2448-
test "bytesToHex" {
2448+
test bytesToHex {
24492449
const input = "input slice";
24502450
const encoded = bytesToHex(input, .lower);
24512451
var decoded: [input.len]u8 = undefined;
24522452
try std.testing.expectEqualSlices(u8, input, try hexToBytes(&decoded, &encoded));
24532453
}
24542454

2455-
test "hexToBytes" {
2455+
test hexToBytes {
24562456
var buf: [32]u8 = undefined;
24572457
try expectFmt("90" ** 32, "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "90" ** 32))});
24582458
try expectFmt("ABCD", "{s}", .{fmtSliceHexUpper(try hexToBytes(&buf, "ABCD"))});

lib/std/fmt/parse_float.zig

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,80 @@
1-
pub const parseFloat = @import("parse_float/parse_float.zig").parseFloat;
2-
pub const ParseFloatError = @import("parse_float/parse_float.zig").ParseFloatError;
3-
4-
const std = @import("std");
1+
const std = @import("../std.zig");
52
const math = std.math;
63
const testing = std.testing;
74
const expect = testing.expect;
85
const expectEqual = testing.expectEqual;
96
const expectError = testing.expectError;
107
const approxEqAbs = std.math.approxEqAbs;
118
const epsilon = 1e-7;
9+
const parse = @import("parse_float/parse.zig");
10+
const convertHex = @import("parse_float/convert_hex.zig").convertHex;
11+
const convertFast = @import("parse_float/convert_fast.zig").convertFast;
12+
const convertEiselLemire = @import("parse_float/convert_eisel_lemire.zig").convertEiselLemire;
13+
const convertSlow = @import("parse_float/convert_slow.zig").convertSlow;
14+
15+
pub const ParseFloatError = error{
16+
InvalidCharacter,
17+
};
18+
19+
pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T {
20+
if (@typeInfo(T) != .Float) {
21+
@compileError("Cannot parse a float into a non-floating point type.");
22+
}
23+
24+
if (T == f80) {
25+
@compileError("TODO support parsing float to f80");
26+
}
27+
28+
if (s.len == 0) {
29+
return error.InvalidCharacter;
30+
}
31+
32+
var i: usize = 0;
33+
const negative = s[i] == '-';
34+
if (s[i] == '-' or s[i] == '+') {
35+
i += 1;
36+
}
37+
if (s.len == i) {
38+
return error.InvalidCharacter;
39+
}
40+
41+
const n = parse.parseNumber(T, s[i..], negative) orelse {
42+
return parse.parseInfOrNan(T, s[i..], negative) orelse error.InvalidCharacter;
43+
};
44+
45+
if (n.hex) {
46+
return convertHex(T, n);
47+
}
48+
49+
if (convertFast(T, n)) |f| {
50+
return f;
51+
}
52+
53+
if (T == f16 or T == f32 or T == f64) {
54+
// If significant digits were truncated, then we can have rounding error
55+
// only if `mantissa + 1` produces a different result. We also avoid
56+
// redundantly using the Eisel-Lemire algorithm if it was unable to
57+
// correctly round on the first pass.
58+
if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {
59+
if (!n.many_digits) {
60+
return bf.toFloat(T, n.negative);
61+
}
62+
if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
63+
if (bf.eql(bf2)) {
64+
return bf.toFloat(T, n.negative);
65+
}
66+
}
67+
}
68+
}
69+
70+
// Unable to correctly round the float using the Eisel-Lemire algorithm.
71+
// Fallback to a slower, but always correct algorithm.
72+
return convertSlow(T, s[i..]).toFloat(T, negative);
73+
}
1274

1375
// See https://github.com/tiehuis/parse-number-fxx-test-data for a wider-selection of test-data.
1476

15-
test "parseFloat" {
77+
test parseFloat {
1678
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
1779
try testing.expectError(error.InvalidCharacter, parseFloat(T, ""));
1880
try testing.expectError(error.InvalidCharacter, parseFloat(T, " 1"));

0 commit comments

Comments
 (0)