Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2KB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "uthash.h"
  5. struct item {
  6. unsigned char *sort_field;
  7. size_t sort_field_len; /** Sort field length, in bytes */
  8. int some_user_data;
  9. UT_hash_handle hh;
  10. };
  11. int sort_func(const struct item *a, const struct item *b)
  12. {
  13. int va = *(int*)(void*)a->sort_field;
  14. int vb = *(int*)(void*)b->sort_field;
  15. return (va < vb) ? -1 : (va > vb);
  16. }
  17. int main()
  18. {
  19. size_t i;
  20. struct item *p, *tmp;
  21. int total = 0;
  22. /** The sorted list */
  23. struct item *list = NULL;
  24. int counter = 0;
  25. /* fill in the sorted list */
  26. for(i=0; i<100; i++) {
  27. p = (struct item *)malloc(sizeof *p);
  28. p->sort_field_len = sizeof(int);
  29. p->sort_field = (unsigned char *)malloc(p->sort_field_len);
  30. *(int*)(void*)p->sort_field = counter++;
  31. HASH_ADD_KEYPTR_INORDER(hh, list, p->sort_field, p->sort_field_len, p, sort_func);
  32. }
  33. printf("filling in is ok\n");
  34. HASH_ITER(hh, list, p, tmp) {
  35. total += *(int*)(void*)p->sort_field;
  36. HASH_DEL(list, p);
  37. free(p->sort_field);
  38. free(p);
  39. }
  40. assert(total == 4950); // sum of 0 through 99
  41. printf("cleanup is ok\n");
  42. return 0;
  43. }