Created
October 27, 2014 21:18
-
-
Save alexrs/96421e70dbfce7a9149c to your computer and use it in GitHub Desktop.
Unit testing using Minunit for the first practical exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static char * test_intToBinary() { | |
int num[SIZE_INT]; | |
int num115[SIZE_INT] = {0,1,1,1,0,0,1,1}; | |
intToBinary(115, num); | |
mu_assert("intToBinary(115) == 01110011", memcmp(num, num115, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_intToComp1Positive() { | |
int num[SIZE_INT]; | |
int num115[SIZE_INT] = {0,1,1,1,0,0,1,1}; | |
intToComp1(115, num); | |
mu_assert("intToComp1(115) == 01110011", memcmp(num, num115, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_intToComp1Negative() { | |
int num[SIZE_INT]; | |
int num115[SIZE_INT] = {1,0,0,0,1,1,0,0}; | |
intToComp1(-115, num); | |
mu_assert("intToComp1(-115) == 10001100", memcmp(num, num115, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_intToComp2Positive() { | |
int num[SIZE_INT]; | |
int num115[SIZE_INT] = {0,1,1,1,1,1,1,1}; | |
intToComp2(127, num); | |
mu_assert("intToComp2(127) == 01111111", memcmp(num, num115, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_intToComp2Negative() { | |
int num[SIZE_INT]; | |
int num115[SIZE_INT] = {1,0,0,0,0,0,0,1}; | |
intToComp2(-127, num); | |
mu_assert("intToComp1(-127) == 10000001", memcmp(num, num115, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * all_tests() { | |
mu_run_test(test_intToBinary); | |
mu_run_test(test_intToComp1Positive); | |
mu_run_test(test_intToComp1Negative); | |
mu_run_test(test_intToComp2Positive); | |
mu_run_test(test_intToComp2Negative); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
char *result = all_tests(); | |
if (result != 0) { | |
printf("%s\n", result); | |
} | |
else { | |
printf("ALL TESTS PASSED\n"); | |
} | |
printf("Tests run: %d\n", tests_run); | |
return result != 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static char * test_FloatToIEEE754inf() { | |
int num[PRECISION_SIZE]; | |
int infRep[PRECISION_SIZE] = {0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToIEEE754(INFINITY, num); | |
mu_assert("FloatToIEE754(inf) == 0 11111111 000000000000000000000000", memcmp(num, infRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToIEEE754minf() { | |
int num[PRECISION_SIZE]; | |
int minfRep[PRECISION_SIZE] = {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToIEEE754(-INFINITY, num); | |
mu_assert("FloatToIEE754(-inf) == 1 11111111 000000000000000000000000", memcmp(num, minfRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToIEEE754nan() { | |
int num[PRECISION_SIZE]; | |
int nanRep[PRECISION_SIZE] = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; | |
FloatToIEEE754(NAN, num); | |
mu_assert("FloatToIEE754(NaN) == 0 11111111 111111111111111111111111", memcmp(num, nanRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToIEEE754zero() { | |
int num[PRECISION_SIZE]; | |
int zeroRep[PRECISION_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; | |
FloatToIEEE754(0, num); | |
mu_assert("FloatToIEE754(zero) == 0 00000000 000000000000000000000000", memcmp(num, zeroRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToIEEE754positive() { | |
int num[PRECISION_SIZE]; | |
int pos_number[PRECISION_SIZE] = {0,1,0,0,0,0,0,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToIEEE754(27, num); | |
mu_assert("FloatToIEE754(27) == 0 10000011 10110000000000000000000", memcmp(num, pos_number, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToIEEE754negative() { | |
int num[PRECISION_SIZE]; | |
int neg_number[PRECISION_SIZE] = {1,1,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToIEEE754(-72, num); | |
mu_assert("FloatToIEE754(-72) == 1 10000101 00100000000000000000000", memcmp(num, neg_number, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToIEEE754Huge() { | |
int num[PRECISION_SIZE]; | |
int huge_number[PRECISION_SIZE] = {0,1,1,1,1,0,1,0,0,0,1,1,0,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,0,1,0,0}; | |
FloatToIEEE754(234213431390809802134213487696125908.0, num); | |
mu_assert("FloatToIEE754(234213431390809802134213487696125908) == 0 11110100 01101000110111001110100", memcmp(num, huge_number, sizeof(num)) == 0); | |
return 0; | |
} | |
int tests_run = 0; | |
static char * all_tests() { | |
mu_run_test(test_FloatToIEEE754inf); | |
mu_run_test(test_FloatToIEEE754minf); | |
mu_run_test(test_FloatToIEEE754nan); | |
mu_run_test(test_FloatToIEEE754zero); | |
mu_run_test(test_FloatToIEEE754positive); | |
mu_run_test(test_FloatToIEEE754negative); | |
mu_run_test(test_FloatToIEEE754Huge); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
char *result = all_tests(); | |
if (result != 0) { | |
printf("%s\n", result); | |
} | |
else { | |
printf("ALL TESTS PASSED\n"); | |
} | |
printf("Tests run: %d\n", tests_run); | |
return result != 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int tests_run = 0; | |
static char * test_IEEE754ToFloatPositive() | |
{ | |
int num[SIZE_FLOAT] = {0,1,0,0,0,1,0,0,1,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0}; | |
float result = IEEE754ToFloat(num); | |
mu_assert("IEEE754ToFloatPositive == 1234.25", result == 1234.25f); | |
return 0; | |
} | |
static char * test_IEEE754ToFloatNegative() | |
{ | |
int num[SIZE_FLOAT] = {1,1,0,0,0,1,0,0,1,0,0,1,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0}; | |
float result = IEEE754ToFloat(num); | |
mu_assert("IEEE754ToFloatPositive == -1234.25", result == -1234.25f); | |
return 0; | |
} | |
static char * test_IEEE754ToFloatNegativeInfinity() | |
{ | |
int num[SIZE_FLOAT] = {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
float result = IEEE754ToFloat(num); | |
mu_assert("test_IEEE754ToFloatNegativeInfinity == -inf", result == -1.0/0.0); | |
return 0; | |
} | |
static char * test_IEEE754ToFloatPositiveInfinity() | |
{ | |
int num[SIZE_FLOAT] = {0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
float result = IEEE754ToFloat(num); | |
mu_assert("test_IEEE754ToFloatPositiveInfinity == inf", result == 1.0/0.0); | |
return 0; | |
} | |
static char * test_IEEE754ToFloatNaN() | |
{ | |
int num[SIZE_FLOAT] = {0,1,1,1,1,1,1,1,1,0,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0}; | |
float result = IEEE754ToFloat(num); | |
mu_assert("IEEE754ToFloatNaN == NaN", result != result); | |
return 0; | |
} | |
static char * all_tests() | |
{ | |
mu_run_test(test_IEEE754ToFloatPositive); | |
mu_run_test(test_IEEE754ToFloatNegative); | |
mu_run_test(test_IEEE754ToFloatNegativeInfinity); | |
mu_run_test(test_IEEE754ToFloatPositiveInfinity); | |
mu_run_test(test_IEEE754ToFloatNaN); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
char *result = all_tests(); | |
if (result != 0) { | |
printf("%s\n", result); | |
} | |
else { | |
printf("ALL TESTS PASSED\n"); | |
} | |
printf("Tests run: %d\n", tests_run); | |
return result != 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int tests_run = 0; | |
static char * test_nextDownInf() { | |
int num[PRECISION_SIZE]; | |
int infRep[PRECISION_SIZE] = {0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
nextDown(infRep, num); | |
mu_assert("nextDown(inf) == 0 11111111 000000000000000000000000", memcmp(num, infRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownMInf() { | |
int num[PRECISION_SIZE]; | |
int minfRep[PRECISION_SIZE] = {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
nextDown(minfRep, num); | |
mu_assert("nextDown(minf) == 1 11111111 000000000000000000000000", memcmp(num, minfRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownZero() { | |
int num[PRECISION_SIZE]; | |
int zeroRep[PRECISION_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
int closestToZero[PRECISION_SIZE] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; | |
nextDown(zeroRep, num); | |
mu_assert("nextDown(zero) == 0 00000000 000000000000000000000001", memcmp(num, closestToZero, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownNotNorm() { | |
int num[PRECISION_SIZE]; | |
int notNormRep[PRECISION_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0}; | |
int closestRep[PRECISION_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1}; | |
nextDown(notNormRep, num); | |
mu_assert("nextDown(NotNorm) == 0 00000000 000000000000000000111111", memcmp(num, closestRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownNaN() { | |
int num[PRECISION_SIZE]; | |
int NaN[PRECISION_SIZE] = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; | |
nextDown(NaN, num); | |
mu_assert("nextDown(NaN) == 0 11111111 11111111111111111111111", memcmp(num, NaN, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownPositive() { | |
int num[PRECISION_SIZE]; | |
int positiveRep[PRECISION_SIZE] = {0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; | |
int closestRep[PRECISION_SIZE] = {0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
nextDown(positiveRep, num); | |
mu_assert("nextDown(Positive) == 0 10000001 000000000000000000000000", memcmp(num, closestRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownNegative() { | |
int num[PRECISION_SIZE]; | |
int negativeRep[PRECISION_SIZE] = {1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; | |
int closestRep[PRECISION_SIZE] = {1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0}; | |
nextDown(negativeRep, num); | |
mu_assert("nextDown(Negative) == 1 10000001 000000000000000000000010", memcmp(num, closestRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_nextDownMantissaOne() { | |
int num[PRECISION_SIZE]; | |
int mantissaOne[PRECISION_SIZE] = {1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; | |
int closestRep[PRECISION_SIZE] = {1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
nextDown(mantissaOne, num); | |
mu_assert("nextDown(MantissaOne) == 1 10000010 000000000000000000000000", memcmp(num, closestRep, sizeof(num)) == 0); | |
return 0; | |
} | |
int tests_run = 0; | |
static char * all_tests() { | |
mu_run_test(test_nextDownInf); | |
mu_run_test(test_nextDownMInf); | |
mu_run_test(test_nextDownZero); | |
mu_run_test(test_nextDownNotNorm); | |
mu_run_test(test_nextDownNaN); | |
mu_run_test(test_nextDownPositive); | |
mu_run_test(test_nextDownNegative); | |
mu_run_test(test_nextDownMantissaOne); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
char *result = all_tests(); | |
if (result != 0) { | |
printf("%s\n", result); | |
} | |
else { | |
printf("ALL TESTS PASSED\n"); | |
} | |
printf("Tests run: %d\n", tests_run); | |
return result != 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int tests_run = 0; | |
static char * test_FloatToDoublePositiveInf() { | |
int num[PRECISION_SIZE]; | |
int infRep[PRECISION_SIZE] = {0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToDouble(INFINITY, num); | |
mu_assert("FloatToDouble(inf) == 011111111111000000000000000000000000000000000000000000000000000", memcmp(num, infRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToDoubleNegativeInf() { | |
int num[PRECISION_SIZE]; | |
int minfRep[PRECISION_SIZE] = {1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToDouble(-INFINITY, num); | |
mu_assert("FloatToDouble(-inf) == 111111111111000000000000000000000000000000000000000000000000000", memcmp(num, minfRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToDoubleNaN() { | |
int num[PRECISION_SIZE]; | |
int nanRep[PRECISION_SIZE] = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; | |
FloatToDouble(NAN, num); | |
mu_assert("FloatToDouble(NaN) == 0111111111111111111111111111111111111111111111111111111111111111", memcmp(num, nanRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToDoubleZero() { | |
int num[PRECISION_SIZE]; | |
int zeroRep[PRECISION_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; | |
FloatToDouble(0, num); | |
mu_assert("FloatToDouble(zero) == 0 00000000 000000000000000000000000", memcmp(num, zeroRep, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToDoublePositive() { | |
int num[PRECISION_SIZE]; | |
int pos_number[PRECISION_SIZE] = {0,1,0,0,0,0,0,0,0,0,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToDouble(27.5, num); | |
mu_assert("FloatToDouble(27.5) == 01000000 00111011 10000000 0000000000000000 00000000 00000000 00000000", memcmp(num, pos_number, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToDoubleNegative() { | |
int num[PRECISION_SIZE]; | |
int neg_number[PRECISION_SIZE] = {1,1,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToDouble(-72.5, num); | |
mu_assert("FloatToDouble(-72.5) == 1 10000000101 0010001000000000000000000000000000000000000000000000", memcmp(num, neg_number, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * test_FloatToDoubleHuge() { | |
int num[PRECISION_SIZE]; | |
int huge_number[PRECISION_SIZE] = {0,1,0,0,0,1,1,1,0,1,0,0,0,1,1,0,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
FloatToDouble(234213422514880577886321096999829504.0, num); | |
mu_assert("FloatToDouble(234213431390809802134213487696125908) == 0 10001110100 0110100011011100111010001110010101101111111110010000", memcmp(num, huge_number, sizeof(num)) == 0); | |
return 0; | |
} | |
static char * all_tests() { | |
mu_run_test(test_FloatToDoublePositiveInf); | |
mu_run_test(test_FloatToDoubleNegativeInf); | |
mu_run_test(test_FloatToDoubleNaN); | |
mu_run_test(test_FloatToDoubleZero); | |
mu_run_test(test_FloatToDoublePositive); | |
mu_run_test(test_FloatToDoubleNegative); | |
mu_run_test(test_FloatToDoubleHuge); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
char *result = all_tests(); | |
if (result != 0) { | |
printf("%s\n", result); | |
} | |
else { | |
printf("ALL TESTS PASSED\n"); | |
} | |
printf("Tests run: %d\n", tests_run); | |
return result != 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment