Advertisement
pveselov

test93.c

May 18th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define HASH_BLOOM 16
  4.  
  5. #include "uthash.h"
  6.  
  7. #undef uthash_malloc
  8. #undef uthash_fatal
  9. #define uthash_malloc(sz) alt_malloc(sz)
  10. #define uthash_fatal(s) alt_fatal(s)
  11.  
  12. typedef struct example_user_t {
  13.     int id;
  14.     int cookie;
  15.     UT_hash_handle hh;
  16. } example_user_t;
  17.  
  18. static int malloc_cnt = 0;
  19. static int malloc_failed;
  20. static int is_fatal;
  21.  
  22. static void *alt_malloc(size_t sz)
  23. {
  24.     if (--malloc_cnt <= 0) {
  25.         // printf("fail malloc req for %ld\n", sz);
  26.         malloc_failed = 1;
  27.         return 0;
  28.     }
  29.     // printf("OK malloc req for %ld\n", sz);
  30.     malloc_failed = 0;
  31.     return malloc(sz);
  32. }
  33.  
  34. static void alt_fatal(char * s) {
  35.     printf("alt_fatal: %s\n", s);
  36.     is_fatal = 1;
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.  
  42. #define init(a) do { \
  43.     users = 0; \
  44.     user = malloc(sizeof(example_user_t)); \
  45.     user->id = id; \
  46.     is_fatal = 0; \
  47.     malloc_cnt = a; \
  48.     printf("adding to hash...\n"); \
  49.     HASH_ADD_INT(users, id, user); \
  50. } while(0)
  51.  
  52.     // $TODO: this test doesn't work because
  53.     // uthash_fatal can't successfully return - it
  54.     // makes uthash write into *0. This either needs to
  55.     // be broken up into small test files, or use threads,
  56.     // a thread can hard exit. But there are no threads
  57.     // in other tests, which means compilation changes, etc.
  58.  
  59.     example_user_t * users, * user;
  60.     int id = 0;
  61.  
  62.     init(3); // bloom filter must fail
  63.     if (!is_fatal) {
  64.         printf("fatal not called after bloom failure");
  65.     }
  66.  
  67.     init(2); // bucket creation must fail
  68.     if (!is_fatal) {
  69.         printf("fatal not called after bucket creation failure");
  70.     }
  71.  
  72.     init(1); // table creation must fail
  73.     if (!is_fatal) {
  74.         printf("fatal not called after table creation failure");
  75.     }
  76.  
  77.     init(4); // hash must create OK
  78.     HASH_ADD_INT(users, id, user);
  79.     if (is_fatal) {
  80.         printf("fatal error when creating hash normally\n");
  81.         // bad idea to continue running
  82.         return 1;
  83.     }
  84.  
  85.     // let's add users until expansion fails.
  86.     while (1) {
  87.         user = malloc(sizeof(example_user_t));
  88.         user->id = ++id;
  89.         HASH_ADD_INT(users, id, user);
  90.         if (malloc_failed) {
  91.  
  92.             if (!is_fatal) {
  93.                 printf("fatal not called after bucket not extended\n");
  94.             }
  95.             if (id < 10) {
  96.                 printf("there is no way your bucket size is 10\n");
  97.             }
  98.  
  99.             // we can't really do anything, the hash is not in consistent
  100.             // state, so assume this is a success.
  101.  
  102.         }
  103.     }
  104.  
  105.     printf("OK\n");
  106.  
  107.     return 0;
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement