blob: 620cdac9ca69fad6d075c6d1ef5c3bb908e0c0e2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +00007
scroggo@google.com5a6324e2013-04-11 20:11:40 +00008#include "SkCommandLineFlags.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +00009#include "SkGraphics.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000010#include "SkOSFile.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000011#include "SkRunnable.h"
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000012#include "SkTArray.h"
13#include "SkTemplates.h"
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000014#include "SkThreadPool.h"
15#include "SkTime.h"
reed@android.comed673312009-02-27 16:24:51 +000016#include "Test.h"
17
robertphillips@google.combdb1be52012-09-07 18:24:43 +000018#if SK_SUPPORT_GPU
19#include "GrContext.h"
20#endif
21
reed@android.comed673312009-02-27 16:24:51 +000022using namespace skiatest;
23
reed@android.comd252db02009-04-01 18:31:44 +000024// need to explicitly declare this, or we get some weird infinite loop llist
25template TestRegistry* TestRegistry::gHead;
26
reed@android.comed673312009-02-27 16:24:51 +000027class Iter {
28public:
29 Iter(Reporter* r) : fReporter(r) {
30 r->ref();
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000031 this->reset();
32 }
33
34 void reset() {
reed@android.comed673312009-02-27 16:24:51 +000035 fReg = TestRegistry::Head();
36 }
reed@android.com80e39a72009-04-02 16:59:40 +000037
reed@android.comed673312009-02-27 16:24:51 +000038 ~Iter() {
39 fReporter->unref();
40 }
reed@android.com80e39a72009-04-02 16:59:40 +000041
reed@android.comed673312009-02-27 16:24:51 +000042 Test* next() {
43 if (fReg) {
44 TestRegistry::Factory fact = fReg->factory();
45 fReg = fReg->next();
46 Test* test = fact(NULL);
47 test->setReporter(fReporter);
48 return test;
49 }
50 return NULL;
51 }
reed@android.com80e39a72009-04-02 16:59:40 +000052
reed@android.comed673312009-02-27 16:24:51 +000053private:
54 Reporter* fReporter;
55 const TestRegistry* fReg;
56};
57
reed@android.comd252db02009-04-01 18:31:44 +000058class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000059public:
caryclark@google.com07e97fc2013-07-08 17:17:02 +000060 DebugfReporter(bool allowExtendedTest, bool allowThreaded, bool verbose)
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000061 : fNextIndex(0)
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +000062 , fPending(0)
caryclark@google.comd54e1e92013-04-10 15:57:31 +000063 , fTotal(0)
caryclark@google.com16cfe402013-04-18 18:47:37 +000064 , fAllowExtendedTest(allowExtendedTest)
caryclark@google.com07e97fc2013-07-08 17:17:02 +000065 , fAllowThreaded(allowThreaded)
66 , fVerbose(verbose) {
caryclark@google.comd54e1e92013-04-10 15:57:31 +000067 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000068
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000069 void setTotal(int total) {
reed@android.com57b799e2009-04-01 20:26:42 +000070 fTotal = total;
71 }
caryclark@google.comd54e1e92013-04-10 15:57:31 +000072
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000073 virtual bool allowExtendedTest() const SK_OVERRIDE {
skia.committer@gmail.com391ca662013-04-11 07:01:45 +000074 return fAllowExtendedTest;
caryclark@google.comd54e1e92013-04-10 15:57:31 +000075 }
76
commit-bot@chromium.orge1c54292013-04-22 17:35:55 +000077 virtual bool allowThreaded() const SK_OVERRIDE {
caryclark@google.com16cfe402013-04-18 18:47:37 +000078 return fAllowThreaded;
79 }
80
caryclark@google.com07e97fc2013-07-08 17:17:02 +000081 virtual bool verbose() const SK_OVERRIDE {
82 return fVerbose;
83 }
84
reed@android.comed673312009-02-27 16:24:51 +000085protected:
86 virtual void onStart(Test* test) {
commit-bot@chromium.orgf6842e72013-10-01 18:43:50 +000087 SkAutoMutexAcquire lock(fStartEndMutex);
88 fNextIndex++;
89 fPending++;
90 SkDebugf("[%3d/%3d] (%d) %s\n", fNextIndex, fTotal, fPending, test->getName());
reed@android.comed673312009-02-27 16:24:51 +000091 }
commit-bot@chromium.orgf6842e72013-10-01 18:43:50 +000092
commit-bot@chromium.org1f792862013-06-18 20:50:34 +000093 virtual void onReportFailed(const SkString& desc) {
94 SkDebugf("\tFAILED: %s\n", desc.c_str());
reed@android.comed673312009-02-27 16:24:51 +000095 }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000096
97 virtual void onEnd(Test* test) {
commit-bot@chromium.orgf6842e72013-10-01 18:43:50 +000098 SkAutoMutexAcquire lock(fStartEndMutex);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +000099 if (!test->passed()) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000100 SkDebugf("---- %s FAILED\n", test->getName());
101 }
102
commit-bot@chromium.orgf6842e72013-10-01 18:43:50 +0000103 fPending--;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000104 if (fNextIndex == fTotal) {
105 // Just waiting on straggler tests. Shame them by printing their name and runtime.
106 SkDebugf(" (%d) %5.1fs %s\n",
107 fPending, test->elapsedMs() / 1e3, test->getName());
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000108 }
109 }
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000110
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000111private:
commit-bot@chromium.orgf6842e72013-10-01 18:43:50 +0000112 SkMutex fStartEndMutex; // Guards fNextIndex and fPending.
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000113 int32_t fNextIndex;
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000114 int32_t fPending;
commit-bot@chromium.orgf6842e72013-10-01 18:43:50 +0000115
116 // Once the tests get going, these are logically const.
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000117 int fTotal;
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000118 bool fAllowExtendedTest;
caryclark@google.com16cfe402013-04-18 18:47:37 +0000119 bool fAllowThreaded;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000120 bool fVerbose;
reed@android.comed673312009-02-27 16:24:51 +0000121};
122
caryclark@google.comb631eec2013-05-02 13:14:40 +0000123DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \
124 "Multiple matches may be separated by spaces.\n" \
125 "~ causes a matching test to always be skipped\n" \
126 "^ requires the start of the test to match\n" \
127 "$ requires the end of the test to match\n" \
128 "^ and $ requires an exact match\n" \
129 "If a test does not match any list entry,\n" \
130 "it is skipped unless some list entry starts with ~");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000131DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use.");
132DEFINE_string2(resourcePath, i, NULL, "directory for test resources.");
133DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps.");
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000134DEFINE_bool2(single, z, false, "run tests on a single thread internally.");
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000135DEFINE_bool2(verbose, v, false, "enable verbose output.");
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +0000136DEFINE_int32(threads, SkThreadPool::kThreadPerCore,
137 "Run threadsafe tests on a threadpool with this many threads.");
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000138
scroggo@google.comc76218d2013-06-06 14:59:56 +0000139SkString Test::GetTmpDir() {
140 const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0];
141 return SkString(tmpDir);
142}
143
144SkString Test::GetResourcePath() {
145 const char* resourcePath = FLAGS_resourcePath.isEmpty() ? NULL : FLAGS_resourcePath[0];
146 return SkString(resourcePath);
147}
148
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000149// Deletes self when run.
150class SkTestRunnable : public SkRunnable {
151public:
152 // Takes ownership of test.
153 SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {}
154
155 virtual void run() {
156 fTest->run();
157 if(!fTest->passed()) {
158 sk_atomic_inc(fFailCount);
159 }
160 SkDELETE(this);
161 }
162
163private:
164 SkAutoTDelete<Test> fTest;
165 int32_t* fFailCount;
166};
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000167
caryclark@google.com5987f582012-10-02 18:33:14 +0000168int tool_main(int argc, char** argv);
169int tool_main(int argc, char** argv) {
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000170 SkCommandLineFlags::SetUsage("");
171 SkCommandLineFlags::Parse(argc, argv);
172
bsalomon@google.com4e230682013-01-15 20:37:04 +0000173#if SK_ENABLE_INST_COUNT
reed@google.coma2769752012-07-22 22:33:05 +0000174 gPrintInstCount = true;
175#endif
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000176
reed@google.coma2769752012-07-22 22:33:05 +0000177 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000178
reed@google.com91d449e2011-10-26 15:25:18 +0000179 {
180 SkString header("Skia UnitTests:");
reed@google.com9aff1482013-04-11 18:27:52 +0000181 if (!FLAGS_match.isEmpty()) {
caryclark@google.comb631eec2013-05-02 13:14:40 +0000182 header.appendf(" --match");
183 for (int index = 0; index < FLAGS_match.count(); ++index) {
184 header.appendf(" %s", FLAGS_match[index]);
185 }
reed@google.com91d449e2011-10-26 15:25:18 +0000186 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000187 SkString tmpDir = Test::GetTmpDir();
188 if (!tmpDir.isEmpty()) {
189 header.appendf(" --tmpDir %s", tmpDir.c_str());
djsollen@google.comcb626502013-03-20 13:48:20 +0000190 }
scroggo@google.comc76218d2013-06-06 14:59:56 +0000191 SkString resourcePath = Test::GetResourcePath();
192 if (!resourcePath.isEmpty()) {
193 header.appendf(" --resourcePath %s", resourcePath.c_str());
reed@google.com789c6f22013-02-25 20:24:24 +0000194 }
reed@google.com91d449e2011-10-26 15:25:18 +0000195#ifdef SK_DEBUG
196 header.append(" SK_DEBUG");
197#else
198 header.append(" SK_RELEASE");
199#endif
200#ifdef SK_SCALAR_IS_FIXED
201 header.append(" SK_SCALAR_IS_FIXED");
202#else
203 header.append(" SK_SCALAR_IS_FLOAT");
204#endif
reed@google.com5696baa2013-08-29 20:20:39 +0000205 header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8);
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000206 SkDebugf("%s\n", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000207 }
208
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000209 DebugfReporter reporter(FLAGS_extendedTest, !FLAGS_single, FLAGS_verbose);
reed@android.comed673312009-02-27 16:24:51 +0000210 Iter iter(&reporter);
reed@android.com80e39a72009-04-02 16:59:40 +0000211
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000212 // Count tests first.
213 int total = 0;
214 int toRun = 0;
215 Test* test;
sglez@google.com586db932013-07-24 17:24:23 +0000216
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000217 while ((test = iter.next()) != NULL) {
218 SkAutoTDelete<Test> owned(test);
sglez@google.com586db932013-07-24 17:24:23 +0000219
commit-bot@chromium.orga6f37e72013-08-30 15:52:46 +0000220 if(!SkCommandLineFlags::ShouldSkip(FLAGS_match, test->getName())) {
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000221 toRun++;
222 }
223 total++;
224 }
225 reporter.setTotal(toRun);
226
227 // Now run them.
228 iter.reset();
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000229 int32_t failCount = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000230 int skipCount = 0;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000231
232 SkAutoTDelete<SkThreadPool> threadpool(SkNEW_ARGS(SkThreadPool, (FLAGS_threads)));
233 SkTArray<Test*> unsafeTests; // Always passes ownership to an SkTestRunnable
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000234 for (int i = 0; i < total; i++) {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000235 SkAutoTDelete<Test> test(iter.next());
commit-bot@chromium.orga6f37e72013-08-30 15:52:46 +0000236 if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test->getName())) {
bungeman@google.com5af16f82011-09-02 15:06:44 +0000237 ++skipCount;
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000238 } else if (!test->isThreadsafe()) {
239 unsafeTests.push_back() = test.detach();
bungeman@google.com5af16f82011-09-02 15:06:44 +0000240 } else {
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000241 threadpool->add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount)));
bungeman@google.com5af16f82011-09-02 15:06:44 +0000242 }
reed@android.comed673312009-02-27 16:24:51 +0000243 }
reed@android.com57b799e2009-04-01 20:26:42 +0000244
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000245 // Run the tests that aren't threadsafe.
246 for (int i = 0; i < unsafeTests.count(); i++) {
247 SkNEW_ARGS(SkTestRunnable, (unsafeTests[i], &failCount))->run();
248 }
249
250 // Blocks until threaded tests finish.
251 threadpool.free();
252
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000253 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
commit-bot@chromium.org0506b9d2013-04-22 16:43:07 +0000254 toRun, failCount, skipCount);
commit-bot@chromium.org197845a2013-04-19 13:24:28 +0000255 const int testCount = reporter.countTests();
commit-bot@chromium.orgba59d642013-04-11 16:54:09 +0000256 if (FLAGS_verbose && testCount > 0) {
caryclark@google.comd54e1e92013-04-10 15:57:31 +0000257 SkDebugf("Ran %d Internal tests.\n", testCount);
258 }
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000259#if SK_SUPPORT_GPU
260
261#if GR_CACHE_STATS
262 GrContext *gr = GpuTest::GetContext();
263
264 gr->printCacheStats();
265#endif
266
267#endif
268
reed@google.coma2769752012-07-22 22:33:05 +0000269 SkGraphics::Term();
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000270 GpuTest::DestroyContexts();
reed@google.coma2769752012-07-22 22:33:05 +0000271
bungeman@google.com5af16f82011-09-02 15:06:44 +0000272 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000273}
caryclark@google.com5987f582012-10-02 18:33:14 +0000274
borenet@google.com7158e6a2012-11-01 17:43:44 +0000275#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000276int main(int argc, char * const argv[]) {
277 return tool_main(argc, (char**) argv);
278}
279#endif