xref: /aosp_15_r20/external/skia/tools/testrunners/common/TestRunner.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/private/base/SkAttributes.h"
9 #include "include/private/base/SkDebug.h"
10 #include "tools/testrunners/common/TestRunner.h"
11 
12 #include <cstdarg>
13 #include <iomanip>
14 #include <regex>
15 #include <sstream>
16 
StringNonEmpty(std::string name,CommandLineFlags::StringArray flag)17 void TestRunner::FlagValidators::StringNonEmpty(std::string name,
18                                                 CommandLineFlags::StringArray flag) {
19     if (flag.size() == 0) {
20         SK_ABORT("Flag %s cannot be empty.\n", name.c_str());
21     }
22 }
23 
StringAtMostOne(std::string name,CommandLineFlags::StringArray flag)24 void TestRunner::FlagValidators::StringAtMostOne(std::string name,
25                                                  CommandLineFlags::StringArray flag) {
26     if (flag.size() > 1) {
27         SK_ABORT("Flag %s takes one single value, got: %d.\n", name.c_str(), flag.size());
28     }
29 }
30 
StringEven(std::string name,CommandLineFlags::StringArray flag)31 void TestRunner::FlagValidators::StringEven(std::string name, CommandLineFlags::StringArray flag) {
32     if (flag.size() % 2 == 1) {
33         SK_ABORT(
34                 "Flag %s takes an even number of arguments, got: %d.\n", name.c_str(), flag.size());
35     }
36 }
37 
IntGreaterOrEqual(std::string name,int flag,int min)38 void TestRunner::FlagValidators::IntGreaterOrEqual(std::string name, int flag, int min) {
39     if (flag < min) {
40         SK_ABORT("Flag %s must be greater or equal than %d, got: %d.\n", name.c_str(), min, flag);
41     }
42 }
43 
AllOrNone(std::map<std::string,bool> flags)44 void TestRunner::FlagValidators::AllOrNone(std::map<std::string, bool> flags) {
45     std::string names;
46     unsigned int numFlagsSet = 0;
47     for (auto const& [name, isSet] : flags) {
48         if (names == "") {
49             names = name;
50         } else {
51             names += ", " + name;
52         }
53         if (isSet) {
54             numFlagsSet++;
55         }
56     }
57     if (numFlagsSet != flags.size() && numFlagsSet != 0) {
58         SK_ABORT("Either all or none of the following flags must be set: %s.\n", names.c_str());
59     }
60 }
61 
ExactlyOne(std::map<std::string,bool> flags)62 void TestRunner::FlagValidators::ExactlyOne(std::map<std::string, bool> flags) {
63     std::string names;
64     unsigned int numFlagsSet = 0;
65     for (auto const& [name, isSet] : flags) {
66         if (names == "") {
67             names = name;
68         } else {
69             names += ", " + name;
70         }
71         if (isSet) {
72             numFlagsSet++;
73         }
74     }
75     if (numFlagsSet != 1) {
76         SK_ABORT("Exactly one of the following flags must be set: %s.\n", names.c_str());
77     }
78 }
79 
80 #if defined(SK_BUILD_FOR_ANDROID)
81 // We declare the below external variable here, rather than within
82 // TestRunner::InitAndLogCmdlineArgs(), because the latter causes the following linking error:
83 //
84 //     undefined reference to `TestRunner::gSkDebugToStdOut'
85 extern bool gSkDebugToStdOut;
86 #endif
87 
InitAndLogCmdlineArgs(int argc,char ** argv)88 void TestRunner::InitAndLogCmdlineArgs(int argc, char** argv) {
89 #if defined(SK_BUILD_FOR_ANDROID)
90     // If true, sends SkDebugf to stdout as well.
91     //
92     // It is critical that we set this up as early in a test runner as possible, otherwise
93     // SK_ABORT(msg) and other similar macros will just print "Trap" to stdout without logging the
94     // message.
95     gSkDebugToStdOut = true;
96 #endif
97 
98     // Print command-line for debugging purposes.
99     if (argc < 2) {
100         TestRunner::Log("Test runner invoked with no arguments.");
101     } else {
102         std::ostringstream oss;
103         for (int i = 1; i < argc; i++) {
104             if (i > 1) {
105                 oss << " ";
106             }
107             oss << argv[i];
108         }
109         TestRunner::Log("Test runner invoked with arguments: %s", oss.str().c_str());
110     }
111 }
112 
ShouldRunTestCase(const char * name,CommandLineFlags::StringArray & matchFlag,CommandLineFlags::StringArray & skipFlag)113 bool TestRunner::ShouldRunTestCase(const char* name,
114                                    CommandLineFlags::StringArray& matchFlag,
115                                    CommandLineFlags::StringArray& skipFlag) {
116     for (int i = 0; i < skipFlag.size(); i++) {
117         std::regex re(skipFlag[i]);
118         if (std::regex_search(name, re)) {
119             return false;
120         }
121     }
122 
123     if (matchFlag.isEmpty()) {
124         return true;
125     }
126 
127     for (int i = 0; i < matchFlag.size(); i++) {
128         std::regex re(matchFlag[i]);
129         if (std::regex_search(name, re)) {
130             return true;
131         }
132     }
133 
134     return false;
135 }
136 
Log(const char * format,...)137 void TestRunner::Log(const char* format, ...) {
138     std::time_t t = std::time(nullptr);
139     std::tm* now = std::gmtime(&t);
140     std::ostringstream oss;
141     oss << std::put_time(now, "%Y-%m-%d %H:%M:%S UTC");
142     printf("[%s] ", oss.str().c_str());
143 
144     va_list args;
145     va_start(args, format);
146     vprintf(format, args);
147     va_end(args);
148 
149     printf("\n");
150     fflush(stdout);
151 }
152