[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / libc / test / UnitTest / LibcTestMain.cpp
blobc348d5ef1aa1b12c1741f4a0dae30e29a771b4ab
1 //===-- Main function for implementation of base class for libc unittests -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "LibcTest.h"
10 #include "src/__support/CPP/string_view.h"
12 using LIBC_NAMESPACE::cpp::string_view;
13 using LIBC_NAMESPACE::testing::TestOptions;
15 namespace {
17 // A poor-man's getopt_long.
18 // Run unit tests with --gtest_color=no to disable printing colors, or
19 // --gtest_print_time to print timings in milliseconds only (as GTest does, so
20 // external tools such as Android's atest may expect that format to parse the
21 // output). Other command line flags starting with --gtest_ are ignored.
22 // Otherwise, the last command line arg is used as a test filter, if command
23 // line args are specified.
24 TestOptions parseOptions(int argc, char **argv) {
25 TestOptions Options;
27 for (int i = 1; i < argc; ++i) {
28 string_view arg{argv[i]};
30 if (arg == "--gtest_color=no")
31 Options.PrintColor = false;
32 else if (arg == "--gtest_print_time")
33 Options.TimeInMs = true;
34 // Ignore other unsupported gtest specific flags.
35 else if (arg.starts_with("--gtest_"))
36 continue;
37 else
38 Options.TestFilter = argv[i];
41 return Options;
44 } // anonymous namespace
46 // The C++ standard forbids declaring the main function with a linkage specifier
47 // outisde of 'freestanding' mode, only define the linkage for hermetic tests.
48 #if __STDC_HOSTED__
49 #define TEST_MAIN int main
50 #else
51 #define TEST_MAIN extern "C" int main
52 #endif
54 TEST_MAIN(int argc, char **argv, char **envp) {
55 LIBC_NAMESPACE::testing::argc = argc;
56 LIBC_NAMESPACE::testing::argv = argv;
57 LIBC_NAMESPACE::testing::envp = envp;
59 return LIBC_NAMESPACE::testing::Test::runTests(parseOptions(argc, argv));