epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 8 | #include "SkGraphics.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 9 | #include "Test.h" |
| 10 | |
| 11 | using namespace skiatest; |
| 12 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 13 | // need to explicitly declare this, or we get some weird infinite loop llist |
| 14 | template TestRegistry* TestRegistry::gHead; |
| 15 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 16 | class Iter { |
| 17 | public: |
| 18 | Iter(Reporter* r) : fReporter(r) { |
| 19 | r->ref(); |
| 20 | fReg = TestRegistry::Head(); |
| 21 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 22 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 23 | ~Iter() { |
| 24 | fReporter->unref(); |
| 25 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 26 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 27 | Test* next() { |
| 28 | if (fReg) { |
| 29 | TestRegistry::Factory fact = fReg->factory(); |
| 30 | fReg = fReg->next(); |
| 31 | Test* test = fact(NULL); |
| 32 | test->setReporter(fReporter); |
| 33 | return test; |
| 34 | } |
| 35 | return NULL; |
| 36 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 37 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 38 | static int Count() { |
| 39 | const TestRegistry* reg = TestRegistry::Head(); |
| 40 | int count = 0; |
| 41 | while (reg) { |
| 42 | count += 1; |
| 43 | reg = reg->next(); |
| 44 | } |
| 45 | return count; |
| 46 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 47 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 48 | private: |
| 49 | Reporter* fReporter; |
| 50 | const TestRegistry* fReg; |
| 51 | }; |
| 52 | |
| 53 | static const char* result2string(Reporter::Result result) { |
| 54 | return result == Reporter::kPassed ? "passed" : "FAILED"; |
| 55 | } |
| 56 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 57 | class DebugfReporter : public Reporter { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 58 | public: |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 59 | DebugfReporter(bool androidMode) : fAndroidMode(androidMode) {} |
| 60 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 61 | void setIndexOfTotal(int index, int total) { |
| 62 | fIndex = index; |
| 63 | fTotal = total; |
| 64 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 65 | protected: |
| 66 | virtual void onStart(Test* test) { |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 67 | this->dumpState(test, kStarting_State); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 68 | } |
| 69 | virtual void onReport(const char desc[], Reporter::Result result) { |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 70 | if (!fAndroidMode) { |
| 71 | SkDebugf("\t%s: %s\n", result2string(result), desc); |
| 72 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 73 | } |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 74 | virtual void onEnd(Test* test) { |
| 75 | this->dumpState(test, this->getCurrSuccess() ? |
| 76 | kSucceeded_State : kFailed_State); |
| 77 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 78 | private: |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 79 | enum State { |
| 80 | kStarting_State = 1, |
| 81 | kSucceeded_State = 0, |
| 82 | kFailed_State = -2 |
| 83 | }; |
| 84 | |
| 85 | void dumpState(Test* test, State state) { |
| 86 | if (fAndroidMode) { |
| 87 | SkDebugf("INSTRUMENTATION_STATUS: test=%s\n", test->getName()); |
| 88 | SkDebugf("INSTRUMENTATION_STATUS: class=com.skia\n"); |
| 89 | SkDebugf("INSTRUMENTATION_STATUS: current=%d\n", fIndex+1); |
| 90 | SkDebugf("INSTRUMENTATION_STATUS: numtests=%d\n", fTotal); |
| 91 | SkDebugf("INSTRUMENTATION_STATUS_CODE: %d\n", state); |
| 92 | } else { |
| 93 | if (kStarting_State == state) { |
| 94 | SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName()); |
| 95 | } else if (kFailed_State == state) { |
| 96 | SkDebugf("---- FAILED\n"); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 101 | int fIndex, fTotal; |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 102 | bool fAndroidMode; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | int main (int argc, char * const argv[]) { |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 106 | #ifdef SK_ENABLE_INST_COUNT |
| 107 | gPrintInstCount = true; |
| 108 | #endif |
| 109 | SkGraphics::Init(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 110 | |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 111 | bool androidMode = false; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 112 | const char* matchStr = NULL; |
| 113 | |
| 114 | char* const* stop = argv + argc; |
| 115 | for (++argv; argv < stop; ++argv) { |
| 116 | if (strcmp(*argv, "-android") == 0) { |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 117 | androidMode = true; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 118 | |
| 119 | } else if (strcmp(*argv, "--match") == 0) { |
| 120 | ++argv; |
| 121 | if (argv < stop && **argv) { |
| 122 | matchStr = *argv; |
| 123 | } |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 126 | |
reed@google.com | 91d449e | 2011-10-26 15:25:18 +0000 | [diff] [blame] | 127 | { |
| 128 | SkString header("Skia UnitTests:"); |
| 129 | if (matchStr) { |
| 130 | header.appendf(" --match %s", matchStr); |
| 131 | } |
| 132 | #ifdef SK_DEBUG |
| 133 | header.append(" SK_DEBUG"); |
| 134 | #else |
| 135 | header.append(" SK_RELEASE"); |
| 136 | #endif |
| 137 | #ifdef SK_SCALAR_IS_FIXED |
| 138 | header.append(" SK_SCALAR_IS_FIXED"); |
| 139 | #else |
| 140 | header.append(" SK_SCALAR_IS_FLOAT"); |
| 141 | #endif |
| 142 | if (!androidMode) { |
| 143 | SkDebugf("%s\n", header.c_str()); |
| 144 | } |
| 145 | } |
| 146 | |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 147 | DebugfReporter reporter(androidMode); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 148 | Iter iter(&reporter); |
| 149 | Test* test; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 150 | |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 151 | const int count = Iter::Count(); |
| 152 | int index = 0; |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 153 | int failCount = 0; |
| 154 | int skipCount = 0; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 155 | while ((test = iter.next()) != NULL) { |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 156 | reporter.setIndexOfTotal(index, count); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 157 | if (NULL != matchStr && !strstr(test->getName(), matchStr)) { |
| 158 | ++skipCount; |
| 159 | } else { |
| 160 | if (!test->run()) { |
| 161 | ++failCount; |
| 162 | } |
| 163 | } |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 164 | SkDELETE(test); |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 165 | index += 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 166 | } |
reed@android.com | 57b799e | 2009-04-01 20:26:42 +0000 | [diff] [blame] | 167 | |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 168 | if (!androidMode) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 169 | SkDebugf("Finished %d tests, %d failures, %d skipped.\n", |
| 170 | count, failCount, skipCount); |
reed@android.com | eeb3b7f | 2009-04-09 04:06:54 +0000 | [diff] [blame] | 171 | } |
reed@google.com | a276975 | 2012-07-22 22:33:05 +0000 | [diff] [blame] | 172 | |
| 173 | SkGraphics::Term(); |
| 174 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 175 | return (failCount == 0) ? 0 : 1; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 176 | } |