Skip to content

Commit 3476b56

Browse files
committed
[libc++][test] Adds more generic test macros.
These macros are intended to replace the macros in rapid-cxx-test.h. Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D142808
1 parent 783cbf7 commit 3476b56

24 files changed

+412
-119
lines changed

libcxx/docs/Contributing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Pre-commit check list
3131
Before committing or creating a review, please go through this check-list to make
3232
sure you don't forget anything:
3333

34-
- Do you have tests for every public class and/or function you're adding or modifying?
34+
- Do you have :ref:`tests <testing>` for every public class and/or function you're adding or modifying?
3535
- Did you update the synopsis of the relevant headers?
3636
- Did you update the relevant files to track implementation status (in ``docs/Status/``)?
3737
- Did you mark all functions and type declarations with the :ref:`proper visibility macro <visibility-macros>`?

libcxx/docs/TestingLibcxx.rst

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Testing libc++
55
.. contents::
66
:local:
77

8+
.. _testing:
9+
810
Getting Started
911
===============
1012

@@ -121,7 +123,7 @@ modifying files on your local machine will also modify what the Docker container
121123
This is useful for editing source files as you're testing your code in the Docker container.
122124

123125
Writing Tests
124-
-------------
126+
=============
125127

126128
When writing tests for the libc++ test suite, you should follow a few guidelines.
127129
This will ensure that your tests can run on a wide variety of hardware and under
@@ -143,6 +145,189 @@ few requirements to the test suite. Here's some stuff you should know:
143145
necessarily available on all devices we may want to run the tests on (even
144146
though supporting Python is probably trivial for the build-host).
145147

148+
Structure of the testing related directories
149+
--------------------------------------------
150+
151+
The tests of libc++ are stored in libc++'s testing related subdirectories:
152+
153+
- ``libcxx/test/support`` This directory contains several helper headers with
154+
generic parts for the tests. The most important header is ``test_macros.h``.
155+
This file contains configuration information regarding the platform used.
156+
This is similar to the ``__config`` file in libc++'s ``include`` directory.
157+
Since libc++'s tests are used by other Standard libraries, tests should use
158+
the ``TEST_FOO`` macros instead of the ``_LIBCPP_FOO`` macros, which are
159+
specific to libc++.
160+
- ``libcxx/test/std`` This directory contains the tests that validate the library under
161+
test conforms to the C++ Standard. The paths and the names of the test match
162+
the section names in the C++ Standard. Note that the C++ Standard sometimes
163+
reorganises its structure, therefore some tests are at a location based on
164+
where they appeared historically in the standard. We try to strike a balance
165+
between keeping things at up-to-date locations and unnecessary churn.
166+
- ``libcxx/test/libcxx`` This directory contains the tests that validate libc++
167+
specific behavior and implementation details. For example, libc++ has
168+
"wrapped iterators" that perform bounds checks. Since those are specific to
169+
libc++ and not mandated by the Standard, tests for those are located under
170+
``libcxx/test/libcxx``. The structure of this directories follows the
171+
structure of ``libcxx/test/std``.
172+
173+
Structure of a test
174+
-------------------
175+
176+
Some platforms where libc++ is tested have requirement on the signature of
177+
``main`` and require ``main`` to explicitly return a value. Therefore the
178+
typical ``main`` function should look like:
179+
180+
.. code-block:: cpp
181+
182+
int main(int, char**) {
183+
...
184+
return 0;
185+
}
186+
187+
188+
The C++ Standard has ``constexpr`` requirements. The typical way to test that,
189+
is to create a helper ``test`` function that returns a ``bool`` and use the
190+
following ``main`` function:
191+
192+
.. code-block:: cpp
193+
194+
constexpr bool test() {
195+
...
196+
return true;
197+
}
198+
199+
int main(int, char**) {
200+
test()
201+
static_assert(test());
202+
203+
return 0;
204+
}
205+
206+
Tests in libc++ mainly use ``assert`` and ``static_assert`` for testing. There
207+
are a few helper macros and function that can be used to make it easier to
208+
write common tests.
209+
210+
libcxx/test/support/assert_macros.h
211+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212+
213+
The header contains several macros with user specified log messages. This is
214+
useful when a normal assertion failure lacks the information to easily
215+
understand why the test has failed. This usually happens when the test is in a
216+
helper function. For example the ``std::format`` tests use a helper function
217+
for its validation. When the test fails it will give the line in the helper
218+
function with the condition ``out == expected`` failed. Without knowing what
219+
the value of ``format string``, ``out`` and ``expected`` are it is not easy to
220+
understand why the test has failed. By logging these three values the point of
221+
failure can be found without resorting to a debugger.
222+
223+
Several of these macros are documented to take an ``ARG``. This ``ARG``:
224+
225+
- if it is a ``const char*`` or ``std::string`` its contents are written to
226+
the ``stderr``,
227+
- otherwise it must be a callable that is invoked without any additional
228+
arguments and is expected to produce useful output to e.g. ``stderr``.
229+
230+
This makes it possible to write additional information when a test fails,
231+
either by supplying a hard-coded string or generate it at runtime.
232+
233+
TEST_FAIL(ARG)
234+
^^^^^^^^^^^^^^
235+
236+
This macro is an unconditional failure with a log message ``ARG``. The main
237+
use-case is to fail when code is reached that should be unreachable.
238+
239+
240+
TEST_REQUIRE(CONDITION, ARG)
241+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
242+
243+
This macro requires its ``CONDITION`` to evaluate to ``true``. If that fails it
244+
will fail the test with a log message ``ARG``.
245+
246+
247+
TEST_LIBCPP_REQUIRE((CONDITION, ARG)
248+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
249+
250+
If the library under test is libc++ it behaves like ``TEST_REQUIRE``, else it
251+
is a no-op. This makes it possible to test libc++ specific behaviour. For
252+
example testing whether the ``what()`` of an exception thrown matches libc++'s
253+
expectations. (Usually the Standard requires certain exceptions to be thrown,
254+
but not the contents of its ``what()`` message.)
255+
256+
257+
TEST_DOES_NOT_THROW(EXPR)
258+
^^^^^^^^^^^^^^^^^^^^^^^^^
259+
260+
Validates execution of ``EXPR`` does not throw an exception.
261+
262+
TEST_THROWS_TYPE(TYPE, EXPR)
263+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
264+
265+
Validates the execution of ``EXPR`` throws an exception of the type ``TYPE``.
266+
267+
268+
TEST_VALIDATE_EXCEPTION(TYPE, PRED, EXPR)
269+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
270+
271+
Validates the execution of ``EXPR`` throws an exception of the type ``TYPE``
272+
which passes validation of ``PRED``. Using this macro makes it easier to write
273+
tests using exceptions. The code to write a test manually would be:
274+
275+
276+
.. code-block:: cpp
277+
278+
void test_excption([[maybe_unused]] int arg) {
279+
#ifndef TEST_HAS_NO_EXCEPTIONS // do nothing when tests are disabled
280+
try {
281+
foo(arg);
282+
assert(false); // validates foo really throws
283+
} catch ([[maybe_unused]] const bar& e) {
284+
LIBCPP_ASSERT(e.what() == what);
285+
return;
286+
}
287+
assert(false); // validates bar was thrown
288+
#endif
289+
}
290+
291+
The same test using a macro:
292+
293+
.. code-block:: cpp
294+
295+
void test_excption([[maybe_unused]] int arg) {
296+
TEST_VALIDATE_EXCEPTION(bar,
297+
[](const bar& e) {
298+
LIBCPP_ASSERT(e.what() == what);
299+
},
300+
foo(arg));
301+
}
302+
303+
304+
libcxx/test/support/concat_macros.h
305+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
306+
307+
This file contains a helper macro ``TEST_WRITE_CONCATENATED`` to lazily
308+
concatenate its arguments to a ``std::string`` and write it to ``stderr``. When
309+
the output can't be concatenated a default message will be written to
310+
``stderr``. This is useful for tests where the arguments use different
311+
character types like ``char`` and ``wchar_t``, the latter can't simply be
312+
written to ``stderrr``.
313+
314+
This macro is in a different header as ``assert_macros.h`` since it pulls in
315+
additional headers.
316+
317+
.. note: This macro can only be used in test using C++20 or newer. The macro
318+
was added at a time where most of lib++'s C++17 support was complete.
319+
Since it is not expected to add this to existing tests no effort was
320+
taken to make it work in earlier language versions.
321+
322+
323+
Additional reading
324+
------------------
325+
326+
The function ``CxxStandardLibraryTest`` in the file
327+
``libcxx/utils/libcxx/test/format.py`` has documentation about writing test. It
328+
explains the difference between the test named ``foo.pass.cpp`` and named
329+
``foo.verify.cpp`` are.
330+
146331
Benchmarks
147332
==========
148333

libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.functions.format.pass.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@
3535
#include "test_format_string.h"
3636
#include "test_macros.h"
3737
#include "assert_macros.h"
38+
#include "concat_macros.h"
3839

3940
auto test = []<class CharT, class... Args>(
4041
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) {
4142
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
42-
TEST_REQUIRE(
43-
out == expected,
44-
test_concat_message("\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
43+
TEST_REQUIRE(out == expected,
44+
TEST_WRITE_CONCATENATED(
45+
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
4546
};
4647

4748
auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {

libcxx/test/std/containers/container.adaptors/container.adaptors.format/format.functions.vformat.pass.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
#include "format.functions.tests.h"
3333
#include "test_macros.h"
3434
#include "assert_macros.h"
35+
#include "concat_macros.h"
3536

3637
auto test = []<class CharT, class... Args>(
3738
std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) {
3839
std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
39-
TEST_REQUIRE(
40-
out == expected,
41-
test_concat_message("\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
40+
TEST_REQUIRE(out == expected,
41+
TEST_WRITE_CONCATENATED(
42+
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
4243
};
4344

4445
auto test_exception =
@@ -49,11 +50,11 @@ auto test_exception =
4950
#ifndef TEST_HAS_NO_EXCEPTIONS
5051
try {
5152
TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
52-
TEST_FAIL(test_concat_message("\nFormat string ", fmt, "\nDidn't throw an exception.\n"));
53+
TEST_FAIL(TEST_WRITE_CONCATENATED("\nFormat string ", fmt, "\nDidn't throw an exception.\n"));
5354
} catch (const std::format_error& e) {
5455
TEST_LIBCPP_REQUIRE(
5556
e.what() == what,
56-
test_concat_message(
57+
TEST_WRITE_CONCATENATED(
5758
"\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
5859

5960
return;

libcxx/test/std/containers/sequences/vector.bool/vector.bool.fmt/format.functions.format.pass.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
#include "test_format_string.h"
3030
#include "test_macros.h"
3131
#include "assert_macros.h"
32+
#include "concat_macros.h"
3233

3334
auto test = []<class CharT, class... Args>(
3435
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) {
3536
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
36-
TEST_REQUIRE(
37-
out == expected,
38-
test_concat_message("\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
37+
TEST_REQUIRE(out == expected,
38+
TEST_WRITE_CONCATENATED(
39+
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
3940
};
4041

4142
auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {

libcxx/test/std/containers/sequences/vector.bool/vector.bool.fmt/format.functions.vformat.pass.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
#include "format.functions.tests.h"
3131
#include "test_macros.h"
3232
#include "assert_macros.h"
33+
#include "concat_macros.h"
3334

3435
auto test = []<class CharT, class... Args>(
3536
std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) {
3637
std::basic_string<CharT> out = std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
37-
TEST_REQUIRE(
38-
out == expected,
39-
test_concat_message("\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
38+
TEST_REQUIRE(out == expected,
39+
TEST_WRITE_CONCATENATED(
40+
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n'));
4041
};
4142

4243
auto test_exception =
@@ -47,11 +48,11 @@ auto test_exception =
4748
#ifndef TEST_HAS_NO_EXCEPTIONS
4849
try {
4950
TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<context_t<CharT>>(args...));
50-
TEST_FAIL(test_concat_message("\nFormat string ", fmt, "\nDidn't throw an exception.\n"));
51+
TEST_FAIL(TEST_WRITE_CONCATENATED("\nFormat string ", fmt, "\nDidn't throw an exception.\n"));
5152
} catch (const std::format_error& e) {
5253
TEST_LIBCPP_REQUIRE(
5354
e.what() == what,
54-
test_concat_message(
55+
TEST_WRITE_CONCATENATED(
5556
"\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
5657

5758
return;

libcxx/test/std/utilities/format/format.functions/escaped_output.ascii.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "make_string.h"
2727
#include "test_format_string.h"
2828
#include "assert_macros.h"
29+
#include "concat_macros.h"
2930

3031
#ifndef TEST_HAS_NO_LOCALIZATION
3132
# include <iostream>
@@ -38,7 +39,7 @@ auto test_format = []<class CharT, class... Args>(
3839
{
3940
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
4041
TEST_REQUIRE(out == expected,
41-
test_concat_message(
42+
TEST_WRITE_CONCATENATED(
4243
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n'));
4344
}
4445
#ifndef TEST_HAS_NO_LOCALIZATION

libcxx/test/std/utilities/format/format.functions/escaped_output.unicode.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "make_string.h"
3333
#include "test_format_string.h"
3434
#include "assert_macros.h"
35+
#include "concat_macros.h"
3536

3637
#ifndef TEST_HAS_NO_LOCALIZATION
3738
# include <iostream>
@@ -44,7 +45,7 @@ auto test_format = []<class CharT, class... Args>(
4445
{
4546
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
4647
TEST_REQUIRE(out == expected,
47-
test_concat_message(
48+
TEST_WRITE_CONCATENATED(
4849
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n'));
4950
}
5051
#ifndef TEST_HAS_NO_LOCALIZATION

libcxx/test/std/utilities/format/format.functions/format.locale.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
#include "string_literal.h"
2929
#include "test_format_string.h"
3030
#include "assert_macros.h"
31+
#include "concat_macros.h"
3132

3233
auto test =
3334
[]<class CharT, class... Args>(
3435
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
3536
std::basic_string<CharT> out = std::format(std::locale(), fmt, std::forward<Args>(args)...);
3637
TEST_REQUIRE(
3738
out == expected,
38-
test_concat_message(
39+
TEST_WRITE_CONCATENATED(
3940
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n'));
4041
};
4142

libcxx/test/std/utilities/format/format.functions/format.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
#include "string_literal.h"
3131
#include "test_format_string.h"
3232
#include "assert_macros.h"
33+
#include "concat_macros.h"
3334

3435
auto test =
3536
[]<class CharT, class... Args>(
3637
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
3738
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
3839
TEST_REQUIRE(
3940
out == expected,
40-
test_concat_message(
41+
TEST_WRITE_CONCATENATED(
4142
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n'));
4243
};
4344

libcxx/test/std/utilities/format/format.functions/format_tests.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2673,7 +2673,6 @@ void format_tests(TestFunction check, ExceptionTest check_exception) {
26732673
check_exception("The format string contains an invalid escape sequence", SV("{:}-}"), 42);
26742674

26752675
check_exception("The format string contains an invalid escape sequence", SV("} "));
2676-
26772676
check_exception("The arg-id of the format-spec starts with an invalid character", SV("{-"), 42);
26782677
check_exception("Argument index out of bounds", SV("hello {}"));
26792678
check_exception("Argument index out of bounds", SV("hello {0}"));

0 commit comments

Comments
 (0)