Subversion Repositories Kolibri OS

Rev

Rev 7520 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5.  
  6. // suballocator functions
  7. extern void* wtmalloc(size_t size);
  8. extern void  wtfree(void *pointer);
  9. extern void* wtrealloc(void* pointer, size_t size);
  10. extern void* wtcalloc (size_t num, size_t size);
  11. extern int   wtmalloc_freelist_check();
  12. extern int wtmalloc_poiner_check(void *ptr);
  13. extern void wtdump_alloc_stats();
  14.  
  15. #ifdef __GNUC__
  16. void* sysmalloc(size_t sz)
  17. {
  18.         return malloc(sz);
  19. }
  20. #endif
  21.  
  22.  
  23.  
  24. #define NUMPTR 10000
  25.  
  26. char *pointers[NUMPTR];
  27. char values[NUMPTR];
  28. int  sizes[NUMPTR];
  29.  
  30. int checkvalues()
  31. {
  32.         for (int i = 0; i < NUMPTR; i++)
  33.         {
  34.                 if (!pointers[i]) continue;
  35.                 assert(wtmalloc_poiner_check(pointers[i]));
  36.                
  37.                 for (int j = 0; j < sizes[i]; j++)
  38.                         assert(pointers[i][j] == values[i]);
  39.         }
  40.         return 1;
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46.         char *ptr;
  47.         int i, sz;
  48.        
  49.         puts("Test started");
  50.        
  51.         // test start settings
  52.         assert(wtmalloc_freelist_check());
  53.         // test just single alloc/dealloc
  54.         ptr = wtmalloc(1000);
  55.         assert(wtmalloc_poiner_check(ptr));
  56.         wtfree(ptr);
  57.         assert(wtmalloc_freelist_check());
  58.  
  59.         puts("test allocation started");
  60.         // test allocation
  61.         for (i = 0; i < NUMPTR; i++)
  62.         {
  63.                 sz = rand() % 4200;
  64.                 pointers[i] = wtmalloc(sz);
  65.                 sizes[i] = sz;
  66.                 values[i] = sz % 256;
  67.                 memset(pointers[i], values[i], sz);
  68.  
  69.                 assert(wtmalloc_freelist_check());
  70.         }
  71.         assert(checkvalues());
  72.  
  73.         puts("test random deallocation started");
  74.         // random deallocation
  75.         for (i = 0; i < NUMPTR; i++)
  76.         {
  77.                 sz = rand() % 2;
  78.                 if (sz)
  79.                 {
  80.                         wtfree(pointers[i]);
  81.                         pointers[i] = NULL;
  82.                 }
  83.         }
  84.         assert(wtmalloc_freelist_check());
  85.         assert(checkvalues());
  86.  
  87.         puts("test allocation in free list gaps started");
  88.         // test allocation in free list gaps
  89.         for (i = 0; i < NUMPTR; i++)
  90.         {
  91.                 if (pointers[i]) continue;
  92.                
  93.                 sz = rand() % 4200;
  94.                 pointers[i] = wtmalloc(sz);
  95.                 sizes[i] = sz;
  96.                 values[i] = sz % 256;
  97.                 memset(pointers[i], values[i], sz);
  98.         }
  99.         assert(wtmalloc_freelist_check());
  100.         assert(checkvalues());
  101.        
  102.         puts("test realloc started");
  103.         // test realloc
  104.         for (i = 0; i < NUMPTR; i++)
  105.         {
  106.                 sz = rand() % 4200;
  107.                 pointers[i] = wtrealloc(pointers[i], sz);
  108.                
  109.                 sizes[i] = sz;
  110.                 memset(pointers[i], values[i], sz);
  111.         }
  112.         assert(wtmalloc_freelist_check());
  113.         assert(checkvalues());
  114.        
  115.        
  116.         puts("test full deallocation started");
  117.         // full deallocation
  118.         for (i = 0; i < NUMPTR; i++)
  119.         {
  120.                 wtfree(pointers[i]);
  121.                 pointers[i] = NULL;
  122.         }
  123.         assert(wtmalloc_freelist_check());
  124.        
  125.         wtdump_alloc_stats();
  126.  
  127.         printf("\ntests all OK\n");
  128.        
  129.         return 0;
  130.  
  131. }
  132.