From 32f44d39f01b62a51db177b6f3004ee0b835188d Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 11 Apr 2022 15:19:05 +0800 Subject: Implement vec3 scalar and vector product, product by a scalar, get unit vector and print to ostream. Fix wrong vec3 norm implementation. --- test.cpp | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'test.cpp') diff --git a/test.cpp b/test.cpp index 8f413f3..8c450f9 100644 --- a/test.cpp +++ b/test.cpp @@ -24,11 +24,47 @@ TEST(Vec, VecMin) { ASSERT_EQ(d + (-e), f); } -TEST(Vec, VecCrossProduct) { +TEST(Vec, CrossProduct) { vec3i a{1, 1, 1}, b{2, 2, 2}, c{3, 4, 5}, d{6, 7, 8}, e{-3, -6, -3}; - ASSERT_EQ(a * b, vec3i{}); - ASSERT_EQ(c * d, e); + ASSERT_EQ(cross(a, b), vec3i{}); + ASSERT_EQ(a.cross(b), vec3i{}); + ASSERT_EQ(cross(c, d), e); + ASSERT_EQ(c.cross(d), e); - vec3d f{3.0, 4.0, 5.0}, g{6.0, 7.0, 8.0}, h{-3, -6, -3}; - ASSERT_EQ(f * g, h); + vec3d f{3, 4, 5}, g{6, 7, 8}, h{-3, -6, -3}; + ASSERT_EQ(cross(f, g), h); + ASSERT_EQ(f.cross(g), h); +} + +TEST(Vec, DotProduct) { + vec3i c{3, 4, 5}, d{6, 7, 8}; + int e = 18 + 28 + 40; + ASSERT_EQ(dot(c, d), e); + ASSERT_EQ(c.dot(d), e); + + vec3d f{3, 4, 5}, g{6, 7, 8}; + ASSERT_EQ(dot(f, g), e); + ASSERT_EQ(f.dot(g), e); +} + +TEST(Vec, MulByScalar) { + vec3i a{1, 1, 1}, b{2, 2, 2}; + vec3d c{7, 14, 21}, d{1, 2, 3}; + ASSERT_EQ(a * 2, b); + ASSERT_EQ(a / 0.5, b); + ASSERT_EQ(2 * a, b); + ASSERT_EQ(c * (1.0 / 7), d); + ASSERT_EQ(c / 7, d); + ASSERT_EQ(c / 7.0, d); +} + +TEST(Vec, Norm) { + vec3d a{1, 1, 1}, b{2, 2, 2}; + ASSERT_EQ(a.norm(), sqrt(3)); + ASSERT_EQ(b.norm(), sqrt(12)); +} + +TEST(Vec, UnitVec) { + vec3d a{1, 2, 2}, b{1.0 / 3, 2.0 / 3, 2.0 / 3}; + ASSERT_EQ(a.unit_vec(), b); } \ No newline at end of file -- cgit v1.2.3