Schematic editor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

point-array.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * point-array.c
  3. *
  4. * This file is a part of logdiag.
  5. * Copyright Přemysl Janouch 2011
  6. *
  7. * See the file LICENSE for licensing information.
  8. *
  9. */
  10. #include <liblogdiag/liblogdiag.h>
  11. #define POINT_ARRAY_LENGTH 5
  12. typedef struct
  13. {
  14. LdPointArray *points;
  15. }
  16. PointArray;
  17. static void
  18. point_array_setup (PointArray *fixture, gconstpointer test_data)
  19. {
  20. guint i;
  21. fixture->points = ld_point_array_sized_new (POINT_ARRAY_LENGTH);
  22. fixture->points->length = POINT_ARRAY_LENGTH;
  23. for (i = 0; i < POINT_ARRAY_LENGTH; i++)
  24. {
  25. fixture->points->points[i].x = i;
  26. fixture->points->points[i].y = i;
  27. }
  28. }
  29. static void
  30. point_array_teardown (PointArray *fixture, gconstpointer test_data)
  31. {
  32. ld_point_array_free (fixture->points);
  33. }
  34. static void
  35. point_array_test_new (void)
  36. {
  37. LdPointArray *points;
  38. points = ld_point_array_new ();
  39. g_assert_cmpuint (points->length, ==, 0);
  40. ld_point_array_free (points);
  41. }
  42. static void
  43. point_array_test_sized_new (void)
  44. {
  45. LdPointArray *points;
  46. points = ld_point_array_sized_new (5);
  47. g_assert_cmpuint (points->length, ==, 0);
  48. g_assert_cmpuint (points->size, ==, 5);
  49. g_assert (points->points != NULL);
  50. ld_point_array_free (points);
  51. }
  52. static void
  53. point_array_test_insert (PointArray *fixture, gconstpointer user_data)
  54. {
  55. LdPoint points[] = {{3, -1}, {4, -1}, {5, -9}};
  56. const guint offset = 1;
  57. guint i, j;
  58. ld_point_array_insert (fixture->points,
  59. points, offset, G_N_ELEMENTS (points));
  60. g_assert_cmpuint (fixture->points->length,
  61. ==, POINT_ARRAY_LENGTH + G_N_ELEMENTS (points));
  62. for (i = 0, j = 0; i < POINT_ARRAY_LENGTH + G_N_ELEMENTS (points); i++)
  63. {
  64. /* Check that our values have been really inserted. */
  65. if (i >= offset && i < offset + G_N_ELEMENTS (points))
  66. {
  67. g_assert_cmpfloat (fixture->points->points[i].x,
  68. ==, points[i - offset].x);
  69. g_assert_cmpfloat (fixture->points->points[i].y,
  70. ==, points[i - offset].y);
  71. continue;
  72. }
  73. /* And everything else is intact. */
  74. g_assert_cmpfloat (fixture->points->points[i].x, ==, j);
  75. g_assert_cmpfloat (fixture->points->points[i].y, ==, j);
  76. j++;
  77. }
  78. }
  79. static void
  80. point_array_test_remove (PointArray *fixture, gconstpointer user_data)
  81. {
  82. const guint offset = 1;
  83. const guint length = 3;
  84. guint i, j;
  85. ld_point_array_remove (fixture->points, offset, length);
  86. g_assert_cmpuint (fixture->points->length,
  87. ==, POINT_ARRAY_LENGTH - length);
  88. for (i = 0, j = 0; i < POINT_ARRAY_LENGTH; i++)
  89. {
  90. /* Leave out the hole. */
  91. if (i >= offset && i < offset + length)
  92. continue;
  93. /* And test that everything else is intact. */
  94. g_assert_cmpfloat (fixture->points->points[j].x, ==, i);
  95. g_assert_cmpfloat (fixture->points->points[j].y, ==, i);
  96. j++;
  97. }
  98. }
  99. int
  100. main (int argc, char *argv[])
  101. {
  102. g_test_init (&argc, &argv, NULL);
  103. /* Creation. */
  104. g_test_add_func ("/point-array/new", point_array_test_new);
  105. g_test_add_func ("/point-array/sized-new", point_array_test_sized_new);
  106. /* Modification. */
  107. g_test_add ("/point-array/insert", PointArray, NULL,
  108. point_array_setup, point_array_test_insert,
  109. point_array_teardown);
  110. g_test_add ("/point-array/remove", PointArray, NULL,
  111. point_array_setup, point_array_test_remove,
  112. point_array_teardown);
  113. return g_test_run ();
  114. }