diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-05-15 15:54:56 -0600 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-05-15 15:58:05 -0600 |
commit | a5a6b94c038e01ebf1e2de0a0774a69b02fb8e1e (patch) | |
tree | 66a4b977fff9f0119456b6c5a3398d25976f6c53 /libdimension | |
parent | 130cbc32499ade9235873bc5d575816f80636916 (diff) | |
download | dimension-a5a6b94c038e01ebf1e2de0a0774a69b02fb8e1e.tar.xz |
Group tests and benchmarks with the corresponding source code.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/Doxyfile.in | 2 | ||||
-rw-r--r-- | libdimension/Makefile.am | 12 | ||||
-rw-r--r-- | libdimension/bench/Makefile.am | 41 | ||||
-rw-r--r-- | libdimension/bench/array.c | 118 | ||||
-rw-r--r-- | libdimension/bench/geometry.c | 171 | ||||
-rw-r--r-- | libdimension/bench/polynomial.c | 72 | ||||
-rw-r--r-- | libdimension/bench/prtree.c | 119 | ||||
-rw-r--r-- | libdimension/tests/Makefile.am | 81 | ||||
-rw-r--r-- | libdimension/tests/cxx.cpp | 36 | ||||
-rw-r--r-- | libdimension/tests/display-stubs.c | 38 | ||||
-rw-r--r-- | libdimension/tests/display.c | 151 | ||||
-rw-r--r-- | libdimension/tests/error.c | 30 | ||||
-rw-r--r-- | libdimension/tests/gl.c | 87 | ||||
-rw-r--r-- | libdimension/tests/png.c | 106 | ||||
-rw-r--r-- | libdimension/tests/polynomial.c | 62 | ||||
-rw-r--r-- | libdimension/tests/prtree.c | 98 | ||||
-rw-r--r-- | libdimension/tests/render.c | 309 | ||||
-rw-r--r-- | libdimension/tests/tests.h | 46 | ||||
-rw-r--r-- | libdimension/tests/warning-as-error.c | 31 | ||||
-rw-r--r-- | libdimension/tests/warning.c | 30 |
20 files changed, 1637 insertions, 3 deletions
diff --git a/libdimension/Doxyfile.in b/libdimension/Doxyfile.in index 23d7bfb..145bd15 100644 --- a/libdimension/Doxyfile.in +++ b/libdimension/Doxyfile.in @@ -1,7 +1,7 @@ DOXYFILE_ENCODING = UTF-8 PROJECT_NAME = libdimension PROJECT_NUMBER = @VERSION@ -OUTPUT_DIRECTORY = @top_builddir@/doc/libdimension +OUTPUT_DIRECTORY = @top_builddir@/libdimension/doc OUTPUT_LANGUAGE = English FULL_PATH_NAMES = YES OPTIMIZE_OUTPUT_FOR_C = YES diff --git a/libdimension/Makefile.am b/libdimension/Makefile.am index 4d4f58e..0a134b1 100644 --- a/libdimension/Makefile.am +++ b/libdimension/Makefile.am @@ -17,6 +17,9 @@ ## along with this program. If not, see <http://www.gnu.org/licenses/>. ## ########################################################################### +SUBDIRS = bench \ + tests + # Make dmnsn_error() backtraces useful AM_LDFLAGS = -rdynamic @@ -130,6 +133,12 @@ if PROFILE libdimension_la_SOURCES += profile.c endif +pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA = libdimension.pc + +bench: all-recursive + cd bench && $(MAKE) $(AM_MAKEFLAGS) bench + clean-local: clean-docs doc: @@ -137,5 +146,4 @@ doc: clean-docs: rm -rf $(top_builddir)/doc/libdimension -pkgconfigdir = $(libdir)/pkgconfig -pkgconfig_DATA = libdimension.pc +.PHONY: bench doc diff --git a/libdimension/bench/Makefile.am b/libdimension/bench/Makefile.am new file mode 100644 index 0000000..353acb3 --- /dev/null +++ b/libdimension/bench/Makefile.am @@ -0,0 +1,41 @@ +########################################################################### +## Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> ## +## ## +## This file is part of The Dimension Build Suite. ## +## ## +## The Dimension Build Suite is free software; you can redistribute it ## +## and/or modify it under the terms of the GNU General Public License as ## +## published by the Free Software Foundation; either version 3 of the ## +## License, or (at your option) any later version. ## +## ## +## The Dimension Build Suite is distributed in the hope that it will be ## +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty ## +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## +## General Public License for more details. ## +## ## +## You should have received a copy of the GNU General Public License ## +## along with this program. If not, see <http://www.gnu.org/licenses/>. ## +########################################################################### + +INCLUDES = -I$(top_srcdir)/libdimension + +EXTRA_PROGRAMS = bench-array \ + bench-geometry \ + bench-polynomial \ + bench-prtree + +AM_CFLAGS = $(libsandglass_CFLAGS) -fno-inline +AM_LDFLAGS = $(libsandglass_LIBS) $(top_builddir)/libdimension/libdimension.la + +bench_array_SOURCES = array.c +bench_geometry_SOURCES = geometry.c +bench_polynomial_SOURCES = polynomial.c +bench_prtree_SOURCES = prtree.c + +bench: $(EXTRA_PROGRAMS) + ./bench-array + ./bench-geometry + ./bench-polynomial + ./bench-prtree + +.PHONY: bench diff --git a/libdimension/bench/array.c b/libdimension/bench/array.c new file mode 100644 index 0000000..3fe0c83 --- /dev/null +++ b/libdimension/bench/array.c @@ -0,0 +1,118 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Benchmark Suite. * + * * + * The Dimension Benchmark Suite is free software; you can redistribute * + * it and/or modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Benchmark Suite is distributed in the hope that it will * + * be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * + * the GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "dimension.h" +#include <sandglass.h> +#include <stdlib.h> +#include <stdint.h> + +int +main(void) +{ + const unsigned int count = 32; + uint32_t object = 1; + + sandglass_t sandglass; + if (sandglass_init_monotonic(&sandglass, SANDGLASS_CPUTIME) != 0) { + perror("sandglass_create()"); + return EXIT_FAILURE; + } + + /* Benchmark allocation and deallocation */ + dmnsn_array *array; + sandglass_bench_fine(&sandglass, { + array = dmnsn_new_array(sizeof(object)); + dmnsn_delete_array(array); + }); + printf("dmnsn_new_array() + dmnsn_delete_array(): %ld\n", sandglass.grains); + + /* Create our test array */ + array = dmnsn_new_array(sizeof(object)); + + /* dmnsn_array_push() */ + + printf("dmnsn_array_push():"); + + for (unsigned int i = 0; i < count; ++i) { + sandglass_bench_noprecache(&sandglass, dmnsn_array_push(array, &object)); + printf(" %ld", sandglass.grains); + } + printf("\n"); + + /* dmnsn_array_get() */ + sandglass_bench_fine(&sandglass, dmnsn_array_get(array, count/2, &object)); + printf("dmnsn_array_get(): %ld\n", sandglass.grains); + + /* dmnsn_array_set() */ + sandglass_bench_fine(&sandglass, dmnsn_array_set(array, count/2, &object)); + printf("dmnsn_array_set(): %ld\n", sandglass.grains); + + /* dmnsn_array_at() */ + void *ptr; + sandglass_bench_fine(&sandglass, ptr = dmnsn_array_at(array, count/2)); + printf("dmnsn_array_at() = %p: %ld\n", ptr, sandglass.grains); + + /* dmnsn_array_size() */ + size_t size; + sandglass_bench_fine(&sandglass, size = dmnsn_array_size(array)); + printf("dmnsn_array_size() = %zu: %ld\n", size, sandglass.grains); + + /* dmnsn_array_resize() */ + dmnsn_array_resize(array, count); + sandglass_bench_noprecache(&sandglass, dmnsn_array_resize(array, count * 2)); + printf("dmnsn_array_resize(): %ld", sandglass.grains); + + sandglass_bench_noprecache(&sandglass, dmnsn_array_resize(array, count)); + printf(" %ld\n", sandglass.grains); + + /* dmnsn_array_insert() */ + + printf("dmnsn_array_insert():"); + + for (size_t i = 0; i < count; ++i) { + sandglass_bench_noprecache(&sandglass, + dmnsn_array_insert(array, count/2, &object)); + printf(" %ld", sandglass.grains); + } + printf("\n"); + + /* dmnsn_array_remove() */ + + printf("dmnsn_array_remove():"); + + for (size_t i = 0; i < count; ++i) { + sandglass_bench_noprecache(&sandglass, + dmnsn_array_remove(array, count/2)); + printf(" %ld", sandglass.grains); + } + printf("\n"); + + /* dmnsn_array_pop() */ + + printf("dmnsn_array_pop():"); + + for (size_t i = 0; i < count; ++i) { + sandglass_bench_noprecache(&sandglass, dmnsn_array_pop(array, &object)); + printf(" %ld", sandglass.grains); + } + printf("\n"); + + dmnsn_delete_array(array); + return EXIT_SUCCESS; +} diff --git a/libdimension/bench/geometry.c b/libdimension/bench/geometry.c new file mode 100644 index 0000000..4b77c9b --- /dev/null +++ b/libdimension/bench/geometry.c @@ -0,0 +1,171 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Benchmark Suite. * + * * + * The Dimension Benchmark Suite is free software; you can redistribute * + * it and/or modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Benchmark Suite is distributed in the hope that it will * + * be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * + * the GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "dimension.h" +#include <sandglass.h> +#include <stdlib.h> + +int +main(void) +{ + dmnsn_vector vector, vector2; + dmnsn_matrix matrix, matrix2; + dmnsn_line line; + double result; + + sandglass_t sandglass; + if (sandglass_init_monotonic(&sandglass, SANDGLASS_CPUTIME) != 0) { + perror("sandglass_create()"); + return EXIT_FAILURE; + } + + /* dmnsn_new_vector() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_new_vector(1.0, 2.0, 3.0); + }); + printf("dmnsn_new_vector(): %ld\n", sandglass.grains); + + /* dmnsn_matrix_construct() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_new_matrix(1.0, 1.0, 0.0, 0.0, + 1.0, 1.0, 1.0, 0.0, + 0.0, 1.0, 1.0, 0.0); + }); + printf("dmnsn_new_matrix(): %ld\n", sandglass.grains); + + /* dmnsn_identity_matrix() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_identity_matrix(); + }); + printf("dmnsn_identity_matrix(): %ld\n", sandglass.grains); + + /* dmnsn_scale_matrix() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_scale_matrix(vector); + }); + printf("dmnsn_scale_matrix(): %ld\n", sandglass.grains); + + /* dmnsn_identity_matrix() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_translation_matrix(vector); + }); + printf("dmnsn_translation_matrix(): %ld\n", sandglass.grains); + + /* dmnsn_rotation_matrix() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_rotation_matrix(vector); + }); + printf("dmnsn_rotation_matrix(): %ld\n", sandglass.grains); + + /* dmnsn_new_line() */ + vector2 = dmnsn_new_vector(3.0, 2.0, 1.0); + sandglass_bench_fine(&sandglass, { + line = dmnsn_new_line(vector, vector2); + }); + printf("dmnsn_new_line(): %ld\n", sandglass.grains); + + /* dmnsn_vector_add() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_vector_add(vector, vector2); + }); + printf("dmnsn_vector_add(): %ld\n", sandglass.grains); + + /* dmnsn_vector_sub() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_vector_sub(vector, vector2); + }); + printf("dmnsn_vector_sub(): %ld\n", sandglass.grains); + + /* dmnsn_vector_mul() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_vector_mul(2.0, vector); + }); + printf("dmnsn_vector_mul(): %ld\n", sandglass.grains); + + /* dmnsn_vector_div() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_vector_div(vector, 2.0); + }); + printf("dmnsn_vector_div(): %ld\n", sandglass.grains); + + /* dmnsn_vector_cross() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_vector_cross(vector, vector2); + }); + printf("dmnsn_vector_cross(): %ld\n", sandglass.grains); + + /* dmnsn_vector_dot() */ + sandglass_bench_fine(&sandglass, { + result = dmnsn_vector_dot(vector, vector2); + }); + printf("dmnsn_vector_dot(): %ld\n", sandglass.grains); + + /* dmnsn_vector_norm() */ + sandglass_bench_fine(&sandglass, { + result = dmnsn_vector_norm(vector); + }); + printf("dmnsn_vector_norm(): %ld\n", sandglass.grains); + + /* dmnsn_vector_normalize() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_vector_normalize(vector); + }); + printf("dmnsn_vector_normalize(): %ld\n", sandglass.grains); + + /* dmnsn_matrix_inverse() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_matrix_inverse(matrix); + }); + printf("dmnsn_matrix_inverse(): %ld\n", sandglass.grains); + + /* dmnsn_matrix_inverse(HARD) */ + matrix2 = dmnsn_new_matrix(1.0, 1.0, 0.0, 0.0, + 1.0, 1.0, 1.0, 0.0, + 0.0, 1.0, 1.0, 0.0); + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_matrix_inverse(matrix2); + }); + printf("dmnsn_matrix_inverse(HARD): %ld\n", sandglass.grains); + + /* dmnsn_matrix_mul() */ + sandglass_bench_fine(&sandglass, { + matrix = dmnsn_matrix_mul(matrix, matrix2); + }); + printf("dmnsn_matrix_mul(): %ld\n", sandglass.grains); + + /* dmnsn_transform_vector() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_transform_vector(matrix, vector); + }); + printf("dmnsn_transform_vector(): %ld\n", sandglass.grains); + + /* dmnsn_transform_line() */ + sandglass_bench_fine(&sandglass, { + line = dmnsn_transform_line(matrix, line); + }); + printf("dmnsn_transform_line(): %ld\n", sandglass.grains); + + /* dmnsn_line_point() */ + sandglass_bench_fine(&sandglass, { + vector = dmnsn_line_point(line, result); + }); + printf("dmnsn_line_point(): %ld\n", sandglass.grains); + + return EXIT_SUCCESS; +} diff --git a/libdimension/bench/polynomial.c b/libdimension/bench/polynomial.c new file mode 100644 index 0000000..0313060 --- /dev/null +++ b/libdimension/bench/polynomial.c @@ -0,0 +1,72 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Benchmark Suite. * + * * + * The Dimension Benchmark Suite is free software; you can redistribute * + * it and/or modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Benchmark Suite is distributed in the hope that it will * + * be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * + * the GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "dimension.h" +#include <sandglass.h> +#include <stdlib.h> + +int +main(void) +{ +#define NPOLY 5 + double p[NPOLY][NPOLY + 1], x[NPOLY]; + + /* p[0][] = x - 0.5; */ + p[0][1] = 1.0; + p[0][0] = -0.5; + + /* p[1][] = (x + 0.5)*(x - 0.5) */ + p[1][2] = 1.0; + p[1][1] = 0.0; + p[1][0] = -0.25; + + /* p[2][] = (x + 1)*(x - 1.2345)*(x - 100) */ + p[2][3] = 1.0; + p[2][2] = -100.2345; + p[2][1] = 22.2155; + p[2][0] = 123.45; + + /* p[3][] = (x + 1)*(x - 1.2345)*(x - 5)*(x - 100) */ + p[3][4] = 1.0; + p[3][3] = -105.2345; + p[3][2] = 523.388; + p[3][1] = 12.3725; + p[3][0] = -617.25; + + /* p[4][] = (x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) */ + p[4][5] = 1.0; + p[4][4] = -107.5801; + p[4][3] = 770.2260432; + p[4][2] = -1215.2863928; + p[4][1] = -646.270936; + p[4][0] = 1447.8216; + + sandglass_t sandglass; + if (sandglass_init_monotonic(&sandglass, SANDGLASS_CPUTIME) != 0) { + perror("sandglass_create()"); + return EXIT_FAILURE; + } + + for (size_t i = 0; i < NPOLY; ++i) { + sandglass_bench_fine(&sandglass, dmnsn_solve_polynomial(p[i], i + 1, x)); + printf("dmnsn_solve_polynomial(x^%zu): %ld\n", i + 1, sandglass.grains); + } + + return EXIT_SUCCESS; +} diff --git a/libdimension/bench/prtree.c b/libdimension/bench/prtree.c new file mode 100644 index 0000000..b6f778a --- /dev/null +++ b/libdimension/bench/prtree.c @@ -0,0 +1,119 @@ +/************************************************************************* + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Benchmark Suite. * + * * + * The Dimension Benchmark Suite is free software; you can redistribute * + * it and/or modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Benchmark Suite is distributed in the hope that it will * + * be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See * + * the GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "dimension-impl.h" +#include <sandglass.h> +#include <stdlib.h> + +static bool +dmnsn_fake_intersection_fn(const dmnsn_object *object, dmnsn_line line, + dmnsn_intersection *intersection) +{ + intersection->t = (object->bounding_box.min.z - line.x0.z)/line.n.z; + intersection->normal = dmnsn_x; + return true; +} + +static bool +dmnsn_fake_inside_fn(const dmnsn_object *object, dmnsn_vector point) +{ + return true; +} + +static void +dmnsn_randomize_bounding_box(dmnsn_object *object) +{ + dmnsn_vector a, b; + + a.x = 2.0*((double)rand())/RAND_MAX - 1.0; + a.y = 2.0*((double)rand())/RAND_MAX - 1.0; + a.z = 2.0*((double)rand())/RAND_MAX - 1.0; + + b.x = 2.0*((double)rand())/RAND_MAX - 1.0; + b.y = 2.0*((double)rand())/RAND_MAX - 1.0; + b.z = 2.0*((double)rand())/RAND_MAX - 1.0; + + object->bounding_box.min = dmnsn_vector_min(a, b); + object->bounding_box.max = dmnsn_vector_max(a, b); +} + +static dmnsn_object * +dmnsn_new_fake_object(void) +{ + dmnsn_object *object = dmnsn_new_object(); + /* Generate a bounding box in (-1, -1, -1), (1, 1, 1) */ + dmnsn_randomize_bounding_box(object); + object->intersection_fn = dmnsn_fake_intersection_fn; + object->inside_fn = dmnsn_fake_inside_fn; + return object; +} + +int +main(void) +{ + const size_t nobjects = 10000; + + sandglass_t sandglass; + if (sandglass_init_monotonic(&sandglass, SANDGLASS_CPUTIME) != 0) { + perror("sandglass_create()"); + return EXIT_FAILURE; + } + + dmnsn_array *objects = dmnsn_new_array(sizeof(dmnsn_object *)); + for (size_t i = 0; i < nobjects; ++i) { + dmnsn_object *object = dmnsn_new_fake_object(); + dmnsn_initialize_object(object); + dmnsn_array_push(objects, &object); + } + + dmnsn_prtree *tree; + sandglass_bench_noprecache(&sandglass, { + tree = dmnsn_new_prtree(objects); + }); + printf("dmnsn_new_prtree(): %ld\n", sandglass.grains); + + /* dmnsn_prtree_intersection() */ + dmnsn_line ray = dmnsn_new_line( + dmnsn_new_vector( 1.0, 1.0, -2.0), + dmnsn_new_vector(-0.5, -0.5, 1.0) + ); + dmnsn_intersection intersection; + + sandglass_bench_fine(&sandglass, { + dmnsn_prtree_intersection(tree, ray, &intersection, true); + }); + printf("dmnsn_prtree_intersection(): %ld\n", sandglass.grains); + + /* dmnsn_prtree_inside() */ + sandglass_bench_fine(&sandglass, { + dmnsn_prtree_inside(tree, dmnsn_zero); + }); + printf("dmnsn_prtree_inside(): %ld\n", sandglass.grains); + + /* Cleanup */ + dmnsn_delete_prtree(tree); + for (size_t i = 0; i < nobjects; ++i) { + dmnsn_object *object; + dmnsn_array_get(objects, i, &object); + dmnsn_delete_object(object); + } + + dmnsn_delete_array(objects); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/Makefile.am b/libdimension/tests/Makefile.am new file mode 100644 index 0000000..6b5f8ca --- /dev/null +++ b/libdimension/tests/Makefile.am @@ -0,0 +1,81 @@ +########################################################################### +## Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> ## +## ## +## This file is part of The Dimension Build Suite. ## +## ## +## The Dimension Build Suite is free software; you can redistribute it ## +## and/or modify it under the terms of the GNU General Public License as ## +## published by the Free Software Foundation; either version 3 of the ## +## License, or (at your option) any later version. ## +## ## +## The Dimension Build Suite is distributed in the hope that it will be ## +## useful, but WITHOUT ANY WARRANTY; without even the implied warranty ## +## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## +## General Public License for more details. ## +## ## +## You should have received a copy of the GNU General Public License ## +## along with this program. If not, see <http://www.gnu.org/licenses/>. ## +########################################################################### + +INCLUDES = -I$(top_srcdir)/libdimension + +check_LTLIBRARIES = libdimension-tests.la +check_PROGRAMS = warning-test \ + warning-as-error-test \ + error-test \ + polynomial-test \ + prtree-test \ + png-test \ + gl-test \ + render-test \ + cxx-test +TESTS = $(check_PROGRAMS) +XFAIL_TESTS = warning-as-error-test error-test + +if !PNG +XFAIL_TESTS += png-test +endif + +if !GL +XFAIL_TESTS += gl-test +endif + +libdimension_tests_la_SOURCES = tests.h +libdimension_tests_la_LIBADD = $(top_builddir)/libdimension/libdimension.la + +if GLX +libdimension_tests_la_SOURCES += display.c +libdimension_tests_la_LIBADD += -lX11 -lGL +else +libdimension_tests_la_SOURCES += display-stubs.c +endif + +warning_test_SOURCES = warning.c +warning_test_LDADD = libdimension-tests.la + +warning_as_error_test_SOURCES = warning-as-error.c +warning_as_error_test_LDADD = libdimension-tests.la + +error_test_SOURCES = error.c +error_test_LDADD = libdimension-tests.la + +polynomial_test_SOURCES = polynomial.c +polynomial_test_LDADD = libdimension-tests.la + +prtree_test_SOURCES = prtree.c +prtree_test_LDADD = libdimension-tests.la + +png_test_SOURCES = png.c +png_test_LDADD = libdimension-tests.la + +gl_test_SOURCES = gl.c +gl_test_LDADD = libdimension-tests.la + +render_test_SOURCES = render.c +render_test_LDADD = libdimension-tests.la + +cxx_test_SOURCES = cxx.cpp +cxx_test_LDADD = libdimension-tests.la + +clean-local: + rm -f *.png diff --git a/libdimension/tests/cxx.cpp b/libdimension/tests/cxx.cpp new file mode 100644 index 0000000..7405502 --- /dev/null +++ b/libdimension/tests/cxx.cpp @@ -0,0 +1,36 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "tests.h" +#include <cstdlib> + +/* + * Ensure that dimension.h can be included in a C++ program and behave properly. + */ +int +main(void) +{ + /* Treat warnings as errors for tests */ + dmnsn_die_on_warnings(true); + + /* Make sure we can to bit-ops on this enum type */ + dmnsn_quality quality = DMNSN_RENDER_PIGMENT | DMNSN_RENDER_LIGHTS; + + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/display-stubs.c b/libdimension/tests/display-stubs.c new file mode 100644 index 0000000..e3fb99e --- /dev/null +++ b/libdimension/tests/display-stubs.c @@ -0,0 +1,38 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "tests.h" +#include <errno.h> + +dmnsn_display * +dmnsn_new_display(const dmnsn_canvas *canvas) +{ + errno = ENOSYS; + return NULL; +} + +void +dmnsn_delete_display(dmnsn_display *display) +{ +} + +void +dmnsn_display_flush(dmnsn_display *display) +{ +} diff --git a/libdimension/tests/display.c b/libdimension/tests/display.c new file mode 100644 index 0000000..d4e2a1c --- /dev/null +++ b/libdimension/tests/display.c @@ -0,0 +1,151 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "tests.h" +#include <GL/glx.h> +#include <GL/gl.h> +#include <stdlib.h> + +struct dmnsn_display { + Display *dpy; + Window win; + Colormap cmap; + GLXContext cx; + XEvent event; + XVisualInfo *vi; +}; + +/* XIfEvent callback */ +static Bool +WaitForNotify(Display *d, XEvent *e, char *arg) +{ + return (e->type == MapNotify) && (e->xmap.window == (Window)arg); +} + +dmnsn_display * +dmnsn_new_display(const dmnsn_canvas *canvas) +{ + int attributeList[] = { + GLX_RGBA, + GLX_DOUBLEBUFFER, + GLX_RED_SIZE, 1, + GLX_GREEN_SIZE, 1, + GLX_BLUE_SIZE, 1, + None + }; + XSetWindowAttributes swa; + dmnsn_display *display; + + display = dmnsn_malloc(sizeof(dmnsn_display)); + + display->dpy = NULL; + display->win = 0; + display->cmap = 0; + display->cx = NULL; + display->vi = NULL; + + /* Get an X connection */ + display->dpy = XOpenDisplay(0); + if (!display->dpy) { + dmnsn_delete_display(display); + return NULL; + } + + /* Get an appropriate visual */ + display->vi = glXChooseVisual(display->dpy, DefaultScreen(display->dpy), + attributeList); + if (!display->vi) { + dmnsn_delete_display(display); + return NULL; + } + + /* Create a GLX context */ + display->cx = glXCreateContext(display->dpy, display->vi, 0, GL_TRUE); + if (!display->cx) { + dmnsn_delete_display(display); + return NULL; + } + + /* Create a color map */ + display->cmap = XCreateColormap(display->dpy, + RootWindow(display->dpy, display->vi->screen), + display->vi->visual, AllocNone); + if (!display->cmap) { + dmnsn_delete_display(display); + return NULL; + } + + /* Create a window */ + swa.colormap = display->cmap; + swa.border_pixel = 0; + swa.event_mask = StructureNotifyMask; + display->win = XCreateWindow(display->dpy, + RootWindow(display->dpy, display->vi->screen), + 0, 0, canvas->width, canvas->height, + 0, display->vi->depth, InputOutput, + display->vi->visual, + CWBorderPixel|CWColormap|CWEventMask, &swa); + if (!display->win) { + dmnsn_delete_display(display); + return NULL; + } + + XStoreName(display->dpy, display->win, "glX"); + + XMapWindow(display->dpy, display->win); + XIfEvent(display->dpy, &display->event, WaitForNotify, (char*)display->win); + + /* Connect the context to the window */ + glXMakeCurrent(display->dpy, display->win, display->cx); + glClearColor(0.0, 0.0, 0.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + dmnsn_display_flush(display); + + return display; +} + +void +dmnsn_delete_display(dmnsn_display *display) +{ + if (display) { + if (display->win) + XDestroyWindow(display->dpy, display->win); + + if (display->cmap) + XFreeColormap(display->dpy, display->cmap); + + if (display->cx) + glXDestroyContext(display->dpy, display->cx); + + if (display->vi) + XFree(display->vi); + + if (display->dpy) + XCloseDisplay(display->dpy); + + dmnsn_free(display); + } +} + +void +dmnsn_display_flush(dmnsn_display *display) +{ + glXWaitX(); + glXSwapBuffers(display->dpy, display->win); +} diff --git a/libdimension/tests/error.c b/libdimension/tests/error.c new file mode 100644 index 0000000..5a4d7fd --- /dev/null +++ b/libdimension/tests/error.c @@ -0,0 +1,30 @@ +/************************************************************************* + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +/* Make sure that errors terminate us - this test should fail */ + +#include "dimension.h" +#include <stdlib.h> + +int +main(void) +{ + dmnsn_error("This error is expected."); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/gl.c b/libdimension/tests/gl.c new file mode 100644 index 0000000..8d93cf5 --- /dev/null +++ b/libdimension/tests/gl.c @@ -0,0 +1,87 @@ +/************************************************************************* + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "tests.h" +#include <stdlib.h> + +int +main(void) +{ + /* Treat warnings as errors for tests */ + dmnsn_die_on_warnings(true); + + /* Allocate our canvas */ + dmnsn_canvas *canvas = dmnsn_new_canvas(768, 480); + + /* Optimize the canvas for GL drawing */ + if (dmnsn_gl_optimize_canvas(canvas) != 0) { + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n"); + return EXIT_FAILURE; + } + + /* Paint the canvas blue */ + dmnsn_clear_canvas(canvas, dmnsn_blue); + + /* Create a new glX display */ + dmnsn_display *display = dmnsn_new_display(canvas); + if (!display) { + fprintf(stderr, "--- WARNING: Couldn't initialize X or glX! ---\n"); + return EXIT_SUCCESS; + } + + /* Draw to OpenGL */ + printf("Drawing to OpenGL\n"); + if (dmnsn_gl_write_canvas(canvas) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + dmnsn_display_flush(display); + dmnsn_delete_canvas(canvas); + + /* + * Now test GL import/export + */ + + /* Read the image back from OpenGL */ + printf("Reading from OpenGL\n"); + canvas = dmnsn_gl_read_canvas(0, 0, 768, 480); + if (!canvas) { + dmnsn_delete_display(display); + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Reading from OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + + /* And draw it back */ + printf("Drawing to OpenGL\n"); + if (dmnsn_gl_write_canvas(canvas) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + + dmnsn_display_flush(display); + dmnsn_delete_display(display); + dmnsn_delete_canvas(canvas); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/png.c b/libdimension/tests/png.c new file mode 100644 index 0000000..328ee36 --- /dev/null +++ b/libdimension/tests/png.c @@ -0,0 +1,106 @@ +/************************************************************************* + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "tests.h" +#include <stdlib.h> +#include <stdio.h> + +int +main(void) +{ + /* Treat warnings as errors for tests */ + dmnsn_die_on_warnings(true); + + /* Allocate our canvas */ + dmnsn_canvas *canvas = dmnsn_new_canvas(768, 480); + + /* Optimize the canvas for PNG export */ + if (dmnsn_png_optimize_canvas(canvas) != 0) { + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n"); + return EXIT_FAILURE; + } + + /* Paint the canvas blue */ + dmnsn_clear_canvas(canvas, dmnsn_blue); + + /* Write the image to PNG */ + + printf("Writing scene to PNG\n"); + FILE *ofile = fopen("png1.png", "wb"); + if (!ofile) { + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Couldn't open 'png1.png' for writing! ---\n"); + return EXIT_FAILURE; + } + + if (dmnsn_png_write_canvas(canvas, ofile) != 0) { + fclose(ofile); + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); + return EXIT_FAILURE; + } + + fclose(ofile); + dmnsn_delete_canvas(canvas); + + /* + * Now test PNG import/export + */ + + /* Read the image back from PNG */ + + printf("Reading scene from PNG\n"); + FILE *ifile = fopen("png1.png", "rb"); + if (!ifile) { + fprintf(stderr, "--- Couldn't open 'png1.png' for reading! ---\n"); + return EXIT_FAILURE; + } + + canvas = dmnsn_png_read_canvas(ifile); + if (!canvas) { + fclose(ifile); + fprintf(stderr, "--- Reading canvas from PNG failed! ---\n"); + return EXIT_FAILURE; + } + + fclose(ifile); + + /* And write it back */ + + printf("Writing scene to PNG\n"); + ofile = fopen("png2.png", "wb"); + if (!ofile) { + fprintf(stderr, "--- Couldn't open 'png2.png' for writing! ---\n"); + dmnsn_delete_canvas(canvas); + return EXIT_FAILURE; + } + + if (dmnsn_png_write_canvas(canvas, ofile) != 0) { + fclose(ofile); + dmnsn_delete_canvas(canvas); + fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); + return EXIT_FAILURE; + } + + fclose(ofile); + dmnsn_delete_canvas(canvas); + + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/polynomial.c b/libdimension/tests/polynomial.c new file mode 100644 index 0000000..5632b49 --- /dev/null +++ b/libdimension/tests/polynomial.c @@ -0,0 +1,62 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +/* + * Basic test of numerical polynomial root-finder + */ + +#include "dimension.h" +#include <stddef.h> +#include <stdio.h> + +int +main(void) +{ + /* Treat warnings as errors for tests */ + dmnsn_die_on_warnings(true); + + double poly[6], x[5]; + /* poly[] = (x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) */ + poly[5] = 1.0; + poly[4] = -107.5801; + poly[3] = 770.2260432; + poly[2] = -1215.2863928; + poly[1] = -646.270936; + poly[0] = 1447.8216; + + size_t n = dmnsn_solve_polynomial(poly, 5, x); + if (n != 4) { + fprintf(stderr, + "--- Wrong number of roots found (%zu, should be %u) ---\n", + n, 4); + return EXIT_FAILURE; + } + + for (size_t i = 0; i < n; ++i) { + double evmin = dmnsn_evaluate_polynomial(poly, 5, x[i] - dmnsn_epsilon); + double ev = dmnsn_evaluate_polynomial(poly, 5, x[i]); + double evmax = dmnsn_evaluate_polynomial(poly, 5, x[i] + dmnsn_epsilon); + if (fabs(evmin) < ev || fabs(evmax) < ev) { + fprintf(stderr, "--- Root %.15g is inaccurate! ---\n", x[i]); + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/prtree.c b/libdimension/tests/prtree.c new file mode 100644 index 0000000..cb36153 --- /dev/null +++ b/libdimension/tests/prtree.c @@ -0,0 +1,98 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +/* + * Basic tests of PR-trees + */ + +#include "dimension-impl.h" +#include "../../libdimension/prtree.c" /* For DMNSN_PRTREE_B */ +#include <stdio.h> +#include <stdlib.h> + +unsigned int calls = 0; + +static bool +dmnsn_fake_intersection_fn(const dmnsn_object *object, dmnsn_line line, + dmnsn_intersection *intersection) +{ + intersection->t = (object->bounding_box.min.z - line.x0.z)/line.n.z; + intersection->normal = dmnsn_x; + ++calls; + return true; +} + +static void +dmnsn_randomize_bounding_box(dmnsn_object *object) +{ + dmnsn_vector a, b; + + a.x = 2.0*((double)rand())/RAND_MAX - 1.0; + a.y = 2.0*((double)rand())/RAND_MAX - 1.0; + a.z = 2.0*((double)rand())/RAND_MAX - 1.0; + + b.x = 2.0*((double)rand())/RAND_MAX - 1.0; + b.y = 2.0*((double)rand())/RAND_MAX - 1.0; + b.z = 2.0*((double)rand())/RAND_MAX - 1.0; + + object->bounding_box.min = dmnsn_vector_min(a, b); + object->bounding_box.max = dmnsn_vector_max(a, b); +} + +int +main(void) +{ + /* Treat warnings as errors for tests */ + dmnsn_die_on_warnings(true); + + const size_t nobjects = 128; + dmnsn_scene *scene = dmnsn_new_scene(); + + for (size_t i = 0; i < nobjects; ++i) { + dmnsn_object *object = dmnsn_new_object(); + dmnsn_randomize_bounding_box(object); + object->intersection_fn = dmnsn_fake_intersection_fn; + dmnsn_initialize_object(object); + dmnsn_array_push(scene->objects, &object); + } + + dmnsn_prtree *prtree = dmnsn_new_prtree(scene->objects); + + dmnsn_intersection intersection; + dmnsn_line ray = dmnsn_new_line( + dmnsn_new_vector(0.0, 0.0, -2.0), + dmnsn_new_vector(0.0, 0.0, 1.0) + ); + + if (!dmnsn_prtree_intersection(prtree, ray, &intersection, true)) { + fprintf(stderr, "--- Didn't find intersection! ---\n"); + return EXIT_FAILURE; + } + + if (calls > DMNSN_PRTREE_B) { + fprintf(stderr, + "--- Too many intersection function calls: %u! ---\n", + calls); + return EXIT_FAILURE; + } + + dmnsn_delete_prtree(prtree); + dmnsn_delete_scene(scene); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c new file mode 100644 index 0000000..5b03423 --- /dev/null +++ b/libdimension/tests/render.c @@ -0,0 +1,309 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#include "tests.h" +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> + +/* + * Test scene -- code version of tests/dimension/demo.pov + */ +static dmnsn_scene * +dmnsn_new_test_scene(void) +{ + /* Allocate a new scene */ + dmnsn_scene *scene = dmnsn_new_scene(); + + /* Default finish */ + scene->default_texture->finish = dmnsn_new_finish_combination( + dmnsn_new_ambient_finish( + dmnsn_color_mul(0.1, dmnsn_white) + ), + dmnsn_new_diffuse_finish(0.6) + ); + + /* Allocate a canvas */ + scene->canvas = dmnsn_new_canvas(768, 480); + + /* Set up the transformation matrix for the perspective camera */ + dmnsn_matrix trans = dmnsn_scale_matrix( + dmnsn_new_vector( + ((double)scene->canvas->width)/scene->canvas->height, 1.0, 1.0 + ) + ); + trans = dmnsn_matrix_mul( + dmnsn_rotation_matrix(dmnsn_new_vector(0.0624188099959577, 0.0, 0.0)), + trans + ); + trans = dmnsn_matrix_mul( + dmnsn_translation_matrix(dmnsn_new_vector(0.0, 0.25, -4.0)), + trans + ); + trans = dmnsn_matrix_mul( + dmnsn_rotation_matrix(dmnsn_new_vector(0.0, dmnsn_radians(53.0), 0.0)), + trans + ); + + /* Create a perspective camera */ + scene->camera = dmnsn_new_perspective_camera(); + scene->camera->trans = trans; + + /* Background color */ + scene->background = dmnsn_clear; + + /* Sky sphere */ + scene->sky_sphere = dmnsn_new_sky_sphere(); + dmnsn_pattern *sky_gradient = dmnsn_new_gradient_pattern(dmnsn_y); + dmnsn_map *sky_gradient_color_map = dmnsn_new_color_map(); + dmnsn_add_map_entry(sky_gradient_color_map, 0.0, &dmnsn_orange); + dmnsn_color background = dmnsn_new_color5(0.0, 0.1, 0.2, 0.1, 0.0); + dmnsn_add_map_entry(sky_gradient_color_map, 0.35, &background); + dmnsn_pigment *sky_pigment + = dmnsn_new_color_map_pigment(sky_gradient, sky_gradient_color_map); + dmnsn_array_push(scene->sky_sphere->pigments, &sky_pigment); + + /* Light source */ + dmnsn_light *light = dmnsn_new_point_light( + dmnsn_new_vector(-15.0, 20.0, 10.0), + dmnsn_white + ); + dmnsn_array_push(scene->lights, &light); + + /* Now make our objects */ + + dmnsn_object *cube = dmnsn_new_cube(); + cube->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(dmnsn_radians(45.0), 0.0, 0.0) + ); + cube->texture = dmnsn_new_texture(); + + dmnsn_color cube_color = dmnsn_blue; + cube_color.filter = 0.25; + cube_color.trans = 0.5; + cube->texture->pigment = dmnsn_new_solid_pigment(cube_color); + + dmnsn_color reflect = dmnsn_color_mul(0.5, dmnsn_white); + cube->texture->finish = dmnsn_new_reflective_finish(reflect, reflect, 1.0); + + cube->interior = dmnsn_new_interior(); + cube->interior->ior = 1.1; + + dmnsn_object *sphere = dmnsn_new_sphere(); + sphere->texture = dmnsn_new_texture(); + sphere->texture->pigment = dmnsn_new_solid_pigment(dmnsn_green); + sphere->texture->finish = dmnsn_new_phong_finish(0.2, 40.0); + sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); + + dmnsn_object *csg = dmnsn_new_csg_difference(cube, sphere); + dmnsn_array_push(scene->objects, &csg); + + dmnsn_array *arrow_array = dmnsn_new_array(sizeof(dmnsn_object *)); + + dmnsn_object *cylinder = dmnsn_new_cone(0.1, 0.1, false); + cylinder->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.0, 1.25, 1.0)); + dmnsn_array_push(arrow_array, &cylinder); + + dmnsn_object *cone = dmnsn_new_cone(0.1, 0.0, true); + cone->trans = + dmnsn_matrix_mul( + dmnsn_translation_matrix(dmnsn_new_vector(0.0, 1.375, 0.0)), + dmnsn_scale_matrix(dmnsn_new_vector(1.0, 0.125, 1.0)) + ); + dmnsn_array_push(arrow_array, &cone); + + dmnsn_object *arrow = dmnsn_new_csg_union(arrow_array); + arrow->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0) + ); + dmnsn_pattern *gradient = dmnsn_new_gradient_pattern(dmnsn_y); + dmnsn_map *gradient_color_map = dmnsn_new_color_map(); + dmnsn_add_map_entry(gradient_color_map, 0.0, &dmnsn_red); + dmnsn_add_map_entry(gradient_color_map, 1.0/6.0, &dmnsn_orange); + dmnsn_add_map_entry(gradient_color_map, 2.0/6.0, &dmnsn_yellow); + dmnsn_add_map_entry(gradient_color_map, 3.0/6.0, &dmnsn_green); + dmnsn_add_map_entry(gradient_color_map, 4.0/6.0, &dmnsn_blue); + dmnsn_add_map_entry(gradient_color_map, 5.0/6.0, &dmnsn_magenta); + dmnsn_add_map_entry(gradient_color_map, 1.0, &dmnsn_red); + arrow->texture = dmnsn_new_texture(); + arrow->texture->pigment + = dmnsn_new_color_map_pigment(gradient, gradient_color_map); + arrow->texture->trans = + dmnsn_matrix_mul( + dmnsn_translation_matrix(dmnsn_new_vector(0.0, -1.25, 0.0)), + dmnsn_scale_matrix(dmnsn_new_vector(1.0, 2.75, 1.0)) + ); + dmnsn_array_push(scene->objects, &arrow); + dmnsn_delete_array(arrow_array); + + dmnsn_array *torus_array = dmnsn_new_array(sizeof(dmnsn_object *)); + + dmnsn_object *torus1 = dmnsn_new_torus(0.15, 0.05); + torus1->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -1.0, 0.0)); + dmnsn_array_push(torus_array, &torus1); + + dmnsn_object *torus2 = dmnsn_new_torus(0.15, 0.05); + dmnsn_array_push(torus_array, &torus2); + + dmnsn_object *torus3 = dmnsn_new_torus(0.15, 0.05); + torus3->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, 1.0, 0.0)); + dmnsn_array_push(torus_array, &torus3); + + dmnsn_object *torii = dmnsn_new_csg_union(torus_array); + torii->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0) + ); + torii->texture = dmnsn_new_texture(); + torii->texture->pigment = dmnsn_new_solid_pigment(dmnsn_blue); + torii->texture->finish = dmnsn_new_ambient_finish(dmnsn_white); + dmnsn_array_push(scene->objects, &torii); + dmnsn_delete_array(torus_array); + + dmnsn_object *plane = dmnsn_new_plane(dmnsn_new_vector(0.0, 1.0, 0.0)); + plane->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -2.0, 0.0)); + plane->texture = dmnsn_new_texture(); + dmnsn_pattern *checker1 = dmnsn_new_checker_pattern(); + dmnsn_pattern *checker2 = dmnsn_new_checker_pattern(); + dmnsn_map *checker_color_map = dmnsn_new_color_map(); + dmnsn_add_map_entry(checker_color_map, 0.0, &dmnsn_black); + dmnsn_add_map_entry(checker_color_map, 1.0, &dmnsn_white); + dmnsn_pigment *pigment1 = dmnsn_new_solid_pigment(dmnsn_white); + dmnsn_pigment *pigment2 + = dmnsn_new_color_map_pigment(checker1, checker_color_map); + pigment2->trans + = dmnsn_scale_matrix(dmnsn_new_vector(1.0/3.0, 1.0/3.0, 1.0/3.0)); + dmnsn_map *checker_pigment_map = dmnsn_new_pigment_map(); + dmnsn_add_map_entry(checker_pigment_map, 0.0, &pigment1); + dmnsn_add_map_entry(checker_pigment_map, 1.0, &pigment2); + plane->texture->pigment + = dmnsn_new_pigment_map_pigment(checker2, checker_pigment_map); + plane->texture->pigment->quick_color = dmnsn_new_color(1.0, 0.5, 0.75); + dmnsn_array_push(scene->objects, &plane); + + return scene; +} + +int +main(void) +{ + /* Treat warnings as errors for tests */ + dmnsn_die_on_warnings(true); + + /* Create the test scene */ + dmnsn_scene *scene = dmnsn_new_test_scene(); + + /* Optimize the canvas for PNG export */ + bool have_png = true; + errno = 0; + if (dmnsn_png_optimize_canvas(scene->canvas) != 0) { + if (errno == ENOSYS) { + have_png = false; + } else { + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n"); + return EXIT_FAILURE; + } + } + + /* Optimize the canvas for GL drawing */ + bool have_gl = true; + errno = 0; + if (dmnsn_gl_optimize_canvas(scene->canvas) != 0) { + if (errno == ENOSYS) { + have_gl = false; + } else { + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n"); + return EXIT_FAILURE; + } + } + + dmnsn_clear_canvas(scene->canvas, dmnsn_black); + + /* Create a new glX display */ + dmnsn_display *display = NULL; + if (have_gl) { + display = dmnsn_new_display(scene->canvas); + if (!display) { + fprintf(stderr, "--- WARNING: Couldn't initialize X or glX! ---\n"); + } + } + + /* Render the scene */ + + printf("Rendering scene\n"); + dmnsn_progress *progress = dmnsn_raytrace_scene_async(scene); + + /* Display the scene as it's rendered */ + if (display) { + while (dmnsn_get_progress(progress) < 1.0) { + if (dmnsn_gl_write_canvas(scene->canvas) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + dmnsn_display_flush(display); + } + } + + if (dmnsn_finish_progress(progress) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Raytracing failed! ---\n"); + return EXIT_FAILURE; + } + + /* Make sure we show the completed rendering */ + if (display) { + printf("Drawing to OpenGL\n"); + if (dmnsn_gl_write_canvas(scene->canvas) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + dmnsn_display_flush(display); + } + + if (have_png) { + printf("Writing scene to PNG\n"); + FILE *ofile = fopen("render.png", "wb"); + if (!ofile) { + dmnsn_delete_display(display); + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Couldn't open 'render.png' for writing! ---\n"); + return EXIT_FAILURE; + } + + if (dmnsn_png_write_canvas(scene->canvas, ofile) != 0) { + fclose(ofile); + dmnsn_delete_display(display); + dmnsn_delete_scene(scene); + fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); + return EXIT_FAILURE; + } + + fclose(ofile); + } + + dmnsn_delete_display(display); + dmnsn_delete_scene(scene); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/tests.h b/libdimension/tests/tests.h new file mode 100644 index 0000000..d61e442 --- /dev/null +++ b/libdimension/tests/tests.h @@ -0,0 +1,46 @@ +/************************************************************************* + * Copyright (C) 2009-2010 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#ifndef TESTS_H +#define TESTS_H + +#include "dimension.h" + +#ifdef __cplusplus +/* We've been included from a C++ file; mark everything here as extern "C" */ +extern "C" { +#endif + +/* + * Windowing + */ + +typedef struct dmnsn_display dmnsn_display; + +dmnsn_display *dmnsn_new_display(const dmnsn_canvas *canvas); +void dmnsn_delete_display(dmnsn_display *display); + +/* Flush the GL buffers */ +void dmnsn_display_flush(dmnsn_display *display); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_H */ diff --git a/libdimension/tests/warning-as-error.c b/libdimension/tests/warning-as-error.c new file mode 100644 index 0000000..ce274d3 --- /dev/null +++ b/libdimension/tests/warning-as-error.c @@ -0,0 +1,31 @@ +/************************************************************************* + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@gmail.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +/* Make sure warnings kill us in strict mode - this test should fail */ + +#include "dimension.h" +#include <stdlib.h> + +int +main(void) +{ + dmnsn_die_on_warnings(true); + dmnsn_warning("This warning is expected."); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/warning.c b/libdimension/tests/warning.c new file mode 100644 index 0000000..aecedd5 --- /dev/null +++ b/libdimension/tests/warning.c @@ -0,0 +1,30 @@ +/************************************************************************* + * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com> * + * * + * This file is part of The Dimension Test Suite. * + * * + * The Dimension Test Suite is free software; you can redistribute it * + * and/or modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Test Suite is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +/* Make sure warnings don't kill us - this test should pass */ + +#include "dimension.h" +#include <stdlib.h> + +int +main(void) +{ + dmnsn_warning("This warning is expected."); + return EXIT_SUCCESS; +} |