Advertisement
B1KMusic

this is what a unit test generally looks like

May 6th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. #include <string.h>
  2.  
  3. #define TSYSEQ
  4. #include "syseq.c"
  5.  
  6. enum outcome { FAIL, PASS };
  7. typedef int (*testfn_t)(void);
  8.  
  9. char const * const OUTCOME_STR[] = {"FAIL", "PASS"};
  10.  
  11. int
  12. t_static_get_var_name(void)
  13. {/*{{{*/
  14.     char *expect[] = {
  15.         "a",  "c", "z",
  16.         "Aa", "Ac", "Az",
  17.         "Ba", "Bc", "Bz",
  18.     };
  19.     int input[] = {
  20.         0, 2, 25,
  21.         26, 28, 51,
  22.         52, 54, 77,
  23.     };
  24.     size_t inputlen = 9;
  25.     char const * result;
  26.  
  27.     for(int i = 0; i < inputlen; ++i){
  28.         result = get_var_name(input[i]);
  29.         printf("  %i : %s (expect %s)\n", input[i], result, expect[i]);
  30.  
  31.         if(strcmp(result, expect[i]) != 0)
  32.             return FAIL;
  33.     }
  34.  
  35.     return PASS;
  36. }/*}}}*/
  37.  
  38. int
  39. t_get_sum(void)
  40. {/*{{{*/
  41.     struct equation eq = {
  42.         .left = {{2, 3, 5, 7}, 4},
  43.         .right = 82
  44.     };
  45.     struct tuple solution = {{4, 3, 6, 5}, 4};
  46.     double result = get_sum(&eq, &solution);
  47.  
  48.     printf("  equation: ");
  49.     print_equation(&eq);
  50.     printf("  solution: ");
  51.     print_tuple(&solution);
  52.     printf("  result: %0.2f\n", result);
  53.  
  54.     return result == eq.right;
  55. }/*}}}*/
  56.  
  57. int
  58. t_check_sum(void)
  59. {/*{{{*/
  60.     struct equation eq = {
  61.         .left = {{2, 3, 5, 7}, 4},
  62.         .right = 82
  63.     };
  64.     struct tuple solution = {{4, 3, 6, 5}, 4};
  65.  
  66.     printf("  same dataset as get_sum test\n");
  67.  
  68.     if(check_sum(&eq, &solution) == 0)
  69.         return FAIL;
  70.  
  71.     printf("  modify soltution to be incorrect (%0.2f -> %0.2f)\n", solution.var[0], 0.0);
  72.     solution.var[0] = 0;
  73.  
  74.     return check_sum(&eq, &solution) == 0;
  75. }/*}}}*/
  76.  
  77. int
  78. t_print_equation(void)
  79. {/*{{{*/
  80.     struct equation eq = {
  81.         .left = {{2, 4, 6, 7}, 4},
  82.         .right = 12
  83.     };
  84.  
  85.     puts("Note: Maintainer must review print output manually.");
  86.     print_equation(&eq);
  87.     return PASS;
  88. }/*}}}*/
  89.  
  90. int
  91. t_print_tuple(void)
  92. {/*{{{*/
  93.     struct tuple tuples[] = {
  94.         {{2,5,7,13}, 4},
  95.         {.type = T_NOSOLUTION},
  96.         {.type = T_INFINITE},
  97.         {.type = T_ERROR},
  98.     };
  99.     char const * descriptions[] = {
  100.         "Normal 4-tuple",
  101.         "No solutions",
  102.         "Infinite solutions",
  103.         "Error",
  104.     };
  105.  
  106.     puts("Note: Maintainer must review print output manually.");
  107.  
  108.     for(int i = 0; i < 4; ++i){
  109.         puts(descriptions[i]);
  110.         printf("  ");
  111.         print_tuple(tuples + i);
  112.     }
  113.  
  114.     return PASS;
  115. }/*}}}*/
  116.  
  117. int
  118. t_tuple_type_str(void)
  119. {/*{{{*/
  120.     char *expect[] = {
  121.         "One solution exists",
  122.         "No solutions exist",
  123.         "Infinite solutions exist",
  124.         "An unknown error has occurred",
  125.         "Invalid type",
  126.     };
  127.  
  128.     for(int i = 0; i <= T_END; ++i){
  129.         printf("  %i : %s (expect: %s)\n", i, tuple_type_str(i), expect[i]);
  130.  
  131.         if(tuple_type_str(i) != expect[i])
  132.             return FAIL; /* Should work assuming compiler re-uses .rodata addresses for string literals */
  133.     }
  134.  
  135.     return PASS;
  136. }/*}}}*/
  137.  
  138. int
  139. run_test(testfn_t fn, char const *description)
  140. {/*{{{*/
  141.     int result;
  142.  
  143.     puts(description);
  144.     result = fn();
  145.     puts(OUTCOME_STR[result]);
  146.     putchar('\n');
  147.  
  148.     return result;
  149. }/*}}}*/
  150.  
  151. int
  152. main(void)
  153. {
  154.     int passed = 0;
  155.     int total = 0;
  156.     struct { testfn_t fn; char const *description; int end; } args[] = {
  157.         {t_tuple_type_str, "tuple_type_str : 0..T_END"},
  158.         {t_static_get_var_name, "get_var_name (static)"},
  159.         {t_get_sum, "get_sum"},
  160.         {t_check_sum, "check_sum"},
  161.         {t_print_equation, "print_equation (manual review)"},
  162.         {t_print_tuple, "print_tuple (manual review)"},
  163.         {.end = 1}
  164.     };
  165.  
  166.     for(int i = 0; !args[i].end; ++i){
  167.         passed += run_test(args[i].fn, args[i].description);
  168.         ++total;
  169.     }
  170.  
  171.     printf("%i of %i tests passed\n", passed, total);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement