yeah568

Untitled

Oct 6th, 2014
219
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5.  
  6. //////////////////////
  7. //
  8. // STACK
  9.  
  10. #define STACK_NAME_SIZE 100
  11.  
  12. struct StackElement {
  13.   char   fill[8];
  14.   char   name[STACK_NAME_SIZE];
  15.   struct StackElement *next;
  16. };
  17.  
  18. struct StackElement *stackTop = 0;
  19.  
  20. void push (char* aName) {
  21.   struct StackElement* e = (struct StackElement*) malloc (sizeof (struct StackElement));
  22.   strncpy (e->name, aName, STACK_NAME_SIZE);
  23.   e->next  = stackTop;
  24.   stackTop = e;
  25. }
  26.  
  27. char* pop () {
  28.   struct StackElement* e = stackTop;
  29.   stackTop = e->next;
  30.   free (e);
  31.   return e->name;
  32. }
  33.  
  34. ///
  35. //////////////////////
  36.  
  37.  
  38. //////////////////////
  39. //
  40. // TEST CASES
  41.  
  42. #define INT_ARRAY_SIZE (sizeof(struct StackElement)/sizeof(int))
  43.  
  44. //
  45. // Correctly prints "Zero"
  46. //
  47. void test1 () {
  48.   printf ("test1:\n");
  49.   int* ip = (int *) malloc (INT_ARRAY_SIZE*sizeof(int));
  50.   int* ipc = ip;
  51.   free (ip);
  52.   push ("Zero");
  53.   printf ("%s\n", pop ());
  54. }
  55.  
  56. //
  57. // Should produce the same output as test1, but it fails
  58. //
  59. void test2 () {
  60.   printf ("test2:\n");
  61.   int* ip = (int *) malloc (INT_ARRAY_SIZE*sizeof(int));
  62.   int* ipc = ip;
  63.   free (ip);
  64.   while (ipc - ip < INT_ARRAY_SIZE)
  65.     *(ipc++) = 0;
  66.   push ("Zero");
  67.   printf ("%s\n", pop ());
  68. }
  69.  
  70. //
  71. // Correctly prints "One", "Zero"
  72. //
  73. void test3 () {
  74.   printf ("test3:\n");
  75.   push ("Zero");
  76.   int* ip = (int *) malloc (INT_ARRAY_SIZE*sizeof(int));
  77.   int* ipc = ip;
  78.   free (ip);
  79.   push ("One");
  80.   printf ("%s\n", pop ());
  81.   printf ("%s\n", pop ());
  82. }
  83.  
  84. //
  85. // Should produce same output as test3, but it fails
  86. //
  87. void test4 () {
  88.   printf ("test4:\n");
  89.   push ("Zero");
  90.   int* ip = (int *) malloc (INT_ARRAY_SIZE*sizeof(int));
  91.   int* ipc = ip;
  92.   free (ip);
  93.   while (ipc - ip < INT_ARRAY_SIZE)
  94.     *(ipc++) = 0;
  95.   push ("One");
  96.   printf ("%s\n", pop ());
  97.   printf ("%s\n", pop ());
  98. }
  99.  
  100. //
  101. // Correctly prints "One", "Zero", "Three", "Two"
  102. //
  103. void test5 () {
  104.   printf ("test5:\n");
  105.   push ("Zero");
  106.   push ("One");
  107.   printf ("%s\n", pop ());
  108.   printf ("%s\n", pop ());
  109.   push("Two");
  110.   push("Three");
  111.   printf ("%s\n", pop ());
  112.   printf ("%s\n", pop ());
  113. }
  114.  
  115. //
  116. // Should produce the same output as test5, but if fails
  117. //
  118. void test6 () {
  119.   printf ("test6:\n");
  120.   char *x[2];
  121.   push ("Zero");
  122.   push ("One");
  123.   x[0] = pop ();
  124.   x[1] = pop ();
  125.   push("Two");
  126.   push("Three");
  127.   printf ("%s\n", x[0]);
  128.   printf ("%s\n", x[1]);
  129.   printf ("%s\n", pop());
  130.   printf ("%s\n", pop());
  131. }
  132.  
  133. //
  134. // Should print list of "string #" for every number from numPushes-1 down to 0,
  135. // in descending order, for any value of numPushes > 0
  136. //
  137. void test7 (int numPushes) {
  138.   char  s[100];
  139.   char* t;
  140.   int   i;
  141.  
  142.   for (i=0; i<numPushes; i++) {
  143.     snprintf (s, sizeof (s), "string %d", i);
  144.     push (s);
  145.   }
  146.   for (i=0; i<numPushes; i++) {
  147.     t = pop ();
  148.     printf ("%s\n", t);
  149.   }
  150. }
  151.  
  152. //
  153. // on linux, run ulimit -v 10000 before running test
  154. // to be sure deallocations are occuring
  155. // that limits max vm size to 10000 kb, which is around 9-MB
  156. // if test8 prints "okay" then stack elements are being freed
  157. // if you instead get a "segmentation fault", they are not
  158. //
  159. void test8() {
  160.   char s[100];
  161.   int i;
  162.   for (i=0; i<100000; i++) {
  163.     push ("hello");
  164.     pop  ();
  165.   }
  166.   printf ("okay\n");
  167. }
  168.  
  169. //
  170. //////////////////////
  171.  
  172.  
  173. //////////////////////
  174. //
  175. // MAIN LINE
  176.  
  177. #define USAGE "usage: dangling-pointer test-number (where test-number is 1-8)"
  178.  
  179. int main (int argc, char** argv) {
  180.   if (argc < 2) {
  181.     printf ("%s\n", USAGE);
  182.     exit (EXIT_FAILURE);
  183.   }
  184.   switch (strtol (argv[1],0,10)) {
  185.     case 1:
  186.       test1 ();
  187.       break;
  188.     case 2:
  189.       test2 ();
  190.       break;
  191.     case 3:
  192.       test3 ();
  193.       break;
  194.     case 4:
  195.       test4 ();
  196.       break;
  197.     case 5:
  198.       test5 ();
  199.       break;
  200.     case 6:
  201.       test6 ();
  202.       break;
  203.     case 7:
  204.       if (argc != 3) {
  205.         printf ("%s numPushes\n", USAGE);
  206.         exit (EXIT_FAILURE);
  207.       }
  208.       test7 (strtol (argv[2] ,0, 10));
  209.       break;
  210.     case 8:
  211.       test8();
  212.       break;
  213.     default:
  214.       printf ("%s\n", USAGE);
  215.       exit (EXIT_FAILURE);
  216.   }
  217. }
RAW Paste Data