1*c303c47eSjoerg============== 2*c303c47eSjoergTesting libc++ 3*c303c47eSjoerg============== 4*c303c47eSjoerg 5*c303c47eSjoerg.. contents:: 6*c303c47eSjoerg :local: 7*c303c47eSjoerg 8*c303c47eSjoergGetting Started 9*c303c47eSjoerg=============== 10*c303c47eSjoerg 11*c303c47eSjoerglibc++ uses LIT to configure and run its tests. 12*c303c47eSjoerg 13*c303c47eSjoergThe primary way to run the libc++ tests is by using ``make check-cxx``. 14*c303c47eSjoerg 15*c303c47eSjoergHowever since libc++ can be used in any number of possible 16*c303c47eSjoergconfigurations it is important to customize the way LIT builds and runs 17*c303c47eSjoergthe tests. This guide provides information on how to use LIT directly to 18*c303c47eSjoergtest libc++. 19*c303c47eSjoerg 20*c303c47eSjoergPlease see the `Lit Command Guide`_ for more information about LIT. 21*c303c47eSjoerg 22*c303c47eSjoerg.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html 23*c303c47eSjoerg 24*c303c47eSjoergUsage 25*c303c47eSjoerg----- 26*c303c47eSjoerg 27*c303c47eSjoergAfter building libc++, you can run parts of the libc++ test suite by simply 28*c303c47eSjoergrunning ``llvm-lit`` on a specified test or directory. If you're unsure 29*c303c47eSjoergwhether the required libraries have been built, you can use the 30*c303c47eSjoerg`cxx-test-depends` target. For example: 31*c303c47eSjoerg 32*c303c47eSjoerg.. code-block:: bash 33*c303c47eSjoerg 34*c303c47eSjoerg $ cd <monorepo-root> 35*c303c47eSjoerg $ make -C <build> cxx-test-depends # If you want to make sure the targets get rebuilt 36*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests 37*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test 38*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic 39*c303c47eSjoerg 40*c303c47eSjoergIn the default configuration, the tests are built against headers that form a 41*c303c47eSjoergfake installation root of libc++. This installation root has to be updated when 42*c303c47eSjoergchanges are made to the headers, so you should re-run the `cxx-test-depends` 43*c303c47eSjoergtarget before running the tests manually with `lit` when you make any sort of 44*c303c47eSjoergchange, including to the headers. 45*c303c47eSjoerg 46*c303c47eSjoergSometimes you'll want to change the way LIT is running the tests. Custom options 47*c303c47eSjoergcan be specified using the `--param=<name>=<val>` flag. The most common option 48*c303c47eSjoergyou'll want to change is the standard dialect (ie -std=c++XX). By default the 49*c303c47eSjoergtest suite will select the newest C++ dialect supported by the compiler and use 50*c303c47eSjoergthat. However if you want to manually specify the option like so: 51*c303c47eSjoerg 52*c303c47eSjoerg.. code-block:: bash 53*c303c47eSjoerg 54*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std 55*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param=std=c++03 # Run the tests in C++03 56*c303c47eSjoerg 57*c303c47eSjoergOccasionally you'll want to add extra compile or link flags when testing. 58*c303c47eSjoergYou can do this as follows: 59*c303c47eSjoerg 60*c303c47eSjoerg.. code-block:: bash 61*c303c47eSjoerg 62*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test --param=compile_flags='-Wcustom-warning' 63*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test --param=link_flags='-L/custom/library/path' 64*c303c47eSjoerg 65*c303c47eSjoergSome other common examples include: 66*c303c47eSjoerg 67*c303c47eSjoerg.. code-block:: bash 68*c303c47eSjoerg 69*c303c47eSjoerg # Specify a custom compiler. 70*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test/std --param=cxx_under_test=/opt/bin/g++ 71*c303c47eSjoerg 72*c303c47eSjoerg # Disable warnings in the test suite 73*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test --param=enable_warnings=False 74*c303c47eSjoerg 75*c303c47eSjoerg # Use UBSAN when running the tests. 76*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test --param=use_sanitizer=Undefined 77*c303c47eSjoerg 78*c303c47eSjoergUsing a custom site configuration 79*c303c47eSjoerg--------------------------------- 80*c303c47eSjoerg 81*c303c47eSjoergBy default, the libc++ test suite will use a site configuration that matches 82*c303c47eSjoergthe current CMake configuration. It does so by generating a ``lit.site.cfg`` 83*c303c47eSjoergfile in the build directory from one of the configuration file templates in 84*c303c47eSjoerg``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around 85*c303c47eSjoerg``llvm/utils/lit/lit.py``) to that file. So when you're running 86*c303c47eSjoerg``<build>/bin/llvm-lit``, the generated ``lit.site.cfg`` file is always loaded 87*c303c47eSjoerginstead of ``libcxx/test/lit.cfg.py``. If you want to use a custom site 88*c303c47eSjoergconfiguration, simply point the CMake build to it using 89*c303c47eSjoerg``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration 90*c303c47eSjoergwill be used instead. That file can use CMake variables inside it to make 91*c303c47eSjoergconfiguration easier. 92*c303c47eSjoerg 93*c303c47eSjoerg .. code-block:: bash 94*c303c47eSjoerg 95*c303c47eSjoerg $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config> 96*c303c47eSjoerg $ make -C <build> cxx-test-depends 97*c303c47eSjoerg $ <build>/bin/llvm-lit -sv libcxx/test # will use your custom config file 98*c303c47eSjoerg 99*c303c47eSjoerg 100*c303c47eSjoergLIT Options 101*c303c47eSjoerg=========== 102*c303c47eSjoerg 103*c303c47eSjoerg:program:`lit` [*options*...] [*filenames*...] 104*c303c47eSjoerg 105*c303c47eSjoergCommand Line Options 106*c303c47eSjoerg-------------------- 107*c303c47eSjoerg 108*c303c47eSjoergTo use these options you pass them on the LIT command line as ``--param NAME`` 109*c303c47eSjoergor ``--param NAME=VALUE``. Some options have default values specified during 110*c303c47eSjoergCMake's configuration. Passing the option on the command line will override the 111*c303c47eSjoergdefault. 112*c303c47eSjoerg 113*c303c47eSjoerg.. program:: lit 114*c303c47eSjoerg 115*c303c47eSjoerg.. option:: cxx_under_test=<path/to/compiler> 116*c303c47eSjoerg 117*c303c47eSjoerg Specify the compiler used to build the tests. 118*c303c47eSjoerg 119*c303c47eSjoerg.. option:: stdlib=<stdlib name> 120*c303c47eSjoerg 121*c303c47eSjoerg **Values**: libc++, libstdc++, msvc 122*c303c47eSjoerg 123*c303c47eSjoerg Specify the C++ standard library being tested. The default is libc++ if this 124*c303c47eSjoerg option is not provided. This option is intended to allow running the libc++ 125*c303c47eSjoerg test suite against other standard library implementations. 126*c303c47eSjoerg 127*c303c47eSjoerg.. option:: std=<standard version> 128*c303c47eSjoerg 129*c303c47eSjoerg **Values**: c++03, c++11, c++14, c++17, c++20, c++2b 130*c303c47eSjoerg 131*c303c47eSjoerg Change the standard version used when building the tests. 132*c303c47eSjoerg 133*c303c47eSjoerg.. option:: cxx_headers=<path/to/headers> 134*c303c47eSjoerg 135*c303c47eSjoerg Specify the c++ standard library headers that are tested. By default the 136*c303c47eSjoerg headers in the source tree are used. 137*c303c47eSjoerg 138*c303c47eSjoerg.. option:: cxx_library_root=<path/to/lib/> 139*c303c47eSjoerg 140*c303c47eSjoerg Specify the directory of the libc++ library to be tested. By default the 141*c303c47eSjoerg library folder of the build directory is used. 142*c303c47eSjoerg 143*c303c47eSjoerg 144*c303c47eSjoerg.. option:: cxx_runtime_root=<path/to/lib/> 145*c303c47eSjoerg 146*c303c47eSjoerg Specify the directory of the libc++ library to use at runtime. This directory 147*c303c47eSjoerg is not added to the linkers search path. This can be used to compile tests 148*c303c47eSjoerg against one version of libc++ and run them using another. The default value 149*c303c47eSjoerg for this option is `cxx_library_root`. 150*c303c47eSjoerg 151*c303c47eSjoerg.. option:: use_system_cxx_lib=<bool> 152*c303c47eSjoerg 153*c303c47eSjoerg **Default**: False 154*c303c47eSjoerg 155*c303c47eSjoerg Enable or disable testing against the installed version of libc++ library. 156*c303c47eSjoerg This impacts whether the ``use_system_cxx_lib`` Lit feature is defined or 157*c303c47eSjoerg not. The ``cxx_library_root`` and ``cxx_runtime_root`` parameters should 158*c303c47eSjoerg still be used to specify the path of the library to link to and run against, 159*c303c47eSjoerg respectively. 160*c303c47eSjoerg 161*c303c47eSjoerg.. option:: debug_level=<level> 162*c303c47eSjoerg 163*c303c47eSjoerg **Values**: 0, 1 164*c303c47eSjoerg 165*c303c47eSjoerg Enable the use of debug mode. Level 0 enables assertions and level 1 enables 166*c303c47eSjoerg assertions and debugging of iterator misuse. 167*c303c47eSjoerg 168*c303c47eSjoerg.. option:: use_sanitizer=<sanitizer name> 169*c303c47eSjoerg 170*c303c47eSjoerg **Values**: Memory, MemoryWithOrigins, Address, Undefined 171*c303c47eSjoerg 172*c303c47eSjoerg Run the tests using the given sanitizer. If LLVM_USE_SANITIZER was given when 173*c303c47eSjoerg building libc++ then that sanitizer will be used by default. 174*c303c47eSjoerg 175*c303c47eSjoerg.. option:: llvm_unwinder 176*c303c47eSjoerg 177*c303c47eSjoerg Enable the use of LLVM unwinder instead of libgcc. 178*c303c47eSjoerg 179*c303c47eSjoerg.. option:: builtins_library 180*c303c47eSjoerg 181*c303c47eSjoerg Path to the builtins library to use instead of libgcc. 182*c303c47eSjoerg 183*c303c47eSjoerg 184*c303c47eSjoergWriting Tests 185*c303c47eSjoerg------------- 186*c303c47eSjoerg 187*c303c47eSjoergWhen writing tests for the libc++ test suite, you should follow a few guidelines. 188*c303c47eSjoergThis will ensure that your tests can run on a wide variety of hardware and under 189*c303c47eSjoerga wide variety of configurations. We have several unusual configurations such as 190*c303c47eSjoergbuilding the tests on one host but running them on a different host, which add a 191*c303c47eSjoergfew requirements to the test suite. Here's some stuff you should know: 192*c303c47eSjoerg 193*c303c47eSjoerg- All tests are run in a temporary directory that is unique to that test and 194*c303c47eSjoerg cleaned up after the test is done. 195*c303c47eSjoerg- When a test needs data files as inputs, these data files can be saved in the 196*c303c47eSjoerg repository (when reasonable) and referenced by the test as 197*c303c47eSjoerg ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or 198*c303c47eSjoerg directories will be made available to the test in the temporary directory 199*c303c47eSjoerg where it is run. 200*c303c47eSjoerg- You should never hardcode a path from the build-host in a test, because that 201*c303c47eSjoerg path will not necessarily be available on the host where the tests are run. 202*c303c47eSjoerg- You should try to reduce the runtime dependencies of each test to the minimum. 203*c303c47eSjoerg For example, requiring Python to run a test is bad, since Python is not 204*c303c47eSjoerg necessarily available on all devices we may want to run the tests on (even 205*c303c47eSjoerg though supporting Python is probably trivial for the build-host). 206*c303c47eSjoerg 207*c303c47eSjoergBenchmarks 208*c303c47eSjoerg========== 209*c303c47eSjoerg 210*c303c47eSjoergLibc++ contains benchmark tests separately from the test of the test suite. 211*c303c47eSjoergThe benchmarks are written using the `Google Benchmark`_ library, a copy of which 212*c303c47eSjoergis stored in the libc++ repository. 213*c303c47eSjoerg 214*c303c47eSjoergFor more information about using the Google Benchmark library see the 215*c303c47eSjoerg`official documentation <https://github.com/google/benchmark>`_. 216*c303c47eSjoerg 217*c303c47eSjoerg.. _`Google Benchmark`: https://github.com/google/benchmark 218*c303c47eSjoerg 219*c303c47eSjoergBuilding Benchmarks 220*c303c47eSjoerg------------------- 221*c303c47eSjoerg 222*c303c47eSjoergThe benchmark tests are not built by default. The benchmarks can be built using 223*c303c47eSjoergthe ``cxx-benchmarks`` target. 224*c303c47eSjoerg 225*c303c47eSjoergAn example build would look like: 226*c303c47eSjoerg 227*c303c47eSjoerg.. code-block:: bash 228*c303c47eSjoerg 229*c303c47eSjoerg $ cd build 230*c303c47eSjoerg $ cmake [options] <path to libcxx sources> 231*c303c47eSjoerg $ make cxx-benchmarks 232*c303c47eSjoerg 233*c303c47eSjoergThis will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be 234*c303c47eSjoergbuilt against the just-built libc++. The compiled tests are output into 235*c303c47eSjoerg``build/benchmarks``. 236*c303c47eSjoerg 237*c303c47eSjoergThe benchmarks can also be built against the platforms native standard library 238*c303c47eSjoergusing the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This 239*c303c47eSjoergis useful for comparing the performance of libc++ to other standard libraries. 240*c303c47eSjoergThe compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and 241*c303c47eSjoerg``<test>.native.out`` otherwise. 242*c303c47eSjoerg 243*c303c47eSjoergAlso See: 244*c303c47eSjoerg 245*c303c47eSjoerg * :ref:`Building Libc++ <build instructions>` 246*c303c47eSjoerg * :ref:`CMake Options` 247*c303c47eSjoerg 248*c303c47eSjoergRunning Benchmarks 249*c303c47eSjoerg------------------ 250*c303c47eSjoerg 251*c303c47eSjoergThe benchmarks must be run manually by the user. Currently there is no way 252*c303c47eSjoergto run them as part of the build. 253*c303c47eSjoerg 254*c303c47eSjoergFor example: 255*c303c47eSjoerg 256*c303c47eSjoerg.. code-block:: bash 257*c303c47eSjoerg 258*c303c47eSjoerg $ cd build/benchmarks 259*c303c47eSjoerg $ make cxx-benchmarks 260*c303c47eSjoerg $ ./algorithms.libcxx.out # Runs all the benchmarks 261*c303c47eSjoerg $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks 262*c303c47eSjoerg 263*c303c47eSjoergFor more information about running benchmarks see `Google Benchmark`_. 264