25-Add more tests for floats
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include "math.h"
|
||||
#include <math.h>
|
||||
#include "../print/print.h"
|
||||
|
||||
int cAdd(int x, int y)
|
||||
@@ -72,6 +73,20 @@ int math_testExpected(char *name, int x, int y, int expected, int result)
|
||||
}
|
||||
}
|
||||
|
||||
int math_testExpected_f(char *name, double x, double y, double expected, double result)
|
||||
{
|
||||
if (expected == result)
|
||||
{
|
||||
float_succPrefixTwo(name, x, y, expected, result);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
float_errPrefixTwo(name, x, y, expected, result);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int math_test(char *name, int (*correctFunction)(int, int), int (*testFunction)(int, int), int x, int y)
|
||||
{
|
||||
int expected = correctFunction(x, y);
|
||||
@@ -79,10 +94,10 @@ int math_test(char *name, int (*correctFunction)(int, int), int (*testFunction)(
|
||||
return math_testExpected(name, x, y, expected, result);
|
||||
}
|
||||
|
||||
float math_test_f(char *name, float (*correctFunction)(float, float), float (*testFunction)(float, float), float x, float y) {
|
||||
float expected = correctFunction(x, y);
|
||||
float result = testFunction(x, y);
|
||||
return math_testExpected(name, x, y, expected, result);
|
||||
float math_test_f(char *name, double (*correctFunction)(double, double), double (*testFunction)(double, double), double x, double y) {
|
||||
double expected = correctFunction(x, y);
|
||||
double result = testFunction(x, y);
|
||||
return math_testExpected_f(name, x, y, expected, result);
|
||||
}
|
||||
|
||||
int math_testOneArg(char *name, int (*correctFunction)(int), int (*testFunction)(int), int x)
|
||||
@@ -101,18 +116,18 @@ int math_testOneArg(char *name, int (*correctFunction)(int), int (*testFunction)
|
||||
}
|
||||
}
|
||||
|
||||
float math_testOneArg_f(char *name, float (*correctFunction)(float), float (*testFunction)(float), float x)
|
||||
double math_testOneArg_f(char *name, double (*correctFunction)(double), double (*testFunction)(double), double x)
|
||||
{
|
||||
float expected = correctFunction(x);
|
||||
float result = testFunction(x);
|
||||
if (expected == result)
|
||||
{
|
||||
succPrefixOne(name, x, expected, result);
|
||||
float_succPrefixOne(name, x, expected, result);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
errPrefixOne(name, x, expected, result);
|
||||
float_errPrefixOne(name, x, expected, result);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -125,6 +140,16 @@ void math_simpleTest(char *name, int expected, int result) {
|
||||
}
|
||||
}
|
||||
|
||||
int math_argumentTest_f(char* name, double expected, double result) {
|
||||
if (expected == result) {
|
||||
succ_f(name, expected, result);
|
||||
return 0;
|
||||
} else {
|
||||
err_f(name, expected, result);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int runMathTests()
|
||||
{
|
||||
printf("\nAddition Tests \n");
|
||||
@@ -170,6 +195,12 @@ int runMathTests()
|
||||
math_test_f("fadd", fcAdd, fadd, 2.0, 0.0);
|
||||
math_test_f("fadd", fcAdd, fadd, 1.0, 5.0);
|
||||
math_test_f("fadd", fcAdd, fadd, -1.0, -1.0);
|
||||
math_test_f("fadd", fcAdd, fadd, 10.0, 10.0);
|
||||
math_test_f("fadd", fcAdd, fadd, 1.337, 0.0);
|
||||
math_test_f("fadd", fcAdd, fadd, 1.5, 2.0);
|
||||
math_test_f("fadd", fcAdd, fadd, -10.0, 10.0);
|
||||
math_test_f("fadd", fcAdd, fadd, -10.0, -10.0);
|
||||
math_test_f("fadd", fcAdd, fadd, -10.0, -1.0);
|
||||
|
||||
printf("\nFloat Subtraction Tests \n");
|
||||
math_test_f("fsub", fcSub, fsub, 0.0, 0.0);
|
||||
@@ -177,6 +208,8 @@ int runMathTests()
|
||||
math_test_f("fsub", fcSub, fsub, 2.0, 0.0);
|
||||
math_test_f("fsub", fcSub, fsub, 1.0, 5.0);
|
||||
math_test_f("fsub", fcSub, fsub, -1.0, -1.0);
|
||||
math_test_f("fsub", fcSub, fsub, -1.0, 0.0);
|
||||
math_test_f("fsub", fcSub, fsub, -1.0, 1.337);
|
||||
|
||||
printf("\nFloat Multiplication Tests \n");
|
||||
math_test_f("fmul", fcMul, fmul, 0.0, 0.0);
|
||||
@@ -189,6 +222,7 @@ int runMathTests()
|
||||
math_testOneArg_f("fneg", fcNeg, fneg, 0.0);
|
||||
math_testOneArg_f("fneg", fcNeg, fneg, 1.0);
|
||||
math_testOneArg_f("fneg", fcNeg, fneg, -1.0);
|
||||
math_testOneArg_f("fneg", fcNeg, fneg, -10.0);
|
||||
|
||||
printf("\nFloat Identity Tests\n");
|
||||
math_testOneArg_f("fid", fcId, fid, 0.0);
|
||||
@@ -200,6 +234,10 @@ int runMathTests()
|
||||
math_testOneArg("selfMinus", cSelfMinus, selfMinus, 0);
|
||||
math_testOneArg("selfMinus", cSelfMinus, selfMinus, 100);
|
||||
math_testOneArg("selfMinus", cSelfMinus, selfMinus, -50);
|
||||
math_testOneArg_f("fselfMinus", fcSelfMinus, fselfMinus, 5);
|
||||
math_testOneArg_f("fselfMinus", fcSelfMinus, fselfMinus, 0);
|
||||
math_testOneArg_f("fselfMinus", fcSelfMinus, fselfMinus, 100);
|
||||
math_testOneArg_f("fselfMinus", fcSelfMinus, fselfMinus, -50);
|
||||
math_simpleTest("precedence t1", 16, t1());
|
||||
math_simpleTest("precedence t2", 16, t2());
|
||||
math_simpleTest("precedence t3", 16, t3());
|
||||
@@ -208,4 +246,52 @@ int runMathTests()
|
||||
math_simpleTest("precedence t6", 16, t6());
|
||||
math_simpleTest("precedence t7", 16, t7());
|
||||
math_simpleTest("precedence t8", 18, t8());
|
||||
|
||||
printf("\nFloat And Integer Addition\n");
|
||||
math_argumentTest_f("mixadd(1.0, 1)", 2.0, mixadd(1.0, 1));
|
||||
math_argumentTest_f("mixadd(0.0, 0)", 0.0, mixadd(0.0, 0));
|
||||
math_argumentTest_f("mixadd(10.0, 10)", 20.0, mixadd(10.0, 10));
|
||||
math_argumentTest_f("mixadd(1.337, 0)", 1.337, mixadd(1.337, 0));
|
||||
math_argumentTest_f("mixadd(1.5, 2)", 3.5, mixadd(1.5, 2));
|
||||
math_argumentTest_f("mixadd(-10.0, 10)", 0.0, mixadd(-10.0, 10));
|
||||
math_argumentTest_f("mixadd(-10.0, -10)", -20.0, mixadd(-10.0, -10));
|
||||
math_argumentTest_f("mixadd(-10.0, -1)", -11.0, mixadd(-10.0, -1));
|
||||
|
||||
printf("\nFloat And Integer Subtraction\n");
|
||||
math_argumentTest_f("mixsub(10.0, 10)", 0.0, mixsub(10.0, 10));
|
||||
math_argumentTest_f("mixsub(0.0, 0)", 0.0, mixsub(0.0, 0));
|
||||
math_argumentTest_f("mixsub(-10.0, -10)", -20.0, mixsub(-10.0, -10));
|
||||
math_argumentTest_f("mixsub(-1.0, 1)", -2.0, mixsub(-1.0, 1));
|
||||
math_argumentTest_f("mixsub(0.0, -1)", 1.0, mixsub(0.0, -1));
|
||||
|
||||
printf("\nFloat And Integer Multiplication\n");
|
||||
math_argumentTest_f("mixmul(0.0, 0)", 0.0, mixmul(0.0, 0));
|
||||
math_argumentTest_f("mixmul(10.0, 0)", 0.0, mixmul(10.0, 0));
|
||||
math_argumentTest_f("mixmul(-1.0, 0)", 0.0, mixmul(-1.0, 0));
|
||||
math_argumentTest_f("mixmul(-10.0, 0)", 0.0, mixmul(-10.0, 0));
|
||||
math_argumentTest_f("mixmul(10.0, -1)", -10.0, mixmul(10.0, -1));
|
||||
math_argumentTest_f("mixmul(0.0, -1)", 0.0, mixmul(0.0, -1));
|
||||
math_argumentTest_f("mixmul(-1.0, -1)", 10, mixmul(-1.0, -1));
|
||||
math_argumentTest_f("mixmul(1.0, 1)", 1, mixmul(1.0, 1));
|
||||
math_argumentTest_f("mixmul(1.0, 1)", 0, mixmul(0.0, 1));
|
||||
math_argumentTest_f("mixmul(1.0, 1)", -1, mixmul(-1.0, 1));
|
||||
math_argumentTest_f("mixmul(1.5, 6)", 9, mixmul(1.5, 6));
|
||||
math_argumentTest_f("mixmul(2.0, 2)", 4, mixmul(2.0, 2));
|
||||
math_argumentTest_f("mixmul(100.0, 10)", 1000.0, mixmul(100.0, 10));
|
||||
|
||||
printf("\nFloat And Integer Division\n");
|
||||
math_argumentTest_f("mixdiv(10.0, 0)", INFINITY, mixdiv(10.0, 0));
|
||||
math_argumentTest_f("mixdiv(10.0, 1)", 10.0, mixdiv(10.0, 1));
|
||||
math_argumentTest_f("mixdiv(0.0, 1)", 0.0, mixdiv(0.0, 1));
|
||||
math_argumentTest_f("mixdiv(-1.0, 1)", -1.0, mixdiv(-1.0, 1));
|
||||
math_argumentTest_f("mixdiv(1.5, 2)", 1.5 / 2, mixdiv(1.5, 2));
|
||||
math_argumentTest_f("mixdiv(1.0, 10)", 1.0 / 10, mixdiv(1.0, 10));
|
||||
math_argumentTest_f("mixdiv(1.0, 100)", 1.0 / 100, mixdiv(1.0, 100));
|
||||
math_argumentTest_f("mixdiv(1.0, 1000)", 1.0 / 1000, mixdiv(1.0, 1000));
|
||||
math_argumentTest_f("mixdiv(1.0, 10000)", 1.0 / 10000, mixdiv(1.0, 10000));
|
||||
math_argumentTest_f("mixdiv(1.0, 100000)", 1.0 / 100000, mixdiv(1.0, 100000));
|
||||
math_argumentTest_f("mixdiv(1.0, 1000000)", 1.0 / 1000000, mixdiv(1.0, 1000000));
|
||||
math_argumentTest_f("mixdiv(1.0, 10000000)", 1.0 / 10000000, mixdiv(1.0, 10000000));
|
||||
|
||||
|
||||
}
|
||||
@@ -22,4 +22,8 @@ double fsub(double x, double y);
|
||||
double fmul(double x, double y);
|
||||
double fneg(double x);
|
||||
double fid(double x);
|
||||
double fselfMinus(double x);
|
||||
double fselfMinus(double x);
|
||||
double mixadd(double x, int y);
|
||||
double mixsub(double x, int y);
|
||||
double mixmul(double x, int y);
|
||||
double mixdiv(double x, int y);
|
||||
@@ -30,12 +30,20 @@ void err(char* name, int expected, int result) {
|
||||
|
||||
void succ_f(char* name, double expected, double result) {
|
||||
incSuccess();
|
||||
printf("\033[0;32mSUCCESS:\t%s:\tGOT: %f\tExpected: %f\033[0;0m\n", name, result, expected);
|
||||
if (expected <= 0.000001 || result <= 0.000001) {
|
||||
printf("\033[0;32mSUCCESS:\t%s:\tGOT: %.*e\tExpected: %.*e\033[0;0m\n", name, result, expected);
|
||||
} else {
|
||||
printf("\033[0;32mSUCCESS:\t%s:\tGOT: %f\tExpected: %f\033[0;0m\n", name, result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
void err_f(char* name, double expected, double result) {
|
||||
incFailure();
|
||||
printf("\033[0;31mERROR:\t\t%s:\tGOT: %f\tExpected: %f\033[0;0m\n", name, result, expected);
|
||||
if (expected <= 0.000001 || result <= 0.000001) {
|
||||
printf("\033[0;31mERROR:\t\t%s:\tGOT: %.*e\tExpected: %.*e\033[0;0m\n", name, result, expected);
|
||||
} else {
|
||||
printf("\033[0;31mERROR:\t\t%s:\tGOT: %f\tExpected: %Df\033[0;0m\n", name, result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
void succPrefixOne(char* name, int x, int expected, int result) {
|
||||
@@ -48,6 +56,16 @@ void errPrefixOne(char* name, int x, int expected, int result) {
|
||||
printf("\033[0;31mERROR:\t\t%s(%d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
void float_succPrefixOne(char* name, double x, double expected, double result) {
|
||||
incSuccess();
|
||||
printf("\033[0;32mSUCCESS:\t%s(%f)\tGOT: %f\tExpected: %f\033[0;0m\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
void float_errPrefixOne(char* name, double x, double expected, double result) {
|
||||
incFailure();
|
||||
printf("\033[0;31mERROR:\t\t%s(%f)\tGOT: %f\tExpected: %f\033[0;0m\n", name, x, result, expected);
|
||||
}
|
||||
|
||||
void succPrefixTwo(char* name, int x, int y, int expected, int result) {
|
||||
incSuccess();
|
||||
printf("\033[0;32mSUCCESS:\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected);
|
||||
@@ -58,6 +76,16 @@ void errPrefixTwo(char* name, int x, int y, int expected, int result) {
|
||||
printf("\033[0;31mERROR:\t\t%s(%d, %d)\tGOT: %d\tExpected: %d\033[0;0m\n", name, x, y, result, expected);
|
||||
}
|
||||
|
||||
void float_succPrefixTwo(char* name, double x, double y, double expected, double result) {
|
||||
incSuccess();
|
||||
printf("\033[0;32mSUCCESS:\t%s(%f, %f)\tGOT: %f\tExpected: %f\033[0;0m\n", name, x, y, result, expected);
|
||||
}
|
||||
|
||||
void float_errPrefixTwo(char* name, double x, double y, double expected, double result) {
|
||||
incFailure();
|
||||
printf("\033[0;31mERROR:\t\t%s(%f, %f)\tGOT: %f\tExpected: %f\033[0;0m\n", name, x, y, result, expected);
|
||||
}
|
||||
|
||||
void bool_succPrefixOne(char* name, bool x, bool expected, bool result) {
|
||||
incSuccess();
|
||||
printf("\033[0;32mSUCCESS:\t%s%s\tGOT: %s\tExpected: %s\033[0;0m\n", name, printBool(x), printBool(result), printBool(expected));
|
||||
|
||||
@@ -12,9 +12,15 @@ void err_f(char* name, double expected, double result);
|
||||
void succPrefixOne(char* name, int x, int expected, int result);
|
||||
void errPrefixOne(char* name, int x, int expected, int result);
|
||||
|
||||
void float_succPrefixOne(char* name, double x, double expected, double result);
|
||||
void float_errPrefixOne(char* name, double x, double expected, double result);
|
||||
|
||||
void succPrefixTwo(char* name, int x, int y, int expected, int result);
|
||||
void errPrefixTwo(char* name, int x, int y, int expected, int result);
|
||||
|
||||
void float_succPrefixTwo(char* name, double x, double y, double expected, double result);
|
||||
void float_errPrefixTwo(char* name, double x, double y, double expected, double result);
|
||||
|
||||
void succInfixTwo(char* name, int x, int y, int expected, int result);
|
||||
void errInfixTwo(char* name, int x, int y, int expected, int result);
|
||||
|
||||
|
||||
@@ -395,6 +395,20 @@ function t8(): int {
|
||||
return (1 + 5) * 3;
|
||||
}
|
||||
|
||||
function mixadd(x: float, y: int): float {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
function mixsub(x: float, y: int): float {
|
||||
return x - y;
|
||||
}
|
||||
|
||||
function mixmul(x: float, y: int): float {
|
||||
return x * y;
|
||||
}
|
||||
|
||||
function mixdiv(x: float, y: int): float {
|
||||
return x / y;
|
||||
}
|
||||
|
||||
add(1, 1);
|
||||
|
||||
Reference in New Issue
Block a user