From e651ac258ae65a03731000ddb1582804570ea3dc Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Thu, 3 Feb 2011 00:03:08 +0100
Subject: Add a unit test for LdPointArray.
---
tests/point-array.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 137 insertions(+)
create mode 100644 tests/point-array.c
(limited to 'tests')
diff --git a/tests/point-array.c b/tests/point-array.c
new file mode 100644
index 0000000..90490de
--- /dev/null
+++ b/tests/point-array.c
@@ -0,0 +1,137 @@
+/*
+ * point-array.c
+ *
+ * This file is a part of logdiag.
+ * Copyright Přemysl Janouch 2011. All rights reserved.
+ *
+ * See the file LICENSE for licensing information.
+ *
+ */
+
+#include
+
+#define POINT_ARRAY_LENGTH 5
+
+typedef struct
+{
+ LdPointArray *points;
+}
+PointArray;
+
+static void
+point_array_setup (PointArray *fixture, gconstpointer test_data)
+{
+ guint i;
+
+ fixture->points = ld_point_array_sized_new (POINT_ARRAY_LENGTH);
+ fixture->points->length = POINT_ARRAY_LENGTH;
+ for (i = 0; i < POINT_ARRAY_LENGTH; i++)
+ {
+ fixture->points->points[i].x = i;
+ fixture->points->points[i].y = i;
+ }
+}
+
+static void
+point_array_teardown (PointArray *fixture, gconstpointer test_data)
+{
+ ld_point_array_free (fixture->points);
+}
+
+static void
+point_array_test_new (void)
+{
+ LdPointArray *points;
+
+ points = ld_point_array_new ();
+ g_assert_cmpuint (points->length, ==, 0);
+ ld_point_array_free (points);
+}
+
+static void
+point_array_test_sized_new (void)
+{
+ LdPointArray *points;
+
+ points = ld_point_array_sized_new (5);
+ g_assert_cmpuint (points->length, ==, 0);
+ g_assert_cmpuint (points->size, ==, 5);
+ g_assert (points->points != NULL);
+ ld_point_array_free (points);
+}
+
+static void
+point_array_test_insert (PointArray *fixture, gconstpointer user_data)
+{
+ LdPoint points[] = {{3, -1}, {4, -1}, {5, -9}};
+ const guint offset = 1;
+ guint i, j;
+
+ ld_point_array_insert (fixture->points,
+ points, offset, G_N_ELEMENTS (points));
+ g_assert_cmpuint (fixture->points->length,
+ ==, POINT_ARRAY_LENGTH + G_N_ELEMENTS (points));
+
+ for (i = 0, j = 0; i < POINT_ARRAY_LENGTH + G_N_ELEMENTS (points); i++)
+ {
+ /* Check that our values have been really inserted. */
+ if (i >= offset && i < offset + G_N_ELEMENTS (points))
+ {
+ g_assert_cmpfloat (fixture->points->points[i].x,
+ ==, points[i - offset].x);
+ g_assert_cmpfloat (fixture->points->points[i].y,
+ ==, points[i - offset].y);
+ continue;
+ }
+
+ /* And everything else is intact. */
+ g_assert_cmpfloat (fixture->points->points[i].x, ==, j);
+ g_assert_cmpfloat (fixture->points->points[i].y, ==, j);
+ j++;
+ }
+}
+
+static void
+point_array_test_remove (PointArray *fixture, gconstpointer user_data)
+{
+ const guint offset = 1;
+ const guint length = 3;
+ guint i, j;
+
+ ld_point_array_remove (fixture->points, offset, length);
+ g_assert_cmpuint (fixture->points->length,
+ ==, POINT_ARRAY_LENGTH - length);
+
+ for (i = 0, j = 0; i < POINT_ARRAY_LENGTH; i++)
+ {
+ /* Leave out the hole. */
+ if (i >= offset && i < offset + length)
+ continue;
+
+ /* And test that everything else is intact. */
+ g_assert_cmpfloat (fixture->points->points[j].x, ==, i);
+ g_assert_cmpfloat (fixture->points->points[j].y, ==, i);
+ j++;
+ }
+}
+
+int
+main (int argc, char *argv[])
+{
+ gtk_test_init (&argc, &argv, NULL);
+
+ /* Creation. */
+ g_test_add_func ("/point-array/new", point_array_test_new);
+ g_test_add_func ("/point-array/sized-new", point_array_test_sized_new);
+
+ /* Modification. */
+ g_test_add ("/point-array/insert", PointArray, NULL,
+ point_array_setup, point_array_test_insert,
+ point_array_teardown);
+ g_test_add ("/point-array/remove", PointArray, NULL,
+ point_array_setup, point_array_test_remove,
+ point_array_teardown);
+
+ return g_test_run ();
+}
+
--
cgit v1.2.3-70-g09d2