Skip to content

Commit 00b2e74

Browse files
committed
fix tensor test
1 parent 26c88fe commit 00b2e74

File tree

1 file changed

+111
-115
lines changed

1 file changed

+111
-115
lines changed

test/runtime/cartesian_tensor_test.cpp

Lines changed: 111 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,39 @@ import mp_units;
3838
using namespace mp_units;
3939
using Catch::Matchers::WithinAbs;
4040

41-
TEST_CASE("cartesian_tensor construction and access", "[tensor]")
41+
TEST_CASE("cartesian_tensor — core", "[tensor]")
4242
{
43-
// 2x3 fill-ctor order is row-major
44-
cartesian_tensor<int, 2, 3> A{1, 2, 3, 4, 5, 6};
45-
REQUIRE(A(0, 0) == 1);
46-
REQUIRE(A(0, 1) == 2);
47-
REQUIRE(A(0, 2) == 3);
48-
REQUIRE(A(1, 0) == 4);
49-
REQUIRE(A(1, 1) == 5);
50-
REQUIRE(A(1, 2) == 6);
51-
}
43+
SECTION("construction and access (row-major fill)")
44+
{
45+
cartesian_tensor<int, 2, 3> A{1, 2, 3, 4, 5, 6};
46+
REQUIRE(A(0, 0) == 1);
47+
REQUIRE(A(0, 1) == 2);
48+
REQUIRE(A(0, 2) == 3);
49+
REQUIRE(A(1, 0) == 4);
50+
REQUIRE(A(1, 1) == 5);
51+
REQUIRE(A(1, 2) == 6);
52+
}
5253

53-
TEST_CASE("elementwise + and - with common_type", "[tensor]")
54-
{
55-
cartesian_tensor<int, 2, 2> A{1, 2, 3, 4};
56-
cartesian_tensor<double, 2, 2> B{0.5, 1.5, 2.5, 3.5};
57-
58-
auto S = A + B;
59-
static_assert(std::is_same_v<decltype(S), cartesian_tensor<double, 2, 2>>);
60-
REQUIRE(S(0, 0) == 1.5);
61-
REQUIRE(S(0, 1) == 3.5);
62-
REQUIRE(S(1, 0) == 5.5);
63-
REQUIRE(S(1, 1) == 7.5);
64-
65-
auto D = B - A;
66-
REQUIRE_THAT(D(0, 0), WithinAbs(-0.5, 1e-12));
67-
REQUIRE_THAT(D(0, 1), WithinAbs(-0.5, 1e-12));
68-
REQUIRE_THAT(D(1, 0), WithinAbs(-0.5, 1e-12));
69-
REQUIRE_THAT(D(1, 1), WithinAbs(-0.5, 1e-12));
70-
}
54+
SECTION("elementwise + and - with common_type")
55+
{
56+
cartesian_tensor<int, 2, 2> A{1, 2, 3, 4};
57+
cartesian_tensor<double, 2, 2> B{0.5, 1.5, 2.5, 3.5};
58+
59+
auto S = A + B;
60+
static_assert(std::is_same_v<decltype(S), cartesian_tensor<double, 2, 2>>);
61+
REQUIRE(S(0, 0) == 1.5);
62+
REQUIRE(S(0, 1) == 3.5);
63+
REQUIRE(S(1, 0) == 5.5);
64+
REQUIRE(S(1, 1) == 7.5);
65+
66+
auto D = B - A;
67+
REQUIRE_THAT(D(0, 0), WithinAbs(-0.5, 1e-12));
68+
REQUIRE_THAT(D(0, 1), WithinAbs(-0.5, 1e-12));
69+
REQUIRE_THAT(D(1, 0), WithinAbs(-0.5, 1e-12));
70+
REQUIRE_THAT(D(1, 1), WithinAbs(-0.5, 1e-12));
71+
}
7172

72-
TEST_CASE("elementwise modulo", "[tensor]")
73-
{
74-
SECTION("integral % integral")
73+
SECTION("elementwise modulo (integral)")
7574
{
7675
cartesian_tensor<int, 2, 3> A{10, 11, 12, 13, 14, 15};
7776
cartesian_tensor<int, 2, 3> B{4, 5, 7, 4, 5, 7};
@@ -83,97 +82,94 @@ TEST_CASE("elementwise modulo", "[tensor]")
8382
REQUIRE(R(1, 1) == 4);
8483
REQUIRE(R(1, 2) == 1);
8584
}
86-
}
8785

88-
TEST_CASE("scalar multiply/divide", "[tensor]")
89-
{
90-
cartesian_tensor<double, 2, 2> A{1, 2, 3, 4};
91-
92-
auto T1 = A * 2.0;
93-
REQUIRE(T1(0, 0) == 2.0);
94-
REQUIRE(T1(0, 1) == 4.0);
95-
REQUIRE(T1(1, 0) == 6.0);
96-
REQUIRE(T1(1, 1) == 8.0);
97-
98-
auto T2 = 2.0 * A;
99-
REQUIRE(T2(0, 0) == 2.0);
100-
REQUIRE(T2(0, 1) == 4.0);
101-
REQUIRE(T2(1, 0) == 6.0);
102-
REQUIRE(T2(1, 1) == 8.0);
103-
104-
auto T3 = A / 2.0;
105-
REQUIRE(T3(0, 0) == 0.5);
106-
REQUIRE(T3(0, 1) == 1.0);
107-
REQUIRE(T3(1, 0) == 1.5);
108-
REQUIRE(T3(1, 1) == 2.0);
109-
}
86+
SECTION("scalar multiply/divide")
87+
{
88+
cartesian_tensor<double, 2, 2> A{1, 2, 3, 4};
89+
90+
auto T1 = A * 2.0;
91+
REQUIRE(T1(0, 0) == 2.0);
92+
REQUIRE(T1(0, 1) == 4.0);
93+
REQUIRE(T1(1, 0) == 6.0);
94+
REQUIRE(T1(1, 1) == 8.0);
95+
96+
auto T2 = 2.0 * A;
97+
REQUIRE(T2(0, 0) == 2.0);
98+
REQUIRE(T2(0, 1) == 4.0);
99+
REQUIRE(T2(1, 0) == 6.0);
100+
REQUIRE(T2(1, 1) == 8.0);
101+
102+
auto T3 = A / 2.0;
103+
REQUIRE(T3(0, 0) == 0.5);
104+
REQUIRE(T3(0, 1) == 1.0);
105+
REQUIRE(T3(1, 0) == 1.5);
106+
REQUIRE(T3(1, 1) == 2.0);
107+
}
110108

111-
TEST_CASE("matmul (R×K) * (K×C) -> (R×C)", "[tensor]")
112-
{
113-
cartesian_tensor<int, 2, 3> A{1, 2, 3, 4, 5, 6};
114-
cartesian_tensor<int, 3, 2> B{7, 8, 9, 10, 11, 12};
115-
auto C = matmul(A, B); // 2x2
116-
117-
static_assert(std::is_same_v<decltype(C), cartesian_tensor<int, 2, 2>>);
118-
REQUIRE(C(0, 0) == 58);
119-
REQUIRE(C(0, 1) == 64);
120-
REQUIRE(C(1, 0) == 139);
121-
REQUIRE(C(1, 1) == 154);
122-
}
109+
SECTION("matmul (R×K) * (K×C) -> (R×C)")
110+
{
111+
cartesian_tensor<int, 2, 3> A{1, 2, 3, 4, 5, 6};
112+
cartesian_tensor<int, 3, 2> B{7, 8, 9, 10, 11, 12};
113+
auto C = matmul(A, B);
114+
115+
static_assert(std::is_same_v<decltype(C), cartesian_tensor<int, 2, 2>>);
116+
REQUIRE(C(0, 0) == 58);
117+
REQUIRE(C(0, 1) == 64);
118+
REQUIRE(C(1, 0) == 139);
119+
REQUIRE(C(1, 1) == 154);
120+
}
123121

124-
TEST_CASE("matvec (3x3) * vector -> vector", "[tensor][vector]")
125-
{
126-
cartesian_tensor<double, 3, 3> M{1, 2, 3, 0, 1, 4, 5, 6, 0};
127-
cartesian_vector<double> x{1, 2, 3};
128-
auto y = matvec(M, x);
129-
REQUIRE_THAT(y[0], WithinAbs(14.0, 1e-12)); // 1*1 + 2*2 + 3*3
130-
REQUIRE_THAT(y[1], WithinAbs(14.0, 1e-12)); // 0*1 + 1*2 + 4*3
131-
REQUIRE_THAT(y[2], WithinAbs(17.0, 1e-12)); // 5*1 + 6*2 + 0*3
132-
}
122+
SECTION("matvec (3x3) * vector -> vector")
123+
{
124+
cartesian_tensor<double, 3, 3> M{1, 2, 3, 0, 1, 4, 5, 6, 0};
125+
cartesian_vector<double> x{1, 2, 3};
126+
auto y = matvec(M, x);
127+
REQUIRE_THAT(y[0], WithinAbs(14.0, 1e-12)); // 1*1 + 2*2 + 3*3
128+
REQUIRE_THAT(y[1], WithinAbs(14.0, 1e-12)); // 0*1 + 1*2 + 4*3
129+
REQUIRE_THAT(y[2], WithinAbs(17.0, 1e-12)); // 5*1 + 6*2 + 0*3
130+
}
133131

134-
TEST_CASE("double contraction A : B", "[tensor]")
135-
{
136-
cartesian_tensor<int, 2, 2> A{1, 2, 3, 4};
137-
cartesian_tensor<int, 2, 2> B{5, 6, 7, 8};
138-
// 1*5 + 2*6 + 3*7 + 4*8 = 70
139-
auto s = double_contraction(A, B);
140-
REQUIRE(s == 70);
141-
}
132+
SECTION("double contraction A : B")
133+
{
134+
cartesian_tensor<int, 2, 2> A{1, 2, 3, 4};
135+
cartesian_tensor<int, 2, 2> B{5, 6, 7, 8};
136+
auto s = double_contraction(A, B); // 1*5 + 2*6 + 3*7 + 4*8
137+
REQUIRE(s == 70);
138+
}
142139

143-
TEST_CASE("outer_numeric (vector ⊗ vector)", "[tensor][vector]")
144-
{
145-
cartesian_vector<int> a{1, 2, 3};
146-
cartesian_vector<int> b{4, 5, 6};
147-
auto T = outer_numeric(a, b); // 3x3
148-
REQUIRE(T(0, 0) == 4);
149-
REQUIRE(T(0, 1) == 5);
150-
REQUIRE(T(0, 2) == 6);
151-
REQUIRE(T(1, 0) == 8);
152-
REQUIRE(T(1, 1) == 10);
153-
REQUIRE(T(1, 2) == 12);
154-
REQUIRE(T(2, 0) == 12);
155-
REQUIRE(T(2, 1) == 15);
156-
REQUIRE(T(2, 2) == 18);
157-
}
140+
SECTION("outer_numeric (vector ⊗ vector)")
141+
{
142+
cartesian_vector<int> a{1, 2, 3};
143+
cartesian_vector<int> b{4, 5, 6};
144+
auto T = outer_numeric(a, b);
145+
REQUIRE(T(0, 0) == 4);
146+
REQUIRE(T(0, 1) == 5);
147+
REQUIRE(T(0, 2) == 6);
148+
REQUIRE(T(1, 0) == 8);
149+
REQUIRE(T(1, 1) == 10);
150+
REQUIRE(T(1, 2) == 12);
151+
REQUIRE(T(2, 0) == 12);
152+
REQUIRE(T(2, 1) == 15);
153+
REQUIRE(T(2, 2) == 18);
154+
}
158155

159156
#if MP_UNITS_HOSTED
160-
TEST_CASE("text output (ostream + fmt)", "[tensor][fmt][ostream]")
161-
{
162-
cartesian_tensor<int, 2, 2> A{1, 2, 3, 4};
163-
164-
std::ostringstream os;
165-
os << A;
166-
CHECK(os.str() == "[[1, 2]\n [3, 4]]");
167-
168-
CHECK(MP_UNITS_STD_FMT::format("{}", A) == os.str());
169-
}
157+
SECTION("text output (ostream + fmt)")
158+
{
159+
cartesian_tensor<int, 2, 2> A{1, 2, 3, 4};
160+
std::ostringstream os;
161+
os << A;
162+
CHECK(os.str() == "[[1, 2]\n [3, 4]]");
163+
CHECK(MP_UNITS_STD_FMT::format("{}", A) == os.str());
164+
}
170165
#endif
171166

172-
TEST_CASE("constexpr basics", "[tensor][constexpr]")
173-
{
174-
constexpr cartesian_tensor<int, 1, 3> A{1, 2, 3};
175-
constexpr cartesian_tensor<int, 1, 3> B{4, 5, 6};
176-
constexpr auto C = A + B;
177-
static_assert(C(0, 0) == 5 && C(0, 1) == 7 && C(0, 2) == 9);
178-
(void)C;
167+
SECTION("constexpr basics")
168+
{
169+
constexpr cartesian_tensor<int, 1, 3> A{1, 2, 3};
170+
constexpr cartesian_tensor<int, 1, 3> B{4, 5, 6};
171+
constexpr auto C = A + B;
172+
static_assert(C(0, 0) == 5 && C(0, 1) == 7 && C(0, 2) == 9);
173+
(void)C;
174+
}
179175
}

0 commit comments

Comments
 (0)