ray-tracer-challenge/test/test_intersections.c
2024-07-24 00:39:42 -05:00

111 lines
2.8 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include "intersections.h"
#include "runner.h"
static bool intersections_test_0(const char ** scenario)
{
*scenario = "An intersection encapsulates t and object";
struct sphere s = sphere();
struct intersection i = intersection(3.5f, &s);
return
float_equal(i.t, 3.5f) &&
i.object == &s;
}
static bool intersections_test_1(const char ** scenario)
{
*scenario = "Aggregating intersections2";
struct sphere s = sphere();
struct intersection i1 = intersection(1.0f, &s);
struct intersection i2 = intersection(2.0f, &s);
struct intersections2 xs = intersections2(i1, i2);
return
xs.count == 2 &&
float_equal(xs.i[0].t, 1.0f) &&
float_equal(xs.i[1].t, 2.0f);
}
static bool intersections_test_2(const char ** scenario)
{
*scenario = "The hit, when all intersections2 have positive t";
struct sphere s = sphere();
struct intersection i1 = intersection(1.0f, &s);
struct intersection i2 = intersection(2.0f, &s);
struct intersections2 xs = intersections2(i2, i1);
struct intersection * i = hit((struct intersections *)&xs);
return
i != NULL &&
i->t == i1.t &&
i->object == i1.object;
}
static bool intersections_test_3(const char ** scenario)
{
*scenario = "The hit, when some intersections2 have negative t";
struct sphere s = sphere();
struct intersection i1 = intersection(-1.0f, &s);
struct intersection i2 = intersection( 1.0f, &s);
struct intersections2 xs = intersections2(i2, i1);
struct intersection * i = hit((struct intersections *)&xs);
return
i != NULL &&
i->t == i2.t &&
i->object == i2.object;
}
static bool intersections_test_4(const char ** scenario)
{
*scenario = "The hit, when all intersections2 have negative t";
struct sphere s = sphere();
struct intersection i1 = intersection(-2.0f, &s);
struct intersection i2 = intersection(-1.0f, &s);
struct intersections2 xs = intersections2(i2, i1);
struct intersection * i = hit((struct intersections *)&xs);
return i == NULL;
}
static bool intersections_test_5(const char ** scenario)
{
*scenario = "The hit is always the lowest nonnegative intersection";
struct sphere s = sphere();
struct intersection i1 = intersection( 5.0f, &s);
struct intersection i2 = intersection( 7.0f, &s);
struct intersection i3 = intersection(-3.0f, &s);
struct intersection i4 = intersection( 2.0f, &s);
struct intersections4 xs = intersections4(i1, i2, i3, i4);
struct intersection * i = hit((struct intersections *)&xs);
return
i != NULL &&
i->t == i4.t &&
i->object == i4.object;
}
test_t intersections_tests[] = {
intersections_test_0,
intersections_test_1,
intersections_test_2,
intersections_test_3,
intersections_test_4,
intersections_test_5,
};
RUNNER(intersections_tests);