blob: 39803a92697d4569cd8c8cc238308ece6931a139 [file] [log] [blame]
Chris Daltonc3cb0992020-11-02 10:52:26 -07001/*
2 * Copyright 2020 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
8#include "include/utils/SkRandom.h"
9#include "src/core/SkGeometry.h"
10#include "src/gpu/GrVx.h"
11#include "tests/Test.h"
12#include <limits>
Chris Daltond461f6e2020-11-23 13:51:06 -070013#include <numeric>
Chris Daltonc3cb0992020-11-02 10:52:26 -070014
15using namespace grvx;
16using skvx::bit_pun;
17
18DEF_TEST(grvx_cross_dot, r) {
19 REPORTER_ASSERT(r, grvx::cross({0,1}, {0,1}) == 0);
20 REPORTER_ASSERT(r, grvx::cross({1,0}, {1,0}) == 0);
21 REPORTER_ASSERT(r, grvx::cross({1,1}, {1,1}) == 0);
22 REPORTER_ASSERT(r, grvx::cross({1,1}, {1,-1}) == -2);
23 REPORTER_ASSERT(r, grvx::cross({1,1}, {-1,1}) == 2);
24
25 REPORTER_ASSERT(r, grvx::dot({0,1}, {1,0}) == 0);
26 REPORTER_ASSERT(r, grvx::dot({1,0}, {0,1}) == 0);
27 REPORTER_ASSERT(r, grvx::dot({1,1}, {1,-1}) == 0);
28 REPORTER_ASSERT(r, grvx::dot({1,1}, {1,1}) == 2);
29 REPORTER_ASSERT(r, grvx::dot({1,1}, {-1,-1}) == -2);
30
31 SkRandom rand;
32 for (int i = 0; i < 100; ++i) {
33 float a=rand.nextRangeF(-1,1), b=rand.nextRangeF(-1,1), c=rand.nextRangeF(-1,1),
34 d=rand.nextRangeF(-1,1);
35 constexpr static float kTolerance = 1.f / (1 << 20);
36 REPORTER_ASSERT(r, SkScalarNearlyEqual(
37 grvx::cross({a,b}, {c,d}), SkPoint::CrossProduct({a,b}, {c,d}), kTolerance));
38 REPORTER_ASSERT(r, SkScalarNearlyEqual(
39 grvx::dot({a,b}, {c,d}), SkPoint::DotProduct({a,b}, {c,d}), kTolerance));
40 }
41}