Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "tokenizer.h"
  4. #include "history.h"
  5.  
  6. /*
  7.     The following is a simple testing script with a single example of input
  8.     your code is expected to handle. The testing of your code should be more
  9.     thorough and explore other input cases.
  10. */
  11.  
  12. #define TEST_TOKENIZER 1
  13. #define TEST_HISTORY 1
  14.  
  15. /* MinUnit: http://www.jera.com/techinfo/jtns/jtn002.html */
  16.  #define mu_assert(message, test) do { if (!(test)) return message; } while (0)
  17.  #define mu_run_test(test) do { char *message = test(); tests_run++; if (message) return message; } while (0)
  18. int tests_run;
  19. /* end MinUnit */
  20.  
  21.  
  22. /* Tokenizer test cases */
  23. static char* test_string_length() {
  24.     mu_assert("string_length('happy') == 5", string_length("happy") == 5);
  25.     return 0;
  26. }
  27. static char* test_is_valid_character() {
  28.     mu_assert("is_valid_character(' ') == 0", is_valid_character(' ') == 0);
  29.     mu_assert("is_valid_character('h') == 1", is_valid_character('h') == 1);
  30.     return 0;
  31. }
  32.  
  33. static char* test_find_word_start() {
  34.     char* str = "  happy";
  35.     mu_assert("*find_word_start('  happy') == 'h'", *find_word_start(str) == 'h');
  36.     return 0;
  37. }
  38.  
  39. static char* test_find_word_end() {
  40.     char* str = "happy joy";
  41.     mu_assert("*find_word_end('happy joy') == ' '", *find_word_end(str) == ' ');
  42.     return 0;
  43. }
  44.  
  45. static char* test_count_words() {
  46.     char* str = "happy happy joy joy";
  47.     mu_assert("count_words('happy happy joy joy') == 4", count_words(str) == 4);
  48.     return 0;
  49. }
  50.  
  51. static char* test_tokenize() {
  52.     char* str = "happy happy joy joy";
  53.     char** tokens = tokenize(str);
  54.     mu_assert("tokens[0] == 'happy'", strcmp(tokens[0], "happy") == 0);
  55.     mu_assert("tokens[1] == 'happy'", strcmp(tokens[1], "happy") == 0);
  56.     mu_assert("tokens[2] == 'joy'", strcmp(tokens[2], "joy") == 0);
  57.     mu_assert("tokens[3] == 'joy'", strcmp(tokens[3], "joy") == 0);
  58.     free_tokens(tokens);
  59.     return 0;
  60. }
  61.  
  62. /* History test cases */
  63. static char* test_add_history() {
  64.     List* list = init_history();
  65.     add_history(list, "happy");
  66.     mu_assert("add_history(list, 'happy')", strcmp(list->root->str, "happy") == 0);
  67.     add_history(list, "joy");
  68.     mu_assert("add_history(list, 'joy')", strcmp(list->root->next->str, "joy") == 0);
  69.     return 0;
  70. }
  71.  
  72. static char* test_get_history() {
  73.     List* list = init_history();
  74.     add_history(list, "happy");
  75.     mu_assert("get_history(list, 1)", strcmp(get_history(list, 1), "happy") == 0);
  76.     return 0;
  77. }
  78.  
  79.  
  80. static char* all_tests() {
  81.     if (TEST_TOKENIZER) {
  82.         mu_run_test(test_string_length);
  83.         mu_run_test(test_is_valid_character);
  84.         mu_run_test(test_find_word_start);
  85.         mu_run_test(test_find_word_end);
  86.         mu_run_test(test_count_words);
  87.         mu_run_test(test_tokenize);
  88.     }
  89.  
  90.     if (TEST_HISTORY) {
  91.         mu_run_test(test_add_history);
  92.         mu_run_test(test_get_history);
  93.     }
  94.  
  95.     return 0;
  96. }
  97.  
  98.  int main(int argc, char **argv) {  
  99.     char *result = all_tests();
  100.  
  101.     if (result != 0)
  102.         printf("Failed test: %s\n", result);
  103.     else
  104.         printf("ALL TESTS PASSED\n");
  105.    
  106.     printf("Tests run: %d\n", tests_run);
  107.  
  108.     return result != 0;
  109.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement