Commit graph

148 commits

Author SHA1 Message Date
gooncreeper
9fa9c7abd0 std.testing.expectError: fix error names 2024-11-12 14:35:14 +02:00
Linus Groh
8588964972 Replace deprecated default initializations with decl literals 2024-09-12 16:01:23 +01:00
Andrew Kelley
892ce7ef52 rework fuzzing API
The previous API used `std.testing.fuzzInput(.{})` however that has the
problem that users call it multiple times incorrectly, and there might
be work happening to obtain the corpus which should not be included in
coverage analysis, and which must not slow down iteration speed.

This commit restructures it so that the main loop lives in libfuzzer and
directly calls the "test one" function.

In this commit I was a little too aggressive because I made the test
runner export `fuzzer_one` for this purpose. This was motivated by
performance, but it causes "exported symbol collision: fuzzer_one" to
occur when more than one fuzz test is provided.

There are three ways to solve this:

1. libfuzzer needs to be passed a function pointer instead. Possible
   performance downside.

2. build runner needs to build a different process per fuzz test.
   Potentially wasteful and unclear how to isolate them.

3. test runner needs to perform a relocation at runtime to point the
   function call to the relevant unit test. Portability issues and
   dubious performance gains.
2024-09-11 13:41:29 -07:00
mlugg
0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.

This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
2024-08-28 08:39:59 +01:00
Andrew Kelley
6f3767862d implement std.testing.fuzzInput
For now this returns a dummy fuzz input.
2024-07-25 18:52:20 -07:00
Andrew Kelley
5058beb179 implement std.testing.fuzzInput
for the -fno-fuzz case. The other case will take more work in libfuzzer.
2024-07-25 18:52:20 -07:00
Andrew Kelley
33c7984183 add std.testing.random_seed
closes #17609
2024-07-23 11:43:12 -07:00
Krzysztof Wolicki
0cc42d090f
std.fs.Dir: Rename OpenDirOptions to OpenOptions (#20542)
* std.fs.Dir: Rename OpenDirOptions to OpenOptions
https://ziglang.org/documentation/master/#Avoid-Redundant-Names-in-Fully-Qualified-Namespaces

* std.fs.Dir: Add deprecated alias `OpenDirOptions`
2024-07-09 13:36:38 -07:00
rohlem
17ce3e5a17 fix std.testing.expectEqual for comptime-only union
switch from `inline for` with `std.mem.eql`
to `inline else` and tag comparison;
expectEqualDeep(Inner) was already doing this.

add a previously-failing test case.
2024-06-18 19:01:05 +02:00
Andrew Kelley
3b77f1ed7e rename zig-cache to .zig-cache
closes #20077
2024-05-29 10:20:15 -07:00
David Rubin
ffb63a05a3 riscv: finally fix bug + airAggregateInit
i just hadn't realized that I placed the `riscv_start` branch in the non-simplified
starts
2024-05-11 02:17:24 -07:00
David Rubin
d9e0cafe64 riscv: add stage2_riscv to test matrix and bypass failing tests 2024-05-11 02:17:24 -07:00
Andrew Kelley
12191c8a22 std: promote tests to doctests
Now these show up as "example usage" in generated documentation.
2024-03-21 14:11:46 -07:00
Travis Staloch
30bf8d7147 testing.expectFmt() - reuse expectEqualStrings() 2024-02-27 14:40:28 -08:00
Wooster
ad7a09d95a std.testing.expectEqualSlices: some improvements
This mainly replaces ChunkIterator with std.mem.window and also
prints \n, \r, \t using Unicode symbols instead of periods because
they're common non-printable characters.
This same code exists in std.debug.hexdump.
At some point maybe this code could be exposed through a public
function. Then we could reuse the code in both places.
2024-01-08 00:13:21 -07:00
Carl Åstholm
e5994f5f57 Update std.testing.expectEqual and friends to use peer type resolution
This commit changes the type of the second parameter to `anytype`, which should make it easier to pass literals to these functions. This change shouldn't *silently* break existing code (the assertions themselves should retain the same behavior as before) but it may result in some new compile errors when struct/union/array literals or builtins like `@bitCast` are used for the second argument. These compile errors can be fixed by explicitly coercing these expressions to the correct type using `@as`.
2024-01-03 21:20:48 +01:00
Andrew Kelley
519ba9bb65 Revert "Merge pull request #12060 from Vexu/IterableDir"
This reverts commit da94227f78, reversing
changes made to 8f943b3d33.

I was against this change originally, but decided to approve it to keep
an open mind. After a year of trying it in practice, I firmly believe
that the previous way of doing it was better.
2023-11-22 12:35:33 -07:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Robin Voetter
3ca1f88898
std.testing: disable expectEqualSlices printing for spirv 2023-10-15 14:00:26 +02:00
Robin Voetter
dc44baf763
std.testing: allow print() at comptime
This allows functions like expectEqual to be performed at comptime. If
an error is detected, the result is logged via a compile error.
2023-10-15 14:00:08 +02:00
travisstaloch
759b0fe00a
std.testing: expectEqualDeep() - support self-referential structs 2023-09-23 20:25:57 +00:00
Gregory Anders
cab9da35bd std: enable FailingAllocator to fail on resize
Now that allocator.resize() is allowed to fail, programs may wish to
test code paths that handle resize() failure. The simplest way to do
this now is to replace the vtable of the testing allocator with one
that uses Allocator.noResize for the 'resize' function pointer.

An alternative way to support this testing capability is to augment the
FailingAllocator (which is already useful for testing allocation failure
scenarios) to intentionally fail on calls to resize(). To do this, add a
'resize_fail_index' parameter to the FailingAllocator that causes
resize() to fail after the given number of calls.
2023-09-06 19:06:32 +03:00
Ryan Liptak
90fde14c5f std.testing.expectEqualSlices: On failure, print address for pointer types
When comparing slice elements, `std.meta.eql` is used which only compares pointer address and length to determine equality for pointer types. This previously led to confusing results where `expectEqualSlices` would appear to fail on seemingly equal slices (judging by the output of `expectEqualSlices`. For example:

try testing.expectEqualSlices(
    []const i64,
    &[_][]const i64{ &[_]i64{ 1, 2, 3 }, &[_]i64{ 5, 5, 5 } },
    &[_][]const i64{ &[_]i64{ 1, 2, 3 }, &[_]i64{ 5, 5, 5 } },
);

Previously, this would result in:

============ expected this output: =============  len: 2 (0x2)

[0]: { 1, 2, 3 }
[1]: { 5, 5, 5 }

============= instead found this: ==============  len: 2 (0x2)

[0]: { 1, 2, 3 }
[1]: { 5, 5, 5 }

================================================

After this commit, it will result in:

============ expected this output: =============  len: 2 (0x2)

[0]i64@7ff7e2773758: { 1, 2, 3 }
[1]i64@7ff7e2773770: { 5, 5, 5 }

============= instead found this: ==============  len: 2 (0x2)

[0]i64@7ff7e2773788: { 1, 2, 3 }
[1]i64@7ff7e27737a0: { 5, 5, 5 }

================================================
2023-08-05 11:54:26 -07:00
Jacob G-W
3c08fe931a make @typeInfo not return private decls
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
2023-07-25 16:19:08 -07:00
antlilja
b463e429b8 Remove len parameter from splat in standard lib 2023-07-12 15:35:57 -07:00
Robin Voetter
aa398034eb
testing: disable printing for targets that do not support it
The SPIR-V cannot print the helpful error messages from the
std.testing module. This commit overrides the default print
function used here to one that only prints if the target supports
it. For now, its only done for SPIR-V, but this function could
be adapted to more targets that need it.
2023-07-01 14:29:11 +02:00
Motiejus Jakštys
d41111d7ef mem: rename align*Generic to mem.align*
Anecdote 1: The generic version is way more popular than the non-generic
one in Zig codebase:

     git grep -w alignForward | wc -l
    56
     git grep -w alignForwardGeneric | wc -l
    149

     git grep -w alignBackward | wc -l
    6
     git grep -w alignBackwardGeneric | wc -l
    15

Anecdote 2: In my project (turbonss) that does much arithmetic and
alignment I exclusively use the Generic functions.

Anecdote 3: we used only the Generic versions in the Macho Man's linker
workshop.
2023-06-17 12:49:13 -07:00
Eric Joldasov
d04a262a3d
std.math: hard deprecate obsolete constants (soft deprecated in 0.10)
Followup to 5b8ac9821d.
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-13 23:45:12 +06:00
mlugg
4976b58ab1
Prevent analysis of functions only referenced at comptime
The idea here is that there are two ways we can reference a function at runtime:

* Through a direct call, i.e. where the function is comptime-known
* Through a function pointer

This means we can easily perform a form of rudimentary escape analysis
on functions. If we ever see a `decl_ref` or `ref` of a function, we
have a function pointer, which could "leak" into runtime code, so we
emit the function; but for a plain `decl_val`, there's no need to.

This change means that `comptime { _ = f; }` no longer forces a function
to be emitted, which was used for some things (mainly tests). These use
sites have been replaced with `_ = &f;`, which still triggers analysis
of the function body, since you're taking a pointer to the function.

Resolves: #6256
Resolves: #15353
2023-05-29 23:06:08 +01:00
Linus Groh
0f6fa3f20b std: Move std.debug.{TTY.Config,detectTTYConfig} to std.io.tty
Also get rid of the TTY wrapper struct, which was exlusively used as a
namespace - this is done by the tty.zig root struct now.

detectTTYConfig has been renamed to just detectConfig, which is enough
given the new namespace. Additionally, a doc comment had been added.
2023-05-24 10:15:02 +01:00
Linus Groh
39c2eee285 std.debug: Rename TTY.Color enum values to snake case 2023-05-24 10:15:02 +01:00
dweiller
bd3360e03d convert s[start..start+len] to s[start..][0..len] 2023-05-07 15:55:21 +10:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Jiacai Liu
d8c3c11c6c
std: add expectEqualDeep (#13995) 2023-01-29 22:00:14 +00:00
r00ster91
aac2d6b56f std.builtin: rename Type.UnionField and Type.StructField's field_type to type 2022-12-17 14:11:33 +01:00
Ryan Liptak
88d927a511 std.debug.TTY: Fix colors not resetting on Windows
This fixes a regression introduced in #12298 where colors would never reset in a Windows console because the attributes would be queried on every `setColor` call, and then try to 'reset' the attributes to what it just queried (i.e. it was essentially doing a complicated no-op on .Reset).

This fixes the problem while (I think) keeping with the spirit of the changes in #12298--that is, `TTY.Config` is not specifically tied to stderr like it was before #12298. To that end, detectTTYConfig now takes a `File` and that's what gets used to query the initial attributes to reset to.

(for context, before #12298, the first `setColor` call is where the reset attributes would get queried and it would always use stderr to do it)
2022-12-15 03:49:26 -05:00
Andrew Kelley
6378644d4e
Merge pull request #13907 from Vexu/call-merge
Remove `stack` option from `@call`
2022-12-13 18:15:18 -05:00
Veikka Tuominen
08b2d491bc update usages of @call 2022-12-13 13:14:20 +02:00
r00ster91
d8b4588d5f fix(terminal): handle some possible errors and resolve TODOs 2022-12-13 09:11:30 +01:00
Veikka Tuominen
9d93b2ccf1 Eliminate BoundFn type from the language
Closes #9484
2022-12-09 20:37:18 -07:00
Ryan Liptak
71c82393eb std.testing: Fully absorb expectEqualBytes into expectEqualSlices
- In #13720, expectEqualBytes was added as a standalone function
- In #13723, expectEqualSlices was made to use expectEqualBytes when the type was u8
- In this commit, expectEqualSlices has fully absorbed expectEqualBytes, and expectEqualBytes itself has been removed

For non-`u8` types, expectEqualSlices will now work similarly to expectEqualBytes (highlighting diffs in red), but will use a full line for each index and therefore will only print a maximum of 16 indexes.
2022-12-09 04:36:27 -05:00
Veikka Tuominen
92f1a29c40 AstGen: make @compileError operand implicitly comptime
This matches the language reference.
2022-12-07 14:48:24 +02:00
Ryan Liptak
c37afa2811 std.testing: Improve expectEqualBytes for large inputs and make expectEqualSlices use it
`expectEqualBytes` will now truncate the hexdump of each input to a maximum window of 256 bytes, which makes it safe to use for arbitrarily large inputs. Therefore, it can be used in `expectEqualSlices` when the type is u8.
2022-12-01 02:18:51 -05:00
Ryan Liptak
34fa6a1e04 std.testing: Add expectEqualBytes that outputs hexdumps with diffs highlighted in red
The coloring is controlled by `std.debug.detectTTYConfig` so it will be disabled when appropriate.
2022-11-30 18:57:37 -05:00
Cody Tapscott
a0af3c7a19 std.os: Fix std.os.chdir for WASI
Test coverage was lacking for chdir() on WASI, allowing this to
regress.

This change makes os.chdir() compile again, and improves the test
logic to use our standard CWD support for WASI.
2022-11-26 22:06:15 +02:00
Nick Cernis
8a5818535b
Make invalidFmtError public and use in place of compileErrors for bad format strings (#13526)
* Export invalidFmtErr

To allow consistent use of "invalid format string" compile error
response for badly formatted format strings.

See https://github.com/ziglang/zig/pull/13489#issuecomment-1311759340.

* Replace format compile errors with invalidFmtErr

- Provides more consistent compile errors.
- Gives user info about the type of the badly formated value.

* Rename invalidFmtErr as invalidFmtError

For consistency. Zig seems to use “Error” more often than “Err”.

* std: add invalid format string checks to remaining custom formatters

* pass reference-trace to comp when building build file; fix checkobjectstep
2022-11-12 21:03:24 +02:00
r00ster91
654e0b6679 fix(text): hyphenation and other fixes 2022-10-05 21:19:10 +02:00
r00ster91
f6312e4b69 docs: minor improvements
Just some minor improvements when passing by.
2022-09-29 21:40:25 +02:00
r00ster91
fda0b2c372 fix(std.testing.refAllDeclsRecursive): silently return if !builtin.is_test 2022-09-29 21:40:25 +02:00
Jacob Young
5e0d8a435e testing: fix copy paste typo 2022-09-29 14:39:10 +03:00