array-test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <stdlib.h>
  26. #include <assert.h>
  27. #include <string.h>
  28. #include "wayland-util.h"
  29. #include "wayland-private.h"
  30. #include "test-runner.h"
  31. TEST(array_init)
  32. {
  33. struct wl_array array;
  34. /* fill with garbage to emulate uninitialized memory */
  35. memset(&array, 0x57, sizeof array);
  36. wl_array_init(&array);
  37. assert(array.size == 0);
  38. assert(array.alloc == 0);
  39. assert(array.data == 0);
  40. }
  41. TEST(array_release)
  42. {
  43. struct wl_array array;
  44. void *ptr;
  45. wl_array_init(&array);
  46. ptr = wl_array_add(&array, 1);
  47. assert(ptr != NULL);
  48. assert(array.data != NULL);
  49. wl_array_release(&array);
  50. assert(array.data == WL_ARRAY_POISON_PTR);
  51. }
  52. TEST(array_add)
  53. {
  54. struct mydata {
  55. unsigned int a;
  56. unsigned int b;
  57. double c;
  58. double d;
  59. };
  60. const unsigned int iterations = 1321; /* this is arbitrary */
  61. const int datasize = sizeof(struct mydata);
  62. struct wl_array array;
  63. size_t i;
  64. wl_array_init(&array);
  65. /* add some data */
  66. for (i = 0; i < iterations; i++) {
  67. struct mydata* ptr = wl_array_add(&array, datasize);
  68. assert(ptr);
  69. assert((i + 1) * datasize == array.size);
  70. ptr->a = i * 3;
  71. ptr->b = 20000 - i;
  72. ptr->c = (double)(i);
  73. ptr->d = (double)(i / 2.);
  74. }
  75. /* verify the data */
  76. for (i = 0; i < iterations; ++i) {
  77. struct mydata* check = (struct mydata*)array.data + i;
  78. assert(check->a == i * 3);
  79. assert(check->b == 20000 - i);
  80. assert(check->c == (double)(i));
  81. assert(check->d == (double)(i/2.));
  82. }
  83. wl_array_release(&array);
  84. }
  85. TEST(array_copy)
  86. {
  87. const int iterations = 1529; /* this is arbitrary */
  88. struct wl_array source;
  89. struct wl_array copy;
  90. int i;
  91. wl_array_init(&source);
  92. /* add some data */
  93. for (i = 0; i < iterations; i++) {
  94. int *p = wl_array_add(&source, sizeof(int));
  95. assert(p);
  96. *p = i * 2 + i;
  97. }
  98. /* copy the array */
  99. wl_array_init(&copy);
  100. wl_array_copy(&copy, &source);
  101. /* check the copy */
  102. for (i = 0; i < iterations; i++) {
  103. int *s = (int *)source.data + i;
  104. int *c = (int *)copy.data + i;
  105. assert(*s == *c); /* verify the values are the same */
  106. assert(s != c); /* ensure the addresses aren't the same */
  107. assert(*s == i * 2 + i); /* sanity check */
  108. }
  109. wl_array_release(&source);
  110. wl_array_release(&copy);
  111. }
  112. TEST(array_for_each)
  113. {
  114. static const int elements[] = { 77, 12, 45192, 53280, 334455 };
  115. struct wl_array array;
  116. int *p;
  117. int i;
  118. wl_array_init(&array);
  119. for (i = 0; i < 5; i++) {
  120. p = wl_array_add(&array, sizeof *p);
  121. assert(p);
  122. *p = elements[i];
  123. }
  124. i = 0;
  125. wl_array_for_each(p, &array) {
  126. assert(*p == elements[i]);
  127. i++;
  128. }
  129. assert(i == 5);
  130. wl_array_release(&array);
  131. }