map-test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdint.h>
  28. #include <assert.h>
  29. #include "wayland-private.h"
  30. #include "test-runner.h"
  31. TEST(map_insert_new)
  32. {
  33. struct wl_map map;
  34. uint32_t i, j, k, a, b, c;
  35. wl_map_init(&map, WL_MAP_SERVER_SIDE);
  36. i = wl_map_insert_new(&map, 0, &a);
  37. j = wl_map_insert_new(&map, 0, &b);
  38. k = wl_map_insert_new(&map, 0, &c);
  39. assert(i == WL_SERVER_ID_START);
  40. assert(j == WL_SERVER_ID_START + 1);
  41. assert(k == WL_SERVER_ID_START + 2);
  42. assert(wl_map_lookup(&map, i) == &a);
  43. assert(wl_map_lookup(&map, j) == &b);
  44. assert(wl_map_lookup(&map, k) == &c);
  45. wl_map_release(&map);
  46. wl_map_init(&map, WL_MAP_CLIENT_SIDE);
  47. i = wl_map_insert_new(&map, 0, &a);
  48. assert(i == 0);
  49. assert(wl_map_lookup(&map, i) == &a);
  50. wl_map_release(&map);
  51. }
  52. TEST(map_insert_at)
  53. {
  54. struct wl_map map;
  55. uint32_t a, b, c;
  56. wl_map_init(&map, WL_MAP_CLIENT_SIDE);
  57. assert(wl_map_insert_at(&map, 0, WL_SERVER_ID_START, &a) == 0);
  58. assert(wl_map_insert_at(&map, 0, WL_SERVER_ID_START + 3, &b) == -1);
  59. assert(wl_map_insert_at(&map, 0, WL_SERVER_ID_START + 1, &c) == 0);
  60. assert(wl_map_lookup(&map, WL_SERVER_ID_START) == &a);
  61. assert(wl_map_lookup(&map, WL_SERVER_ID_START + 1) == &c);
  62. wl_map_release(&map);
  63. }
  64. TEST(map_remove)
  65. {
  66. struct wl_map map;
  67. uint32_t i, j, k, l, a, b, c, d;
  68. wl_map_init(&map, WL_MAP_SERVER_SIDE);
  69. i = wl_map_insert_new(&map, 0, &a);
  70. j = wl_map_insert_new(&map, 0, &b);
  71. k = wl_map_insert_new(&map, 0, &c);
  72. assert(i == WL_SERVER_ID_START);
  73. assert(j == WL_SERVER_ID_START + 1);
  74. assert(k == WL_SERVER_ID_START + 2);
  75. assert(wl_map_lookup(&map, i) == &a);
  76. assert(wl_map_lookup(&map, j) == &b);
  77. assert(wl_map_lookup(&map, k) == &c);
  78. wl_map_remove(&map, j);
  79. assert(wl_map_lookup(&map, j) == NULL);
  80. /* Verify that we insert d at the hole left by removing b */
  81. l = wl_map_insert_new(&map, 0, &d);
  82. assert(l == WL_SERVER_ID_START + 1);
  83. assert(wl_map_lookup(&map, l) == &d);
  84. wl_map_release(&map);
  85. }
  86. TEST(map_flags)
  87. {
  88. struct wl_map map;
  89. uint32_t i, j, a, b;
  90. wl_map_init(&map, WL_MAP_SERVER_SIDE);
  91. i = wl_map_insert_new(&map, 0, &a);
  92. j = wl_map_insert_new(&map, 1, &b);
  93. assert(i == WL_SERVER_ID_START);
  94. assert(j == WL_SERVER_ID_START + 1);
  95. assert(wl_map_lookup(&map, i) == &a);
  96. assert(wl_map_lookup(&map, j) == &b);
  97. assert(wl_map_lookup_flags(&map, i) == 0);
  98. assert(wl_map_lookup_flags(&map, j) == 1);
  99. wl_map_release(&map);
  100. }
  101. static enum wl_iterator_result never_run(void *element, void *data, uint32_t flags)
  102. {
  103. assert(0);
  104. }
  105. TEST(map_iter_empty)
  106. {
  107. struct wl_map map;
  108. wl_map_init(&map, WL_MAP_SERVER_SIDE);
  109. wl_map_for_each(&map, never_run, NULL);
  110. wl_map_release(&map);
  111. }