Test.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "tests/Test.h"
  8. #include <stdlib.h>
  9. #include "include/core/SkString.h"
  10. #include "include/core/SkTime.h"
  11. #include "tools/flags/CommandLineFlags.h"
  12. static DEFINE_string2(tmpDir, t, nullptr, "Temp directory to use.");
  13. void skiatest::Reporter::bumpTestCount() {}
  14. bool skiatest::Reporter::allowExtendedTest() const { return false; }
  15. bool skiatest::Reporter::verbose() const { return false; }
  16. void skiatest::Reporter::reportFailedWithContext(const skiatest::Failure& f) {
  17. SkString fullMessage = f.message;
  18. if (!fContextStack.empty()) {
  19. fullMessage.append(" [");
  20. for (int i = 0; i < fContextStack.count(); ++i) {
  21. if (i > 0) {
  22. fullMessage.append(", ");
  23. }
  24. fullMessage.append(fContextStack[i]);
  25. }
  26. fullMessage.append("]");
  27. }
  28. this->reportFailed(skiatest::Failure(f.fileName, f.lineNo, f.condition, fullMessage));
  29. }
  30. SkString skiatest::Failure::toString() const {
  31. SkString result = SkStringPrintf("%s:%d\t", this->fileName, this->lineNo);
  32. if (!this->message.isEmpty()) {
  33. result.append(this->message);
  34. if (strlen(this->condition) > 0) {
  35. result.append(": ");
  36. }
  37. }
  38. result.append(this->condition);
  39. return result;
  40. }
  41. SkString skiatest::GetTmpDir() {
  42. if (!FLAGS_tmpDir.isEmpty()) {
  43. return SkString(FLAGS_tmpDir[0]);
  44. }
  45. #ifdef SK_BUILD_FOR_ANDROID
  46. const char* environmentVariable = "TMPDIR";
  47. const char* defaultValue = "/data/local/tmp";
  48. #elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_UNIX)
  49. const char* environmentVariable = "TMPDIR";
  50. const char* defaultValue = "/tmp";
  51. #elif defined(SK_BUILD_FOR_WIN)
  52. const char* environmentVariable = "TEMP";
  53. const char* defaultValue = nullptr;
  54. #else
  55. const char* environmentVariable = nullptr;
  56. const char* defaultValue = nullptr;
  57. #endif
  58. const char* tmpdir = environmentVariable ? getenv(environmentVariable) : nullptr;
  59. return SkString(tmpdir ? tmpdir : defaultValue);
  60. }
  61. skiatest::Timer::Timer() : fStartNanos(SkTime::GetNSecs()) {}
  62. double skiatest::Timer::elapsedNs() const {
  63. return SkTime::GetNSecs() - fStartNanos;
  64. }
  65. double skiatest::Timer::elapsedMs() const { return this->elapsedNs() * 1e-6; }
  66. SkMSec skiatest::Timer::elapsedMsInt() const {
  67. const double elapsedMs = this->elapsedMs();
  68. SkASSERT(SK_MSecMax >= elapsedMs);
  69. return static_cast<SkMSec>(elapsedMs);
  70. }