test_gradient.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**************************************************************************/
  2. /* test_gradient.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_GRADIENT_H
  31. #define TEST_GRADIENT_H
  32. #include "scene/resources/gradient.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestGradient {
  35. TEST_CASE("[Gradient] Default gradient") {
  36. // Black-white gradient.
  37. Ref<Gradient> gradient = memnew(Gradient);
  38. CHECK_MESSAGE(
  39. gradient->get_point_count() == 2,
  40. "Default gradient should contain the expected number of points.");
  41. CHECK_MESSAGE(
  42. gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)),
  43. "Default gradient should return the expected interpolated value at offset 0.0.");
  44. CHECK_MESSAGE(
  45. gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)),
  46. "Default gradient should return the expected interpolated value at offset 0.4.");
  47. CHECK_MESSAGE(
  48. gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)),
  49. "Default gradient should return the expected interpolated value at offset 0.8.");
  50. CHECK_MESSAGE(
  51. gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)),
  52. "Default gradient should return the expected interpolated value at offset 1.0.");
  53. // Out of bounds checks.
  54. CHECK_MESSAGE(
  55. gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)),
  56. "Default gradient should return the expected interpolated value at offset -1.0.");
  57. CHECK_MESSAGE(
  58. gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)),
  59. "Default gradient should return the expected interpolated value at offset 1234.0.");
  60. }
  61. TEST_CASE("[Gradient] Custom gradient (points specified in order)") {
  62. // Red-yellow-green gradient (with overbright green).
  63. Ref<Gradient> gradient = memnew(Gradient);
  64. Vector<Gradient::Point> points;
  65. points.push_back({ 0.0, Color(1, 0, 0) });
  66. points.push_back({ 0.5, Color(1, 1, 0) });
  67. points.push_back({ 1.0, Color(0, 2, 0) });
  68. gradient->set_points(points);
  69. CHECK_MESSAGE(
  70. gradient->get_point_count() == 3,
  71. "Custom gradient should contain the expected number of points.");
  72. CHECK_MESSAGE(
  73. gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)),
  74. "Custom gradient should return the expected interpolated value at offset 0.0.");
  75. CHECK_MESSAGE(
  76. gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)),
  77. "Custom gradient should return the expected interpolated value at offset 0.25.");
  78. CHECK_MESSAGE(
  79. gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)),
  80. "Custom gradient should return the expected interpolated value at offset 0.5.");
  81. CHECK_MESSAGE(
  82. gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)),
  83. "Custom gradient should return the expected interpolated value at offset 0.75.");
  84. CHECK_MESSAGE(
  85. gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)),
  86. "Custom gradient should return the expected interpolated value at offset 1.0.");
  87. gradient->remove_point(1);
  88. CHECK_MESSAGE(
  89. gradient->get_point_count() == 2,
  90. "Custom gradient should contain the expected number of points after removing one point.");
  91. CHECK_MESSAGE(
  92. gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)),
  93. "Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1.");
  94. }
  95. TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") {
  96. // HSL rainbow with points specified out of order.
  97. // These should be sorted automatically when adding points.
  98. Ref<Gradient> gradient = memnew(Gradient);
  99. Vector<Gradient::Point> points;
  100. points.push_back({ 0.2, Color(1, 0, 0) });
  101. points.push_back({ 0.0, Color(1, 1, 0) });
  102. points.push_back({ 0.8, Color(0, 1, 0) });
  103. points.push_back({ 0.4, Color(0, 1, 1) });
  104. points.push_back({ 1.0, Color(0, 0, 1) });
  105. points.push_back({ 0.6, Color(1, 0, 1) });
  106. gradient->set_points(points);
  107. CHECK_MESSAGE(
  108. gradient->get_point_count() == 6,
  109. "Custom out-of-order gradient should contain the expected number of points.");
  110. CHECK_MESSAGE(
  111. gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)),
  112. "Custom out-of-order gradient should return the expected interpolated value at offset 0.0.");
  113. CHECK_MESSAGE(
  114. gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)),
  115. "Custom out-of-order gradient should return the expected interpolated value at offset 0.3.");
  116. CHECK_MESSAGE(
  117. gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)),
  118. "Custom out-of-order gradient should return the expected interpolated value at offset 0.6.");
  119. CHECK_MESSAGE(
  120. gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)),
  121. "Custom out-of-order gradient should return the expected interpolated value at offset 1.0.");
  122. gradient->remove_point(0);
  123. CHECK_MESSAGE(
  124. gradient->get_point_count() == 5,
  125. "Custom out-of-order gradient should contain the expected number of points after removing one point.");
  126. // The color will be clamped to the nearest point (which is at offset 0.2).
  127. CHECK_MESSAGE(
  128. gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)),
  129. "Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0.");
  130. }
  131. } // namespace TestGradient
  132. #endif // TEST_GRADIENT_H