Skip to content

Commit

Permalink
Test sphere class isHit method #12
Browse files Browse the repository at this point in the history
  • Loading branch information
smercer10 committed Feb 25, 2024
1 parent 55d94bf commit 917b753
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FetchContent_Declare(

FetchContent_MakeAvailable(googletest)

add_executable(raydiance_test vec3_test.cpp colour_test.cpp ray_test.cpp globals_test.cpp object_test.cpp interval_test.cpp)
add_executable(raydiance_test vec3_test.cpp colour_test.cpp ray_test.cpp globals_test.cpp object_test.cpp interval_test.cpp sphere_test.cpp)

target_link_libraries(raydiance_test raydiance_lib GTest::gtest_main)

Expand Down
33 changes: 33 additions & 0 deletions tests/sphere_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "raydiance/sphere.h"
#include <gtest/gtest.h>

TEST(SphereTest, IsHit) {
sphere s1{point3{0.0, 0.0, -4.0}, 1.0};
sphere s2{point3{2.0, 0.0, 2.0}, 2.0};
sphere s3{point3{0.0, 0.0, 5.0}, 2.0};

ray r{point3{0.0, 0.0, 0.0}, vec3{0.0, 0.0, 1.0}};
intersection i;

EXPECT_FALSE(s1.isHit(r, interval{0.0, infinity}, i));

EXPECT_TRUE(s2.isHit(r, interval{0.0, infinity}, i));
EXPECT_DOUBLE_EQ(i.t, 2.0);
EXPECT_DOUBLE_EQ(i.p.x(), 0.0);
EXPECT_DOUBLE_EQ(i.p.y(), 0.0);
EXPECT_DOUBLE_EQ(i.p.z(), 2.0);
EXPECT_DOUBLE_EQ(i.normal.x(), 1.0);
EXPECT_DOUBLE_EQ(i.normal.y(), 0.0);
EXPECT_DOUBLE_EQ(i.normal.z(), 0.0);
EXPECT_FALSE(i.frontFace);

EXPECT_TRUE(s3.isHit(r, interval{0.0, infinity}, i));
EXPECT_DOUBLE_EQ(i.t, 3.0);
EXPECT_DOUBLE_EQ(i.p.x(), 0.0);
EXPECT_DOUBLE_EQ(i.p.y(), 0.0);
EXPECT_DOUBLE_EQ(i.p.z(), 3.0);
EXPECT_DOUBLE_EQ(i.normal.x(), 0.0);
EXPECT_DOUBLE_EQ(i.normal.y(), 0.0);
EXPECT_DOUBLE_EQ(i.normal.z(), -1.0);
EXPECT_TRUE(i.frontFace);
}

0 comments on commit 917b753

Please sign in to comment.