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.

117 lines
3.0KB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include "uthash.h"
  4. typedef struct {
  5. char name[32];
  6. int weight;
  7. UT_hash_handle hh;
  8. } hstruct_t;
  9. static int cmpfunc(const hstruct_t *s1, const hstruct_t *s2)
  10. {
  11. return (s1->weight < s2->weight) ? -1 : (s1->weight > s2->weight);
  12. }
  13. // Test that CMPFUNC can safely be a macro.
  14. #define CMPFUNC(a,b) cmpfunc(a,b)
  15. void printtable(const hstruct_t *hTable)
  16. {
  17. const hstruct_t *search, *tmp;
  18. HASH_ITER(hh, hTable, search, tmp) {
  19. printf("%d: %s\n", search->weight, search->name);
  20. }
  21. printf("###\n");
  22. }
  23. void delitem(hstruct_t **hTable, const char *name)
  24. {
  25. hstruct_t *item;
  26. HASH_FIND_STR(*hTable, name, item);
  27. HASH_DEL(*hTable, item);
  28. }
  29. int main()
  30. {
  31. hstruct_t *hTable = NULL;
  32. hstruct_t *replaced = NULL;
  33. unsigned hashvalue;
  34. hstruct_t tst[] = {
  35. {"muh1", 2, {0}},
  36. {"muh2", 8, {0}},
  37. {"muh3", 1, {0}},
  38. {"muh4", 8, {0}},
  39. {"muh5", 3, {0}},
  40. {"muh6", 5, {0}},
  41. {"muh7", 6, {0}},
  42. {"muh8", 15, {0}},
  43. {"muh9", 6, {0}},
  44. {"muh10", 9, {0}},
  45. {"muh11", 10, {0}},
  46. {"muh12", 43, {0}},
  47. {"muh12", 7, {0}}
  48. };
  49. int index;
  50. for (index = 0; index < 11; ++index) {
  51. HASH_ADD_INORDER(hh, hTable, name[0], strlen(tst[index].name), &tst[index], CMPFUNC);
  52. }
  53. // test HASH_ADD_BYHASHVALUE_INORDER
  54. HASH_VALUE(tst[11].name, strlen(tst[11].name), hashvalue);
  55. HASH_ADD_BYHASHVALUE_INORDER(hh, hTable, name[0], strlen(tst[11].name), hashvalue, &tst[11], CMPFUNC);
  56. printtable(hTable);
  57. // replace "43: muh12" with "7: muh12"
  58. HASH_REPLACE_INORDER(hh, hTable, name[0], strlen(tst[11].name), &tst[12], replaced, CMPFUNC);
  59. assert(replaced == &tst[11]);
  60. printtable(hTable);
  61. // rehash "1: muh3" to "9: muh3"
  62. tst[2].weight = 9;
  63. HASH_REPLACE_INORDER(hh, hTable, name[0], strlen(tst[2].name), &tst[2], replaced, CMPFUNC);
  64. assert(replaced == &tst[2]);
  65. printtable(hTable);
  66. // rehash "6: muh7" to "16: muh7"
  67. tst[6].weight = 16;
  68. HASH_VALUE(&tst[6].name[0], strlen(tst[6].name), hashvalue);
  69. HASH_REPLACE_BYHASHVALUE_INORDER(hh, hTable, name[0], strlen(tst[6].name), hashvalue, &tst[6], replaced, CMPFUNC);
  70. assert(replaced == &tst[6]);
  71. printtable(hTable);
  72. // remove "8: muh2"...
  73. HASH_DELETE(hh, hTable, &tst[1]);
  74. printtable(hTable);
  75. // ...and then reinsert "8: muh2"
  76. HASH_VALUE(tst[1].name, strlen(tst[1].name), hashvalue);
  77. HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh, hTable, tst[1].name, strlen(tst[1].name), hashvalue, &tst[1], CMPFUNC);
  78. printtable(hTable);
  79. delitem(&hTable, "muh1");
  80. delitem(&hTable, "muh7");
  81. delitem(&hTable, "muh3");
  82. delitem(&hTable, "muh9");
  83. delitem(&hTable, "muh2");
  84. delitem(&hTable, "muh11");
  85. delitem(&hTable, "muh4");
  86. delitem(&hTable, "muh6");
  87. delitem(&hTable, "muh5");
  88. delitem(&hTable, "muh8");
  89. delitem(&hTable, "muh10");
  90. delitem(&hTable, "muh12");
  91. printtable(hTable);
  92. return 0;
  93. }