ptrarray.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdio.h>
  2. #include <glib.h>
  3. #include "test.h"
  4. /* Redefine the private structure only to verify proper allocations */
  5. typedef struct _GPtrArrayPriv {
  6. gpointer *pdata;
  7. guint len;
  8. guint size;
  9. } GPtrArrayPriv;
  10. /* Don't add more than 32 items to this please */
  11. static const char *items [] = {
  12. "Apples", "Oranges", "Plumbs", "Goats", "Snorps", "Grapes",
  13. "Tickle", "Place", "Coffee", "Cookies", "Cake", "Cheese",
  14. "Tseng", "Holiday", "Avenue", "Smashing", "Water", "Toilet",
  15. NULL
  16. };
  17. static GPtrArray *ptrarray_alloc_and_fill(gint *item_count)
  18. {
  19. GPtrArray *array = g_ptr_array_new();
  20. gint i;
  21. for(i = 0; items[i] != NULL; i++) {
  22. g_ptr_array_add(array, (gpointer)items[i]);
  23. }
  24. if(item_count != NULL) {
  25. *item_count = i;
  26. }
  27. return array;
  28. }
  29. static gint guess_size(gint length)
  30. {
  31. gint size = 1;
  32. while(size < length) {
  33. size <<= 1;
  34. }
  35. return size;
  36. }
  37. char *ptrarray_alloc()
  38. {
  39. GPtrArrayPriv *array;
  40. gint i;
  41. array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
  42. if(array->size != guess_size(array->len)) {
  43. return g_strdup_printf("Size should be %d, but it is %d",
  44. guess_size(array->len), array->size);
  45. }
  46. if(array->len != i) {
  47. return g_strdup_printf("Expected %d node(s) in the array", i);
  48. }
  49. g_ptr_array_free((GPtrArray *)array, TRUE);
  50. return NULL;
  51. }
  52. char *ptrarray_for_iterate()
  53. {
  54. GPtrArray *array = ptrarray_alloc_and_fill(NULL);
  55. gint i;
  56. for(i = 0; i < array->len; i++) {
  57. char *item = (char *)g_ptr_array_index(array, i);
  58. if(item != items[i]) {
  59. return g_strdup_printf(
  60. "Expected item at %d to be %s, but it was %s",
  61. i, items[i], item);
  62. }
  63. }
  64. g_ptr_array_free(array, TRUE);
  65. return NULL;
  66. }
  67. static gint foreach_iterate_index = 0;
  68. static char *foreach_iterate_error = NULL;
  69. void foreach_callback(gpointer data, gpointer user_data)
  70. {
  71. char *item = (char *)data;
  72. const char *item_cmp = items[foreach_iterate_index++];
  73. if(foreach_iterate_error != NULL) {
  74. return;
  75. }
  76. if(item != item_cmp) {
  77. foreach_iterate_error = g_strdup_printf(
  78. "Expected item at %d to be %s, but it was %s",
  79. foreach_iterate_index - 1, item_cmp, item);
  80. }
  81. }
  82. char *ptrarray_foreach_iterate()
  83. {
  84. GPtrArray *array = ptrarray_alloc_and_fill(NULL);
  85. foreach_iterate_index = 0;
  86. foreach_iterate_error = NULL;
  87. g_ptr_array_foreach(array, foreach_callback, array);
  88. g_ptr_array_free(array, TRUE);
  89. return foreach_iterate_error;
  90. }
  91. char *ptrarray_set_size()
  92. {
  93. GPtrArray *array = g_ptr_array_new();
  94. gint i, grow_length = 50;
  95. g_ptr_array_add(array, (gpointer)items[0]);
  96. g_ptr_array_add(array, (gpointer)items[1]);
  97. g_ptr_array_set_size(array, grow_length);
  98. if(array->len != grow_length) {
  99. return g_strdup_printf("Array length should be 50, it is %d",
  100. array->len);
  101. } else if(array->pdata[0] != items[0]) {
  102. return g_strdup_printf("Item 0 was overwritten, should be %s",
  103. items[0]);
  104. } else if(array->pdata[1] != items[1]) {
  105. return g_strdup_printf("Item 1 was overwritten, should be %s",
  106. items[1]);
  107. }
  108. for(i = 2; i < array->len; i++) {
  109. if(array->pdata[i] != NULL) {
  110. return g_strdup_printf("Item %d is not NULL, it is %p",
  111. i, array->pdata[i]);
  112. }
  113. }
  114. g_ptr_array_free(array, TRUE);
  115. return NULL;
  116. }
  117. static Test ptrarray_tests [] = {
  118. {"ptrarray_alloc", ptrarray_alloc},
  119. {"ptrarray_for_iterate", ptrarray_for_iterate},
  120. {"ptrarray_foreach_iterate", ptrarray_foreach_iterate},
  121. {"ptrarray_set_size", ptrarray_set_size},
  122. {NULL, NULL}
  123. };
  124. DEFINE_TEST_GROUP_INIT(ptrarray_tests_init, ptrarray_tests)