1 //===- TestPoly3D.cpp - Poly3D unit tests------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/TypeSize.h"
10 #include "gtest/gtest.h"
17 template <> struct LinearPolyBaseTypeTraits
<Poly3D
> {
18 using ScalarTy
= int64_t;
19 static const unsigned Dimensions
= 3;
23 using Poly3DBase
= LinearPolyBase
<Poly3D
>;
24 class Poly3D
: public Poly3DBase
{
26 using ScalarTy
= Poly3DBase::ScalarTy
;
27 Poly3D(ScalarTy x
, ScalarTy y
, ScalarTy z
) : Poly3DBase({x
, y
, z
}) {}
28 Poly3D(const Poly3DBase
&Convert
) : Poly3DBase(Convert
) {}
31 TEST(LinearPolyBase
, Poly3D_isZero
) {
32 EXPECT_TRUE(Poly3D(0, 0, 0).isZero());
33 EXPECT_TRUE(Poly3D(0, 0, 1).isNonZero());
34 EXPECT_TRUE(Poly3D(0, 0, 1));
37 TEST(LinearPolyBase
, Poly3D_Equality
) {
38 EXPECT_EQ(Poly3D(1, 2, 3), Poly3D(1, 2, 3));
39 EXPECT_NE(Poly3D(1, 2, 3), Poly3D(1, 2, 4));
42 TEST(LinearPolyBase
, Poly3D_GetValue
) {
43 EXPECT_EQ(Poly3D(1, 2, 3).getValue(0), 1);
44 EXPECT_EQ(Poly3D(1, 2, 3).getValue(1), 2);
45 EXPECT_EQ(Poly3D(1, 2, 3).getValue(2), 3);
48 TEST(LinearPolyBase
, Poly3D_Add
) {
50 EXPECT_EQ(Poly3D(42, 0, 0) + Poly3D(0, 42, 0) + Poly3D(0, 0, 42),
55 X
+= Poly3D(0, 42, 0);
56 X
+= Poly3D(0, 0, 42);
57 EXPECT_EQ(X
, Poly3D(42, 42, 42));
60 TEST(LinearPolyBase
, Poly3D_Sub
) {
62 EXPECT_EQ(Poly3D(42, 42, 42) - Poly3D(42, 0, 0) - Poly3D(0, 42, 0) -
68 X
-= Poly3D(42, 0, 0);
69 X
-= Poly3D(0, 42, 0);
70 X
-= Poly3D(0, 0, 42);
71 EXPECT_EQ(X
, Poly3D(0, 0, 0));
74 TEST(LinearPolyBase
, Poly3D_Scale
) {
76 EXPECT_EQ(Poly3D(1, 2, 4) * 2, Poly3D(2, 4, 8));
77 EXPECT_EQ(Poly3D(1, 2, 4) * -2, Poly3D(-2, -4, -8));
80 TEST(LinearPolyBase
, Poly3D_Invert
) {
82 EXPECT_EQ(-Poly3D(2, 4, 8), Poly3D(-2, -4, -8));
87 template <> struct LinearPolyBaseTypeTraits
<Univariate3D
> {
88 using ScalarTy
= int64_t;
89 static const unsigned Dimensions
= 3;
93 using Univariate3DBase
= UnivariateLinearPolyBase
<Univariate3D
>;
94 class Univariate3D
: public Univariate3DBase
{
96 using ScalarTy
= Univariate3DBase::ScalarTy
;
97 Univariate3D(ScalarTy x
, unsigned Dim
) : Univariate3DBase(x
, Dim
) {}
98 Univariate3D(const Univariate3DBase
&Convert
) : Univariate3DBase(Convert
) {}
101 TEST(UnivariateLinearPolyBase
, Univariate3D_isZero
) {
102 EXPECT_TRUE(Univariate3D(0, 0).isZero());
103 EXPECT_TRUE(Univariate3D(0, 1).isZero());
104 EXPECT_TRUE(Univariate3D(0, 2).isZero());
105 EXPECT_TRUE(Univariate3D(1, 0).isNonZero());
106 EXPECT_TRUE(Univariate3D(1, 1).isNonZero());
107 EXPECT_TRUE(Univariate3D(1, 2).isNonZero());
108 EXPECT_TRUE(Univariate3D(1, 0));
111 TEST(UnivariateLinearPolyBase
, Univariate3D_Equality
) {
112 EXPECT_EQ(Univariate3D(1, 0), Univariate3D(1, 0));
113 EXPECT_NE(Univariate3D(1, 0), Univariate3D(1, 2));
114 EXPECT_NE(Univariate3D(1, 0), Univariate3D(1, 1));
115 EXPECT_NE(Univariate3D(1, 0), Univariate3D(2, 0));
116 EXPECT_NE(Univariate3D(1, 0), Univariate3D(0, 0));
119 TEST(UnivariateLinearPolyBase
, Univariate3D_GetValue
) {
120 EXPECT_EQ(Univariate3D(42, 0).getValue(0), 42);
121 EXPECT_EQ(Univariate3D(42, 0).getValue(1), 0);
122 EXPECT_EQ(Univariate3D(42, 0).getValue(2), 0);
124 EXPECT_EQ(Univariate3D(42, 1).getValue(0), 0);
125 EXPECT_EQ(Univariate3D(42, 1).getValue(1), 42);
126 EXPECT_EQ(Univariate3D(42, 1).getValue(2), 0);
129 TEST(UnivariateLinearPolyBase
, Univariate3D_Add
) {
131 EXPECT_EQ(Univariate3D(42, 0) + Univariate3D(42, 0), Univariate3D(84, 0));
132 EXPECT_EQ(Univariate3D(42, 1) + Univariate3D(42, 1), Univariate3D(84, 1));
133 EXPECT_DEBUG_DEATH(Univariate3D(42, 0) + Univariate3D(42, 1),
134 "Invalid dimensions");
137 Univariate3D
X(42, 0);
138 X
+= Univariate3D(42, 0);
139 EXPECT_EQ(X
, Univariate3D(84, 0));
141 // Test 'getWithIncrement' method
142 EXPECT_EQ(Univariate3D(42, 0).getWithIncrement(1), Univariate3D(43, 0));
143 EXPECT_EQ(Univariate3D(42, 1).getWithIncrement(2), Univariate3D(44, 1));
144 EXPECT_EQ(Univariate3D(42, 2).getWithIncrement(3), Univariate3D(45, 2));
147 TEST(UnivariateLinearPolyBase
, Univariate3D_Sub
) {
149 EXPECT_EQ(Univariate3D(84, 0) - Univariate3D(42, 0), Univariate3D(42, 0));
150 EXPECT_EQ(Univariate3D(84, 1) - Univariate3D(42, 1), Univariate3D(42, 1));
151 EXPECT_DEBUG_DEATH(Univariate3D(84, 0) - Univariate3D(42, 1),
152 "Invalid dimensions");
155 Univariate3D
X(84, 0);
156 X
-= Univariate3D(42, 0);
157 EXPECT_EQ(X
, Univariate3D(42, 0));
159 // Test 'getWithDecrement' method
160 EXPECT_EQ(Univariate3D(43, 0).getWithDecrement(1), Univariate3D(42, 0));
161 EXPECT_EQ(Univariate3D(44, 1).getWithDecrement(2), Univariate3D(42, 1));
162 EXPECT_EQ(Univariate3D(45, 2).getWithDecrement(3), Univariate3D(42, 2));
165 TEST(UnivariateLinearPolyBase
, Univariate3D_Scale
) {
167 EXPECT_EQ(Univariate3D(4, 0) * 2, Univariate3D(8, 0));
168 EXPECT_EQ(Univariate3D(4, 1) * -2, Univariate3D(-8, 1));
171 TEST(UnivariateLinearPolyBase
, Univariate3D_Invert
) {
173 EXPECT_EQ(-Univariate3D(4, 0), Univariate3D(-4, 0));
174 EXPECT_EQ(-Univariate3D(4, 1), Univariate3D(-4, 1));