Subversion Repositories Kolibri OS

Rev

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