SkFixed15Test.cpp 1019 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * Copyright 2016 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 "src/core/SkFixed15.h"
  8. #include "tests/Test.h"
  9. DEF_TEST(SkFixed15, r) {
  10. // For all v, v*0 == 0, v*1 == v.
  11. for (uint16_t bits = 0; bits <= 32768; bits++) {
  12. auto v = SkFixed15::Load(bits);
  13. REPORTER_ASSERT(r, v * 0.0f == 0.0f);
  14. REPORTER_ASSERT(r, v * 1.0f == v);
  15. }
  16. // Division and multiplication by powers of 2 is exact.
  17. SkFixed15 v = 1.0f;
  18. REPORTER_ASSERT(r, (v>>1) == 0.50f);
  19. REPORTER_ASSERT(r, (v>>2) == 0.25f);
  20. REPORTER_ASSERT(r, (v>>2<<1) == 0.50f);
  21. // FromU8() should be just as good as going through float.
  22. for (int x = 0; x < 256; x++) {
  23. REPORTER_ASSERT(r, SkFixed15::FromU8(x) == SkFixed15(x * (1/255.0f)));
  24. }
  25. // to_u8() and FromU8() should roundtrip all bytes.
  26. for (int x = 0; x < 256; x++) {
  27. REPORTER_ASSERT(r, x == SkFixed15::FromU8(x).to_u8());
  28. }
  29. }