epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 7 | |
scroggo@google.com | 5a6324e | 2013-04-11 20:11:40 +0000 | [diff] [blame] | 8 | #include "SkCommandLineFlags.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 9 | #include "SkGraphics.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 10 | #include "SkOSFile.h" |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 11 | #include "SkTArray.h" |
| 12 | #include "SkTemplates.h" |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 13 | #include "SkThreadPool.h" |
| 14 | #include "SkTime.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 15 | #include "Test.h" |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 16 | #include "OverwriteLine.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 17 | |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContext.h" |
| 20 | #endif |
| 21 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 22 | using namespace skiatest; |
| 23 | |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 24 | DEFINE_string2(match, m, NULL, "[~][^]substring[$] [...] of test name to run.\n" \ |
| 25 | "Multiple matches may be separated by spaces.\n" \ |
| 26 | "~ causes a matching test to always be skipped\n" \ |
| 27 | "^ requires the start of the test to match\n" \ |
| 28 | "$ requires the end of the test to match\n" \ |
| 29 | "^ and $ requires an exact match\n" \ |
| 30 | "If a test does not match any list entry,\n" \ |
| 31 | "it is skipped unless some list entry starts with ~"); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 32 | DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use."); |
commit-bot@chromium.org | f9a2759 | 2013-10-29 19:50:39 +0000 | [diff] [blame] | 33 | DEFINE_string2(resourcePath, i, "resources", "directory for test resources."); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 34 | DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps."); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 35 | DEFINE_bool2(single, z, false, "run tests on a single thread internally."); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 36 | DEFINE_bool2(verbose, v, false, "enable verbose output."); |
commit-bot@chromium.org | 44c661f | 2013-04-22 15:23:14 +0000 | [diff] [blame] | 37 | DEFINE_int32(threads, SkThreadPool::kThreadPerCore, |
| 38 | "Run threadsafe tests on a threadpool with this many threads."); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 39 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 40 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 41 | template TestRegistry* TestRegistry::gHead; |
| 42 | |
| 43 | class Iter { |
| 44 | public: |
| 45 | Iter() { this->reset(); } |
| 46 | void reset() { fReg = TestRegistry::Head(); } |
| 47 | |
| 48 | Test* next(Reporter* r) { |
| 49 | if (fReg) { |
| 50 | TestRegistry::Factory fact = fReg->factory(); |
| 51 | fReg = fReg->next(); |
| 52 | Test* test = fact(NULL); |
| 53 | test->setReporter(r); |
| 54 | return test; |
| 55 | } |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | const TestRegistry* fReg; |
| 61 | }; |
| 62 | |
| 63 | class DebugfReporter : public Reporter { |
| 64 | public: |
| 65 | explicit DebugfReporter(int total) : fDone(0), fTotal(total) {} |
| 66 | |
| 67 | virtual bool allowExtendedTest() const SK_OVERRIDE { return FLAGS_extendedTest; } |
| 68 | virtual bool allowThreaded() const SK_OVERRIDE { return !FLAGS_single; } |
| 69 | virtual bool verbose() const SK_OVERRIDE { return FLAGS_verbose; } |
| 70 | |
| 71 | protected: |
| 72 | virtual void onReportFailed(const SkString& desc) SK_OVERRIDE { |
| 73 | SkDebugf("\nFAILED: %s", desc.c_str()); |
| 74 | } |
| 75 | |
| 76 | virtual void onEnd(Test* test) SK_OVERRIDE { |
| 77 | const int done = 1 + sk_atomic_inc(&fDone); |
| 78 | |
| 79 | if (!test->passed()) { |
| 80 | SkDebugf("\n---- %s FAILED", test->getName()); |
| 81 | } |
| 82 | |
| 83 | SkString prefix(kSkOverwriteLine); |
| 84 | SkString time; |
| 85 | if (FLAGS_verbose) { |
| 86 | prefix.printf("\n"); |
| 87 | time.printf("%5dms ", test->elapsedMs()); |
| 88 | } |
| 89 | SkDebugf("%s[%3d/%3d] %s%s", prefix.c_str(), done, fTotal, time.c_str(), test->getName()); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | int32_t fDone; // atomic |
| 94 | const int fTotal; |
| 95 | }; |
| 96 | |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 97 | SkString Test::GetTmpDir() { |
| 98 | const char* tmpDir = FLAGS_tmpDir.isEmpty() ? NULL : FLAGS_tmpDir[0]; |
| 99 | return SkString(tmpDir); |
| 100 | } |
| 101 | |
| 102 | SkString Test::GetResourcePath() { |
| 103 | const char* resourcePath = FLAGS_resourcePath.isEmpty() ? NULL : FLAGS_resourcePath[0]; |
| 104 | return SkString(resourcePath); |
| 105 | } |
| 106 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 107 | // Deletes self when run. |
| 108 | class SkTestRunnable : public SkRunnable { |
| 109 | public: |
| 110 | // Takes ownership of test. |
| 111 | SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {} |
| 112 | |
| 113 | virtual void run() { |
| 114 | fTest->run(); |
| 115 | if(!fTest->passed()) { |
| 116 | sk_atomic_inc(fFailCount); |
| 117 | } |
| 118 | SkDELETE(this); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | SkAutoTDelete<Test> fTest; |
| 123 | int32_t* fFailCount; |
| 124 | }; |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 125 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 126 | int tool_main(int argc, char** argv); |
| 127 | int tool_main(int argc, char** argv) { |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 128 | SkCommandLineFlags::SetUsage(""); |
| 129 | SkCommandLineFlags::Parse(argc, argv); |
| 130 | |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 131 | #if SK_ENABLE_INST_COUNT |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 132 | gPrintInstCount = true; |
| 133 | #endif |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 134 | |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 135 | SkGraphics::Init(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 136 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 137 | { |
| 138 | SkString header("Skia UnitTests:"); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 139 | if (!FLAGS_match.isEmpty()) { |
caryclark@google.com | b631eec | 2013-05-02 13:14:40 +0000 | [diff] [blame] | 140 | header.appendf(" --match"); |
| 141 | for (int index = 0; index < FLAGS_match.count(); ++index) { |
| 142 | header.appendf(" %s", FLAGS_match[index]); |
| 143 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 144 | } |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 145 | SkString tmpDir = Test::GetTmpDir(); |
| 146 | if (!tmpDir.isEmpty()) { |
| 147 | header.appendf(" --tmpDir %s", tmpDir.c_str()); |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 148 | } |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 149 | SkString resourcePath = Test::GetResourcePath(); |
| 150 | if (!resourcePath.isEmpty()) { |
| 151 | header.appendf(" --resourcePath %s", resourcePath.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 152 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 153 | #ifdef SK_DEBUG |
| 154 | header.append(" SK_DEBUG"); |
| 155 | #else |
| 156 | header.append(" SK_RELEASE"); |
| 157 | #endif |
reed@google.com | 5696baa | 2013-08-29 20:20:39 +0000 | [diff] [blame] | 158 | header.appendf(" skia_arch_width=%d", (int)sizeof(void*) * 8); |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 159 | SkDebugf(header.c_str()); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 160 | } |
| 161 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 162 | |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 163 | // Count tests first. |
| 164 | int total = 0; |
| 165 | int toRun = 0; |
| 166 | Test* test; |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 167 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 168 | Iter iter; |
| 169 | while ((test = iter.next(NULL/*reporter not needed*/)) != NULL) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 170 | SkAutoTDelete<Test> owned(test); |
sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 171 | |
commit-bot@chromium.org | a6f37e7 | 2013-08-30 15:52:46 +0000 | [diff] [blame] | 172 | if(!SkCommandLineFlags::ShouldSkip(FLAGS_match, test->getName())) { |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 173 | toRun++; |
| 174 | } |
| 175 | total++; |
| 176 | } |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 177 | |
| 178 | // Now run them. |
| 179 | iter.reset(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 180 | int32_t failCount = 0; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 181 | int skipCount = 0; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 182 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 183 | SkThreadPool threadpool(FLAGS_threads); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 184 | SkTArray<Test*> unsafeTests; // Always passes ownership to an SkTestRunnable |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 185 | |
| 186 | DebugfReporter reporter(toRun); |
commit-bot@chromium.org | 0506b9d | 2013-04-22 16:43:07 +0000 | [diff] [blame] | 187 | for (int i = 0; i < total; i++) { |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 188 | SkAutoTDelete<Test> test(iter.next(&reporter)); |
commit-bot@chromium.org | a6f37e7 | 2013-08-30 15:52:46 +0000 | [diff] [blame] | 189 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, test->getName())) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 190 | ++skipCount; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 191 | } else if (!test->isThreadsafe()) { |
| 192 | unsafeTests.push_back() = test.detach(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 193 | } else { |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 194 | threadpool.add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount))); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 195 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 196 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 197 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 198 | // Run the tests that aren't threadsafe. |
| 199 | for (int i = 0; i < unsafeTests.count(); i++) { |
| 200 | SkNEW_ARGS(SkTestRunnable, (unsafeTests[i], &failCount))->run(); |
| 201 | } |
| 202 | |
commit-bot@chromium.org | a7538ba | 2013-10-10 18:49:04 +0000 | [diff] [blame] | 203 | // Block until threaded tests finish. |
| 204 | threadpool.wait(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame] | 205 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 206 | if (FLAGS_verbose) { |
| 207 | SkDebugf("\nFinished %d tests, %d failures, %d skipped. (%d internal tests)", |
| 208 | toRun, failCount, skipCount, reporter.countTests()); |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 209 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 210 | SkGraphics::Term(); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 211 | GpuTest::DestroyContexts(); |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 212 | |
commit-bot@chromium.org | 261c666 | 2014-01-02 16:19:53 +0000 | [diff] [blame] | 213 | SkDebugf("\n"); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 214 | return (failCount == 0) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 215 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 216 | |
borenet@google.com | 7158e6a | 2012-11-01 17:43:44 +0000 | [diff] [blame] | 217 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 218 | int main(int argc, char * const argv[]) { |
| 219 | return tool_main(argc, (char**) argv); |
| 220 | } |
| 221 | #endif |