Add tests for SkNextPow2
In a follow-up CL, I plan to replaces uses of GrNextPow2 with
SkNextPow2 (since there appears to be no Ganesh-specific logic
in the implementation). There aren't any existing unit tests for
the function, so this adds some.
Change-Id: Ie4e3231b2021664d2d321c4aa11e3fcecaf9e749
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/906956
Reviewed-by: Florin Malita <fmalita@google.com>
diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp
index 45a69c3..84365ff 100644
--- a/tests/MathTest.cpp
+++ b/tests/MathTest.cpp
@@ -788,6 +788,41 @@
}
}
+DEF_TEST(SkNextPow2, reporter) {
+ // start off with some easy-to verify cases and some edge cases.
+ const struct {
+ int fInput;
+ int fExpected;
+ } cases[] = {
+ { 1, 1 },
+ { 2, 2 },
+ { 3, 4 },
+ { 4, 4 },
+ { 5, 8 },
+ { 1073741822, 1073741824},
+ { 1073741823, 1073741824},
+ { 1073741824, 1073741824}, // Anything larger than this will overflow
+ };
+
+ for (auto c : cases) {
+ int actual = SkNextPow2(c.fInput);
+ REPORTER_ASSERT(reporter, c.fExpected == actual,
+ "SkNextPow2(%d) == %d not %d", c.fInput, actual, c.fExpected);
+ REPORTER_ASSERT(reporter, actual == SkNextPow2_portable(c.fInput));
+ REPORTER_ASSERT(reporter, ((uint32_t)actual) == GrNextPow2(c.fInput));
+ }
+
+ // exhaustive search for all the between numbers
+ for (int i = 6; i < 63356; i++) {
+ int actual = SkNextPow2(i);
+ int expected = std::pow(2.f, std::ceil(logf(i)/logf(2)));
+ REPORTER_ASSERT(reporter, expected == actual,
+ "SkNextPow2(%d) == %d not %d", i, actual, expected);
+ REPORTER_ASSERT(reporter, actual == SkNextPow2_portable(i));
+ REPORTER_ASSERT(reporter, ((uint32_t)actual) == GrNextPow2(i));
+ }
+}
+
DEF_TEST(DoubleSaturate32, reporter) {
const struct {
double fDouble;