#include "gtest/gtest.h"
#include <vector>
#include <algorithm>
Eigen::Matrix2d M(2.4);
EXPECT_EQ(2.4, M(0, 0));
EXPECT_EQ(2.4, M(0, 1));
EXPECT_EQ(2.4, M(1, 1));
EXPECT_EQ(2.4, M(1, 0));
Eigen::VectorXi v(3);
v[0] = v[1] = v[2] = -5;
EXPECT_EQ(-5, v[0]);
EXPECT_EQ(-5, v[1]);
EXPECT_EQ(-5, v[2]);
Eigen::Vector3i v2(6);
EXPECT_EQ(6, v2[0]);
EXPECT_EQ(6, v2[1]);
EXPECT_EQ(6, v2[2]);
Eigen::VectorXd v3(3);
EXPECT_EQ(v3.size(), 3);
}
TEST(Types, VecConstruct) {
EXPECT_EQ(6.0, a[0]);
EXPECT_EQ(2.0, b[0]);
EXPECT_EQ(3.0, b[1]);
EXPECT_EQ(5.0, c[0]);
EXPECT_EQ(5.0, c[1]);
EXPECT_EQ(5.0, c[2]);
d << 1, 5, 7;
EXPECT_EQ(1, d[0]);
EXPECT_EQ(5, d[1]);
EXPECT_EQ(7, d[2]);
EXPECT_EQ(3.4, e[0]);
EXPECT_EQ(-1.2, e[1]);
}
TEST(Types, VecAssign) {
a = {1, 2};
EXPECT_DOUBLE_EQ(1.0, a[0]);
EXPECT_DOUBLE_EQ(2.0, a[1]);
a = 5.3;
EXPECT_EQ(5.3, a[0]);
EXPECT_EQ(5.3, a[1]);
b = a;
EXPECT_EQ(a[0], b[0]);
EXPECT_EQ(a[1], b[1]);
c = std::move(a);
EXPECT_EQ(b[0], c[0]);
EXPECT_EQ(b[1], c[1]);
a[1] = 4;
EXPECT_EQ(a[1], 4);
}
TEST(Types, VecCompare) {
EXPECT_TRUE(a == b);
EXPECT_TRUE(a >= b);
EXPECT_TRUE(a <= b);
EXPECT_FALSE(a != b);
EXPECT_FALSE(a < b);
EXPECT_FALSE(a > b);
b[0] = 1;
EXPECT_FALSE(a == b);
EXPECT_FALSE(a >= b);
EXPECT_TRUE(a <= b);
EXPECT_TRUE(a != b);
EXPECT_TRUE(a < b);
EXPECT_FALSE(a > b);
a = {1, 1, 0};
b = {1, 1, 1};
EXPECT_FALSE(a == b);
EXPECT_FALSE(a >= b);
EXPECT_TRUE(a <= b);
EXPECT_TRUE(a != b);
EXPECT_TRUE(a < b);
EXPECT_FALSE(a > b);
std::vector<Vec2d> expected = {{0, 0}, {0, 0.5}, {0, 1}, {0.5, 0},
{0.5, 1}, {1, 0}, {1, 0.5}, {1, 1}};
for (size_t i = 0; i < expected.size(); ++i) {
for (size_t j = i + 1; j < expected.size(); ++j) {
EXPECT_LT(expected[i], expected[j])
<< expected[i].transpose() << " < " << expected[j].transpose();
EXPECT_GT(expected[j], expected[i])
<< expected[i].transpose() << " > " << expected[j].transpose();
}
}
std::vector<Vec2d> tosort = expected;
ASSERT_EQ(expected.size(), tosort.size());
for (size_t i = 0; i < expected.size(); ++i)
EXPECT_EQ(expected[i], tosort[i])
<< expected[i].transpose() << " == " << tosort[i].transpose();
}
TEST(Types, VecBasicMath) {
Vec3d a({1, 3, -1.2}), b({1, 0, 2.5});
EXPECT_EQ(
Vec3d({-1, -3, 1.2}), -a);
EXPECT_EQ(
Vec3d({2, 3, 1.3}), a + b);
EXPECT_EQ(
Vec3d({0, 3, -3.7}), a - b);
EXPECT_EQ(
Vec3d({0, -3, 3.7}), b - a);
EXPECT_DOUBLE_EQ(2.1, r[0]);
EXPECT_DOUBLE_EQ(6.3, r[1]);
EXPECT_DOUBLE_EQ(-1.2 * 2.1, r[2]);
r = a * 2.1;
EXPECT_DOUBLE_EQ(2.1, r[0]);
EXPECT_DOUBLE_EQ(6.3, r[1]);
EXPECT_DOUBLE_EQ(-1.2 * 2.1, r[2]);
r = r / 2.1;
EXPECT_DOUBLE_EQ(1, r[0]);
EXPECT_DOUBLE_EQ(3, r[1]);
EXPECT_DOUBLE_EQ(-1.2, r[2]);
EXPECT_DOUBLE_EQ(7.25, b.squaredNorm());
EXPECT_DOUBLE_EQ(2.6925824035672519, b.norm());
a += b;
ASSERT_EQ((
Vec3d{2, 3, 1.3}), a);
a *= 2.1;
EXPECT_DOUBLE_EQ(4.2, a[0]);
EXPECT_DOUBLE_EQ(6.3, a[1]);
EXPECT_DOUBLE_EQ(1.3 * 2.1, a[2]);
a /= 0.5;
EXPECT_DOUBLE_EQ(8.4, a[0]);
EXPECT_DOUBLE_EQ(12.6, a[1]);
EXPECT_DOUBLE_EQ(2 * 1.3 * 2.1, a[2]);
}
TEST(Types, VecIterate) {
for (auto& x : a) x += 4;
EXPECT_EQ((
Vec3d{4, 4, 4}), a);
}
TEST(Types, DISABLED_VecUsageExample) {
a = {3, -4.3, 2};
a = 12.3;
a /= 23.4;
Vec3d b(12.3, 34.5, -2.1);
b -= a;
std::cout << a << std::endl;
}
}