xref: /third_party/skia/tests/SkFixed15Test.cpp (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/core/SkFixed15.h"
9cb93a386Sopenharmony_ci#include "tests/Test.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ciDEF_TEST(SkFixed15, r) {
12cb93a386Sopenharmony_ci    // For all v, v*0 == 0, v*1 == v.
13cb93a386Sopenharmony_ci    for (uint16_t bits = 0; bits <= 32768; bits++) {
14cb93a386Sopenharmony_ci        auto v = SkFixed15::Load(bits);
15cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, v * 0.0f == 0.0f);
16cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, v * 1.0f == v);
17cb93a386Sopenharmony_ci    }
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci    // Division and multiplication by powers of 2 is exact.
20cb93a386Sopenharmony_ci    SkFixed15 v = 1.0f;
21cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, (v>>1)    == 0.50f);
22cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, (v>>2)    == 0.25f);
23cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, (v>>2<<1) == 0.50f);
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    // FromU8() should be just as good as going through float.
26cb93a386Sopenharmony_ci    for (int x = 0; x < 256; x++) {
27cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, SkFixed15::FromU8(x) == SkFixed15(x * (1/255.0f)));
28cb93a386Sopenharmony_ci    }
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci    // to_u8() and FromU8() should roundtrip all bytes.
31cb93a386Sopenharmony_ci    for (int x = 0; x < 256; x++) {
32cb93a386Sopenharmony_ci        REPORTER_ASSERT(r, x == SkFixed15::FromU8(x).to_u8());
33cb93a386Sopenharmony_ci    }
34cb93a386Sopenharmony_ci}
35