| 1 |
============== |
| 2 |
Testing libc++ |
| 3 |
============== |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
:local: |
| 7 |
|
| 8 |
.. _testing: |
| 9 |
|
| 10 |
Getting Started |
| 11 |
=============== |
| 12 |
|
| 13 |
libc++ uses LIT to configure and run its tests. |
| 14 |
|
| 15 |
The primary way to run the libc++ tests is by using ``make check-cxx``. |
| 16 |
|
| 17 |
However since libc++ can be used in any number of possible |
| 18 |
configurations it is important to customize the way LIT builds and runs |
| 19 |
the tests. This guide provides information on how to use LIT directly to |
| 20 |
test libc++. |
| 21 |
|
| 22 |
Please see the `Lit Command Guide`_ for more information about LIT. |
| 23 |
|
| 24 |
.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html |
| 25 |
|
| 26 |
Usage |
| 27 |
----- |
| 28 |
|
| 29 |
After building libc++, you can run parts of the libc++ test suite by simply |
| 30 |
running ``llvm-lit`` on a specified test or directory. If you're unsure |
| 31 |
whether the required libraries have been built, you can use the |
| 32 |
``cxx-test-depends`` target. For example: |
| 33 |
|
| 34 |
.. code-block:: bash |
| 35 |
|
| 36 |
$ cd <monorepo-root> |
| 37 |
$ make -C <build> cxx-test-depends # If you want to make sure the targets get rebuilt |
| 38 |
$ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests |
| 39 |
$ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test |
| 40 |
$ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic |
| 41 |
|
| 42 |
If you used **ninja** as your build system, running ``ninja -C <build> check-cxx`` will run |
| 43 |
all the tests in the libc++ testsuite. |
| 44 |
|
| 45 |
.. note:: |
| 46 |
If you used the Bootstrapping build instead of the default runtimes build, the |
| 47 |
``cxx-test-depends`` target is instead named ``runtimes-test-depends``, and |
| 48 |
you will need to prefix ``<build>/runtimes/runtimes-<target>-bins/`` to the |
| 49 |
paths of all tests. For example, to run all the libcxx tests you can do |
| 50 |
``<build>/bin/llvm-lit -sv <build>/runtimes/runtimes-bins/libcxx/test``. |
| 51 |
|
| 52 |
In the default configuration, the tests are built against headers that form a |
| 53 |
fake installation root of libc++. This installation root has to be updated when |
| 54 |
changes are made to the headers, so you should re-run the ``cxx-test-depends`` |
| 55 |
target before running the tests manually with ``lit`` when you make any sort of |
| 56 |
change, including to the headers. We recommend using the provided ``libcxx/utils/libcxx-lit`` |
| 57 |
script to automate this so you don't have to think about building test dependencies |
| 58 |
every time: |
| 59 |
|
| 60 |
.. code-block:: bash |
| 61 |
|
| 62 |
$ cd <monorepo-root> |
| 63 |
$ libcxx/utils/libcxx-lit <build> -sv libcxx/test/std/re # Build testing dependencies and run all of the std::regex tests |
| 64 |
|
| 65 |
Sometimes you'll want to change the way LIT is running the tests. Custom options |
| 66 |
can be specified using the ``--param <name>=<val>`` flag. The most common option |
| 67 |
you'll want to change is the standard dialect (ie ``-std=c++XX``). By default the |
| 68 |
test suite will select the newest C++ dialect supported by the compiler and use |
| 69 |
that. However, you can manually specify the option like so if you want: |
| 70 |
|
| 71 |
.. code-block:: bash |
| 72 |
|
| 73 |
$ libcxx/utils/libcxx-lit <build> -sv libcxx/test/std/containers # Run the tests with the newest -std |
| 74 |
$ libcxx/utils/libcxx-lit <build> -sv libcxx/test/std/containers --param std=c++03 # Run the tests in C++03 |
| 75 |
|
| 76 |
Other parameters are supported by the test suite. Those are defined in ``libcxx/utils/libcxx/test/params.py``. |
| 77 |
If you want to customize how to run the libc++ test suite beyond what is available |
| 78 |
in ``params.py``, you most likely want to use a custom site configuration instead. |
| 79 |
|
| 80 |
The libc++ test suite works by loading a site configuration that defines various |
| 81 |
"base" parameters (via Lit substitutions). These base parameters represent things |
| 82 |
like the compiler to use for running the tests, which default compiler and linker |
| 83 |
flags to use, and how to run an executable. This system is meant to be easily |
| 84 |
extended for custom needs, in particular when porting the libc++ test suite to |
| 85 |
new platforms. |
| 86 |
|
| 87 |
.. note:: |
| 88 |
If you run the test suite on Apple platforms, we recommend adding the terminal application |
| 89 |
used to run the test suite to the list of "Developer Tools". This prevents the system from |
| 90 |
trying to scan each individual test binary for malware and dramatically speeds up the test |
| 91 |
suite. |
| 92 |
|
| 93 |
Using a Custom Site Configuration |
| 94 |
--------------------------------- |
| 95 |
|
| 96 |
By default, the libc++ test suite will use a site configuration that matches |
| 97 |
the current CMake configuration. It does so by generating a ``lit.site.cfg`` |
| 98 |
file in the build directory from one of the configuration file templates in |
| 99 |
``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around |
| 100 |
``llvm/utils/lit/lit.py``) to that file. So when you're running |
| 101 |
``<build>/bin/llvm-lit`` either directly or indirectly, the generated ``lit.site.cfg`` |
| 102 |
file is always loaded instead of ``libcxx/test/lit.cfg.py``. If you want to use a |
| 103 |
custom site configuration, simply point the CMake build to it using |
| 104 |
``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration |
| 105 |
will be used instead. That file can use CMake variables inside it to make |
| 106 |
configuration easier. |
| 107 |
|
| 108 |
.. code-block:: bash |
| 109 |
|
| 110 |
$ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config> |
| 111 |
$ libcxx/utils/libcxx-lit <build> -sv libcxx/test # will use your custom config file |
| 112 |
|
| 113 |
Additional tools |
| 114 |
---------------- |
| 115 |
|
| 116 |
The libc++ test suite uses a few optional tools to improve the code quality. |
| 117 |
|
| 118 |
These tools are: |
| 119 |
- clang-tidy (you might need additional dev packages to compile libc++-specific clang-tidy checks) |
| 120 |
|
| 121 |
Reproducing CI issues locally |
| 122 |
----------------------------- |
| 123 |
|
| 124 |
Libc++ has extensive CI that tests various configurations of the library. The testing for |
| 125 |
all these configurations is located in ``libcxx/utils/ci/run-buildbot``. Most of our |
| 126 |
CI jobs are being run on a Docker image for reproducibility. The definition of this Docker |
| 127 |
image is located in ``libcxx/utils/ci/Dockerfile``. If you are looking to reproduce the |
| 128 |
failure of a specific CI job locally, you should first drop into a Docker container that |
| 129 |
matches our CI images by running ``libcxx/utils/ci/run-buildbot-container``, and then run |
| 130 |
the specific CI job that you're interested in (from within the container) using the ``run-buildbot`` |
| 131 |
script above. If you want to control which compiler is used, you can set the ``CC`` and the |
| 132 |
``CXX`` environment variables before calling ``run-buildbot`` to select the right compiler. |
| 133 |
Take note that some CI jobs are testing the library on specific platforms and are *not* run |
| 134 |
in our Docker image. In the general case, it is not possible to reproduce these failures |
| 135 |
locally, unless they aren't specific to the platform. |
| 136 |
|
| 137 |
Also note that the Docker container shares the same filesystem as your local machine, so |
| 138 |
modifying files on your local machine will also modify what the Docker container sees. |
| 139 |
This is useful for editing source files as you're testing your code in the Docker container. |
| 140 |
|
| 141 |
Writing Tests |
| 142 |
============= |
| 143 |
|
| 144 |
When writing tests for the libc++ test suite, you should follow a few guidelines. |
| 145 |
This will ensure that your tests can run on a wide variety of hardware and under |
| 146 |
a wide variety of configurations. We have several unusual configurations such as |
| 147 |
building the tests on one host but running them on a different host, which add a |
| 148 |
few requirements to the test suite. Here's some stuff you should know: |
| 149 |
|
| 150 |
- All tests are run in a temporary directory that is unique to that test and |
| 151 |
cleaned up after the test is done. |
| 152 |
- When a test needs data files as inputs, these data files can be saved in the |
| 153 |
repository (when reasonable) and referenced by the test as |
| 154 |
``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or |
| 155 |
directories will be made available to the test in the temporary directory |
| 156 |
where it is run. |
| 157 |
- You should never hardcode a path from the build-host in a test, because that |
| 158 |
path will not necessarily be available on the host where the tests are run. |
| 159 |
- You should try to reduce the runtime dependencies of each test to the minimum. |
| 160 |
For example, requiring Python to run a test is bad, since Python is not |
| 161 |
necessarily available on all devices we may want to run the tests on (even |
| 162 |
though supporting Python is probably trivial for the build-host). |
| 163 |
|
| 164 |
Structure of the testing related directories |
| 165 |
-------------------------------------------- |
| 166 |
|
| 167 |
The tests of libc++ are stored in libc++'s testing related subdirectories: |
| 168 |
|
| 169 |
- ``libcxx/test/support`` This directory contains several helper headers with |
| 170 |
generic parts for the tests. The most important header is ``test_macros.h``. |
| 171 |
This file contains configuration information regarding the platform used. |
| 172 |
This is similar to the ``__config`` file in libc++'s ``include`` directory. |
| 173 |
Since libc++'s tests are used by other Standard libraries, tests should use |
| 174 |
the ``TEST_FOO`` macros instead of the ``_LIBCPP_FOO`` macros, which are |
| 175 |
specific to libc++. |
| 176 |
- ``libcxx/test/std`` This directory contains the tests that validate the library under |
| 177 |
test conforms to the C++ Standard. The paths and the names of the test match |
| 178 |
the section names in the C++ Standard. Note that the C++ Standard sometimes |
| 179 |
reorganises its structure, therefore some tests are at a location based on |
| 180 |
where they appeared historically in the standard. We try to strike a balance |
| 181 |
between keeping things at up-to-date locations and unnecessary churn. |
| 182 |
- ``libcxx/test/libcxx`` This directory contains the tests that validate libc++ |
| 183 |
specific behavior and implementation details. For example, libc++ has |
| 184 |
"wrapped iterators" that perform bounds checks. Since those are specific to |
| 185 |
libc++ and not mandated by the Standard, tests for those are located under |
| 186 |
``libcxx/test/libcxx``. The structure of this directories follows the |
| 187 |
structure of ``libcxx/test/std``. |
| 188 |
|
| 189 |
Structure of a test |
| 190 |
------------------- |
| 191 |
|
| 192 |
Some platforms where libc++ is tested have requirement on the signature of |
| 193 |
``main`` and require ``main`` to explicitly return a value. Therefore the |
| 194 |
typical ``main`` function should look like: |
| 195 |
|
| 196 |
.. code-block:: cpp |
| 197 |
|
| 198 |
int main(int, char**) { |
| 199 |
... |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
The C++ Standard has ``constexpr`` requirements. The typical way to test that, |
| 205 |
is to create a helper ``test`` function that returns a ``bool`` and use the |
| 206 |
following ``main`` function: |
| 207 |
|
| 208 |
.. code-block:: cpp |
| 209 |
|
| 210 |
constexpr bool test() { |
| 211 |
... |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
int main(int, char**) { |
| 216 |
test() |
| 217 |
static_assert(test()); |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
Tests in libc++ mainly use ``assert`` and ``static_assert`` for testing. There |
| 223 |
are a few helper macros and function that can be used to make it easier to |
| 224 |
write common tests. |
| 225 |
|
| 226 |
libcxx/test/support/assert_macros.h |
| 227 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 228 |
|
| 229 |
The header contains several macros with user specified log messages. This is |
| 230 |
useful when a normal assertion failure lacks the information to easily |
| 231 |
understand why the test has failed. This usually happens when the test is in a |
| 232 |
helper function. For example the ``std::format`` tests use a helper function |
| 233 |
for its validation. When the test fails it will give the line in the helper |
| 234 |
function with the condition ``out == expected`` failed. Without knowing what |
| 235 |
the value of ``format string``, ``out`` and ``expected`` are it is not easy to |
| 236 |
understand why the test has failed. By logging these three values the point of |
| 237 |
failure can be found without resorting to a debugger. |
| 238 |
|
| 239 |
Several of these macros are documented to take an ``ARG``. This ``ARG``: |
| 240 |
|
| 241 |
- if it is a ``const char*`` or ``std::string`` its contents are written to |
| 242 |
the ``stderr``, |
| 243 |
- otherwise it must be a callable that is invoked without any additional |
| 244 |
arguments and is expected to produce useful output to e.g. ``stderr``. |
| 245 |
|
| 246 |
This makes it possible to write additional information when a test fails, |
| 247 |
either by supplying a hard-coded string or generate it at runtime. |
| 248 |
|
| 249 |
TEST_FAIL(ARG) |
| 250 |
^^^^^^^^^^^^^^ |
| 251 |
|
| 252 |
This macro is an unconditional failure with a log message ``ARG``. The main |
| 253 |
use-case is to fail when code is reached that should be unreachable. |
| 254 |
|
| 255 |
|
| 256 |
TEST_REQUIRE(CONDITION, ARG) |
| 257 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 258 |
|
| 259 |
This macro requires its ``CONDITION`` to evaluate to ``true``. If that fails it |
| 260 |
will fail the test with a log message ``ARG``. |
| 261 |
|
| 262 |
|
| 263 |
TEST_LIBCPP_REQUIRE((CONDITION, ARG) |
| 264 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 265 |
|
| 266 |
If the library under test is libc++ it behaves like ``TEST_REQUIRE``, else it |
| 267 |
is a no-op. This makes it possible to test libc++ specific behaviour. For |
| 268 |
example testing whether the ``what()`` of an exception thrown matches libc++'s |
| 269 |
expectations. (Usually the Standard requires certain exceptions to be thrown, |
| 270 |
but not the contents of its ``what()`` message.) |
| 271 |
|
| 272 |
|
| 273 |
TEST_DOES_NOT_THROW(EXPR) |
| 274 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 275 |
|
| 276 |
Validates execution of ``EXPR`` does not throw an exception. |
| 277 |
|
| 278 |
TEST_THROWS_TYPE(TYPE, EXPR) |
| 279 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 280 |
|
| 281 |
Validates the execution of ``EXPR`` throws an exception of the type ``TYPE``. |
| 282 |
|
| 283 |
|
| 284 |
TEST_VALIDATE_EXCEPTION(TYPE, PRED, EXPR) |
| 285 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 286 |
|
| 287 |
Validates the execution of ``EXPR`` throws an exception of the type ``TYPE`` |
| 288 |
which passes validation of ``PRED``. Using this macro makes it easier to write |
| 289 |
tests using exceptions. The code to write a test manually would be: |
| 290 |
|
| 291 |
|
| 292 |
.. code-block:: cpp |
| 293 |
|
| 294 |
void test_excption([[maybe_unused]] int arg) { |
| 295 |
#ifndef TEST_HAS_NO_EXCEPTIONS // do nothing when tests are disabled |
| 296 |
try { |
| 297 |
foo(arg); |
| 298 |
assert(false); // validates foo really throws |
| 299 |
} catch ([[maybe_unused]] const bar& e) { |
| 300 |
LIBCPP_ASSERT(e.what() == what); |
| 301 |
return; |
| 302 |
} |
| 303 |
assert(false); // validates bar was thrown |
| 304 |
#endif |
| 305 |
} |
| 306 |
|
| 307 |
The same test using a macro: |
| 308 |
|
| 309 |
.. code-block:: cpp |
| 310 |
|
| 311 |
void test_excption([[maybe_unused]] int arg) { |
| 312 |
TEST_VALIDATE_EXCEPTION(bar, |
| 313 |
[](const bar& e) { |
| 314 |
LIBCPP_ASSERT(e.what() == what); |
| 315 |
}, |
| 316 |
foo(arg)); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
libcxx/test/support/concat_macros.h |
| 321 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 322 |
|
| 323 |
This file contains a helper macro ``TEST_WRITE_CONCATENATED`` to lazily |
| 324 |
concatenate its arguments to a ``std::string`` and write it to ``stderr``. When |
| 325 |
the output can't be concatenated a default message will be written to |
| 326 |
``stderr``. This is useful for tests where the arguments use different |
| 327 |
character types like ``char`` and ``wchar_t``, the latter can't simply be |
| 328 |
written to ``stderr``. |
| 329 |
|
| 330 |
This macro is in a different header as ``assert_macros.h`` since it pulls in |
| 331 |
additional headers. |
| 332 |
|
| 333 |
.. note: This macro can only be used in test using C++20 or newer. The macro |
| 334 |
was added at a time where most of libc++'s C++17 support was complete. |
| 335 |
Since it is not expected to add this to existing tests no effort was |
| 336 |
taken to make it work in earlier language versions. |
| 337 |
|
| 338 |
|
| 339 |
Test names |
| 340 |
---------- |
| 341 |
|
| 342 |
The names of test files have meaning for the libc++-specific configuration of |
| 343 |
Lit. Based on the pattern that matches the name of a test file, Lit will test |
| 344 |
the code contained therein in different ways. Refer to the `Lit Meaning of libc++ |
| 345 |
Test Filenames`_ when determining the names for new test files. |
| 346 |
|
| 347 |
.. _Lit Meaning of libc++ Test Filenames: |
| 348 |
.. list-table:: Lit Meaning of libc++ Test Filenames |
| 349 |
:widths: 25 75 |
| 350 |
:header-rows: 1 |
| 351 |
|
| 352 |
* - Name Pattern |
| 353 |
- Meaning |
| 354 |
* - ``FOO.pass.cpp`` |
| 355 |
- Checks whether the C++ code in the file compiles, links and runs successfully. |
| 356 |
* - ``FOO.pass.mm`` |
| 357 |
- Same as ``FOO.pass.cpp``, but for Objective-C++. |
| 358 |
|
| 359 |
* - ``FOO.compile.pass.cpp`` |
| 360 |
- Checks whether the C++ code in the file compiles successfully. In general, prefer ``compile`` tests over ``verify`` tests, |
| 361 |
subject to the specific recommendations, below, for when to write ``verify`` tests. |
| 362 |
* - ``FOO.compile.pass.mm`` |
| 363 |
- Same as ``FOO.compile.pass.cpp``, but for Objective-C++. |
| 364 |
* - ``FOO.compile.fail.cpp`` |
| 365 |
- Checks that the code in the file does *not* compile successfully. |
| 366 |
|
| 367 |
* - ``FOO.verify.cpp`` |
| 368 |
- Compiles with clang-verify. This type of test is automatically marked as UNSUPPORTED if the compiler does not support clang-verify. |
| 369 |
For additional information about how to write ``verify`` tests, see the `Internals Manual <https://clang.llvm.org/docs/InternalsManual.html#verifying-diagnostics>`_. |
| 370 |
Prefer `verify` tests over ``compile`` tests to test that compilation fails for a particular reason. For example, use a ``verify`` test |
| 371 |
to ensure that |
| 372 |
|
| 373 |
* an expected ``static_assert`` is triggered; |
| 374 |
* the use of deprecated functions generates the proper warning; |
| 375 |
* removed functions are no longer usable; or |
| 376 |
* return values from functions marked ``[[nodiscard]]`` are stored. |
| 377 |
|
| 378 |
* - ``FOO.link.pass.cpp`` |
| 379 |
- Checks that the C++ code in the file compiles and links successfully -- no run attempted. |
| 380 |
* - ``FOO.link.pass.mm`` |
| 381 |
- Same as ``FOO.link.pass.cpp``, but for Objective-C++. |
| 382 |
* - ``FOO.link.fail.cpp`` |
| 383 |
- Checks whether the C++ code in the file fails to link after successful compilation. |
| 384 |
* - ``FOO.link.fail.mm`` |
| 385 |
- Same as ``FOO.link.fail.cpp``, but for Objective-C++. |
| 386 |
|
| 387 |
* - ``FOO.sh.<anything>`` |
| 388 |
- A *builtin Lit Shell* test. |
| 389 |
* - ``FOO.gen.<anything>`` |
| 390 |
- A variant of a *Lit Shell* test that generates one or more Lit tests on the fly. Executing this test must generate one or more files as expected |
| 391 |
by LLVM split-file. Each generated file will drive an invocation of a separate Lit test. The format of the generated file will determine the type |
| 392 |
of Lit test to be executed. This can be used to generate multiple Lit tests from a single source file, which is useful for testing repetitive properties |
| 393 |
in the library. Be careful not to abuse this since this is not a replacement for usual code reuse techniques. |
| 394 |
|
| 395 |
|
| 396 |
libc++-Specific Lit Features |
| 397 |
---------------------------- |
| 398 |
|
| 399 |
Custom Directives |
| 400 |
~~~~~~~~~~~~~~~~~ |
| 401 |
|
| 402 |
Lit has many directives built in (e.g., ``DEFINE``, ``UNSUPPORTED``). In addition to those directives, libc++ adds two additional libc++-specific directives that makes |
| 403 |
writing tests easier. See `libc++-specific Lit Directives`_ for more information about the ``FILE_DEPENDENCIES``, ``ADDITIONAL_COMPILE_FLAGS``, and ``MODULE_DEPENDENCIES`` libc++-specific directives. |
| 404 |
|
| 405 |
.. _libc++-specific Lit Directives: |
| 406 |
.. list-table:: libc++-specific Lit Directives |
| 407 |
:widths: 20 35 45 |
| 408 |
:header-rows: 1 |
| 409 |
|
| 410 |
* - Directive |
| 411 |
- Parameters |
| 412 |
- Usage |
| 413 |
* - ``FILE_DEPENDENCIES`` |
| 414 |
- ``// FILE_DEPENDENCIES: file, directory, /path/to/file, ...`` |
| 415 |
- The paths given to the ``FILE_DEPENDENCIES`` directive can specify directories or specific files upon which a given test depend. For example, a test that requires some test |
| 416 |
input stored in a data file would use this libc++-specific Lit directive. When a test file contains the ``FILE_DEPENDENCIES`` directive, Lit will collect the named files and copy |
| 417 |
them to the directory represented by the ``%T`` substitution before the test executes. The copy is performed from the directory represented by the ``%S`` substitution |
| 418 |
(i.e. the source directory of the test being executed) which makes it possible to use relative paths to specify the location of dependency files. After Lit copies |
| 419 |
all the dependent files to the directory specified by the ``%T`` substitution, that directory should contain *all* the necessary inputs to run. In other words, |
| 420 |
it should be possible to copy the contents of the directory specified by the ``%T`` substitution to a remote host where the execution of the test will actually occur. |
| 421 |
* - ``ADDITIONAL_COMPILE_FLAGS`` |
| 422 |
- ``// ADDITIONAL_COMPILE_FLAGS: flag1 flag2 ...`` |
| 423 |
- The additional compiler flags specified by a space-separated list to the ``ADDITIONAL_COMPILE_FLAGS`` libc++-specific Lit directive will be added to the end of the ``%{compile_flags}`` |
| 424 |
substitution for the test that contains it. This libc++-specific Lit directive makes it possible to add special compilation flags without having to resort to writing a ``.sh.cpp`` test (see |
| 425 |
`Lit Meaning of libc++ Test Filenames`_), more powerful but perhaps overkill. |
| 426 |
* - ``MODULE_DEPENDENCIES`` |
| 427 |
- ``// MODULE_DEPENDENCIES: std std.compat`` |
| 428 |
- This directive will build the required C++23 standard library |
| 429 |
modules and add the additional compiler flags in |
| 430 |
%{compile_flags}. (Libc++ offers these modules in C++20 as an |
| 431 |
extension.) |
| 432 |
|
| 433 |
|
| 434 |
Benchmarks |
| 435 |
========== |
| 436 |
|
| 437 |
Libc++ contains benchmark tests separately from the test of the test suite. |
| 438 |
The benchmarks are written using the `Google Benchmark`_ library, a copy of which |
| 439 |
is stored in the libc++ repository. |
| 440 |
|
| 441 |
For more information about using the Google Benchmark library see the |
| 442 |
`official documentation <https://github.com/google/benchmark>`_. |
| 443 |
|
| 444 |
.. _`Google Benchmark`: https://github.com/google/benchmark |
| 445 |
|
| 446 |
Building Benchmarks |
| 447 |
------------------- |
| 448 |
|
| 449 |
The benchmark tests are not built by default. The benchmarks can be built using |
| 450 |
the ``cxx-benchmarks`` target. |
| 451 |
|
| 452 |
An example build would look like: |
| 453 |
|
| 454 |
.. code-block:: bash |
| 455 |
|
| 456 |
$ ninja -C build cxx-benchmarks |
| 457 |
|
| 458 |
This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be |
| 459 |
built against the just-built libc++. The compiled tests are output into |
| 460 |
``build/projects/libcxx/benchmarks``. |
| 461 |
|
| 462 |
Also See: |
| 463 |
|
| 464 |
* :ref:`Building Libc++ <build instructions>` |
| 465 |
* :ref:`CMake Options` |
| 466 |
|
| 467 |
Running Benchmarks |
| 468 |
------------------ |
| 469 |
|
| 470 |
The benchmarks must be run manually by the user. Currently there is no way |
| 471 |
to run them as part of the build. |
| 472 |
|
| 473 |
For example: |
| 474 |
|
| 475 |
.. code-block:: bash |
| 476 |
|
| 477 |
$ cd build/projects/libcxx/benchmarks |
| 478 |
$ ./algorithms.bench.out # Runs all the benchmarks |
| 479 |
$ ./algorithms.bench.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks |
| 480 |
|
| 481 |
For more information about running benchmarks see `Google Benchmark`_. |
| 482 |
|
| 483 |
|
| 484 |
.. _testing-hardening-assertions: |
| 485 |
|
| 486 |
Testing hardening assertions |
| 487 |
============================ |
| 488 |
|
| 489 |
Each hardening assertion should be tested using death tests (via the |
| 490 |
``TEST_LIBCPP_ASSERT_FAILURE`` macro). Use the ``libcpp-hardening-mode`` Lit |
| 491 |
feature to make sure the assertion is enabled in (and only in) the intended |
| 492 |
modes. The convention is to use `assert.` in the name of the test file to make |
| 493 |
it easier to identify as a hardening test, e.g. ``assert.my_func.pass.cpp``. |
| 494 |
A toy example: |
| 495 |
|
| 496 |
.. code-block:: cpp |
| 497 |
|
| 498 |
// Note: the following three annotations are currently needed to use the |
| 499 |
// `TEST_LIBCPP_ASSERT_FAILURE`. |
| 500 |
// REQUIRES: has-unix-headers |
| 501 |
// UNSUPPORTED: c++03 |
| 502 |
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing |
| 503 |
|
| 504 |
// Example: only run this test in `fast`/`extensive`/`debug` modes. |
| 505 |
// UNSUPPORTED: libcpp-hardening-mode=none |
| 506 |
// Example: only run this test in the `debug` mode. |
| 507 |
// REQUIRES: libcpp-hardening-mode=debug |
| 508 |
// Example: only run this test in `extensive`/`debug` modes. |
| 509 |
// REQUIRES: libcpp-hardening-mode={{extensive|debug}} |
| 510 |
|
| 511 |
#include <header_being_tested> |
| 512 |
|
| 513 |
#include "check_assertion.h" // Contains the `TEST_LIBCPP_ASSERT_FAILURE` macro |
| 514 |
|
| 515 |
int main(int, char**) { |
| 516 |
std::type_being_tested foo; |
| 517 |
int bad_input = -1; |
| 518 |
TEST_LIBCPP_ASSERT_FAILURE(foo.some_function_that_asserts(bad_input), |
| 519 |
"The expected assertion message"); |
| 520 |
|
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
Note that error messages are only tested (matched) if the ``debug`` |
| 525 |
hardening mode is used. |
| 526 |
|