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 //===----------------------------------------------------------------------===//
8
9 #include "LibcTest.h"
10 #include "src/__support/CPP/string_view.h"
11
12 using LIBC_NAMESPACE::cpp::string_view;
13 using LIBC_NAMESPACE::testing::TestOptions;
14
15 namespace {
16
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.
parseOptions(int argc,char ** argv)24 TestOptions parseOptions(int argc, char **argv) {
25 TestOptions Options;
26
27 for (int i = 1; i < argc; ++i) {
28 string_view arg{argv[i]};
29
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];
39 }
40
41 return Options;
42 }
43
44 } // anonymous namespace
45
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
53
TEST_MAIN(int argc,char ** argv,char ** envp)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;
58
59 return LIBC_NAMESPACE::testing::Test::runTests(parseOptions(argc, argv));
60 }
61