Skip to content

Commit

Permalink
test: add all bezier2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DoggyXomaX committed Aug 3, 2024
1 parent 95c4e60 commit d933775
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ option(HMAH_BEZIER_INCLUDE_BEZIER2 "Include Bezier2 class in lib" ON)
option(HMAH_BEZIER_INCLUDE_BEZIER3 "Include Bezier3 class in lib" ON)
option(HMAH_BEZIER_STATIC "Build static library" ON)
option(HMAH_BEZIER_SHARED "Build shared library" OFF)
option(HMAH_BEZIER_BUILD_TESTS "Build tests for lib testing" ON)

set(FORCE_INCLUDE_ALL ON)

Expand All @@ -26,4 +27,8 @@ elseif (${HMAH_BEZIER_SHARED})
add_library(hmahbezier SHARED ${SOURCES})
else ()
message(FATAL_ERROR "Choose HMAH_BEZIER_STATIC or HMAH_BEZIER_SHARED to build!")
endif ()

if (${HMAH_BEZIER_BUILD_TESTS})
add_subdirectory(tests)
endif ()
14 changes: 14 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.14)
project(HmahBezierLibTests)

unset(CMAKE_CXX_COMPILER)

function(add_test TEST_NAME)
add_executable(${TEST_NAME} "${TEST_NAME}.c")
target_include_directories(${TEST_NAME} PRIVATE ../src)
target_link_libraries(${TEST_NAME} PRIVATE hmahbezier)
endfunction()

add_test(linear_test)
add_test(quadratic_test)
add_test(cubic_test)
21 changes: 21 additions & 0 deletions tests/cubic_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "bezier2.h"
#include "test_plot.h"

#include <stdio.h>

#define PCOUNT 10

int main() {
printf("Cubic bezier test with %d points\n", PCOUNT);

const Bezier2f curve = Bezier2f_CreateCubic(0, 0, 20, 20, 20, 0, 40, 0);
const float dt = 1.0f / (float)PCOUNT;

Point2f points[PCOUNT];

float t = 0.0f;
for (int i = 0; i < PCOUNT; i++, t += dt)
points[i] = Bezier2f_Get(&curve, t);

plot2Df((const float*)points, PCOUNT);
}
21 changes: 21 additions & 0 deletions tests/linear_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "bezier2.h"
#include "test_plot.h"

#include <stdio.h>

#define PCOUNT 10

int main() {
printf("Linear bezier test with %d points\n", PCOUNT);

const Bezier2f curve = Bezier2f_CreateLinear(0, 0, 20, 20);
const float dt = 1.0f / (float)PCOUNT;

Point2f points[PCOUNT];

float t = 0.0f;
for (int i = 0; i < PCOUNT; i++, t += dt)
points[i] = Bezier2f_Get(&curve, t);

plot2Df((const float*)points, PCOUNT);
}
21 changes: 21 additions & 0 deletions tests/quadratic_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "bezier2.h"
#include "test_plot.h"

#include <stdio.h>

#define PCOUNT 10

int main() {
printf("Quadratic bezier test with %d points\n", PCOUNT);

const Bezier2f curve = Bezier2f_CreateQuadratic(0, 0, 20, 20, 40, 0);
const float dt = 1.0f / (float)PCOUNT;

Point2f points[PCOUNT];

float t = 0.0f;
for (int i = 0; i < PCOUNT; i++, t += dt)
points[i] = Bezier2f_Get(&curve, t);

plot2Df((const float*)points, PCOUNT);
}
38 changes: 38 additions & 0 deletions tests/test_plot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef __HMAH_BEZIER_TEST_PLOT_H__
#define __HMAH_BEZIER_TEST_PLOT_H__

#include <stdio.h>
#include <stddef.h>

#define PLOT_FILL '#'
#define PLOT_SPACE '.'
#define PLOT_WIDTH 40
#define PLOT_HEIGHT 20

float absf(const float v) {
return v < 0.0f ? -v : v;
}

void plot2Df(const float* points, const size_t pointsCount) {
char canvas[PLOT_HEIGHT][PLOT_WIDTH];
for (int y = 0; y < PLOT_HEIGHT; y++)
for (int x = 0; x < PLOT_WIDTH; x++)
canvas[y][x] = PLOT_SPACE;

for (size_t i = 0; i < pointsCount; i++) {
const int x = (int)points[i * 2 + 0];
const int y = (int)points[i * 2 + 1];
if (x < 0 || x >= PLOT_WIDTH || y < 0 || y >= PLOT_HEIGHT) continue;
canvas[y][x] = PLOT_FILL;
}

printf("Plot %dx%d\n", PLOT_WIDTH, PLOT_HEIGHT);
for (int y = 0; y < PLOT_HEIGHT; y++) {
for (int x = 0; x < PLOT_WIDTH; x++) {
putchar(canvas[y][x]);
}
putchar('\n');
}
}

#endif // __HMAH_BEZIER_TEST_PLOT_H__

0 comments on commit d933775

Please sign in to comment.