xref: /aosp_15_r20/external/skia/tests/Test.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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  */
7 
8 #include "tests/Test.h"
9 
10 #include "include/core/SkString.h"
11 #include "src/base/SkTime.h"
12 #include "tools/flags/CommandLineFlags.h"
13 #include "tools/timer/TimeUtils.h"
14 
15 #include <cstdlib>
16 #include <cstring>
17 
18 static DEFINE_string2(tmpDir, t, nullptr, "Temp directory to use.");
19 
bumpTestCount()20 void skiatest::Reporter::bumpTestCount() {}
21 
allowExtendedTest() const22 bool skiatest::Reporter::allowExtendedTest() const { return false; }
23 
verbose() const24 bool skiatest::Reporter::verbose() const { return false; }
25 
26 template skiatest::TestRegistry* skiatest::TestRegistry::gHead;
27 
reportFailedWithContext(const skiatest::Failure & f)28 void skiatest::Reporter::reportFailedWithContext(const skiatest::Failure& f) {
29     SkString fullMessage = f.message;
30     if (!fContextStack.empty()) {
31         fullMessage.append(" [");
32         for (int i = 0; i < fContextStack.size(); ++i) {
33             if (i > 0) {
34                 fullMessage.append(", ");
35             }
36             fullMessage.append(fContextStack[i]);
37         }
38         fullMessage.append("]");
39     }
40     this->reportFailed(skiatest::Failure(f.fileName, f.lineNo, f.condition, fullMessage));
41 }
42 
toString() const43 SkString skiatest::Failure::toString() const {
44     SkString result = SkStringPrintf("%s:%d\t", this->fileName, this->lineNo);
45     if (!this->message.isEmpty()) {
46         result.append(this->message);
47         if (strlen(this->condition) > 0) {
48             result.append(": ");
49         }
50     }
51     result.append(this->condition);
52     return result;
53 }
54 
GetTmpDir()55 SkString skiatest::GetTmpDir() {
56     if (!FLAGS_tmpDir.isEmpty()) {
57         return SkString(FLAGS_tmpDir[0]);
58     }
59 #ifdef SK_BUILD_FOR_ANDROID
60     const char* environmentVariable = "TMPDIR";
61     const char* defaultValue = "/data/local/tmp";
62 #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX)
63     const char* environmentVariable = "TMPDIR";
64     const char* defaultValue = "/tmp";
65 #elif defined(SK_BUILD_FOR_WIN)
66     const char* environmentVariable = "TEMP";
67     const char* defaultValue = nullptr;
68 #else
69     const char* environmentVariable = nullptr;
70     const char* defaultValue = nullptr;
71 #endif
72     const char* tmpdir = environmentVariable ? getenv(environmentVariable) : nullptr;
73     return SkString(tmpdir ? tmpdir : defaultValue);
74 }
75 
Timer()76 skiatest::Timer::Timer() : fStartNanos(SkTime::GetNSecs()) {}
77 
elapsedNs() const78 double skiatest::Timer::elapsedNs() const {
79     return SkTime::GetNSecs() - fStartNanos;
80 }
81 
elapsedMs() const82 double skiatest::Timer::elapsedMs() const { return this->elapsedNs() * 1e-6; }
83 
elapsedMsInt() const84 TimeUtils::MSec skiatest::Timer::elapsedMsInt() const {
85     const double elapsedMs = this->elapsedMs();
86     SkASSERT(TimeUtils::MSecMax >= elapsedMs);
87     return static_cast<TimeUtils::MSec>(elapsedMs);
88 }
89