skia_test.cpp revision 789c6f291ecfff925086015360da525d6de1c835
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 */
8#include "SkGraphics.h"
9#include "Test.h"
10#include "SkOSFile.h"
11
12#if SK_SUPPORT_GPU
13#include "GrContext.h"
14#endif
15
16using namespace skiatest;
17
18// need to explicitly declare this, or we get some weird infinite loop llist
19template TestRegistry* TestRegistry::gHead;
20
21class Iter {
22public:
23    Iter(Reporter* r) : fReporter(r) {
24        r->ref();
25        fReg = TestRegistry::Head();
26    }
27
28    ~Iter() {
29        fReporter->unref();
30    }
31
32    Test* next() {
33        if (fReg) {
34            TestRegistry::Factory fact = fReg->factory();
35            fReg = fReg->next();
36            Test* test = fact(NULL);
37            test->setReporter(fReporter);
38            return test;
39        }
40        return NULL;
41    }
42
43    static int Count() {
44        const TestRegistry* reg = TestRegistry::Head();
45        int count = 0;
46        while (reg) {
47            count += 1;
48            reg = reg->next();
49        }
50        return count;
51    }
52
53private:
54    Reporter* fReporter;
55    const TestRegistry* fReg;
56};
57
58static const char* result2string(Reporter::Result result) {
59    return result == Reporter::kPassed ? "passed" : "FAILED";
60}
61
62class DebugfReporter : public Reporter {
63public:
64    DebugfReporter() : fIndex(0), fTotal(0) {}
65
66    void setIndexOfTotal(int index, int total) {
67        fIndex = index;
68        fTotal = total;
69    }
70protected:
71    virtual void onStart(Test* test) {
72        SkDebugf("[%d/%d] %s...\n", fIndex+1, fTotal, test->getName());
73    }
74    virtual void onReport(const char desc[], Reporter::Result result) {
75        SkDebugf("\t%s: %s\n", result2string(result), desc);
76    }
77    virtual void onEnd(Test* test) {
78        if (!this->getCurrSuccess()) {
79            SkDebugf("---- FAILED\n");
80        }
81    }
82private:
83    int fIndex, fTotal;
84};
85
86static const char* make_canonical_dir_path(const char* path, SkString* storage) {
87    if (path) {
88        // clean it up so it always has a trailing searator
89        size_t len = strlen(path);
90        if (0 == len) {
91            path = NULL;
92        } else if (SkPATH_SEPARATOR != path[len - 1]) {
93            // resize to len + 1, to make room for searator
94            storage->set(path, len + 1);
95            storage->writable_str()[len] = SkPATH_SEPARATOR;
96            path = storage->c_str();
97        }
98    }
99    return path;
100}
101
102static const char* gTmpDir;
103
104const char* Test::GetTmpDir() {
105    return gTmpDir;
106}
107
108int tool_main(int argc, char** argv);
109int tool_main(int argc, char** argv) {
110#if SK_ENABLE_INST_COUNT
111    gPrintInstCount = true;
112#endif
113    SkGraphics::Init();
114
115    const char* matchStr = NULL;
116
117    char* const* stop = argv + argc;
118    for (++argv; argv < stop; ++argv) {
119        if (strcmp(*argv, "--match") == 0) {
120            ++argv;
121            if (argv < stop && **argv) {
122                matchStr = *argv;
123            } else {
124                SkDebugf("no following argument to --match\n");
125                return -1;
126            }
127        } else if (0 == strcmp(*argv, "--tmpDir")) {
128            ++argv;
129            if (argv < stop && **argv) {
130                gTmpDir = *argv;
131            } else {
132                SkDebugf("no following argument to --tmpDir\n");
133                return -1;
134            }
135        }
136    }
137
138    SkString tmpDirStorage;
139    gTmpDir = make_canonical_dir_path(gTmpDir, &tmpDirStorage);
140
141    {
142        SkString header("Skia UnitTests:");
143        if (matchStr) {
144            header.appendf(" --match %s", matchStr);
145        }
146        if (gTmpDir) {
147            header.appendf(" --tmpDir %s", gTmpDir);
148        }
149#ifdef SK_DEBUG
150        header.append(" SK_DEBUG");
151#else
152        header.append(" SK_RELEASE");
153#endif
154#ifdef SK_SCALAR_IS_FIXED
155        header.append(" SK_SCALAR_IS_FIXED");
156#else
157        header.append(" SK_SCALAR_IS_FLOAT");
158#endif
159        SkDebugf("%s\n", header.c_str());
160    }
161
162    DebugfReporter reporter;
163    Iter iter(&reporter);
164    Test* test;
165
166    const int count = Iter::Count();
167    int index = 0;
168    int failCount = 0;
169    int skipCount = 0;
170    while ((test = iter.next()) != NULL) {
171        reporter.setIndexOfTotal(index, count);
172        if (NULL != matchStr && !strstr(test->getName(), matchStr)) {
173            ++skipCount;
174        } else {
175            if (!test->run()) {
176                ++failCount;
177            }
178        }
179        SkDELETE(test);
180        index += 1;
181    }
182
183    SkDebugf("Finished %d tests, %d failures, %d skipped.\n",
184             count, failCount, skipCount);
185
186#if SK_SUPPORT_GPU
187
188#if GR_CACHE_STATS
189    GrContext *gr = GpuTest::GetContext();
190
191    gr->printCacheStats();
192#endif
193
194#endif
195
196    SkGraphics::Term();
197    GpuTest::DestroyContexts();
198
199    return (failCount == 0) ? 0 : 1;
200}
201
202#if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
203int main(int argc, char * const argv[]) {
204    return tool_main(argc, (char**) argv);
205}
206#endif
207