• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
13 #include <numeric>
14 
15 using namespace grvx;
16 using skvx::bit_pun;
17 
DEF_TEST(grvx_cross_dot,r)18 DEF_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 }
42