blob: 84318da82483db90c9555915585881245262d108 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com5e5adfd2009-03-07 03:39:23 +00008#include "SkGraphics.h"
reed@android.comed673312009-02-27 16:24:51 +00009#include "Test.h"
10
robertphillips@google.combdb1be52012-09-07 18:24:43 +000011#if SK_SUPPORT_GPU
12#include "GrContext.h"
13#endif
14
reed@android.comed673312009-02-27 16:24:51 +000015using namespace skiatest;
16
reed@android.comd252db02009-04-01 18:31:44 +000017// need to explicitly declare this, or we get some weird infinite loop llist
18template TestRegistry* TestRegistry::gHead;
19
reed@android.comed673312009-02-27 16:24:51 +000020class Iter {
21public:
22 Iter(Reporter* r) : fReporter(r) {
23 r->ref();
24 fReg = TestRegistry::Head();
25 }
reed@android.com80e39a72009-04-02 16:59:40 +000026
reed@android.comed673312009-02-27 16:24:51 +000027 ~Iter() {
28 fReporter->unref();
29 }
reed@android.com80e39a72009-04-02 16:59:40 +000030
reed@android.comed673312009-02-27 16:24:51 +000031 Test* next() {
32 if (fReg) {
33 TestRegistry::Factory fact = fReg->factory();
34 fReg = fReg->next();
35 Test* test = fact(NULL);
36 test->setReporter(fReporter);
37 return test;
38 }
39 return NULL;
40 }
reed@android.com80e39a72009-04-02 16:59:40 +000041
reed@android.com57b799e2009-04-01 20:26:42 +000042 static int Count() {
43 const TestRegistry* reg = TestRegistry::Head();
44 int count = 0;
45 while (reg) {
46 count += 1;
47 reg = reg->next();
48 }
49 return count;
50 }
reed@android.com80e39a72009-04-02 16:59:40 +000051
reed@android.comed673312009-02-27 16:24:51 +000052private:
53 Reporter* fReporter;
54 const TestRegistry* fReg;
55};
56
57static const char* result2string(Reporter::Result result) {
58 return result == Reporter::kPassed ? "passed" : "FAILED";
59}
60
reed@android.comd252db02009-04-01 18:31:44 +000061class DebugfReporter : public Reporter {
reed@android.com57b799e2009-04-01 20:26:42 +000062public:
djsollen@google.comf4d1b392012-11-29 16:29:58 +000063 DebugfReporter() : fIndex(0), fTotal(0) {}
reed@android.comeeb3b7f2009-04-09 04:06:54 +000064
reed@android.com57b799e2009-04-01 20:26:42 +000065 void setIndexOfTotal(int index, int total) {
66 fIndex = index;
67 fTotal = total;
68 }
reed@android.comed673312009-02-27 16:24:51 +000069protected:
70 virtual void onStart(Test* test) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000071 SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
reed@android.comed673312009-02-27 16:24:51 +000072 }
73 virtual void onReport(const char desc[], Reporter::Result result) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000074 SkDebugf("\t%s: %s\n", result2string(result), desc);
reed@android.comed673312009-02-27 16:24:51 +000075 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +000076 virtual void onEnd(Test* test) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000077 if (!this->getCurrSuccess()) {
78 SkDebugf("---- FAILED\n");
reed@android.comeeb3b7f2009-04-09 04:06:54 +000079 }
80 }
djsollen@google.comf4d1b392012-11-29 16:29:58 +000081private:
reed@android.com57b799e2009-04-01 20:26:42 +000082 int fIndex, fTotal;
reed@android.comed673312009-02-27 16:24:51 +000083};
84
caryclark@google.com5987f582012-10-02 18:33:14 +000085int tool_main(int argc, char** argv);
86int tool_main(int argc, char** argv) {
bsalomon@google.com4e230682013-01-15 20:37:04 +000087#if SK_ENABLE_INST_COUNT
reed@google.coma2769752012-07-22 22:33:05 +000088 gPrintInstCount = true;
89#endif
90 SkGraphics::Init();
bungeman@google.com5af16f82011-09-02 15:06:44 +000091
bungeman@google.com5af16f82011-09-02 15:06:44 +000092 const char* matchStr = NULL;
93
94 char* const* stop = argv + argc;
95 for (++argv; argv < stop; ++argv) {
djsollen@google.comf4d1b392012-11-29 16:29:58 +000096 if (strcmp(*argv, "--match") == 0) {
bungeman@google.com5af16f82011-09-02 15:06:44 +000097 ++argv;
98 if (argv < stop && **argv) {
99 matchStr = *argv;
100 }
reed@android.comeeb3b7f2009-04-09 04:06:54 +0000101 }
102 }
reed@android.com5e5adfd2009-03-07 03:39:23 +0000103
reed@google.com91d449e2011-10-26 15:25:18 +0000104 {
105 SkString header("Skia UnitTests:");
106 if (matchStr) {
107 header.appendf(" --match %s", matchStr);
108 }
109#ifdef SK_DEBUG
110 header.append(" SK_DEBUG");
111#else
112 header.append(" SK_RELEASE");
113#endif
114#ifdef SK_SCALAR_IS_FIXED
115 header.append(" SK_SCALAR_IS_FIXED");
116#else
117 header.append(" SK_SCALAR_IS_FLOAT");
118#endif
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000119 SkDebugf("%s\n", header.c_str());
reed@google.com91d449e2011-10-26 15:25:18 +0000120 }
121
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000122 DebugfReporter reporter;
reed@android.comed673312009-02-27 16:24:51 +0000123 Iter iter(&reporter);
124 Test* test;
reed@android.com80e39a72009-04-02 16:59:40 +0000125
reed@android.com57b799e2009-04-01 20:26:42 +0000126 const int count = Iter::Count();
127 int index = 0;
bungeman@google.com5af16f82011-09-02 15:06:44 +0000128 int failCount = 0;
129 int skipCount = 0;
reed@android.comed673312009-02-27 16:24:51 +0000130 while ((test = iter.next()) != NULL) {
reed@android.com57b799e2009-04-01 20:26:42 +0000131 reporter.setIndexOfTotal(index, count);
bungeman@google.com5af16f82011-09-02 15:06:44 +0000132 if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
133 ++skipCount;
134 } else {
135 if (!test->run()) {
136 ++failCount;
137 }
138 }
reed@android.comed673312009-02-27 16:24:51 +0000139 SkDELETE(test);
reed@android.com57b799e2009-04-01 20:26:42 +0000140 index += 1;
reed@android.comed673312009-02-27 16:24:51 +0000141 }
reed@android.com57b799e2009-04-01 20:26:42 +0000142
djsollen@google.comf4d1b392012-11-29 16:29:58 +0000143 SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
144 count, failCount, skipCount);
reed@google.coma2769752012-07-22 22:33:05 +0000145
robertphillips@google.combdb1be52012-09-07 18:24:43 +0000146#if SK_SUPPORT_GPU
147
148#if GR_CACHE_STATS
149 GrContext *gr = GpuTest::GetContext();
150
151 gr->printCacheStats();
152#endif
153
154#endif
155
reed@google.coma2769752012-07-22 22:33:05 +0000156 SkGraphics::Term();
djsollen@google.com0945bde2012-11-29 15:28:45 +0000157 GpuTest::DestroyContext();
reed@google.coma2769752012-07-22 22:33:05 +0000158
bungeman@google.com5af16f82011-09-02 15:06:44 +0000159 return (failCount == 0) ? 0 : 1;
reed@android.comed673312009-02-27 16:24:51 +0000160}
caryclark@google.com5987f582012-10-02 18:33:14 +0000161
borenet@google.com7158e6a2012-11-01 17:43:44 +0000162#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
caryclark@google.com5987f582012-10-02 18:33:14 +0000163int main(int argc, char * const argv[]) {
164 return tool_main(argc, (char**) argv);
165}
166#endif
167