array.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <glib.h>
  4. #include "test.h"
  5. /* example from glib documentation */
  6. RESULT
  7. test_array_big ()
  8. {
  9. GArray *garray;
  10. gint i;
  11. /* We create a new array to store gint values.
  12. We don't want it zero-terminated or cleared to 0's. */
  13. garray = g_array_new (FALSE, FALSE, sizeof (gint));
  14. for (i = 0; i < 10000; i++)
  15. g_array_append_val (garray, i);
  16. for (i = 0; i < 10000; i++)
  17. if (g_array_index (garray, gint, i) != i)
  18. return FAILED ("array value didn't match");
  19. g_array_free (garray, TRUE);
  20. return NULL;
  21. }
  22. RESULT
  23. test_array_index ()
  24. {
  25. GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
  26. int v;
  27. v = 27;
  28. g_array_append_val (array, v);
  29. if (27 != g_array_index (array, int, 0))
  30. return FAILED ("");
  31. g_array_free (array, TRUE);
  32. return NULL;
  33. }
  34. RESULT
  35. test_array_append_zero_terminated ()
  36. {
  37. GArray *array = g_array_new (TRUE, FALSE, sizeof (int));
  38. int v;
  39. v = 27;
  40. g_array_append_val (array, v);
  41. if (27 != g_array_index (array, int, 0))
  42. return FAILED ("g_array_append_val failed");
  43. if (0 != g_array_index (array, int, 1))
  44. return FAILED ("zero_terminated didn't append a zero element");
  45. g_array_free (array, TRUE);
  46. return NULL;
  47. }
  48. RESULT
  49. test_array_append ()
  50. {
  51. GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
  52. int v;
  53. if (0 != array->len)
  54. return FAILED ("initial array length not zero");
  55. v = 27;
  56. g_array_append_val (array, v);
  57. if (1 != array->len)
  58. return FAILED ("array append failed");
  59. g_array_free (array, TRUE);
  60. return NULL;
  61. }
  62. RESULT
  63. test_array_insert_val ()
  64. {
  65. GArray *array = g_array_new (FALSE, FALSE, sizeof (gpointer));
  66. gpointer ptr0, ptr1, ptr2, ptr3;
  67. g_array_insert_val (array, 0, array);
  68. if (array != g_array_index (array, gpointer, 0))
  69. return FAILED ("1 The value in the array is incorrect");
  70. g_array_insert_val (array, 1, array);
  71. if (array != g_array_index (array, gpointer, 1))
  72. return FAILED ("2 The value in the array is incorrect");
  73. g_array_insert_val (array, 2, array);
  74. if (array != g_array_index (array, gpointer, 2))
  75. return FAILED ("3 The value in the array is incorrect");
  76. g_array_free (array, TRUE);
  77. array = g_array_new (FALSE, FALSE, sizeof (gpointer));
  78. ptr0 = array;
  79. ptr1 = array + 1;
  80. ptr2 = array + 2;
  81. ptr3 = array + 3;
  82. g_array_insert_val (array, 0, ptr0);
  83. g_array_insert_val (array, 1, ptr1);
  84. g_array_insert_val (array, 2, ptr2);
  85. g_array_insert_val (array, 1, ptr3);
  86. if (ptr0 != g_array_index (array, gpointer, 0))
  87. return FAILED ("4 The value in the array is incorrect");
  88. if (ptr3 != g_array_index (array, gpointer, 1))
  89. return FAILED ("5 The value in the array is incorrect");
  90. if (ptr1 != g_array_index (array, gpointer, 2))
  91. return FAILED ("6 The value in the array is incorrect");
  92. if (ptr2 != g_array_index (array, gpointer, 3))
  93. return FAILED ("7 The value in the array is incorrect");
  94. g_array_free (array, TRUE);
  95. return NULL;
  96. }
  97. RESULT
  98. test_array_remove ()
  99. {
  100. GArray *array = g_array_new (FALSE, FALSE, sizeof (int));
  101. int v[] = {30, 29, 28, 27, 26, 25};
  102. g_array_append_vals (array, v, 6);
  103. if (6 != array->len)
  104. return FAILED ("append_vals fail");
  105. g_array_remove_index (array, 3);
  106. if (5 != array->len)
  107. return FAILED ("remove_index failed to update length");
  108. if (26 != g_array_index (array, int, 3))
  109. return FAILED ("remove_index failed to update the array");
  110. g_array_free (array, TRUE);
  111. return NULL;
  112. }
  113. static Test array_tests [] = {
  114. {"big", test_array_big},
  115. {"append", test_array_append},
  116. {"insert_val", test_array_insert_val},
  117. {"index", test_array_index},
  118. {"remove", test_array_remove},
  119. {"append_zero_term", test_array_append_zero_terminated},
  120. {NULL, NULL}
  121. };
  122. DEFINE_TEST_GROUP_INIT(array_tests_init, array_tests)