1 //===-- Main function for implementation of base class for libc unittests -===//
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
7 //===----------------------------------------------------------------------===//
10 #include "src/__support/CPP/string_view.h"
12 using LIBC_NAMESPACE::cpp::string_view
;
13 using LIBC_NAMESPACE::testing::TestOptions
;
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
) {
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_"))
38 Options
.TestFilter
= argv
[i
];
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.
49 #define TEST_MAIN int main
51 #define TEST_MAIN extern "C" int main
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
));