Original author: Pierre Sarrazin. Preserve upstream GPLv3-or-later and file-specific notices. Source: http://sarrazip.com/dev/coco-planes-0.1.0.tar.gz Archive SHA-256: 29191e6ba225965caa637ee726e1189639dc7fcdf2b68310fb93fbffd56c9c52
147 lines
3.6 KiB
C
147 lines
3.6 KiB
C
// Unit testing program.
|
|
|
|
#include "Score.h"
|
|
#include "util.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
#define utAssert(cond) \
|
|
do { \
|
|
if (!(cond)) \
|
|
_utFailAssert(#cond, __FILE__, __LINE__, NULL, 0); \
|
|
} while (0)
|
|
|
|
|
|
#define utAssertF(cond, fmt, ...) \
|
|
do { \
|
|
if (!(cond)) \
|
|
_utFailAssert(#cond, __FILE__, __LINE__, (fmt), __VA_ARGS__); \
|
|
} while (0)
|
|
|
|
|
|
size_t numFailedAsserts = 0;
|
|
|
|
|
|
void
|
|
_utFailAssert(const char *cond, const char *filename, int lineno, const char *fmt, ...)
|
|
{
|
|
printf("*** ASSERT FAILED: %s:%d: %s: ", filename, lineno, cond);
|
|
if (fmt)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
vprintf(fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
putchar('\n');
|
|
++numFailedAsserts;
|
|
exit(1);
|
|
}
|
|
|
|
|
|
uint16_t
|
|
bradToDeg(uint8_t brads)
|
|
{
|
|
return (uint16_t) brads * 180 / 128;
|
|
}
|
|
|
|
|
|
//////////
|
|
|
|
|
|
void
|
|
test_Score(void)
|
|
{
|
|
Score s;
|
|
Score_init(&s);
|
|
utAssert(!Score_isWinningScore(&s));
|
|
for (byte i = 0; i < WINNING_SCORE; ++i)
|
|
{
|
|
Score_addPoint(&s);
|
|
//printf("%u,%u.\n", s.digits[0], s.digits[1]);
|
|
BOOL isWinningScore = Score_isWinningScore(&s);
|
|
utAssertF((isWinningScore != 0) == (i + 1 == WINNING_SCORE),
|
|
"i=%u, isWinningScore=%u, WINNING_SCORE=%u", i, isWinningScore, WINNING_SCORE);
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
test_approxAtan2Signed8(void)
|
|
{
|
|
int8_t brads;
|
|
|
|
brads = approxAtan2Signed8(0, +100); // right
|
|
utAssertF(brads == 0, "brads=%d", brads);
|
|
brads = approxAtan2Signed8(0, -100); // left
|
|
utAssertF(brads == -128, "brads=%d", brads);
|
|
brads = approxAtan2Signed8(+100, 0); // down, b/c y axis points down
|
|
utAssertF(brads == -64, "brads=%d", brads);
|
|
brads = approxAtan2Signed8(-100, 0); // up
|
|
utAssertF(brads == +64, "brads=%d", brads);
|
|
|
|
brads = approxAtan2Signed8(-100, +100); // up and right
|
|
utAssertF(brads == +32, "brads=%d", brads);
|
|
brads = approxAtan2Signed8(-100, -100); // up and left
|
|
utAssertF(brads == +96, "brads=%d", brads);
|
|
brads = approxAtan2Signed8(+100, +100); // down and right
|
|
utAssertF(brads == -32, "brads=%d", brads);
|
|
brads = approxAtan2Signed8(+100, -100); // down and left
|
|
utAssertF(brads == -96, "brads=%d", brads);
|
|
|
|
// Check that no asserts fail when computing various angles.
|
|
//
|
|
for (int16_t y = -8; y <= 8; ++y)
|
|
for (int16_t x = -127; x <= 127; ++x)
|
|
approxAtan2Signed8((int8_t) y, (int8_t) x);
|
|
|
|
}
|
|
|
|
|
|
void
|
|
test_computeAngleIfNotTooFar(void)
|
|
{
|
|
int8_t angleInBrads = 0;
|
|
BOOL success = FALSE;
|
|
|
|
// Target is up and to the right of the shooter.
|
|
success = computeAngleIfNotTooFar(128, 96, 0, 160, 80, 64, 64 * 64, &angleInBrads);
|
|
utAssert(success);
|
|
//printf("angleInBrads=%d (%u deg)\n", angleInBrads, bradToDeg(angleInBrads));
|
|
utAssertF(angleInBrads == +16, "angleInBrads=%d", angleInBrads);
|
|
|
|
// Target is down and to the right of the shooter.
|
|
success = computeAngleIfNotTooFar(128, 96, 0, 160, 112, 64, 64 * 64, &angleInBrads);
|
|
utAssert(success);
|
|
//printf("angleInBrads=%d (%u deg)\n", angleInBrads, bradToDeg(angleInBrads));
|
|
utAssertF(angleInBrads == -16, "angleInBrads=%d", angleInBrads);
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
enable6847T1TrueLowerCase();
|
|
* (char *) 0x0167 = 57; /* write RTS instruction */
|
|
|
|
if (* (unsigned short *) 0xFFF8 == 0xFEF7) // if CoCo 3
|
|
{
|
|
rgb();
|
|
width(80);
|
|
cls(255);
|
|
palette(8, 63);
|
|
palette(0, 0);
|
|
setBorderColor(0);
|
|
}
|
|
|
|
test_Score();
|
|
test_approxAtan2Signed8();
|
|
test_computeAngleIfNotTooFar();
|
|
|
|
if (numFailedAsserts == 0)
|
|
printf("SUCCESS.\n");
|
|
else
|
|
printf("%u FAILED ASSERTS.\n", numFailedAsserts);
|
|
return 0;
|
|
}
|