hash_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/hash.h>
  20. #include <pj/rand.h>
  21. #include <pj/log.h>
  22. #include <pj/pool.h>
  23. #include "test.h"
  24. #if INCLUDE_HASH_TEST
  25. #define HASH_COUNT 31
  26. static int hash_test_with_key(pj_pool_t *pool, unsigned char key)
  27. {
  28. pj_hash_table_t *ht;
  29. unsigned value = 0x12345;
  30. pj_hash_iterator_t it_buf, *it;
  31. unsigned *entry;
  32. ht = pj_hash_create(pool, HASH_COUNT);
  33. if (!ht)
  34. return -10;
  35. pj_hash_set(pool, ht, &key, sizeof(key), 0, &value);
  36. entry = (unsigned*) pj_hash_get(ht, &key, sizeof(key), NULL);
  37. if (!entry)
  38. return -20;
  39. if (*entry != value)
  40. return -30;
  41. if (pj_hash_count(ht) != 1)
  42. return -30;
  43. it = pj_hash_first(ht, &it_buf);
  44. if (it == NULL)
  45. return -40;
  46. entry = (unsigned*) pj_hash_this(ht, it);
  47. if (!entry)
  48. return -50;
  49. if (*entry != value)
  50. return -60;
  51. it = pj_hash_next(ht, it);
  52. if (it != NULL)
  53. return -70;
  54. /* Erase item */
  55. pj_hash_set(NULL, ht, &key, sizeof(key), 0, NULL);
  56. if (pj_hash_get(ht, &key, sizeof(key), NULL) != NULL)
  57. return -80;
  58. if (pj_hash_count(ht) != 0)
  59. return -90;
  60. it = pj_hash_first(ht, &it_buf);
  61. if (it != NULL)
  62. return -100;
  63. return 0;
  64. }
  65. static int hash_collision_test(pj_pool_t *pool)
  66. {
  67. enum {
  68. COUNT = HASH_COUNT * 4
  69. };
  70. pj_hash_table_t *ht;
  71. pj_hash_iterator_t it_buf, *it;
  72. unsigned char *values;
  73. unsigned i;
  74. ht = pj_hash_create(pool, HASH_COUNT);
  75. if (!ht)
  76. return -200;
  77. values = (unsigned char*) pj_pool_alloc(pool, COUNT);
  78. for (i=0; i<COUNT; ++i) {
  79. values[i] = (unsigned char)i;
  80. pj_hash_set(pool, ht, &i, sizeof(i), 0, &values[i]);
  81. }
  82. if (pj_hash_count(ht) != COUNT)
  83. return -210;
  84. for (i=0; i<COUNT; ++i) {
  85. unsigned char *entry;
  86. entry = (unsigned char*) pj_hash_get(ht, &i, sizeof(i), NULL);
  87. if (!entry)
  88. return -220;
  89. if (*entry != values[i])
  90. return -230;
  91. }
  92. i = 0;
  93. it = pj_hash_first(ht, &it_buf);
  94. while (it) {
  95. ++i;
  96. it = pj_hash_next(ht, it);
  97. }
  98. if (i != COUNT)
  99. return -240;
  100. return 0;
  101. }
  102. /*
  103. * Hash table test.
  104. */
  105. int hash_test(void)
  106. {
  107. pj_pool_t *pool = pj_pool_create(mem, "hash", 512, 512, NULL);
  108. int rc;
  109. unsigned i;
  110. /* Test to fill in each row in the table */
  111. for (i=0; i<=HASH_COUNT; ++i) {
  112. rc = hash_test_with_key(pool, (unsigned char)i);
  113. if (rc != 0) {
  114. pj_pool_release(pool);
  115. return rc;
  116. }
  117. }
  118. /* Collision test */
  119. rc = hash_collision_test(pool);
  120. if (rc != 0) {
  121. pj_pool_release(pool);
  122. return rc;
  123. }
  124. pj_pool_release(pool);
  125. return 0;
  126. }
  127. #endif /* INCLUDE_HASH_TEST */