home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / toollib_2 / !ToolLib / tests / c / testset < prev   
Text File  |  1996-04-02  |  5KB  |  183 lines

  1. /* testset.c */
  2. /* A test harness for the set ADT
  3.  * (c) Paul Field
  4.  * V1.00 - 15/7/1995
  5.  * v1.10 -  2/4/1996 : slight alterations to set interface because of exceptions
  6.  */
  7.  
  8. #include "set.h"
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <errno.h>
  14.  
  15.  
  16.  
  17. static void _test_truth(const char *expression, bool value, unsigned line)
  18.  { if (!value)
  19.     { printf("line %u: %s is not true\n", line, expression);
  20.       exit(EXIT_FAILURE);
  21.     }
  22.  }
  23.  
  24. #define test_truth(a) _test_truth(#a, a, __LINE__)
  25.  
  26.  
  27.  
  28. /* Functions needed for strings to be in a set */
  29.  
  30. static int vstrcmp(const void *o1, const void *o2)
  31.  { return strcmp(o1,o2);
  32.  }
  33.  
  34.  
  35.  
  36.  
  37. static size_t string_size(const void *o)
  38.  { return strlen(o)+1;
  39.  }
  40.  
  41.  
  42.  
  43.  
  44. /* Check that an element is in the set */
  45. static bool is_in(const set *s, const char *string)
  46.  { return strcmp(set_contains(s, string), string) == 0;
  47.  }
  48.  
  49.  
  50.  
  51.  
  52. static void check_functionality(void)
  53.  { set *strings;
  54.    const char s1[] = "111";
  55.    const char s2[] = "2222";
  56.    const char s3[] = "3";
  57.  
  58.    /***** Check set with dynamic structures *****/
  59.  
  60.    /**** Check set_create ****/
  61.    strings = set_create(vstrcmp, string_size, 0);
  62.    test_truth(set_elements(strings) == 0);
  63.  
  64.    /*** Add one element and check set ****/
  65.    set_insert(strings, s1);
  66.    test_truth(set_elements(strings) == 1);
  67.    test_truth(is_in(strings, s1));
  68.    test_truth(!set_contains(strings, s2));
  69.    test_truth(!set_contains(strings, s3));
  70.  
  71.    /*** Add second element and check set ****/
  72.    /* We add s3 before s2 because we know about the internal ordering of sets and
  73.     * we don't want to insert elements in order
  74.     */
  75.    set_insert(strings, s3);
  76.    test_truth(set_elements(strings) == 2);
  77.    test_truth(is_in(strings, s1));
  78.    test_truth(!set_contains(strings, s2));
  79.    test_truth(is_in(strings, s3));
  80.  
  81.    /*** Add third element and check set ****/
  82.    set_insert(strings, s2);
  83.    test_truth(set_elements(strings) == 3);
  84.    test_truth(is_in(strings, s1));
  85.    test_truth(is_in(strings, s2));
  86.    test_truth(is_in(strings, s3));
  87.  
  88.    /*** Now add elements that are already in the set ***/
  89.    set_insert(strings, s1);
  90.    set_insert(strings, s2);
  91.    set_insert(strings, s3);
  92.    test_truth(set_elements(strings) == 3);
  93.    test_truth(is_in(strings, s1));
  94.    test_truth(is_in(strings, s2));
  95.    test_truth(is_in(strings, s3));
  96.  
  97.    /*** Now remove an element that doesn't exist ***/
  98.    set_remove(strings, "4444");
  99.    test_truth(set_elements(strings) == 3);
  100.    test_truth(is_in(strings, s1));
  101.    test_truth(is_in(strings, s2));
  102.    test_truth(is_in(strings, s3));
  103.  
  104.    /*** Remove elements that do exist ***/
  105.    set_remove(strings, s2);
  106.    test_truth(set_elements(strings) == 2);
  107.    test_truth(is_in(strings, s1));
  108.    test_truth(!set_contains(strings, s2));
  109.    test_truth(is_in(strings, s3));
  110.  
  111.    set_remove(strings, s1);
  112.    test_truth(set_elements(strings) == 1);
  113.    test_truth(!set_contains(strings, s1));
  114.    test_truth(!set_contains(strings, s2));
  115.    test_truth(is_in(strings, s3));
  116.  
  117.    set_remove(strings, s3);
  118.    test_truth(set_elements(strings) == 0);
  119.    test_truth(!set_contains(strings, s1));
  120.    test_truth(!set_contains(strings, s2));
  121.    test_truth(!set_contains(strings, s3));
  122.  
  123.    /*** Add an element and then destroy the set - just to catch memory problems ***/
  124.    set_insert(strings, s1);
  125.    set_destroy(strings);
  126.  
  127.  
  128.    /***** Short test for non-dynamic elements *****/
  129.    { char a = 'a', b = 'b', c = 'c';
  130.      set *chars;
  131.  
  132.      chars = set_create(NULL, NULL, sizeof(char));
  133.      set_insert(chars, &a);
  134.      set_insert(chars, &c);
  135.      set_insert(chars, &b);
  136.      test_truth(set_elements(chars) == 3);
  137.      test_truth(*(char*)set_contains(chars, &a) == a);
  138.      test_truth(*(char*)set_contains(chars, &b) == b);
  139.      test_truth(*(char*)set_contains(chars, &c) == c);
  140.      set_destroy(chars);
  141.    }
  142.  }
  143.  
  144.  
  145.  
  146.  
  147. static void violate_precondition(unsigned conditionno)
  148.  {
  149.  }
  150.  
  151.  
  152.  
  153.  
  154. static const char errmess[] =
  155.   "Please give a single integer (0-0) as the program argument\n"
  156.   "0 tests set system, /** 1-?? test various debugging routines**/";
  157.  
  158.  
  159.  
  160.  
  161. int main(int argc, char *argv[])
  162.  { unsigned code;
  163.  
  164.    if (argc != 2)
  165.     { puts(errmess);
  166.       return EXIT_FAILURE;
  167.     }
  168.    errno = 0;
  169.    code = (unsigned) strtoul(argv[1], NULL, 10);
  170.    if (code > 0 || errno == ERANGE)
  171.     { puts(errmess);
  172.       return EXIT_FAILURE;
  173.     }
  174.    if (code == 0)
  175.     { check_functionality();
  176.       puts("All is well - remember to check with NDEBUG defined and not defined");
  177.     }
  178.    else
  179.     { violate_precondition(code);
  180.     }
  181.    return EXIT_SUCCESS;
  182.  }
  183.