1============== 2Testing libc++ 3============== 4 5.. contents:: 6 :local: 7 8.. _testing: 9 10Getting Started 11=============== 12 13libc++ uses LIT to configure and run its tests. 14 15The primary way to run the libc++ tests is by using ``make check-cxx``. 16 17However since libc++ can be used in any number of possible 18configurations it is important to customize the way LIT builds and runs 19the tests. This guide provides information on how to use LIT directly to 20test libc++. 21 22Please see the `Lit Command Guide`_ for more information about LIT. 23 24.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html 25 26Usage 27----- 28 29After building libc++, you can run parts of the libc++ test suite by simply 30running ``llvm-lit`` on a specified test or directory. If you're unsure 31whether 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 42If you used **ninja** as your build system, running ``ninja -C <build> check-cxx`` will run 43all 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 52In the default configuration, the tests are built against headers that form a 53fake installation root of libc++. This installation root has to be updated when 54changes are made to the headers, so you should re-run the ``cxx-test-depends`` 55target before running the tests manually with ``lit`` when you make any sort of 56change, including to the headers. We recommend using the provided ``libcxx/utils/libcxx-lit`` 57script to automate this so you don't have to think about building test dependencies 58every 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 65Sometimes you'll want to change the way LIT is running the tests. Custom options 66can be specified using the ``--param <name>=<val>`` flag. The most common option 67you'll want to change is the standard dialect (ie ``-std=c++XX``). By default the 68test suite will select the newest C++ dialect supported by the compiler and use 69that. 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 76Other parameters are supported by the test suite. Those are defined in ``libcxx/utils/libcxx/test/params.py``. 77If you want to customize how to run the libc++ test suite beyond what is available 78in ``params.py``, you most likely want to use a custom site configuration instead. 79 80The libc++ test suite works by loading a site configuration that defines various 81"base" parameters (via Lit substitutions). These base parameters represent things 82like the compiler to use for running the tests, which default compiler and linker 83flags to use, and how to run an executable. This system is meant to be easily 84extended for custom needs, in particular when porting the libc++ test suite to 85new platforms. 86 87Using a Custom Site Configuration 88--------------------------------- 89 90By default, the libc++ test suite will use a site configuration that matches 91the current CMake configuration. It does so by generating a ``lit.site.cfg`` 92file in the build directory from one of the configuration file templates in 93``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around 94``llvm/utils/lit/lit.py``) to that file. So when you're running 95``<build>/bin/llvm-lit`` either directly or indirectly, the generated ``lit.site.cfg`` 96file is always loaded instead of ``libcxx/test/lit.cfg.py``. If you want to use a 97custom site configuration, simply point the CMake build to it using 98``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration 99will be used instead. That file can use CMake variables inside it to make 100configuration easier. 101 102 .. code-block:: bash 103 104 $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config> 105 $ libcxx/utils/libcxx-lit <build> -sv libcxx/test # will use your custom config file 106 107Additional tools 108---------------- 109 110The libc++ test suite uses a few optional tools to improve the code quality. 111 112These tools are: 113- clang-tidy (you might need additional dev packages to compile libc++-specific clang-tidy checks) 114 115Reproducing CI issues locally 116----------------------------- 117 118Libc++ has extensive CI that tests various configurations of the library. The testing for 119all these configurations is located in ``libcxx/utils/ci/run-buildbot``. Most of our 120CI jobs are being run on a Docker image for reproducibility. The definition of this Docker 121image is located in ``libcxx/utils/ci/Dockerfile``. If you are looking to reproduce the 122failure of a specific CI job locally, you should first drop into a Docker container that 123matches our CI images by running ``libcxx/utils/ci/run-buildbot-container``, and then run 124the specific CI job that you're interested in (from within the container) using the ``run-buildbot`` 125script above. If you want to control which compiler is used, you can set the ``CC`` and the 126``CXX`` environment variables before calling ``run-buildbot`` to select the right compiler. 127Take note that some CI jobs are testing the library on specific platforms and are *not* run 128in our Docker image. In the general case, it is not possible to reproduce these failures 129locally, unless they aren't specific to the platform. 130 131Also note that the Docker container shares the same filesystem as your local machine, so 132modifying files on your local machine will also modify what the Docker container sees. 133This is useful for editing source files as you're testing your code in the Docker container. 134 135Writing Tests 136============= 137 138When writing tests for the libc++ test suite, you should follow a few guidelines. 139This will ensure that your tests can run on a wide variety of hardware and under 140a wide variety of configurations. We have several unusual configurations such as 141building the tests on one host but running them on a different host, which add a 142few requirements to the test suite. Here's some stuff you should know: 143 144- All tests are run in a temporary directory that is unique to that test and 145 cleaned up after the test is done. 146- When a test needs data files as inputs, these data files can be saved in the 147 repository (when reasonable) and referenced by the test as 148 ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or 149 directories will be made available to the test in the temporary directory 150 where it is run. 151- You should never hardcode a path from the build-host in a test, because that 152 path will not necessarily be available on the host where the tests are run. 153- You should try to reduce the runtime dependencies of each test to the minimum. 154 For example, requiring Python to run a test is bad, since Python is not 155 necessarily available on all devices we may want to run the tests on (even 156 though supporting Python is probably trivial for the build-host). 157 158Structure of the testing related directories 159-------------------------------------------- 160 161The tests of libc++ are stored in libc++'s testing related subdirectories: 162 163- ``libcxx/test/support`` This directory contains several helper headers with 164 generic parts for the tests. The most important header is ``test_macros.h``. 165 This file contains configuration information regarding the platform used. 166 This is similar to the ``__config`` file in libc++'s ``include`` directory. 167 Since libc++'s tests are used by other Standard libraries, tests should use 168 the ``TEST_FOO`` macros instead of the ``_LIBCPP_FOO`` macros, which are 169 specific to libc++. 170- ``libcxx/test/std`` This directory contains the tests that validate the library under 171 test conforms to the C++ Standard. The paths and the names of the test match 172 the section names in the C++ Standard. Note that the C++ Standard sometimes 173 reorganises its structure, therefore some tests are at a location based on 174 where they appeared historically in the standard. We try to strike a balance 175 between keeping things at up-to-date locations and unnecessary churn. 176- ``libcxx/test/libcxx`` This directory contains the tests that validate libc++ 177 specific behavior and implementation details. For example, libc++ has 178 "wrapped iterators" that perform bounds checks. Since those are specific to 179 libc++ and not mandated by the Standard, tests for those are located under 180 ``libcxx/test/libcxx``. The structure of this directories follows the 181 structure of ``libcxx/test/std``. 182 183Structure of a test 184------------------- 185 186Some platforms where libc++ is tested have requirement on the signature of 187``main`` and require ``main`` to explicitly return a value. Therefore the 188typical ``main`` function should look like: 189 190.. code-block:: cpp 191 192 int main(int, char**) { 193 ... 194 return 0; 195 } 196 197 198The C++ Standard has ``constexpr`` requirements. The typical way to test that, 199is to create a helper ``test`` function that returns a ``bool`` and use the 200following ``main`` function: 201 202.. code-block:: cpp 203 204 constexpr bool test() { 205 ... 206 return true; 207 } 208 209 int main(int, char**) { 210 test() 211 static_assert(test()); 212 213 return 0; 214 } 215 216Tests in libc++ mainly use ``assert`` and ``static_assert`` for testing. There 217are a few helper macros and function that can be used to make it easier to 218write common tests. 219 220libcxx/test/support/assert_macros.h 221~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 222 223The header contains several macros with user specified log messages. This is 224useful when a normal assertion failure lacks the information to easily 225understand why the test has failed. This usually happens when the test is in a 226helper function. For example the ``std::format`` tests use a helper function 227for its validation. When the test fails it will give the line in the helper 228function with the condition ``out == expected`` failed. Without knowing what 229the value of ``format string``, ``out`` and ``expected`` are it is not easy to 230understand why the test has failed. By logging these three values the point of 231failure can be found without resorting to a debugger. 232 233Several of these macros are documented to take an ``ARG``. This ``ARG``: 234 235 - if it is a ``const char*`` or ``std::string`` its contents are written to 236 the ``stderr``, 237 - otherwise it must be a callable that is invoked without any additional 238 arguments and is expected to produce useful output to e.g. ``stderr``. 239 240This makes it possible to write additional information when a test fails, 241either by supplying a hard-coded string or generate it at runtime. 242 243TEST_FAIL(ARG) 244^^^^^^^^^^^^^^ 245 246This macro is an unconditional failure with a log message ``ARG``. The main 247use-case is to fail when code is reached that should be unreachable. 248 249 250TEST_REQUIRE(CONDITION, ARG) 251^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 252 253This macro requires its ``CONDITION`` to evaluate to ``true``. If that fails it 254will fail the test with a log message ``ARG``. 255 256 257TEST_LIBCPP_REQUIRE((CONDITION, ARG) 258^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 259 260If the library under test is libc++ it behaves like ``TEST_REQUIRE``, else it 261is a no-op. This makes it possible to test libc++ specific behaviour. For 262example testing whether the ``what()`` of an exception thrown matches libc++'s 263expectations. (Usually the Standard requires certain exceptions to be thrown, 264but not the contents of its ``what()`` message.) 265 266 267TEST_DOES_NOT_THROW(EXPR) 268^^^^^^^^^^^^^^^^^^^^^^^^^ 269 270Validates execution of ``EXPR`` does not throw an exception. 271 272TEST_THROWS_TYPE(TYPE, EXPR) 273^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 274 275Validates the execution of ``EXPR`` throws an exception of the type ``TYPE``. 276 277 278TEST_VALIDATE_EXCEPTION(TYPE, PRED, EXPR) 279^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 280 281Validates the execution of ``EXPR`` throws an exception of the type ``TYPE`` 282which passes validation of ``PRED``. Using this macro makes it easier to write 283tests using exceptions. The code to write a test manually would be: 284 285 286.. code-block:: cpp 287 288 void test_excption([[maybe_unused]] int arg) { 289 #ifndef TEST_HAS_NO_EXCEPTIONS // do nothing when tests are disabled 290 try { 291 foo(arg); 292 assert(false); // validates foo really throws 293 } catch ([[maybe_unused]] const bar& e) { 294 LIBCPP_ASSERT(e.what() == what); 295 return; 296 } 297 assert(false); // validates bar was thrown 298 #endif 299 } 300 301The same test using a macro: 302 303.. code-block:: cpp 304 305 void test_excption([[maybe_unused]] int arg) { 306 TEST_VALIDATE_EXCEPTION(bar, 307 [](const bar& e) { 308 LIBCPP_ASSERT(e.what() == what); 309 }, 310 foo(arg)); 311 } 312 313 314libcxx/test/support/concat_macros.h 315~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 316 317This file contains a helper macro ``TEST_WRITE_CONCATENATED`` to lazily 318concatenate its arguments to a ``std::string`` and write it to ``stderr``. When 319the output can't be concatenated a default message will be written to 320``stderr``. This is useful for tests where the arguments use different 321character types like ``char`` and ``wchar_t``, the latter can't simply be 322written to ``stderr``. 323 324This macro is in a different header as ``assert_macros.h`` since it pulls in 325additional headers. 326 327 .. note: This macro can only be used in test using C++20 or newer. The macro 328 was added at a time where most of lib++'s C++17 support was complete. 329 Since it is not expected to add this to existing tests no effort was 330 taken to make it work in earlier language versions. 331 332 333Additional reading 334------------------ 335 336The function ``CxxStandardLibraryTest`` in the file 337``libcxx/utils/libcxx/test/format.py`` has documentation about writing test. It 338explains the difference between the test named ``foo.pass.cpp`` and named 339``foo.verify.cpp`` are. 340 341Benchmarks 342========== 343 344Libc++ contains benchmark tests separately from the test of the test suite. 345The benchmarks are written using the `Google Benchmark`_ library, a copy of which 346is stored in the libc++ repository. 347 348For more information about using the Google Benchmark library see the 349`official documentation <https://github.com/google/benchmark>`_. 350 351.. _`Google Benchmark`: https://github.com/google/benchmark 352 353Building Benchmarks 354------------------- 355 356The benchmark tests are not built by default. The benchmarks can be built using 357the ``cxx-benchmarks`` target. 358 359An example build would look like: 360 361.. code-block:: bash 362 363 $ cd build 364 $ ninja cxx-benchmarks 365 366This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be 367built against the just-built libc++. The compiled tests are output into 368``build/projects/libcxx/benchmarks``. 369 370The benchmarks can also be built against the platforms native standard library 371using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This 372is useful for comparing the performance of libc++ to other standard libraries. 373The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and 374``<test>.native.out`` otherwise. 375 376Also See: 377 378 * :ref:`Building Libc++ <build instructions>` 379 * :ref:`CMake Options` 380 381Running Benchmarks 382------------------ 383 384The benchmarks must be run manually by the user. Currently there is no way 385to run them as part of the build. 386 387For example: 388 389.. code-block:: bash 390 391 $ cd build/projects/libcxx/benchmarks 392 $ ./algorithms.libcxx.out # Runs all the benchmarks 393 $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks 394 395For more information about running benchmarks see `Google Benchmark`_. 396