InterpolatorTest.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2014 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 "include/utils/SkInterpolator.h"
  8. #include "tests/Test.h"
  9. static SkScalar* iset(SkScalar array[3], int a, int b, int c) {
  10. array[0] = SkIntToScalar(a);
  11. array[1] = SkIntToScalar(b);
  12. array[2] = SkIntToScalar(c);
  13. return array;
  14. }
  15. DEF_TEST(Interpolator, reporter) {
  16. SkInterpolator inter(3, 2);
  17. SkScalar v1[3], v2[3], v[3];
  18. SkInterpolator::Result result;
  19. inter.setKeyFrame(0, 100, iset(v1, 10, 20, 30), 0);
  20. inter.setKeyFrame(1, 200, iset(v2, 110, 220, 330));
  21. result = inter.timeToValues(0, v);
  22. REPORTER_ASSERT(reporter, result == SkInterpolator::kFreezeStart_Result);
  23. REPORTER_ASSERT(reporter, memcmp(v, v1, sizeof(v)) == 0);
  24. result = inter.timeToValues(99, v);
  25. REPORTER_ASSERT(reporter, result == SkInterpolator::kFreezeStart_Result);
  26. REPORTER_ASSERT(reporter, memcmp(v, v1, sizeof(v)) == 0);
  27. result = inter.timeToValues(100, v);
  28. REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
  29. REPORTER_ASSERT(reporter, memcmp(v, v1, sizeof(v)) == 0);
  30. result = inter.timeToValues(200, v);
  31. REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
  32. REPORTER_ASSERT(reporter, memcmp(v, v2, sizeof(v)) == 0);
  33. result = inter.timeToValues(201, v);
  34. REPORTER_ASSERT(reporter, result == SkInterpolator::kFreezeEnd_Result);
  35. REPORTER_ASSERT(reporter, memcmp(v, v2, sizeof(v)) == 0);
  36. result = inter.timeToValues(150, v);
  37. REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
  38. // Found failing when we re-enabled this test:
  39. #if 0
  40. SkScalar vv[3];
  41. REPORTER_ASSERT(reporter, memcmp(v, iset(vv, 60, 120, 180), sizeof(v)) == 0);
  42. #endif
  43. result = inter.timeToValues(125, v);
  44. REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
  45. result = inter.timeToValues(175, v);
  46. REPORTER_ASSERT(reporter, result == SkInterpolator::kNormal_Result);
  47. for (SkScalar val = -0.1f; val <= 1.1f; val += 0.1f) {
  48. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkTPin(0.f, val, 1.f),
  49. SkUnitCubicInterp(val, 1.f/3, 1.f/3, 2.f/3, 2.f/3)));
  50. }
  51. // These numbers come from
  52. // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag.
  53. const SkScalar testTransitions[][4] = {
  54. { 0.25f, 0.1f, 0.25f, 1 }, // ease
  55. { 0.42f, 0, 1, 1 }, // ease in
  56. { 0, 0, 0.58f, 1 }, // ease out
  57. { 0.42f, 0, 0.58f, 1 }, // ease in out
  58. };
  59. const SkScalar expectedOutput[][5] = {
  60. { 0.0947876f, 0.513367f, 0.80249f, 0.940796f, 0.994263f }, // ease
  61. { 0.0170288f, 0.129639f, 0.31543f, 0.554749f, 0.839417f }, // ease in
  62. { 0.160583f, 0.445251f, 0.684692f, 0.870361f, 0.982971f }, // ease out
  63. { 0.0197144f, 0.187439f, 0.500122f, 0.812561f, 0.980286f }, // ease in out
  64. };
  65. int i = 0;
  66. for (const SkScalar* t : testTransitions) {
  67. int j = 0;
  68. for (SkScalar val = 0.1f; val < 1; val += 0.2f) {
  69. REPORTER_ASSERT(reporter, SkScalarNearlyEqual(expectedOutput[i][j++],
  70. SkUnitCubicInterp(val, t[0], t[1], t[2], t[3])));
  71. }
  72. ++i;
  73. }
  74. }