146035553Spatrick============== 246035553SpatrickTesting libc++ 346035553Spatrick============== 446035553Spatrick 546035553Spatrick.. contents:: 646035553Spatrick :local: 746035553Spatrick 846035553SpatrickGetting Started 946035553Spatrick=============== 1046035553Spatrick 1146035553Spatricklibc++ uses LIT to configure and run its tests. 1246035553Spatrick 13037e7968SpatrickThe primary way to run the libc++ tests is by using ``make check-cxx``. 1446035553Spatrick 1546035553SpatrickHowever since libc++ can be used in any number of possible 1646035553Spatrickconfigurations it is important to customize the way LIT builds and runs 1746035553Spatrickthe tests. This guide provides information on how to use LIT directly to 1846035553Spatricktest libc++. 1946035553Spatrick 2046035553SpatrickPlease see the `Lit Command Guide`_ for more information about LIT. 2146035553Spatrick 22037e7968Spatrick.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html 2346035553Spatrick 24037e7968SpatrickUsage 25037e7968Spatrick----- 2646035553Spatrick 27037e7968SpatrickAfter building libc++, you can run parts of the libc++ test suite by simply 28037e7968Spatrickrunning ``llvm-lit`` on a specified test or directory. If you're unsure 29037e7968Spatrickwhether the required libraries have been built, you can use the 30*4bdff4beSrobert``cxx-test-depends`` target. For example: 3146035553Spatrick 3246035553Spatrick.. code-block:: bash 3346035553Spatrick 34037e7968Spatrick $ cd <monorepo-root> 3576d0caaeSpatrick $ make -C <build> cxx-test-depends # If you want to make sure the targets get rebuilt 36037e7968Spatrick $ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests 37037e7968Spatrick $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test 38037e7968Spatrick $ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic 3946035553Spatrick 40*4bdff4beSrobert.. note:: 41*4bdff4beSrobert If you used the Bootstrapping build instead of the default runtimes build, the 42*4bdff4beSrobert ``cxx-test-depends`` target is instead named ``runtimes-test-depends``, and 43*4bdff4beSrobert you will need to prefix ``<build>/runtimes/runtimes-<target>-bins/`` to the 44*4bdff4beSrobert paths of all tests. 45*4bdff4beSrobert 4676d0caaeSpatrickIn the default configuration, the tests are built against headers that form a 4776d0caaeSpatrickfake installation root of libc++. This installation root has to be updated when 48*4bdff4beSrobertchanges are made to the headers, so you should re-run the ``cxx-test-depends`` 49*4bdff4beSroberttarget before running the tests manually with ``lit`` when you make any sort of 5076d0caaeSpatrickchange, including to the headers. 5176d0caaeSpatrick 5246035553SpatrickSometimes you'll want to change the way LIT is running the tests. Custom options 53*4bdff4beSrobertcan be specified using the ``--param <name>=<val>`` flag. The most common option 54*4bdff4beSrobertyou'll want to change is the standard dialect (ie ``-std=c++XX``). By default the 5546035553Spatricktest suite will select the newest C++ dialect supported by the compiler and use 56*4bdff4beSrobertthat. However, you can manually specify the option like so if you want: 5746035553Spatrick 5846035553Spatrick.. code-block:: bash 5946035553Spatrick 60037e7968Spatrick $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std 61*4bdff4beSrobert $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param std=c++03 # Run the tests in C++03 6246035553Spatrick 63*4bdff4beSrobertOther parameters are supported by the test suite. Those are defined in ``libcxx/utils/libcxx/test/params.py``. 64*4bdff4beSrobertIf you want to customize how to run the libc++ test suite beyond what is available 65*4bdff4beSrobertin ``params.py``, you most likely want to use a custom site configuration instead. 6646035553Spatrick 67*4bdff4beSrobertThe libc++ test suite works by loading a site configuration that defines various 68*4bdff4beSrobert"base" parameters (via Lit substitutions). These base parameters represent things 69*4bdff4beSrobertlike the compiler to use for running the tests, which default compiler and linker 70*4bdff4beSrobertflags to use, and how to run an executable. This system is meant to be easily 71*4bdff4beSrobertextended for custom needs, in particular when porting the libc++ test suite to 72*4bdff4beSrobertnew platforms. 7346035553Spatrick 74*4bdff4beSrobertUsing a Custom Site Configuration 75037e7968Spatrick--------------------------------- 76037e7968Spatrick 77037e7968SpatrickBy default, the libc++ test suite will use a site configuration that matches 78037e7968Spatrickthe current CMake configuration. It does so by generating a ``lit.site.cfg`` 7976d0caaeSpatrickfile in the build directory from one of the configuration file templates in 8076d0caaeSpatrick``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around 8176d0caaeSpatrick``llvm/utils/lit/lit.py``) to that file. So when you're running 8276d0caaeSpatrick``<build>/bin/llvm-lit``, the generated ``lit.site.cfg`` file is always loaded 8376d0caaeSpatrickinstead of ``libcxx/test/lit.cfg.py``. If you want to use a custom site 8476d0caaeSpatrickconfiguration, simply point the CMake build to it using 8576d0caaeSpatrick``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration 8676d0caaeSpatrickwill be used instead. That file can use CMake variables inside it to make 8776d0caaeSpatrickconfiguration easier. 88037e7968Spatrick 89037e7968Spatrick .. code-block:: bash 90037e7968Spatrick 91037e7968Spatrick $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config> 9276d0caaeSpatrick $ make -C <build> cxx-test-depends 93037e7968Spatrick $ <build>/bin/llvm-lit -sv libcxx/test # will use your custom config file 9446035553Spatrick 95*4bdff4beSrobertAdditional tools 96*4bdff4beSrobert---------------- 9746035553Spatrick 98*4bdff4beSrobertThe libc++ test suite uses a few optional tools to improve the code quality. 9946035553Spatrick 100*4bdff4beSrobertThese tools are: 101*4bdff4beSrobert- clang-tidy (you might need additional dev packages to compile libc++-specific clang-tidy checks) 10246035553Spatrick 103037e7968SpatrickWriting Tests 104037e7968Spatrick------------- 10546035553Spatrick 106037e7968SpatrickWhen writing tests for the libc++ test suite, you should follow a few guidelines. 107037e7968SpatrickThis will ensure that your tests can run on a wide variety of hardware and under 108037e7968Spatricka wide variety of configurations. We have several unusual configurations such as 109037e7968Spatrickbuilding the tests on one host but running them on a different host, which add a 110037e7968Spatrickfew requirements to the test suite. Here's some stuff you should know: 11146035553Spatrick 112037e7968Spatrick- All tests are run in a temporary directory that is unique to that test and 113037e7968Spatrick cleaned up after the test is done. 114037e7968Spatrick- When a test needs data files as inputs, these data files can be saved in the 11576d0caaeSpatrick repository (when reasonable) and referenced by the test as 116037e7968Spatrick ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or 117037e7968Spatrick directories will be made available to the test in the temporary directory 118037e7968Spatrick where it is run. 119037e7968Spatrick- You should never hardcode a path from the build-host in a test, because that 120037e7968Spatrick path will not necessarily be available on the host where the tests are run. 121037e7968Spatrick- You should try to reduce the runtime dependencies of each test to the minimum. 122037e7968Spatrick For example, requiring Python to run a test is bad, since Python is not 123037e7968Spatrick necessarily available on all devices we may want to run the tests on (even 124037e7968Spatrick though supporting Python is probably trivial for the build-host). 12546035553Spatrick 12646035553SpatrickBenchmarks 12746035553Spatrick========== 12846035553Spatrick 12946035553SpatrickLibc++ contains benchmark tests separately from the test of the test suite. 13046035553SpatrickThe benchmarks are written using the `Google Benchmark`_ library, a copy of which 13146035553Spatrickis stored in the libc++ repository. 13246035553Spatrick 13346035553SpatrickFor more information about using the Google Benchmark library see the 13446035553Spatrick`official documentation <https://github.com/google/benchmark>`_. 13546035553Spatrick 13646035553Spatrick.. _`Google Benchmark`: https://github.com/google/benchmark 13746035553Spatrick 13846035553SpatrickBuilding Benchmarks 13946035553Spatrick------------------- 14046035553Spatrick 14146035553SpatrickThe benchmark tests are not built by default. The benchmarks can be built using 14246035553Spatrickthe ``cxx-benchmarks`` target. 14346035553Spatrick 14446035553SpatrickAn example build would look like: 14546035553Spatrick 14646035553Spatrick.. code-block:: bash 14746035553Spatrick 14846035553Spatrick $ cd build 149*4bdff4beSrobert $ ninja cxx-benchmarks 15046035553Spatrick 15146035553SpatrickThis will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be 15246035553Spatrickbuilt against the just-built libc++. The compiled tests are output into 153*4bdff4beSrobert``build/projects/libcxx/benchmarks``. 15446035553Spatrick 15546035553SpatrickThe benchmarks can also be built against the platforms native standard library 15646035553Spatrickusing the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This 15746035553Spatrickis useful for comparing the performance of libc++ to other standard libraries. 15846035553SpatrickThe compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and 15946035553Spatrick``<test>.native.out`` otherwise. 16046035553Spatrick 16146035553SpatrickAlso See: 16246035553Spatrick 16346035553Spatrick * :ref:`Building Libc++ <build instructions>` 16446035553Spatrick * :ref:`CMake Options` 16546035553Spatrick 16646035553SpatrickRunning Benchmarks 16746035553Spatrick------------------ 16846035553Spatrick 16946035553SpatrickThe benchmarks must be run manually by the user. Currently there is no way 17046035553Spatrickto run them as part of the build. 17146035553Spatrick 17246035553SpatrickFor example: 17346035553Spatrick 17446035553Spatrick.. code-block:: bash 17546035553Spatrick 176*4bdff4beSrobert $ cd build/projects/libcxx/benchmarks 17746035553Spatrick $ ./algorithms.libcxx.out # Runs all the benchmarks 17846035553Spatrick $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks 17946035553Spatrick 18046035553SpatrickFor more information about running benchmarks see `Google Benchmark`_. 181