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. --- CMakeLists.txt | 17 +++++++ tests/point-array.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 tests/point-array.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a7e62ec..00c6b71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,6 +142,9 @@ set (liblogdiag_HEADERS liblogdiag/ld-lua-symbol.h liblogdiag/ld-lua-symbol-private.h) +set (logdiag_TESTS + point-array) + set (logdiag_SOURCES src/ld-window-main.c src/logdiag.c) @@ -220,6 +223,20 @@ set_target_properties (liblogdiag PROPERTIES OUTPUT_NAME logdiag) add_executable (logdiag WIN32 ${logdiag_SOURCES} ${logdiag_HEADERS}) target_link_libraries (logdiag liblogdiag ${logdiag_LIBS}) +# Testing +option (BUILD_TESTING "Build tests" OFF) + +if (BUILD_TESTING) + enable_testing () + + foreach (name ${logdiag_TESTS}) + add_executable (test-${name} tests/${name}.c) + target_link_libraries (test-${name} liblogdiag ${logdiag_LIBS}) + + add_test (test-${name} test-${name}) + endforeach (name) +endif (BUILD_TESTING) + # Generate documentation if (GTK_DOC_FOUND) GTK_DOC_RUN (WORKING_DIR ${project_DOC_DIR} 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