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.

81 lines
1.6KB

  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #define HASH_FUNCTION(s, len, hashv) (hashv) = TrivialHash((const char *)s, len)
  4. #include "uthash.h"
  5. unsigned int TrivialHash(const char *s, size_t len)
  6. {
  7. unsigned int h = 0;
  8. size_t i;
  9. for (i=0; i < len; ++i) {
  10. h += (unsigned char)s[i];
  11. }
  12. return h;
  13. }
  14. struct test_t {
  15. int a;
  16. int b;
  17. UT_hash_handle hh;
  18. };
  19. struct test_t *make_test(int value)
  20. {
  21. struct test_t *test = (struct test_t *)malloc(sizeof *test);
  22. assert(test != NULL);
  23. test->a = value;
  24. return test;
  25. }
  26. int main()
  27. {
  28. struct test_t *tests = NULL;
  29. struct test_t *test = NULL;
  30. int x;
  31. unsigned int h;
  32. x = 0x0042;
  33. HASH_VALUE(&x, sizeof x, h);
  34. assert(h == 0x42);
  35. x = 0x4002;
  36. HASH_VALUE(&x, sizeof x, h);
  37. assert(h == 0x42);
  38. test = make_test(0x0042);
  39. HASH_ADD_INT(tests, a, test);
  40. test = make_test(0x4002);
  41. HASH_ADD_INT(tests, a, test);
  42. x = 0x4002;
  43. test = NULL;
  44. HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x42, test);
  45. assert(test != NULL);
  46. assert(test->a == 0x4002);
  47. x = 0x0042;
  48. test = NULL;
  49. HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x42, test);
  50. assert(test != NULL);
  51. assert(test->a == 0x0042);
  52. x = 0x4002;
  53. test = NULL;
  54. HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x43, test);
  55. assert(test == NULL);
  56. x = 0x0042;
  57. test = NULL;
  58. HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x43, test);
  59. assert(test == NULL);
  60. x = 0x4003;
  61. test = NULL;
  62. HASH_FIND_BYHASHVALUE(hh, tests, &x, sizeof x, 0x42, test);
  63. assert(test == NULL);
  64. HASH_CLEAR(hh, tests);
  65. }