blob: 25395f460de19585b60c43e6dc92321d6d161da0 [file] [log] [blame]
Eric Fiselierd720d1f2015-08-22 19:40:49 +00001==============
2Testing libc++
3==============
4
5.. contents::
6 :local:
7
8Getting Started
9===============
10
Louis Dionnef58d8292020-03-11 17:03:00 -040011libc++ uses LIT to configure and run its tests.
Marshall Clowca321972019-09-05 00:38:36 +000012
Louis Dionne089ced02020-04-07 15:02:37 -040013The primary way to run the libc++ tests is by using ``make check-cxx``.
Marshall Clowca321972019-09-05 00:38:36 +000014
15However since libc++ can be used in any number of possible
16configurations it is important to customize the way LIT builds and runs
17the tests. This guide provides information on how to use LIT directly to
18test libc++.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000019
20Please see the `Lit Command Guide`_ for more information about LIT.
21
Sylvestre Ledrub09c9382020-03-22 22:42:03 +010022.. _LIT Command Guide: https://llvm.org/docs/CommandGuide/lit.html
Eric Fiselierd720d1f2015-08-22 19:40:49 +000023
Louis Dionne089ced02020-04-07 15:02:37 -040024Usage
25-----
Eric Fiselierd720d1f2015-08-22 19:40:49 +000026
Louis Dionne089ced02020-04-07 15:02:37 -040027After building libc++, you can run parts of the libc++ test suite by simply
Louis Dionnee35df792020-04-15 15:05:14 -040028running ``llvm-lit`` on a specified test or directory. If you're unsure
Louis Dionne3efccd72020-10-23 09:32:50 -040029whether the required libraries have been built, you can use the
Louis Dionne4eaeee42022-08-02 16:15:55 -040030``cxx-test-depends`` target. For example:
Eric Fiselier4ac88092015-10-14 20:44:44 +000031
32.. code-block:: bash
33
Louis Dionne089ced02020-04-07 15:02:37 -040034 $ cd <monorepo-root>
Louis Dionne10735312021-01-12 10:56:57 -050035 $ make -C <build> cxx-test-depends # If you want to make sure the targets get rebuilt
Louis Dionne089ced02020-04-07 15:02:37 -040036 $ <build>/bin/llvm-lit -sv libcxx/test/std/re # Run all of the std::regex tests
37 $ <build>/bin/llvm-lit -sv libcxx/test/std/depr/depr.c.headers/stdlib_h.pass.cpp # Run a single test
38 $ <build>/bin/llvm-lit -sv libcxx/test/std/atomics libcxx/test/std/threads # Test std::thread and std::atomic
Eric Fiselier4ac88092015-10-14 20:44:44 +000039
Louis Dionne94870d82020-06-26 12:08:59 -040040In the default configuration, the tests are built against headers that form a
41fake installation root of libc++. This installation root has to be updated when
Louis Dionne4eaeee42022-08-02 16:15:55 -040042changes are made to the headers, so you should re-run the ``cxx-test-depends``
43target before running the tests manually with ``lit`` when you make any sort of
Louis Dionne94870d82020-06-26 12:08:59 -040044change, including to the headers.
45
Eric Fiselier4ac88092015-10-14 20:44:44 +000046Sometimes you'll want to change the way LIT is running the tests. Custom options
Louis Dionne4eaeee42022-08-02 16:15:55 -040047can be specified using the ``--param <name>=<val>`` flag. The most common option
48you'll want to change is the standard dialect (ie ``-std=c++XX``). By default the
Eric Fiselier4ac88092015-10-14 20:44:44 +000049test suite will select the newest C++ dialect supported by the compiler and use
Louis Dionne4eaeee42022-08-02 16:15:55 -040050that. However, you can manually specify the option like so if you want:
Eric Fiselier4ac88092015-10-14 20:44:44 +000051
52.. code-block:: bash
53
Louis Dionne089ced02020-04-07 15:02:37 -040054 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers # Run the tests with the newest -std
Louis Dionne4eaeee42022-08-02 16:15:55 -040055 $ <build>/bin/llvm-lit -sv libcxx/test/std/containers --param std=c++03 # Run the tests in C++03
Eric Fiselier4ac88092015-10-14 20:44:44 +000056
Louis Dionne4eaeee42022-08-02 16:15:55 -040057Other parameters are supported by the test suite. Those are defined in ``libcxx/utils/libcxx/test/params.py``.
58If you want to customize how to run the libc++ test suite beyond what is available
59in ``params.py``, you most likely want to use a custom site configuration instead.
Eric Fiselier4ac88092015-10-14 20:44:44 +000060
Louis Dionne4eaeee42022-08-02 16:15:55 -040061The libc++ test suite works by loading a site configuration that defines various
62"base" parameters (via Lit substitutions). These base parameters represent things
63like the compiler to use for running the tests, which default compiler and linker
64flags to use, and how to run an executable. This system is meant to be easily
65extended for custom needs, in particular when porting the libc++ test suite to
66new platforms.
Eric Fiselier4ac88092015-10-14 20:44:44 +000067
Louis Dionne4eaeee42022-08-02 16:15:55 -040068Using a Custom Site Configuration
Louis Dionne089ced02020-04-07 15:02:37 -040069---------------------------------
70
71By default, the libc++ test suite will use a site configuration that matches
72the current CMake configuration. It does so by generating a ``lit.site.cfg``
Louis Dionne6da743b2020-08-29 17:13:02 -040073file in the build directory from one of the configuration file templates in
74``libcxx/test/configs/``, and pointing ``llvm-lit`` (which is a wrapper around
75``llvm/utils/lit/lit.py``) to that file. So when you're running
76``<build>/bin/llvm-lit``, the generated ``lit.site.cfg`` file is always loaded
77instead of ``libcxx/test/lit.cfg.py``. If you want to use a custom site
78configuration, simply point the CMake build to it using
79``-DLIBCXX_TEST_CONFIG=<path-to-site-config>``, and that site configuration
80will be used instead. That file can use CMake variables inside it to make
81configuration easier.
Louis Dionne089ced02020-04-07 15:02:37 -040082
83 .. code-block:: bash
84
Louis Dionne19608c62020-06-12 15:19:55 -040085 $ cmake <options> -DLIBCXX_TEST_CONFIG=<path-to-site-config>
Louis Dionne10735312021-01-12 10:56:57 -050086 $ make -C <build> cxx-test-depends
Louis Dionne19608c62020-06-12 15:19:55 -040087 $ <build>/bin/llvm-lit -sv libcxx/test # will use your custom config file
Louis Dionne089ced02020-04-07 15:02:37 -040088
Louis Dionne9e7cf262020-04-02 17:14:45 -040089Writing Tests
90-------------
91
92When writing tests for the libc++ test suite, you should follow a few guidelines.
93This will ensure that your tests can run on a wide variety of hardware and under
94a wide variety of configurations. We have several unusual configurations such as
95building the tests on one host but running them on a different host, which add a
96few requirements to the test suite. Here's some stuff you should know:
97
98- All tests are run in a temporary directory that is unique to that test and
99 cleaned up after the test is done.
100- When a test needs data files as inputs, these data files can be saved in the
Marek Kurdej0dd96e52020-10-29 14:39:09 +0100101 repository (when reasonable) and referenced by the test as
Louis Dionne9e7cf262020-04-02 17:14:45 -0400102 ``// FILE_DEPENDENCIES: <path-to-dependencies>``. Copies of these files or
103 directories will be made available to the test in the temporary directory
104 where it is run.
105- You should never hardcode a path from the build-host in a test, because that
106 path will not necessarily be available on the host where the tests are run.
107- You should try to reduce the runtime dependencies of each test to the minimum.
108 For example, requiring Python to run a test is bad, since Python is not
109 necessarily available on all devices we may want to run the tests on (even
110 though supporting Python is probably trivial for the build-host).
111
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000112Benchmarks
113==========
114
115Libc++ contains benchmark tests separately from the test of the test suite.
116The benchmarks are written using the `Google Benchmark`_ library, a copy of which
117is stored in the libc++ repository.
118
119For more information about using the Google Benchmark library see the
120`official documentation <https://github.com/google/benchmark>`_.
121
122.. _`Google Benchmark`: https://github.com/google/benchmark
123
124Building Benchmarks
125-------------------
126
Eric Fiselier93f212c2016-08-29 19:50:49 +0000127The benchmark tests are not built by default. The benchmarks can be built using
128the ``cxx-benchmarks`` target.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000129
130An example build would look like:
131
132.. code-block:: bash
133
134 $ cd build
Shivam Guptae3cb5702021-10-02 07:35:15 +0530135 $ ninja cxx-benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000136
137This will build all of the benchmarks under ``<libcxx-src>/benchmarks`` to be
138built against the just-built libc++. The compiled tests are output into
Shivam Guptae3cb5702021-10-02 07:35:15 +0530139``build/projects/libcxx/benchmarks``.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000140
141The benchmarks can also be built against the platforms native standard library
142using the ``-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON`` CMake option. This
143is useful for comparing the performance of libc++ to other standard libraries.
144The compiled benchmarks are named ``<test>.libcxx.out`` if they test libc++ and
145``<test>.native.out`` otherwise.
146
147Also See:
148
149 * :ref:`Building Libc++ <build instructions>`
150 * :ref:`CMake Options`
151
152Running Benchmarks
153------------------
154
155The benchmarks must be run manually by the user. Currently there is no way
156to run them as part of the build.
157
158For example:
159
160.. code-block:: bash
161
Shivam Guptae3cb5702021-10-02 07:35:15 +0530162 $ cd build/projects/libcxx/benchmarks
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000163 $ ./algorithms.libcxx.out # Runs all the benchmarks
164 $ ./algorithms.libcxx.out --benchmark_filter=BM_Sort.* # Only runs the sort benchmarks
165
166For more information about running benchmarks see `Google Benchmark`_.