• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
main(int argc,char ** argv,char ** envp)46 extern "C" int main(int argc, char **argv, char **envp) {
47   LIBC_NAMESPACE::testing::argc = argc;
48   LIBC_NAMESPACE::testing::argv = argv;
49   LIBC_NAMESPACE::testing::envp = envp;
50 
51   return LIBC_NAMESPACE::testing::Test::runTests(parseOptions(argc, argv));
52 }
53