From ddbbfef4562b607aa644ea2041b05c5f399ecdba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 23 Aug 2018 02:46:40 +0200 Subject: [PATCH] Separate tests --- make.sh | 2 +- raycastlib.c => raycastlib.h | 70 ++--------------------------- test.c | 87 ++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 67 deletions(-) rename raycastlib.c => raycastlib.h (76%) create mode 100644 test.c diff --git a/make.sh b/make.sh index dcf8923..4ee764c 100755 --- a/make.sh +++ b/make.sh @@ -1,2 +1,2 @@ #!/bin/bash -clear; clear; g++ -x c -fmax-errors=5 -pedantic -Wall -Wextra -o test raycastlib.c 2>&1 >/dev/null && ./test +clear; clear; g++ -x c -fmax-errors=5 -pedantic -Wall -Wextra -o test test.c 2>&1 >/dev/null && ./test diff --git a/raycastlib.c b/raycastlib.h similarity index 76% rename from raycastlib.c rename to raycastlib.h index e72cf98..6d70ffd 100644 --- a/raycastlib.c +++ b/raycastlib.h @@ -1,5 +1,7 @@ +#ifndef RAYCASTLIB_H +#define RAYCASTLIB_H + #include -#include /** author: Miloslav "drummyfish" Ciz @@ -66,16 +68,6 @@ void castRayMultiHit(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t), //============================================================================= // privates -#define logVector2D(v) printf("[%d,%d]\n",v.x,v.y) -#define logRay(r) printf("ray:\n");\ - printf(" start: "); logVector2D(r.start);\ - printf(" dir: "); logVector2D(r.direction); -#define logHitResult(h) printf("hit:\n");\ - printf(" sqaure: "); logVector2D(h.square);\ - printf(" pos: "); logVector2D(h.position);\ - printf(" dist: %d", h.distance);\ - printf(" texcoord: %d", h.textureCoord); - uint16_t sqrtInt(uint32_t value) { uint32_t result = 0; @@ -250,58 +242,4 @@ HitResult castRay(Ray ray, int16_t (*arrayFunc)(int16_t, int16_t), return result; } -int16_t testArrayFunc(int16_t x, int16_t y) -{ - return (x < 0 || y < 0 || x > 9 || y > 9) ? 1 : 0; -} - -/** - Simple automatic test function. -*/ - -int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY, - int16_t expectSquareX, int16_t expectSquareY, int16_t expectPointX, - int16_t expectPointY, int16_t tolerateError) -{ - Ray r; - - r.start.x = startX; - r.start.y = startY; - r.direction.x = dirX; - r.direction.y = dirY; - - HitResult h = castRay(r,testArrayFunc,20); - - return - h.square.x == expectSquareX && - h.square.y == expectSquareY && - h.position.x <= expectPointX + tolerateError && - h.position.x >= expectPointX - tolerateError && - h.position.y <= expectPointY + tolerateError && - h.position.y >= expectPointY - tolerateError; -} - -int test() -{ - if (!testSingleRay( - 3 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, - 4 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, - 100, 50, - 10, 7, - 10240, 7936, - 16)) - return 0; - - return 1; -} - -int main() -{ - printf("%d\n",test()); - - return 0; -} - -#undef logVector2D -#undef logRay -#undef logHitResult +#endif diff --git a/test.c b/test.c new file mode 100644 index 0000000..323459e --- /dev/null +++ b/test.c @@ -0,0 +1,87 @@ +#include "raycastlib.h" +#include + +void logVector2D(Vector2D v) +{ + printf("[%d,%d]\n",v.x,v.y); +} + +void logRay(Ray r) +{ + printf("ray:\n"); + printf(" start: "); + logVector2D(r.start); + printf(" dir: "); + logVector2D(r.direction); +} + +void logHitResult(HitResult h) +{ + printf("hit:\n");\ + printf(" sqaure: "); + logVector2D(h.square); + printf(" pos: "); + logVector2D(h.position); + printf(" dist: %d", h.distance); + printf(" texcoord: %d", h.textureCoord); +} + +int16_t testArrayFunc(int16_t x, int16_t y) +{ + return (x < 0 || y < 0 || x > 9 || y > 9) ? 1 : 0; +} + +/** + Simple automatic test function. +*/ + +int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY, + int16_t expectSquareX, int16_t expectSquareY, int16_t expectPointX, + int16_t expectPointY, int16_t tolerateError) +{ + Ray r; + + r.start.x = startX; + r.start.y = startY; + r.direction.x = dirX; + r.direction.y = dirY; + + printf("- casting ray:\n"); + logRay(r); + + HitResult h = castRay(r,testArrayFunc,20); + + printf("- result:\n"); + logHitResult(h); + + int result = + h.square.x == expectSquareX && + h.square.y == expectSquareY && + h.position.x <= expectPointX + tolerateError && + h.position.x >= expectPointX - tolerateError && + h.position.y <= expectPointY + tolerateError && + h.position.y >= expectPointY - tolerateError; + + if (result) + printf("\nOK\n"); + else + printf("\nFAIL\n"); + + return result; +} + +int main() +{ + printf("Testing raycastlib.\n"); + + if (!testSingleRay( + 3 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, + 4 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2, + 100, 50, + 10, 7, + 10240, 7936, + 16)) + return 1; + + return 0; +}