home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / GC / test.c < prev   
Encoding:
C/C++ Source or Header  |  1990-04-25  |  1.8 KB  |  86 lines

  1. /* Somewhat nonconvincing test for garbage collector.                */
  2. /* Note that this intentionally uses the worlds worst implementation */
  3. /* of cons.  It eats up gobs of memory in an attempt to break the    */
  4. /* collector.  Process size should grow to about 1.5 Meg and stay    */
  5. /* there.                                                            */
  6. /* Should take about 25 seconds (2 minutes) to run on a              */
  7. /* Sun 3/60 (Vax 11/750)                                             */
  8. /* (The Vax does reasonably well here because the compiler assures   */
  9. /* longword pointer alignment.)                                      */
  10.  
  11. # include <stdio.h>
  12. # include "cons.h"
  13.  
  14. /* Return reverse(x) concatenated with y */
  15. sexpr reverse1(x, y)
  16. sexpr x, y;
  17. {
  18.     if (null(x)) {
  19.         return(y);
  20.     } else {
  21.         return( reverse1(cdr(x), cons(car(x), y)) );
  22.     }
  23. }
  24.  
  25. sexpr reverse(x)
  26. sexpr x;
  27. {
  28.     return( reverse1(x, nil) );
  29. }
  30.  
  31. sexpr ints(low, up)
  32. int low, up;
  33. {
  34.     if (low > up) {
  35.     return(nil);
  36.     } else {
  37.         return(cons(low, ints(low+1, up)));
  38.     }
  39. }
  40.  
  41. void print_int_list(x)
  42. sexpr x;
  43. {
  44.     if (null(x)) {
  45.         printf("NIL\n");
  46.     } else {
  47.         printf("%d", car(x));
  48.         if (!null(cdr(x))) {
  49.             printf(", ");
  50.             print_int_list(cdr(x));
  51.         } else {
  52.             printf("\n");
  53.         }
  54.     }
  55. }
  56.  
  57. /* Try to force a to be strangely aligned */
  58. struct {
  59.   char dummy;
  60.   sexpr aa;
  61. } A;
  62. #define a A.aa
  63.  
  64. main()
  65. {
  66.     int i;
  67.     sexpr b;
  68.  
  69.     gc_init();
  70.     a = ints(1, 100);
  71.     b = ints(1, 50);
  72.     print_int_list(a);
  73.     print_int_list(b);
  74.     print_int_list(reverse(a));
  75.     print_int_list(reverse(b));
  76.     for (i = 0; i < 100; i++) {
  77.         b = reverse(reverse(b));
  78.     }
  79.     print_int_list(a);
  80.     print_int_list(b);
  81.     print_int_list(reverse(a));
  82.     print_int_list(reverse(b));
  83.     return(0);
  84. }
  85.  
  86.