Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #define TSYSEQ
- #include "syseq.c"
- enum outcome { FAIL, PASS };
- typedef int (*testfn_t)(void);
- char const * const OUTCOME_STR[] = {"FAIL", "PASS"};
- int
- t_static_get_var_name(void)
- {/*{{{*/
- char *expect[] = {
- "a", "c", "z",
- "Aa", "Ac", "Az",
- "Ba", "Bc", "Bz",
- };
- int input[] = {
- 0, 2, 25,
- 26, 28, 51,
- 52, 54, 77,
- };
- size_t inputlen = 9;
- char const * result;
- for(int i = 0; i < inputlen; ++i){
- result = get_var_name(input[i]);
- printf(" %i : %s (expect %s)\n", input[i], result, expect[i]);
- if(strcmp(result, expect[i]) != 0)
- return FAIL;
- }
- return PASS;
- }/*}}}*/
- int
- t_get_sum(void)
- {/*{{{*/
- struct equation eq = {
- .left = {{2, 3, 5, 7}, 4},
- .right = 82
- };
- struct tuple solution = {{4, 3, 6, 5}, 4};
- double result = get_sum(&eq, &solution);
- printf(" equation: ");
- print_equation(&eq);
- printf(" solution: ");
- print_tuple(&solution);
- printf(" result: %0.2f\n", result);
- return result == eq.right;
- }/*}}}*/
- int
- t_check_sum(void)
- {/*{{{*/
- struct equation eq = {
- .left = {{2, 3, 5, 7}, 4},
- .right = 82
- };
- struct tuple solution = {{4, 3, 6, 5}, 4};
- printf(" same dataset as get_sum test\n");
- if(check_sum(&eq, &solution) == 0)
- return FAIL;
- printf(" modify soltution to be incorrect (%0.2f -> %0.2f)\n", solution.var[0], 0.0);
- solution.var[0] = 0;
- return check_sum(&eq, &solution) == 0;
- }/*}}}*/
- int
- t_print_equation(void)
- {/*{{{*/
- struct equation eq = {
- .left = {{2, 4, 6, 7}, 4},
- .right = 12
- };
- puts("Note: Maintainer must review print output manually.");
- print_equation(&eq);
- return PASS;
- }/*}}}*/
- int
- t_print_tuple(void)
- {/*{{{*/
- struct tuple tuples[] = {
- {{2,5,7,13}, 4},
- {.type = T_NOSOLUTION},
- {.type = T_INFINITE},
- {.type = T_ERROR},
- };
- char const * descriptions[] = {
- "Normal 4-tuple",
- "No solutions",
- "Infinite solutions",
- "Error",
- };
- puts("Note: Maintainer must review print output manually.");
- for(int i = 0; i < 4; ++i){
- puts(descriptions[i]);
- printf(" ");
- print_tuple(tuples + i);
- }
- return PASS;
- }/*}}}*/
- int
- t_tuple_type_str(void)
- {/*{{{*/
- char *expect[] = {
- "One solution exists",
- "No solutions exist",
- "Infinite solutions exist",
- "An unknown error has occurred",
- "Invalid type",
- };
- for(int i = 0; i <= T_END; ++i){
- printf(" %i : %s (expect: %s)\n", i, tuple_type_str(i), expect[i]);
- if(tuple_type_str(i) != expect[i])
- return FAIL; /* Should work assuming compiler re-uses .rodata addresses for string literals */
- }
- return PASS;
- }/*}}}*/
- int
- run_test(testfn_t fn, char const *description)
- {/*{{{*/
- int result;
- puts(description);
- result = fn();
- puts(OUTCOME_STR[result]);
- putchar('\n');
- return result;
- }/*}}}*/
- int
- main(void)
- {
- int passed = 0;
- int total = 0;
- struct { testfn_t fn; char const *description; int end; } args[] = {
- {t_tuple_type_str, "tuple_type_str : 0..T_END"},
- {t_static_get_var_name, "get_var_name (static)"},
- {t_get_sum, "get_sum"},
- {t_check_sum, "check_sum"},
- {t_print_equation, "print_equation (manual review)"},
- {t_print_tuple, "print_tuple (manual review)"},
- {.end = 1}
- };
- for(int i = 0; !args[i].end; ++i){
- passed += run_test(args[i].fn, args[i].description);
- ++total;
- }
- printf("%i of %i tests passed\n", passed, total);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement