Flutter Engine
The Flutter Engine
TestRunner.cpp
Go to the documentation of this file.
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
11
12#include <cstdarg>
13#include <iomanip>
14#include <regex>
15#include <sstream>
16
19 if (flag.size() == 0) {
20 SK_ABORT("Flag %s cannot be empty.\n", name.c_str());
21 }
22}
23
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
32 if (flag.size() % 2 == 1) {
34 "Flag %s takes an even number of arguments, got: %d.\n", name.c_str(), flag.size());
35 }
36}
37
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
44void 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
62void 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'
85extern bool gSkDebugToStdOut;
86#endif
87
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
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
137void 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;
146 vprintf(format, args);
147 va_end(args);
148
149 printf("\n");
150 fflush(stdout);
151}
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
FlutterSemanticsFlag flag
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
static float min(float r, float g, float b)
Definition: hsl.cpp:48
char ** argv
Definition: library.h:9
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: SkSLString.cpp:83
void StringEven(std::string name, CommandLineFlags::StringArray flag)
Definition: TestRunner.cpp:31
void IntGreaterOrEqual(std::string name, int flag, int min)
Definition: TestRunner.cpp:38
void ExactlyOne(std::map< std::string, bool > flags)
Definition: TestRunner.cpp:62
void AllOrNone(std::map< std::string, bool > flags)
Definition: TestRunner.cpp:44
void StringNonEmpty(std::string name, CommandLineFlags::StringArray flag)
Definition: TestRunner.cpp:17
void StringAtMostOne(std::string name, CommandLineFlags::StringArray flag)
Definition: TestRunner.cpp:24
bool ShouldRunTestCase(const char *name, CommandLineFlags::StringArray &matchFlag, CommandLineFlags::StringArray &skipFlag)
Definition: TestRunner.cpp:113
void Log(const char *format,...) SK_PRINTF_LIKE(1
Definition: TestRunner.cpp:137
void InitAndLogCmdlineArgs(int argc, char **argv)
Definition: TestRunner.cpp:88
va_start(args, format)
va_end(args)
static const char *const names[]
Definition: symbols.cc:24
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static double time(int loops, Benchmark *bench, Target *target)
Definition: nanobench.cpp:394