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 | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 10 | #include "SkRunnable.h" |
| 11 | #include "SkThreadPool.h" |
| 12 | #include "SkTArray.h" |
| 13 | #include "SkTemplates.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 14 | #include "Test.h" |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 15 | #include "SkOSFile.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
| 19 | #endif |
| 20 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 21 | using namespace skiatest; |
| 22 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 23 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 24 | template TestRegistry* TestRegistry::gHead; |
| 25 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 26 | class Iter { |
| 27 | public: |
| 28 | Iter(Reporter* r) : fReporter(r) { |
| 29 | r->ref(); |
| 30 | fReg = TestRegistry::Head(); |
| 31 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 32 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 33 | ~Iter() { |
| 34 | fReporter->unref(); |
| 35 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 36 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 37 | Test* next() { |
| 38 | if (fReg) { |
| 39 | TestRegistry::Factory fact = fReg->factory(); |
| 40 | fReg = fReg->next(); |
| 41 | Test* test = fact(NULL); |
| 42 | test->setReporter(fReporter); |
| 43 | return test; |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 47 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 48 | static int Count() { |
| 49 | const TestRegistry* reg = TestRegistry::Head(); |
| 50 | int count = 0; |
| 51 | while (reg) { |
| 52 | count += 1; |
| 53 | reg = reg->next(); |
| 54 | } |
| 55 | return count; |
| 56 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 57 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 58 | private: |
| 59 | Reporter* fReporter; |
| 60 | const TestRegistry* fReg; |
| 61 | }; |
| 62 | |
| 63 | static const char* result2string(Reporter::Result result) { |
| 64 | return result == Reporter::kPassed ? "passed" : "FAILED"; |
| 65 | } |
| 66 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 67 | class DebugfReporter : public Reporter { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 68 | public: |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 69 | DebugfReporter(bool allowExtendedTest, bool allowThreaded) |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 70 | : fNextIndex(0) |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 71 | , fTotal(0) |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 72 | , fAllowExtendedTest(allowExtendedTest) |
| 73 | , fAllowThreaded(allowThreaded) { |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 74 | } |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 76 | void setTotal(int total) { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 77 | fTotal = total; |
| 78 | } |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 79 | |
| 80 | virtual bool allowExtendedTest() const { |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 81 | return fAllowExtendedTest; |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 84 | virtual bool allowThreaded() const { |
| 85 | return fAllowThreaded; |
| 86 | } |
| 87 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 88 | protected: |
| 89 | virtual void onStart(Test* test) { |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 90 | const int index = sk_atomic_inc(&fNextIndex); |
| 91 | SkDebugf("[%d/%d] %s...\n", index+1, fTotal, test->getName()); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 92 | } |
| 93 | virtual void onReport(const char desc[], Reporter::Result result) { |
djsollen@google.com | f4d1b39 | 2012-11-29 16:29:58 +0000 | [diff] [blame] | 94 | SkDebugf("\t%s: %s\n", result2string(result), desc); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 95 | } |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 96 | |
| 97 | virtual void onEnd(Test* test) { |
| 98 | if (!test->passed()) { |
| 99 | SkDebugf("---- FAILED\n"); |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 102 | |
djsollen@google.com | f4d1b39 | 2012-11-29 16:29:58 +0000 | [diff] [blame] | 103 | private: |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 104 | int32_t fNextIndex; |
| 105 | int fTotal; |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 106 | bool fAllowExtendedTest; |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 107 | bool fAllowThreaded; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 110 | static const char* make_canonical_dir_path(const char* path, SkString* storage) { |
| 111 | if (path) { |
| 112 | // clean it up so it always has a trailing searator |
| 113 | size_t len = strlen(path); |
| 114 | if (0 == len) { |
| 115 | path = NULL; |
| 116 | } else if (SkPATH_SEPARATOR != path[len - 1]) { |
| 117 | // resize to len + 1, to make room for searator |
| 118 | storage->set(path, len + 1); |
| 119 | storage->writable_str()[len] = SkPATH_SEPARATOR; |
| 120 | path = storage->c_str(); |
| 121 | } |
| 122 | } |
| 123 | return path; |
| 124 | } |
| 125 | |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 126 | static SkString gTmpDir; |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 127 | |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 128 | const SkString& Test::GetTmpDir() { |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 129 | return gTmpDir; |
| 130 | } |
| 131 | |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 132 | static SkString gResourcePath; |
| 133 | |
| 134 | const SkString& Test::GetResourcePath() { |
| 135 | return gResourcePath; |
| 136 | } |
| 137 | |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 138 | DEFINE_string2(match, m, NULL, "substring of test name to run."); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 139 | DEFINE_string2(tmpDir, t, NULL, "tmp directory for tests to use."); |
| 140 | DEFINE_string2(resourcePath, i, NULL, "directory for test resources."); |
| 141 | DEFINE_bool2(extendedTest, x, false, "run extended tests for pathOps."); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 142 | DEFINE_bool2(threaded, z, false, "allow tests to use multiple threads internally."); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 143 | DEFINE_bool2(verbose, v, false, "enable verbose output."); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 144 | DEFINE_int32(threads, 8, |
| 145 | "If >0, run threadsafe tests on a threadpool with this many threads."); |
| 146 | |
| 147 | // Deletes self when run. |
| 148 | class SkTestRunnable : public SkRunnable { |
| 149 | public: |
| 150 | // Takes ownership of test. |
| 151 | SkTestRunnable(Test* test, int32_t* failCount) : fTest(test), fFailCount(failCount) {} |
| 152 | |
| 153 | virtual void run() { |
| 154 | fTest->run(); |
| 155 | if(!fTest->passed()) { |
| 156 | sk_atomic_inc(fFailCount); |
| 157 | } |
| 158 | SkDELETE(this); |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | SkAutoTDelete<Test> fTest; |
| 163 | int32_t* fFailCount; |
| 164 | }; |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 165 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 166 | int tool_main(int argc, char** argv); |
| 167 | int tool_main(int argc, char** argv) { |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 168 | SkCommandLineFlags::SetUsage(""); |
| 169 | SkCommandLineFlags::Parse(argc, argv); |
| 170 | |
| 171 | if (!FLAGS_tmpDir.isEmpty()) { |
| 172 | make_canonical_dir_path(FLAGS_tmpDir[0], &gTmpDir); |
| 173 | } |
| 174 | if (!FLAGS_resourcePath.isEmpty()) { |
| 175 | make_canonical_dir_path(FLAGS_resourcePath[0], &gResourcePath); |
| 176 | } |
| 177 | |
bsalomon@google.com | 4e23068 | 2013-01-15 20:37:04 +0000 | [diff] [blame] | 178 | #if SK_ENABLE_INST_COUNT |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 179 | gPrintInstCount = true; |
| 180 | #endif |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 181 | |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 182 | SkGraphics::Init(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 183 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 184 | { |
| 185 | SkString header("Skia UnitTests:"); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 186 | if (!FLAGS_match.isEmpty()) { |
| 187 | header.appendf(" --match %s", FLAGS_match[0]); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 188 | } |
djsollen@google.com | cb62650 | 2013-03-20 13:48:20 +0000 | [diff] [blame] | 189 | if (!gTmpDir.isEmpty()) { |
| 190 | header.appendf(" --tmpDir %s", gTmpDir.c_str()); |
| 191 | } |
| 192 | if (!gResourcePath.isEmpty()) { |
| 193 | header.appendf(" --resourcePath %s", gResourcePath.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 194 | } |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 195 | #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 |
djsollen@google.com | f4d1b39 | 2012-11-29 16:29:58 +0000 | [diff] [blame] | 205 | SkDebugf("%s\n", header.c_str()); |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 206 | } |
| 207 | |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 208 | DebugfReporter reporter(FLAGS_extendedTest, FLAGS_threaded); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 209 | Iter iter(&reporter); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 210 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 211 | const int count = Iter::Count(); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 212 | reporter.setTotal(count); |
| 213 | int32_t failCount = 0; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 214 | int skipCount = 0; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 215 | |
| 216 | SkAutoTDelete<SkThreadPool> threadpool(SkNEW_ARGS(SkThreadPool, (FLAGS_threads))); |
| 217 | SkTArray<Test*> unsafeTests; // Always passes ownership to an SkTestRunnable |
| 218 | for (int i = 0; i < count; i++) { |
| 219 | SkAutoTDelete<Test> test(iter.next()); |
reed@google.com | 9aff148 | 2013-04-11 18:27:52 +0000 | [diff] [blame] | 220 | if (!FLAGS_match.isEmpty() && !strstr(test->getName(), FLAGS_match[0])) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 221 | ++skipCount; |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 222 | } else if (!test->isThreadsafe()) { |
| 223 | unsafeTests.push_back() = test.detach(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 224 | } else { |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 225 | threadpool->add(SkNEW_ARGS(SkTestRunnable, (test.detach(), &failCount))); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 226 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 227 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 228 | |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 229 | // Run the tests that aren't threadsafe. |
| 230 | for (int i = 0; i < unsafeTests.count(); i++) { |
| 231 | SkNEW_ARGS(SkTestRunnable, (unsafeTests[i], &failCount))->run(); |
| 232 | } |
| 233 | |
| 234 | // Blocks until threaded tests finish. |
| 235 | threadpool.free(); |
| 236 | |
djsollen@google.com | f4d1b39 | 2012-11-29 16:29:58 +0000 | [diff] [blame] | 237 | SkDebugf("Finished %d tests, %d failures, %d skipped.\n", |
| 238 | count, failCount, skipCount); |
commit-bot@chromium.org | 197845a | 2013-04-19 13:24:28 +0000 | [diff] [blame^] | 239 | const int testCount = reporter.countTests(); |
commit-bot@chromium.org | ba59d64 | 2013-04-11 16:54:09 +0000 | [diff] [blame] | 240 | if (FLAGS_verbose && testCount > 0) { |
caryclark@google.com | d54e1e9 | 2013-04-10 15:57:31 +0000 | [diff] [blame] | 241 | SkDebugf("Ran %d Internal tests.\n", testCount); |
| 242 | } |
robertphillips@google.com | bdb1be5 | 2012-09-07 18:24:43 +0000 | [diff] [blame] | 243 | #if SK_SUPPORT_GPU |
| 244 | |
| 245 | #if GR_CACHE_STATS |
| 246 | GrContext *gr = GpuTest::GetContext(); |
| 247 | |
| 248 | gr->printCacheStats(); |
| 249 | #endif |
| 250 | |
| 251 | #endif |
| 252 | |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 253 | SkGraphics::Term(); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 254 | GpuTest::DestroyContexts(); |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 255 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 256 | return (failCount == 0) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 257 | } |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 258 | |
borenet@google.com | 7158e6a | 2012-11-01 17:43:44 +0000 | [diff] [blame] | 259 | #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 260 | int main(int argc, char * const argv[]) { |
| 261 | return tool_main(argc, (char**) argv); |
| 262 | } |
| 263 | #endif |