PointTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Unit tests for src/core/SkPoint.cpp and its header
  8. #include "include/core/SkRect.h"
  9. #include "src/core/SkPointPriv.h"
  10. #include "tests/Test.h"
  11. static void test_casts(skiatest::Reporter* reporter) {
  12. SkPoint p = { 0, 0 };
  13. SkRect r = { 0, 0, 0, 0 };
  14. const SkScalar* pPtr = reinterpret_cast<const SkScalar*>(&p);
  15. const SkScalar* rPtr = reinterpret_cast<const SkScalar*>(&r);
  16. REPORTER_ASSERT(reporter, SkPointPriv::AsScalars(p) == pPtr);
  17. REPORTER_ASSERT(reporter, r.asScalars() == rPtr);
  18. }
  19. // Tests SkPoint::Normalize() for this (x,y)
  20. static void test_Normalize(skiatest::Reporter* reporter,
  21. SkScalar x, SkScalar y) {
  22. SkPoint point;
  23. point.set(x, y);
  24. SkScalar oldLength = point.length();
  25. SkScalar returned = SkPoint::Normalize(&point);
  26. SkScalar newLength = point.length();
  27. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(returned, oldLength));
  28. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(newLength, SK_Scalar1));
  29. }
  30. static void test_normalize_cannormalize_consistent(skiatest::Reporter* reporter) {
  31. const SkScalar values[] = { 1, 1e18f, 1e20f, 1e38f, SK_ScalarInfinity, SK_ScalarNaN };
  32. for (SkScalar val : values) {
  33. const SkScalar variants[] = { val, -val, SkScalarInvert(val), -SkScalarInvert(val) };
  34. for (SkScalar v : variants) {
  35. const SkPoint pts[] = { { 0, v }, { v, 0 }, { 1, v }, { v, 1 }, { v, v } };
  36. for (SkPoint p : pts) {
  37. bool can = SkPointPriv::CanNormalize(p.fX, p.fY);
  38. bool nor = p.normalize();
  39. REPORTER_ASSERT(reporter, can == nor);
  40. }
  41. }
  42. }
  43. }
  44. // Tests that SkPoint::length() and SkPoint::Length() both return
  45. // approximately expectedLength for this (x,y).
  46. static void test_length(skiatest::Reporter* reporter, SkScalar x, SkScalar y,
  47. SkScalar expectedLength) {
  48. SkPoint point;
  49. point.set(x, y);
  50. SkScalar s1 = point.length();
  51. SkScalar s2 = SkPoint::Length(x, y);
  52. //The following should be exactly the same, but need not be.
  53. //See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
  54. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, s2));
  55. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(s1, expectedLength));
  56. test_Normalize(reporter, x, y);
  57. }
  58. // Ugh. Windows compiler can dive into other .cpp files, and sometimes
  59. // notices that I will generate an overflow... which is exactly the point
  60. // of this test!
  61. //
  62. // To avoid this warning, I need to convince the compiler that I might not
  63. // use that big value, hence this hacky helper function: reporter is
  64. // ALWAYS non-null. (shhhhhh, don't tell the compiler that).
  65. template <typename T> T get_value(skiatest::Reporter* reporter, T value) {
  66. return reporter ? value : 0;
  67. }
  68. // On linux gcc, 32bit, we are seeing the compiler propagate up the value
  69. // of SkPoint::length() as a double (which we use sometimes to avoid overflow
  70. // during the computation), even though the signature says float (SkScalar).
  71. //
  72. // force_as_float is meant to capture our latest technique (horrible as
  73. // it is) to force the value to be a float, so we can test whether it was
  74. // finite or not.
  75. static float force_as_float(skiatest::Reporter* reporter, float value) {
  76. uint32_t storage;
  77. memcpy(&storage, &value, 4);
  78. // even the pair of memcpy calls are not sufficient, since those seem to
  79. // be no-op'd, so we add a runtime tests (just like get_value) to force
  80. // the compiler to give us an actual float.
  81. if (nullptr == reporter) {
  82. storage = ~storage;
  83. }
  84. memcpy(&value, &storage, 4);
  85. return value;
  86. }
  87. // test that we handle very large values correctly. i.e. that we can
  88. // successfully normalize something whose mag overflows a float.
  89. static void test_overflow(skiatest::Reporter* reporter) {
  90. SkScalar bigFloat = get_value(reporter, 3.4e38f);
  91. SkPoint pt = { bigFloat, bigFloat };
  92. SkScalar length = pt.length();
  93. length = force_as_float(reporter, length);
  94. // expect this to be non-finite, but dump the results if not.
  95. if (SkScalarIsFinite(length)) {
  96. SkDebugf("length(%g, %g) == %g\n", pt.fX, pt.fY, length);
  97. REPORTER_ASSERT(reporter, !SkScalarIsFinite(length));
  98. }
  99. // this should succeed, even though we can't represent length
  100. REPORTER_ASSERT(reporter, pt.setLength(SK_Scalar1));
  101. // now that pt is normalized, we check its length
  102. length = pt.length();
  103. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(length, SK_Scalar1));
  104. }
  105. DEF_TEST(Point, reporter) {
  106. test_casts(reporter);
  107. static const struct {
  108. SkScalar fX;
  109. SkScalar fY;
  110. SkScalar fLength;
  111. } gRec[] = {
  112. { SkIntToScalar(3), SkIntToScalar(4), SkIntToScalar(5) },
  113. { 0.6f, 0.8f, SK_Scalar1 },
  114. };
  115. for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
  116. test_length(reporter, gRec[i].fX, gRec[i].fY, gRec[i].fLength);
  117. }
  118. test_overflow(reporter);
  119. test_normalize_cannormalize_consistent(reporter);
  120. }
  121. DEF_TEST(Point_setLengthFast, reporter) {
  122. // Scale a (1,1) point to a bunch of different lengths,
  123. // making sure the slow and fast paths are within 0.1%.
  124. const float tests[] = { 1.0f, 0.0f, 1.0e-37f, 3.4e38f, 42.0f, 0.00012f };
  125. const SkPoint kOne = {1.0f, 1.0f};
  126. for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); i++) {
  127. SkPoint slow = kOne, fast = kOne;
  128. slow.setLength(tests[i]);
  129. SkPointPriv::SetLengthFast(&fast, tests[i]);
  130. if (slow.length() < FLT_MIN && fast.length() < FLT_MIN) continue;
  131. SkScalar ratio = slow.length() / fast.length();
  132. REPORTER_ASSERT(reporter, ratio > 0.999f);
  133. REPORTER_ASSERT(reporter, ratio < 1.001f);
  134. }
  135. }