test_htable.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "common/htable.h"
  19. #include "expect.h"
  20. #include "hdfs_test.h"
  21. #include <errno.h>
  22. #include <inttypes.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. // Disable type cast and loss of precision warnings, because the test
  27. // manipulates void* values manually on purpose.
  28. #ifdef WIN32
  29. #pragma warning(disable: 4244 4306)
  30. #endif
  31. static uint32_t simple_hash(const void *key, uint32_t size)
  32. {
  33. uintptr_t k = (uintptr_t)key;
  34. return ((13 + k) * 6367) % size;
  35. }
  36. static int simple_compare(const void *a, const void *b)
  37. {
  38. return a == b;
  39. }
  40. static void expect_102(void *f, void *k, void *v)
  41. {
  42. int *found_102 = f;
  43. uintptr_t key = (uintptr_t)k;
  44. uintptr_t val = (uintptr_t)v;
  45. if ((key == 2) && (val == 102)) {
  46. *found_102 = 1;
  47. } else {
  48. abort();
  49. }
  50. }
  51. static void *htable_pop_val(struct htable *ht, void *key)
  52. {
  53. void *old_key, *old_val;
  54. htable_pop(ht, key, &old_key, &old_val);
  55. return old_val;
  56. }
  57. int main(void)
  58. {
  59. struct htable *ht;
  60. int found_102 = 0;
  61. ht = htable_alloc(4, simple_hash, simple_compare);
  62. EXPECT_INT_EQ(0, htable_used(ht));
  63. EXPECT_INT_EQ(4, htable_capacity(ht));
  64. EXPECT_NULL(htable_get(ht, (void*)123));
  65. EXPECT_NULL(htable_pop_val(ht, (void*)123));
  66. EXPECT_ZERO(htable_put(ht, (void*)123, (void*)456));
  67. EXPECT_INT_EQ(456, (uintptr_t)htable_get(ht, (void*)123));
  68. EXPECT_INT_EQ(456, (uintptr_t)htable_pop_val(ht, (void*)123));
  69. EXPECT_NULL(htable_pop_val(ht, (void*)123));
  70. // Enlarge the hash table
  71. EXPECT_ZERO(htable_put(ht, (void*)1, (void*)101));
  72. EXPECT_ZERO(htable_put(ht, (void*)2, (void*)102));
  73. EXPECT_ZERO(htable_put(ht, (void*)3, (void*)103));
  74. EXPECT_INT_EQ(3, htable_used(ht));
  75. EXPECT_INT_EQ(8, htable_capacity(ht));
  76. EXPECT_INT_EQ(102, (uintptr_t)htable_get(ht, (void*)2));
  77. EXPECT_INT_EQ(101, (uintptr_t)htable_pop_val(ht, (void*)1));
  78. EXPECT_INT_EQ(103, (uintptr_t)htable_pop_val(ht, (void*)3));
  79. EXPECT_INT_EQ(1, htable_used(ht));
  80. htable_visit(ht, expect_102, &found_102);
  81. EXPECT_INT_EQ(1, found_102);
  82. htable_free(ht);
  83. fprintf(stderr, "SUCCESS.\n");
  84. return EXIT_SUCCESS;
  85. }
  86. // vim: ts=4:sw=4:tw=79:et