hashtable.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <glib.h>
  4. #include "test.h"
  5. int foreach_count = 0;
  6. int foreach_fail = 0;
  7. void foreach (gpointer key, gpointer value, gpointer user_data)
  8. {
  9. foreach_count++;
  10. if (GPOINTER_TO_INT (user_data) != 'a')
  11. foreach_fail = 1;
  12. }
  13. RESULT hash_t1 (void)
  14. {
  15. GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
  16. foreach_count = 0;
  17. foreach_fail = 0;
  18. g_hash_table_insert (t, "hello", "world");
  19. g_hash_table_insert (t, "my", "god");
  20. g_hash_table_foreach (t, foreach, GINT_TO_POINTER('a'));
  21. if (foreach_count != 2)
  22. return FAILED ("did not find all keys, got %d expected 2", foreach_count);
  23. if (foreach_fail)
  24. return FAILED("failed to pass the user-data to foreach");
  25. if (!g_hash_table_remove (t, "my"))
  26. return FAILED ("did not find known key");
  27. if (g_hash_table_size (t) != 1)
  28. return FAILED ("unexpected size");
  29. g_hash_table_insert(t, "hello", "moon");
  30. if (strcmp (g_hash_table_lookup (t, "hello"), "moon") != 0)
  31. return FAILED ("did not replace world with moon");
  32. if (!g_hash_table_remove (t, "hello"))
  33. return FAILED ("did not find known key");
  34. if (g_hash_table_size (t) != 0)
  35. return FAILED ("unexpected size");
  36. g_hash_table_destroy (t);
  37. return OK;
  38. }
  39. RESULT hash_t2 (void)
  40. {
  41. return OK;
  42. }
  43. RESULT hash_default (void)
  44. {
  45. GHashTable *hash = g_hash_table_new (NULL, NULL);
  46. if (hash == NULL)
  47. return FAILED ("g_hash_table_new should return a valid hash");
  48. g_hash_table_destroy (hash);
  49. return NULL;
  50. }
  51. RESULT
  52. hash_null_lookup (void)
  53. {
  54. GHashTable *hash = g_hash_table_new (NULL, NULL);
  55. gpointer ok, ov;
  56. g_hash_table_insert (hash, NULL, GINT_TO_POINTER (1));
  57. g_hash_table_insert (hash, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
  58. if (!g_hash_table_lookup_extended (hash, NULL, &ok, &ov))
  59. return FAILED ("Did not find the NULL");
  60. if (ok != NULL)
  61. return FAILED ("Incorrect key found");
  62. if (ov != GINT_TO_POINTER (1))
  63. return FAILED ("Got wrong value %p\n", ov);
  64. if (!g_hash_table_lookup_extended (hash, GINT_TO_POINTER(1), &ok, &ov))
  65. return FAILED ("Did not find the 1");
  66. if (ok != GINT_TO_POINTER(1))
  67. return FAILED ("Incorrect key found");
  68. if (ov != GINT_TO_POINTER (2))
  69. return FAILED ("Got wrong value %p\n", ov);
  70. g_hash_table_destroy (hash);
  71. return NULL;
  72. }
  73. static void
  74. counter (gpointer key, gpointer value, gpointer user_data)
  75. {
  76. int *counter = (int *) user_data;
  77. (*counter)++;
  78. }
  79. RESULT hash_grow (void)
  80. {
  81. GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  82. int i, count = 0;
  83. for (i = 0; i < 1000; i++)
  84. g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
  85. for (i = 0; i < 1000; i++){
  86. char buffer [30];
  87. gpointer value;
  88. sprintf (buffer, "%d", i);
  89. value = g_hash_table_lookup (hash, buffer);
  90. sprintf (buffer, "x-%d", i);
  91. if (strcmp (value, buffer) != 0){
  92. return FAILED ("Failed to lookup the key %d, the value was %s\n", i, value);
  93. }
  94. }
  95. if (g_hash_table_size (hash) != 1000)
  96. return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash));
  97. /* Now do the manual count, lets not trust the internals */
  98. g_hash_table_foreach (hash, counter, &count);
  99. if (count != 1000){
  100. return FAILED ("Foreach count is not 1000");
  101. }
  102. g_hash_table_destroy (hash);
  103. return NULL;
  104. }
  105. static Test hashtable_tests [] = {
  106. {"t1", hash_t1},
  107. {"t2", hash_t2},
  108. {"grow", hash_grow},
  109. {"default", hash_default},
  110. {"null_lookup", hash_null_lookup},
  111. {NULL, NULL}
  112. };
  113. DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)