From da47b2858f6e64ec60dce218544647df32b49077 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 19 Aug 2014 17:49:02 -0400 Subject: libdimension: Modularize tests. --- libdimension/tests/Makefile.am | 50 +-- libdimension/tests/base/dictionary.c | 132 ++++++++ libdimension/tests/base/error/custom-error-fn.c | 37 +++ libdimension/tests/base/error/error.c | 32 ++ libdimension/tests/base/error/warning-as-error.c | 33 ++ libdimension/tests/base/error/warning.c | 32 ++ libdimension/tests/base/pool.c | 87 +++++ libdimension/tests/build/c++03.cpp | 38 +++ libdimension/tests/build/c++11.cpp | 38 +++ libdimension/tests/build/c89.c | 32 ++ libdimension/tests/build/c99.c | 32 ++ libdimension/tests/build/modules/base.c | 29 ++ libdimension/tests/build/modules/canvas.c | 29 ++ libdimension/tests/build/modules/color.c | 29 ++ libdimension/tests/build/modules/concurrency.c | 29 ++ libdimension/tests/build/modules/math.c | 29 ++ libdimension/tests/build/modules/model.c | 29 ++ libdimension/tests/build/modules/pattern.c | 29 ++ libdimension/tests/build/modules/platform.c | 29 ++ libdimension/tests/build/modules/render.c | 29 ++ libdimension/tests/bvh/prtree.c | 115 +++++++ libdimension/tests/c++03.cpp | 38 --- libdimension/tests/c++11.cpp | 38 --- libdimension/tests/c89.c | 32 -- libdimension/tests/c99.c | 32 -- libdimension/tests/canvas/gl.c | 87 +++++ libdimension/tests/canvas/png.c | 111 +++++++ libdimension/tests/concurrency/future.c | 98 ++++++ libdimension/tests/custom-error-fn.c | 37 --- libdimension/tests/dictionary.c | 132 -------- libdimension/tests/error.c | 32 -- libdimension/tests/future.c | 98 ------ libdimension/tests/gl.c | 87 ----- libdimension/tests/math/polynomial.c | 222 +++++++++++++ libdimension/tests/modules/base.c | 29 -- libdimension/tests/modules/canvas.c | 29 -- libdimension/tests/modules/color.c | 29 -- libdimension/tests/modules/concurrency.c | 29 -- libdimension/tests/modules/math.c | 29 -- libdimension/tests/modules/model.c | 29 -- libdimension/tests/modules/pattern.c | 29 -- libdimension/tests/modules/platform.c | 29 -- libdimension/tests/modules/render.c | 29 -- libdimension/tests/png.c | 111 ------- libdimension/tests/polynomial.c | 222 ------------- libdimension/tests/pool.c | 87 ----- libdimension/tests/prtree.c | 115 ------- libdimension/tests/render.c | 406 ----------------------- libdimension/tests/render/render.c | 406 +++++++++++++++++++++++ libdimension/tests/warning-as-error.c | 33 -- libdimension/tests/warning.c | 32 -- 51 files changed, 1818 insertions(+), 1818 deletions(-) create mode 100644 libdimension/tests/base/dictionary.c create mode 100644 libdimension/tests/base/error/custom-error-fn.c create mode 100644 libdimension/tests/base/error/error.c create mode 100644 libdimension/tests/base/error/warning-as-error.c create mode 100644 libdimension/tests/base/error/warning.c create mode 100644 libdimension/tests/base/pool.c create mode 100644 libdimension/tests/build/c++03.cpp create mode 100644 libdimension/tests/build/c++11.cpp create mode 100644 libdimension/tests/build/c89.c create mode 100644 libdimension/tests/build/c99.c create mode 100644 libdimension/tests/build/modules/base.c create mode 100644 libdimension/tests/build/modules/canvas.c create mode 100644 libdimension/tests/build/modules/color.c create mode 100644 libdimension/tests/build/modules/concurrency.c create mode 100644 libdimension/tests/build/modules/math.c create mode 100644 libdimension/tests/build/modules/model.c create mode 100644 libdimension/tests/build/modules/pattern.c create mode 100644 libdimension/tests/build/modules/platform.c create mode 100644 libdimension/tests/build/modules/render.c create mode 100644 libdimension/tests/bvh/prtree.c delete mode 100644 libdimension/tests/c++03.cpp delete mode 100644 libdimension/tests/c++11.cpp delete mode 100644 libdimension/tests/c89.c delete mode 100644 libdimension/tests/c99.c create mode 100644 libdimension/tests/canvas/gl.c create mode 100644 libdimension/tests/canvas/png.c create mode 100644 libdimension/tests/concurrency/future.c delete mode 100644 libdimension/tests/custom-error-fn.c delete mode 100644 libdimension/tests/dictionary.c delete mode 100644 libdimension/tests/error.c delete mode 100644 libdimension/tests/future.c delete mode 100644 libdimension/tests/gl.c create mode 100644 libdimension/tests/math/polynomial.c delete mode 100644 libdimension/tests/modules/base.c delete mode 100644 libdimension/tests/modules/canvas.c delete mode 100644 libdimension/tests/modules/color.c delete mode 100644 libdimension/tests/modules/concurrency.c delete mode 100644 libdimension/tests/modules/math.c delete mode 100644 libdimension/tests/modules/model.c delete mode 100644 libdimension/tests/modules/pattern.c delete mode 100644 libdimension/tests/modules/platform.c delete mode 100644 libdimension/tests/modules/render.c delete mode 100644 libdimension/tests/png.c delete mode 100644 libdimension/tests/polynomial.c delete mode 100644 libdimension/tests/pool.c delete mode 100644 libdimension/tests/prtree.c delete mode 100644 libdimension/tests/render.c create mode 100644 libdimension/tests/render/render.c delete mode 100644 libdimension/tests/warning-as-error.c delete mode 100644 libdimension/tests/warning.c (limited to 'libdimension') diff --git a/libdimension/tests/Makefile.am b/libdimension/tests/Makefile.am index f9ff11f..3c10a54 100644 --- a/libdimension/tests/Makefile.am +++ b/libdimension/tests/Makefile.am @@ -67,71 +67,71 @@ libdimension_unit_test_la_SOURCES = unit-test.c libdimension_unit_test_la_CFLAGS = $(CHECK_CFLAGS) $(AM_CFLAGS) libdimension_unit_test_la_LIBADD = $(CHECK_LIBS) $(top_builddir)/libdimension/libdimension.la -modules_test_SOURCES = modules/base.c \ - modules/canvas.c \ - modules/color.c \ - modules/concurrency.c \ - modules/math.c \ - modules/model.c \ - modules/pattern.c \ - modules/platform.c \ - modules/render.c +modules_test_SOURCES = build/modules/base.c \ + build/modules/canvas.c \ + build/modules/color.c \ + build/modules/concurrency.c \ + build/modules/math.c \ + build/modules/model.c \ + build/modules/pattern.c \ + build/modules/platform.c \ + build/modules/render.c modules_test_LDADD = libdimension-unit-test.la -c89_test_SOURCES = c89.c +c89_test_SOURCES = build/c89.c c89_test_LDADD = libdimension-tests.la c89.o: CFLAGS += -std=c89 -Wpedantic -c99_test_SOURCES = c99.c +c99_test_SOURCES = build/c99.c c99_test_LDADD = libdimension-tests.la c99.o: CFLAGS += -std=c99 -Wpedantic -c__03_test_SOURCES = c++03.cpp +c__03_test_SOURCES = build/c++03.cpp c__03_test_LDADD = libdimension-tests.la c++03.o: CFLAGS += -std=c++03 -Wpedantic -c__11_test_SOURCES = c++11.cpp +c__11_test_SOURCES = build/c++11.cpp c__11_test_LDADD = libdimension-tests.la c++11.o: CFLAGS += -std=c++11 -Wpedantic -warning_test_SOURCES = warning.c +warning_test_SOURCES = base/error/warning.c warning_test_LDADD = libdimension-tests.la -warning_as_error_test_SOURCES = warning-as-error.c +warning_as_error_test_SOURCES = base/error/warning-as-error.c warning_as_error_test_LDADD = libdimension-tests.la -error_test_SOURCES = error.c +error_test_SOURCES = base/error/error.c error_test_LDADD = libdimension-tests.la -custom_error_fn_test_SOURCES = custom-error-fn.c +custom_error_fn_test_SOURCES = base/error/custom-error-fn.c custom_error_fn_test_LDADD = libdimension-tests.la -pool_test_SOURCES = pool.c +pool_test_SOURCES = base/pool.c pool_test_LDADD = libdimension-unit-test.la -dictionary_test_SOURCES = dictionary.c +dictionary_test_SOURCES = base/dictionary.c dictionary_test_LDADD = libdimension-unit-test.la -polynomial_test_SOURCES = polynomial.c +polynomial_test_SOURCES = math/polynomial.c polynomial_test_LDADD = libdimension-unit-test.la -prtree_test_SOURCES = prtree.c +prtree_test_SOURCES = bvh/prtree.c prtree_test_LDADD = libdimension-tests.la -future_test_SOURCES = future.c +future_test_SOURCES = concurrency/future.c future_test_LDADD = libdimension-unit-test.la -png_test_SOURCES = png.c +png_test_SOURCES = canvas/png.c png_test_LDADD = libdimension-tests.la -gl_test_SOURCES = gl.c +gl_test_SOURCES = canvas/gl.c gl_test_LDADD = libdimension-tests.la -render_test_SOURCES = render.c +render_test_SOURCES = render/render.c render_test_LDADD = libdimension-tests.la clean-local: diff --git a/libdimension/tests/base/dictionary.c b/libdimension/tests/base/dictionary.c new file mode 100644 index 0000000..117887a --- /dev/null +++ b/libdimension/tests/base/dictionary.c @@ -0,0 +1,132 @@ +/************************************************************************* + * Copyright (C) 2012 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Tests for dmnsn_dictionary, a string->object map. + */ + +#include "tests.h" + +static dmnsn_dictionary *dict; + +/* + * Dictionary creation/deletion. + */ + +DMNSN_TEST(new_delete, delete_null) +{ + dmnsn_delete_dictionary(NULL); +} + +/* + * Dictionary operations. + */ + +DMNSN_TEST_SETUP(dictionary) +{ + dict = dmnsn_new_dictionary(sizeof(int)); +} + +DMNSN_TEST_TEARDOWN(dictionary) +{ + dmnsn_delete_dictionary(dict); +} + +DMNSN_TEST(dictionary, insert_and_get) +{ + const int value_in = 123; + dmnsn_dictionary_insert(dict, "key", &value_in); + + int value_out; + ck_assert(dmnsn_dictionary_get(dict, "key", &value_out)); + ck_assert_int_eq(value_out, value_in); + + int *value_at = dmnsn_dictionary_at(dict, "key"); + ck_assert_int_eq(*value_at, value_in); +} + +DMNSN_TEST(dictionary, insert_multiple) +{ + const int value1 = 123, value2 = 456, value3 = 789; + int value_out; + + dmnsn_dictionary_insert(dict, "key1", &value1); + dmnsn_dictionary_insert(dict, "key2", &value2); + dmnsn_dictionary_insert(dict, "asdf", &value3); + + ck_assert(dmnsn_dictionary_get(dict, "key1", &value_out)); + ck_assert_int_eq(value_out, value1); + + ck_assert(dmnsn_dictionary_get(dict, "key2", &value_out)); + ck_assert_int_eq(value_out, value2); + + ck_assert(dmnsn_dictionary_get(dict, "asdf", &value_out)); + ck_assert_int_eq(value_out, value3); +} + +DMNSN_TEST(dictionary, insert_overwrites) +{ + const int value1 = 123, value2 = 456; + int value_out; + + // Insert and read back value1 + dmnsn_dictionary_insert(dict, "key", &value1); + ck_assert(dmnsn_dictionary_get(dict, "key", &value_out)); + ck_assert_int_eq(value_out, value1); + + // Insert and read back value2 + dmnsn_dictionary_insert(dict, "key", &value2); + ck_assert(dmnsn_dictionary_get(dict, "key", &value_out)); + ck_assert_int_eq(value_out, value2); +} + +DMNSN_TEST(dictionary, remove) +{ + const int value_in = 123; + dmnsn_dictionary_insert(dict, "key", &value_in); + ck_assert(dmnsn_dictionary_remove(dict, "key")); + ck_assert(!dmnsn_dictionary_remove(dict, "key")); + + int value_out; + ck_assert(!dmnsn_dictionary_get(dict, "key", &value_out)); + + int *value_at = dmnsn_dictionary_at(dict, "key"); + ck_assert(value_at == NULL); +} + +static int sum = 0; + +static void +dmnsn_dictionary_test_apply_callback(void *ptr) +{ + sum += *(int *)ptr; +} + +DMNSN_TEST(dictionary, apply) +{ + const int value1 = 123, value2 = 456, value3 = 789; + + dmnsn_dictionary_insert(dict, "key1", &value1); + dmnsn_dictionary_insert(dict, "key2", &value2); + dmnsn_dictionary_insert(dict, "asdf", &value3); + + dmnsn_dictionary_apply(dict, dmnsn_dictionary_test_apply_callback); + ck_assert_int_eq(sum, value1 + value2 + value3); +} diff --git a/libdimension/tests/base/error/custom-error-fn.c b/libdimension/tests/base/error/custom-error-fn.c new file mode 100644 index 0000000..49a0f9e --- /dev/null +++ b/libdimension/tests/base/error/custom-error-fn.c @@ -0,0 +1,37 @@ +/************************************************************************* + * Copyright (C) 2011 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +// Make sure that custom fatal error handlers work. + +#include "dimension.h" +#include + +void +dmnsn_custom_fatal_error_fn(void) +{ + exit(EXIT_SUCCESS); +} + +int +main(void) +{ + dmnsn_set_fatal_error_fn(dmnsn_custom_fatal_error_fn); + dmnsn_error("This error is expected."); + return EXIT_FAILURE; +} diff --git a/libdimension/tests/base/error/error.c b/libdimension/tests/base/error/error.c new file mode 100644 index 0000000..58a5b43 --- /dev/null +++ b/libdimension/tests/base/error/error.c @@ -0,0 +1,32 @@ +/************************************************************************* + * Copyright (C) 2009-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +// Make sure that errors terminate us - this test should fail + +#include "dimension.h" +#include +#include + +int +main(void) +{ + errno = ENOMEM; + dmnsn_error("This error is expected."); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/base/error/warning-as-error.c b/libdimension/tests/base/error/warning-as-error.c new file mode 100644 index 0000000..7d0fdb7 --- /dev/null +++ b/libdimension/tests/base/error/warning-as-error.c @@ -0,0 +1,33 @@ +/************************************************************************* + * Copyright (C) 2009-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +// Make sure warnings kill us in strict mode - this test should fail + +#include "dimension.h" +#include +#include + +int +main(void) +{ + dmnsn_die_on_warnings(true); + errno = ENOMEM; + dmnsn_warning("This warning is expected."); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/base/error/warning.c b/libdimension/tests/base/error/warning.c new file mode 100644 index 0000000..cc1aed1 --- /dev/null +++ b/libdimension/tests/base/error/warning.c @@ -0,0 +1,32 @@ +/************************************************************************* + * Copyright (C) 2009-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +// Make sure warnings don't kill us - this test should pass + +#include "dimension.h" +#include +#include + +int +main(void) +{ + errno = ENOMEM; + dmnsn_warning("This warning is expected."); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/base/pool.c b/libdimension/tests/base/pool.c new file mode 100644 index 0000000..f8b1259 --- /dev/null +++ b/libdimension/tests/base/pool.c @@ -0,0 +1,87 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Tests for memory pools. + */ + +#include "../../concurrency/future.c" +#include "../../concurrency/threads.c" +#include "../../internal.h" +#include "tests.h" + +static dmnsn_pool *pool; + +DMNSN_TEST_SETUP(pool) +{ + pool = dmnsn_new_pool(); +} + +DMNSN_TEST_TEARDOWN(pool) +{ + dmnsn_delete_pool(pool); +} + +DMNSN_TEST(pool, simple) +{ + for (int i = 0; i < 10000; ++i) { + int *p = DMNSN_PALLOC(pool, int); + *p = i; + } + + // Leak checking will tell us if something bad happened +} + +static int counter = 0; + +static void +callback(void *ptr) +{ + ++counter; +} + +DMNSN_TEST(pool, callback) +{ + DMNSN_PALLOC(pool, int); + DMNSN_PALLOC_TIDY(pool, int, callback); + DMNSN_PALLOC_TIDY(pool, int, callback); + DMNSN_PALLOC(pool, int); + + dmnsn_delete_pool(pool); + pool = NULL; + + ck_assert_int_eq(counter, 2); +} + +static int +alloc_thread(void *ptr, unsigned int thread, unsigned int nthreads) +{ + for (unsigned int i = thread; i < 10000; i += nthreads) { + int *p = DMNSN_PALLOC(pool, int); + *p = i; + } + return 0; +} + +DMNSN_TEST(pool, threaded) +{ + int ret = dmnsn_execute_concurrently(NULL, alloc_thread, NULL, 64); + ck_assert_int_eq(ret, 0); +} diff --git a/libdimension/tests/build/c++03.cpp b/libdimension/tests/build/c++03.cpp new file mode 100644 index 0000000..679fa23 --- /dev/null +++ b/libdimension/tests/build/c++03.cpp @@ -0,0 +1,38 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Ensure that dimension.h can be included in a C++ program and behave properly. + */ + +#include "tests.h" +#include + +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/build/c++11.cpp b/libdimension/tests/build/c++11.cpp new file mode 100644 index 0000000..679fa23 --- /dev/null +++ b/libdimension/tests/build/c++11.cpp @@ -0,0 +1,38 @@ +/************************************************************************* + * Copyright (C) 2010-2011 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Ensure that dimension.h can be included in a C++ program and behave properly. + */ + +#include "tests.h" +#include + +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/build/c89.c b/libdimension/tests/build/c89.c new file mode 100644 index 0000000..9393184 --- /dev/null +++ b/libdimension/tests/build/c89.c @@ -0,0 +1,32 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Check C89 compliance of headers. + */ + +#include "dimension.h" +#include + +int +main(void) +{ + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/build/c99.c b/libdimension/tests/build/c99.c new file mode 100644 index 0000000..e1a04ef --- /dev/null +++ b/libdimension/tests/build/c99.c @@ -0,0 +1,32 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Check strict C99 compliance of headers. + */ + +#include "dimension.h" +#include + +int +main(void) +{ + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/build/modules/base.c b/libdimension/tests/build/modules/base.c new file mode 100644 index 0000000..665c62f --- /dev/null +++ b/libdimension/tests/build/modules/base.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the base module in isolation. + */ + +#include "dimension/base.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/canvas.c b/libdimension/tests/build/modules/canvas.c new file mode 100644 index 0000000..e9e0471 --- /dev/null +++ b/libdimension/tests/build/modules/canvas.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the canvas module in isolation. + */ + +#include "dimension/canvas.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/color.c b/libdimension/tests/build/modules/color.c new file mode 100644 index 0000000..6c9e63f --- /dev/null +++ b/libdimension/tests/build/modules/color.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the color module in isolation. + */ + +#include "dimension/color.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/concurrency.c b/libdimension/tests/build/modules/concurrency.c new file mode 100644 index 0000000..16bb305 --- /dev/null +++ b/libdimension/tests/build/modules/concurrency.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the concurrency module in isolation. + */ + +#include "dimension/concurrency.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/math.c b/libdimension/tests/build/modules/math.c new file mode 100644 index 0000000..5bf3ac1 --- /dev/null +++ b/libdimension/tests/build/modules/math.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the math module in isolation. + */ + +#include "dimension/math.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/model.c b/libdimension/tests/build/modules/model.c new file mode 100644 index 0000000..9bd5f00 --- /dev/null +++ b/libdimension/tests/build/modules/model.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the model module in isolation. + */ + +#include "dimension/model.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/pattern.c b/libdimension/tests/build/modules/pattern.c new file mode 100644 index 0000000..46bb99e --- /dev/null +++ b/libdimension/tests/build/modules/pattern.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the pattern module in isolation. + */ + +#include "dimension/pattern.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/platform.c b/libdimension/tests/build/modules/platform.c new file mode 100644 index 0000000..b0fc978 --- /dev/null +++ b/libdimension/tests/build/modules/platform.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the platform module in isolation. + */ + +#include "dimension/platform.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/build/modules/render.c b/libdimension/tests/build/modules/render.c new file mode 100644 index 0000000..a508c9f --- /dev/null +++ b/libdimension/tests/build/modules/render.c @@ -0,0 +1,29 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Test the render module in isolation. + */ + +#include "dimension/render.h" + +#ifndef DMNSN_INLINE +#error "Missing #include " +#endif diff --git a/libdimension/tests/bvh/prtree.c b/libdimension/tests/bvh/prtree.c new file mode 100644 index 0000000..fb0cea6 --- /dev/null +++ b/libdimension/tests/bvh/prtree.c @@ -0,0 +1,115 @@ +/************************************************************************* + * Copyright (C) 2010-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Basic tests of PR-trees. + */ + +#include "../../platform/platform.c" +#include "../../concurrency/threads.c" +#include "../../concurrency/future.c" +#include "../../bvh/bvh.c" +#include "../../bvh/prtree.c" +#include +#include + +unsigned int calls = 0; + +static bool +dmnsn_fake_intersection_fn(const dmnsn_object *object, dmnsn_ray ray, + dmnsn_intersection *intersection) +{ + intersection->t = (object->aabb.min.z - ray.x0.z)/ray.n.z; + intersection->normal = dmnsn_x; + ++calls; + return true; +} + +static void +dmnsn_randomize_aabb(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->aabb.min = dmnsn_vector_min(a, b); + object->aabb.max = dmnsn_vector_max(a, b); +} + +static const dmnsn_object_vtable dmnsn_fake_vtable = { + .intersection_fn = dmnsn_fake_intersection_fn, +}; + +static dmnsn_object * +dmnsn_new_fake_object(dmnsn_pool *pool) +{ + dmnsn_object *object = dmnsn_new_object(pool); + dmnsn_randomize_aabb(object); + object->vtable = &dmnsn_fake_vtable; + object->trans_inv = dmnsn_identity_matrix(); + return object; +} + +int +main(void) +{ + // Treat warnings as errors for tests + dmnsn_die_on_warnings(true); + + dmnsn_pool *pool = dmnsn_new_pool(); + + const size_t nobjects = 128; + dmnsn_array *objects = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); + + for (size_t i = 0; i < nobjects; ++i) { + dmnsn_object *object = dmnsn_new_fake_object(pool); + dmnsn_array_push(objects, &object); + } + + dmnsn_bvh *bvh = dmnsn_new_bvh(objects, DMNSN_BVH_PRTREE); + + dmnsn_intersection intersection; + dmnsn_ray ray = dmnsn_new_ray( + dmnsn_new_vector(0.0, 0.0, -2.0), + dmnsn_new_vector(0.0, 0.0, 1.0) + ); + + if (!dmnsn_bvh_intersection(bvh, 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_bvh(bvh); + dmnsn_delete_pool(pool); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/c++03.cpp b/libdimension/tests/c++03.cpp deleted file mode 100644 index 679fa23..0000000 --- a/libdimension/tests/c++03.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/************************************************************************* - * Copyright (C) 2010-2011 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Ensure that dimension.h can be included in a C++ program and behave properly. - */ - -#include "tests.h" -#include - -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/c++11.cpp b/libdimension/tests/c++11.cpp deleted file mode 100644 index 679fa23..0000000 --- a/libdimension/tests/c++11.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/************************************************************************* - * Copyright (C) 2010-2011 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Ensure that dimension.h can be included in a C++ program and behave properly. - */ - -#include "tests.h" -#include - -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/c89.c b/libdimension/tests/c89.c deleted file mode 100644 index 9393184..0000000 --- a/libdimension/tests/c89.c +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Check C89 compliance of headers. - */ - -#include "dimension.h" -#include - -int -main(void) -{ - return EXIT_SUCCESS; -} diff --git a/libdimension/tests/c99.c b/libdimension/tests/c99.c deleted file mode 100644 index e1a04ef..0000000 --- a/libdimension/tests/c99.c +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Check strict C99 compliance of headers. - */ - -#include "dimension.h" -#include - -int -main(void) -{ - return EXIT_SUCCESS; -} diff --git a/libdimension/tests/canvas/gl.c b/libdimension/tests/canvas/gl.c new file mode 100644 index 0000000..1546d6c --- /dev/null +++ b/libdimension/tests/canvas/gl.c @@ -0,0 +1,87 @@ +/************************************************************************* + * Copyright (C) 2009-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +#include "tests.h" +#include + +int +main(void) +{ + // Treat warnings as errors for tests + dmnsn_die_on_warnings(true); + + // Allocate our canvas + dmnsn_pool *pool = dmnsn_new_pool(); + dmnsn_canvas *canvas = dmnsn_new_canvas(pool, 768, 480); + + // Paint the test pattern + dmnsn_paint_test_canvas(canvas); + + // Create a new glX display + dmnsn_display *display = dmnsn_new_display(canvas); + if (!display) { + fprintf(stderr, "--- WARNING: Couldn't initialize X or glX! ---\n"); + dmnsn_delete_pool(pool); + return EXIT_SUCCESS; + } + + // Draw to OpenGL + printf("Drawing to OpenGL\n"); + if (dmnsn_gl_write_canvas(canvas) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_pool(pool); + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + dmnsn_display_flush(display); + + /* + * Now test GL import/export + */ + + // Optimize the canvas for GL drawing + if (dmnsn_gl_optimize_canvas(pool, canvas) != 0) { + dmnsn_delete_pool(pool); + fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n"); + return EXIT_FAILURE; + } + + // Read the image back from OpenGL + printf("Reading from OpenGL\n"); + if (dmnsn_gl_read_canvas(canvas, 0, 0) != 0) { + dmnsn_delete_display(display); + dmnsn_delete_pool(pool); + 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_pool(pool); + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + return EXIT_FAILURE; + } + + dmnsn_display_flush(display); + dmnsn_delete_display(display); + dmnsn_delete_pool(pool); + return EXIT_SUCCESS; +} diff --git a/libdimension/tests/canvas/png.c b/libdimension/tests/canvas/png.c new file mode 100644 index 0000000..7311e43 --- /dev/null +++ b/libdimension/tests/canvas/png.c @@ -0,0 +1,111 @@ +/************************************************************************* + * Copyright (C) 2009-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +#include "tests.h" +#include +#include + +int +main(void) +{ + int ret = EXIT_SUCCESS; + + // Treat warnings as errors for tests + dmnsn_die_on_warnings(true); + + // Allocate our canvas + dmnsn_pool *pool = dmnsn_new_pool(); + dmnsn_canvas *canvas = dmnsn_new_canvas(pool, 768, 480); + + // Optimize the canvas for PNG export + if (dmnsn_png_optimize_canvas(pool, canvas) != 0) { + fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n"); + ret = EXIT_FAILURE; + goto exit; + } + + // Paint the test pattern + dmnsn_paint_test_canvas(canvas); + + // Write the image to PNG + + printf("Writing scene to PNG\n"); + FILE *ofile = fopen("png1.png", "wb"); + if (!ofile) { + fprintf(stderr, "--- Couldn't open 'png1.png' for writing! ---\n"); + ret = EXIT_FAILURE; + goto exit; + } + + if (dmnsn_png_write_canvas(canvas, ofile) != 0) { + fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); + fclose(ofile); + ret = EXIT_FAILURE; + goto exit; + } + + fclose(ofile); + + /* + * 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"); + ret = EXIT_FAILURE; + goto exit; + } + + canvas = dmnsn_png_read_canvas(pool, ifile); + if (!canvas) { + fprintf(stderr, "--- Reading canvas from PNG failed! ---\n"); + fclose(ifile); + ret = EXIT_FAILURE; + goto exit; + } + + 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"); + ret = EXIT_FAILURE; + goto exit; + } + + if (dmnsn_png_write_canvas(canvas, ofile) != 0) { + fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); + fclose(ofile); + ret = EXIT_FAILURE; + goto exit; + } + + fclose(ofile); + + exit: + dmnsn_delete_pool(pool); + return ret; +} diff --git a/libdimension/tests/concurrency/future.c b/libdimension/tests/concurrency/future.c new file mode 100644 index 0000000..b226e4f --- /dev/null +++ b/libdimension/tests/concurrency/future.c @@ -0,0 +1,98 @@ +/************************************************************************* + * Copyright (C) 2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Tests for dmnsn_future. + */ + +#include "../../platform/platform.c" +#include "../../concurrency/future.c" +#include "../../concurrency/threads.c" +#include "tests.h" +#include + +static dmnsn_future *future; +static atomic_int counter = ATOMIC_VAR_INIT(0); +static const int NTHREADS = 8; +static const int CHUNK = 100; + +static int +dmnsn_future_test_ccthread(void *ptr, unsigned int thread, unsigned int nthreads) +{ + for (int i = 0; i < CHUNK; ++i) { + for (int j = 0; j < CHUNK; ++j) { + atomic_fetch_add(&counter, 1); + } + dmnsn_future_increment(future); + } + return 0; +} + +static int +dmnsn_future_test_thread(void *ptr) +{ + dmnsn_future_set_total(future, NTHREADS*CHUNK); + return dmnsn_execute_concurrently(future, dmnsn_future_test_ccthread, ptr, NTHREADS); +} + +DMNSN_TEST_SETUP(future) +{ + future = dmnsn_new_future(); + dmnsn_new_thread(future, dmnsn_future_test_thread, NULL); +} + +DMNSN_TEST_TEARDOWN(future) +{ + int res = dmnsn_future_join(future); + ck_assert_int_eq(res, 0); +} + +void * +dmnsn_future_pause_and_check(void *ptr) +{ + for (int i = 0; i < CHUNK; ++i) { + dmnsn_future_pause(future); + + int value = atomic_load(&counter); + ck_assert_int_eq(value%CHUNK, 0); + + for (int j = 0; j < CHUNK; ++j) { + int new_value = atomic_load(&counter); + ck_assert_int_eq(new_value, value); + } + + dmnsn_future_resume(future); + } + + return NULL; +} + +DMNSN_TEST(future, pause_safely) +{ + // Check dmnsn_future_pause() behaviour from two concurrent threads + pthread_t thread; + if (pthread_create(&thread, NULL, dmnsn_future_pause_and_check, NULL) != 0) { + dmnsn_error("Couldn't start thread."); + } + + dmnsn_future_pause_and_check(NULL); + + dmnsn_join_thread(thread, NULL); +} diff --git a/libdimension/tests/custom-error-fn.c b/libdimension/tests/custom-error-fn.c deleted file mode 100644 index 49a0f9e..0000000 --- a/libdimension/tests/custom-error-fn.c +++ /dev/null @@ -1,37 +0,0 @@ -/************************************************************************* - * Copyright (C) 2011 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -// Make sure that custom fatal error handlers work. - -#include "dimension.h" -#include - -void -dmnsn_custom_fatal_error_fn(void) -{ - exit(EXIT_SUCCESS); -} - -int -main(void) -{ - dmnsn_set_fatal_error_fn(dmnsn_custom_fatal_error_fn); - dmnsn_error("This error is expected."); - return EXIT_FAILURE; -} diff --git a/libdimension/tests/dictionary.c b/libdimension/tests/dictionary.c deleted file mode 100644 index 117887a..0000000 --- a/libdimension/tests/dictionary.c +++ /dev/null @@ -1,132 +0,0 @@ -/************************************************************************* - * Copyright (C) 2012 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Tests for dmnsn_dictionary, a string->object map. - */ - -#include "tests.h" - -static dmnsn_dictionary *dict; - -/* - * Dictionary creation/deletion. - */ - -DMNSN_TEST(new_delete, delete_null) -{ - dmnsn_delete_dictionary(NULL); -} - -/* - * Dictionary operations. - */ - -DMNSN_TEST_SETUP(dictionary) -{ - dict = dmnsn_new_dictionary(sizeof(int)); -} - -DMNSN_TEST_TEARDOWN(dictionary) -{ - dmnsn_delete_dictionary(dict); -} - -DMNSN_TEST(dictionary, insert_and_get) -{ - const int value_in = 123; - dmnsn_dictionary_insert(dict, "key", &value_in); - - int value_out; - ck_assert(dmnsn_dictionary_get(dict, "key", &value_out)); - ck_assert_int_eq(value_out, value_in); - - int *value_at = dmnsn_dictionary_at(dict, "key"); - ck_assert_int_eq(*value_at, value_in); -} - -DMNSN_TEST(dictionary, insert_multiple) -{ - const int value1 = 123, value2 = 456, value3 = 789; - int value_out; - - dmnsn_dictionary_insert(dict, "key1", &value1); - dmnsn_dictionary_insert(dict, "key2", &value2); - dmnsn_dictionary_insert(dict, "asdf", &value3); - - ck_assert(dmnsn_dictionary_get(dict, "key1", &value_out)); - ck_assert_int_eq(value_out, value1); - - ck_assert(dmnsn_dictionary_get(dict, "key2", &value_out)); - ck_assert_int_eq(value_out, value2); - - ck_assert(dmnsn_dictionary_get(dict, "asdf", &value_out)); - ck_assert_int_eq(value_out, value3); -} - -DMNSN_TEST(dictionary, insert_overwrites) -{ - const int value1 = 123, value2 = 456; - int value_out; - - // Insert and read back value1 - dmnsn_dictionary_insert(dict, "key", &value1); - ck_assert(dmnsn_dictionary_get(dict, "key", &value_out)); - ck_assert_int_eq(value_out, value1); - - // Insert and read back value2 - dmnsn_dictionary_insert(dict, "key", &value2); - ck_assert(dmnsn_dictionary_get(dict, "key", &value_out)); - ck_assert_int_eq(value_out, value2); -} - -DMNSN_TEST(dictionary, remove) -{ - const int value_in = 123; - dmnsn_dictionary_insert(dict, "key", &value_in); - ck_assert(dmnsn_dictionary_remove(dict, "key")); - ck_assert(!dmnsn_dictionary_remove(dict, "key")); - - int value_out; - ck_assert(!dmnsn_dictionary_get(dict, "key", &value_out)); - - int *value_at = dmnsn_dictionary_at(dict, "key"); - ck_assert(value_at == NULL); -} - -static int sum = 0; - -static void -dmnsn_dictionary_test_apply_callback(void *ptr) -{ - sum += *(int *)ptr; -} - -DMNSN_TEST(dictionary, apply) -{ - const int value1 = 123, value2 = 456, value3 = 789; - - dmnsn_dictionary_insert(dict, "key1", &value1); - dmnsn_dictionary_insert(dict, "key2", &value2); - dmnsn_dictionary_insert(dict, "asdf", &value3); - - dmnsn_dictionary_apply(dict, dmnsn_dictionary_test_apply_callback); - ck_assert_int_eq(sum, value1 + value2 + value3); -} diff --git a/libdimension/tests/error.c b/libdimension/tests/error.c deleted file mode 100644 index 58a5b43..0000000 --- a/libdimension/tests/error.c +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************************************* - * Copyright (C) 2009-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -// Make sure that errors terminate us - this test should fail - -#include "dimension.h" -#include -#include - -int -main(void) -{ - errno = ENOMEM; - dmnsn_error("This error is expected."); - return EXIT_SUCCESS; -} diff --git a/libdimension/tests/future.c b/libdimension/tests/future.c deleted file mode 100644 index a223a44..0000000 --- a/libdimension/tests/future.c +++ /dev/null @@ -1,98 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Tests for dmnsn_future. - */ - -#include "../platform/platform.c" -#include "../concurrency/future.c" -#include "../concurrency/threads.c" -#include "tests.h" -#include - -static dmnsn_future *future; -static atomic_int counter = ATOMIC_VAR_INIT(0); -static const int NTHREADS = 8; -static const int CHUNK = 100; - -static int -dmnsn_future_test_ccthread(void *ptr, unsigned int thread, unsigned int nthreads) -{ - for (int i = 0; i < CHUNK; ++i) { - for (int j = 0; j < CHUNK; ++j) { - atomic_fetch_add(&counter, 1); - } - dmnsn_future_increment(future); - } - return 0; -} - -static int -dmnsn_future_test_thread(void *ptr) -{ - dmnsn_future_set_total(future, NTHREADS*CHUNK); - return dmnsn_execute_concurrently(future, dmnsn_future_test_ccthread, ptr, NTHREADS); -} - -DMNSN_TEST_SETUP(future) -{ - future = dmnsn_new_future(); - dmnsn_new_thread(future, dmnsn_future_test_thread, NULL); -} - -DMNSN_TEST_TEARDOWN(future) -{ - int res = dmnsn_future_join(future); - ck_assert_int_eq(res, 0); -} - -void * -dmnsn_future_pause_and_check(void *ptr) -{ - for (int i = 0; i < CHUNK; ++i) { - dmnsn_future_pause(future); - - int value = atomic_load(&counter); - ck_assert_int_eq(value%CHUNK, 0); - - for (int j = 0; j < CHUNK; ++j) { - int new_value = atomic_load(&counter); - ck_assert_int_eq(new_value, value); - } - - dmnsn_future_resume(future); - } - - return NULL; -} - -DMNSN_TEST(future, pause_safely) -{ - // Check dmnsn_future_pause() behaviour from two concurrent threads - pthread_t thread; - if (pthread_create(&thread, NULL, dmnsn_future_pause_and_check, NULL) != 0) { - dmnsn_error("Couldn't start thread."); - } - - dmnsn_future_pause_and_check(NULL); - - dmnsn_join_thread(thread, NULL); -} diff --git a/libdimension/tests/gl.c b/libdimension/tests/gl.c deleted file mode 100644 index 1546d6c..0000000 --- a/libdimension/tests/gl.c +++ /dev/null @@ -1,87 +0,0 @@ -/************************************************************************* - * Copyright (C) 2009-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -#include "tests.h" -#include - -int -main(void) -{ - // Treat warnings as errors for tests - dmnsn_die_on_warnings(true); - - // Allocate our canvas - dmnsn_pool *pool = dmnsn_new_pool(); - dmnsn_canvas *canvas = dmnsn_new_canvas(pool, 768, 480); - - // Paint the test pattern - dmnsn_paint_test_canvas(canvas); - - // Create a new glX display - dmnsn_display *display = dmnsn_new_display(canvas); - if (!display) { - fprintf(stderr, "--- WARNING: Couldn't initialize X or glX! ---\n"); - dmnsn_delete_pool(pool); - return EXIT_SUCCESS; - } - - // Draw to OpenGL - printf("Drawing to OpenGL\n"); - if (dmnsn_gl_write_canvas(canvas) != 0) { - dmnsn_delete_display(display); - dmnsn_delete_pool(pool); - fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); - return EXIT_FAILURE; - } - dmnsn_display_flush(display); - - /* - * Now test GL import/export - */ - - // Optimize the canvas for GL drawing - if (dmnsn_gl_optimize_canvas(pool, canvas) != 0) { - dmnsn_delete_pool(pool); - fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n"); - return EXIT_FAILURE; - } - - // Read the image back from OpenGL - printf("Reading from OpenGL\n"); - if (dmnsn_gl_read_canvas(canvas, 0, 0) != 0) { - dmnsn_delete_display(display); - dmnsn_delete_pool(pool); - 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_pool(pool); - fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); - return EXIT_FAILURE; - } - - dmnsn_display_flush(display); - dmnsn_delete_display(display); - dmnsn_delete_pool(pool); - return EXIT_SUCCESS; -} diff --git a/libdimension/tests/math/polynomial.c b/libdimension/tests/math/polynomial.c new file mode 100644 index 0000000..eaa15cf --- /dev/null +++ b/libdimension/tests/math/polynomial.c @@ -0,0 +1,222 @@ +/************************************************************************* + * Copyright (C) 2010-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +/** + * @file + * Basic tests of the polynomial root-finder. + */ + +#include "../../math/polynomial.c" +#include "tests.h" +#include + +#define DMNSN_CLOSE_ENOUGH 1.0e-6 + +static void +dmnsn_assert_roots(const double poly[], size_t degree, size_t nroots_ex, ...) +{ + double roots[degree]; + size_t nroots = dmnsn_polynomial_solve(poly, degree, roots); + ck_assert_int_eq(nroots, nroots_ex); + + va_list ap; + va_start(ap, nroots_ex); + for (size_t i = 0; i < nroots; ++i) { + double root_ex = va_arg(ap, double); + bool found = false; + for (size_t j = 0; j < nroots; ++j) { + double root = roots[j]; + if (fabs(root_ex - root) >= dmnsn_epsilon) { + continue; + } + + double evroot = dmnsn_polynomial_evaluate(poly, degree, root); + ck_assert(fabs(evroot) < DMNSN_CLOSE_ENOUGH); + + double evmin = dmnsn_polynomial_evaluate(poly, degree, root - dmnsn_epsilon); + double evmax = dmnsn_polynomial_evaluate(poly, degree, root + dmnsn_epsilon); + ck_assert(fabs(evroot) <= fabs(evmin) && fabs(evroot) <= fabs(evmax)); + + found = true; + break; + } + + if (!found) { + for (size_t j = 0; j < nroots; ++j) { + fprintf(stderr, "roots[%zu] == %.17g\n", j, roots[j]); + } + fprintf(stderr, "----\n"); + ck_abort_msg("Expected root %.17g not found", root_ex); + } + } + va_end(ap); +} + + +DMNSN_TEST(linear, no_positive_roots) +{ + // poly[] = x + 1 + static const double poly[] = { + [1] = 1.0, + [0] = 1.0, + }; + dmnsn_assert_roots(poly, 1, 0); +} + +DMNSN_TEST(linear, one_root) +{ + // poly[] = x - 1 + static const double poly[] = { + [1] = 1.0, + [0] = -1.0, + }; + dmnsn_assert_roots(poly, 1, 1, 1.0); +} + + +DMNSN_TEST(quadratic, no_roots) +{ + // poly[] = x^2 + 1 + static const double poly[] = { + [2] = 1.0, + [1] = 0.0, + [0] = 1.0, + }; + dmnsn_assert_roots(poly, 2, 0); +} + +DMNSN_TEST(quadratic, no_positive_roots) +{ + // poly[] = (x + 1)^2 + static const double poly[] = { + [2] = 1.0, + [1] = 2.0, + [0] = 1.0, + }; + dmnsn_assert_roots(poly, 2, 0); +} + +DMNSN_TEST(quadratic, one_positive_root) +{ + // poly[] = (x + 1)*(x - 1) + static const double poly[] = { + [2] = 1.0, + [1] = 0.0, + [0] = -1.0, + }; + dmnsn_assert_roots(poly, 2, 1, 1.0); +} + +DMNSN_TEST(quadratic, two_roots) +{ + // poly[] = (x - 1.2345)*(x - 2.3456) + static const double poly[] = { + [2] = 1.0, + [1] = -3.5801, + [0] = 2.8956432, + }; + dmnsn_assert_roots(poly, 2, 2, 1.2345, 2.3456); +} + + +DMNSN_TEST(cubic, no_positive_roots) +{ + // poly[] = x^3 + 1 + static const double poly[] = { + [3] = 1.0, + [2] = 0.0, + [1] = 0.0, + [0] = 1.0, + }; + dmnsn_assert_roots(poly, 3, 0); +} + +DMNSN_TEST(cubic, one_root) +{ + // poly[] = x^3 - 1 + static const double poly[] = { + [3] = 1.0, + [2] = 0.0, + [1] = 0.0, + [0] = -1.0, + }; + dmnsn_assert_roots(poly, 3, 1, 1.0); +} + +DMNSN_TEST(cubic, two_roots) +{ + // poly[] = (x - 1)*(x - 4)^2 + static const double poly[] = { + [3] = 1.0, + [2] = -9.0, + [1] = 24.0, + [0] = -16.0, + }; + dmnsn_assert_roots(poly, 3, 2, 1.0, 4.0); +} + +DMNSN_TEST(cubic, three_roots) +{ + // poly[] = (x - 1.2345)*(x - 2.3456)*(x - 100) + static const double poly[] = { + [3] = 1.0, + [2] = -103.5801, + [1] = 360.9056432, + [0] = -289.56432, + }; + dmnsn_assert_roots(poly, 3, 3, 1.2345, 2.3456, 100.0); +} + + +DMNSN_TEST(quintic, four_roots) +{ + // poly[] = 2*(x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) + static const double poly[] = { + [5] = 2.0, + [4] = -215.1602, + [3] = 1540.4520864, + [2] = -2430.5727856, + [1] = -1292.541872, + [0] = 2895.6432, + }; + dmnsn_assert_roots(poly, 5, 4, 1.2345, 2.3456, 5.0, 100.0); +} + +// repeated_root[] = (x - 1)^6 +static const double repeated_root[7] = { + [6] = 1.0, + [5] = -6.0, + [4] = 15.0, + [3] = -20.0, + [2] = 15.0, + [1] = -6.0, + [0] = 1.0, +}; + +DMNSN_TEST(stability, equal_bounds) +{ + double root = dmnsn_bisect_root(repeated_root, 6, 1.0, 1.0); + ck_assert_msg(root == 1.0, "root == %.17g", root); +} + +DMNSN_TEST(stability, equal_values_at_bounds) +{ + double root = dmnsn_bisect_root(repeated_root, 6, 0.9, 1.1); + ck_assert_msg(fabs(root - 1.0) < dmnsn_epsilon, "root == %.17g", root); +} diff --git a/libdimension/tests/modules/base.c b/libdimension/tests/modules/base.c deleted file mode 100644 index 665c62f..0000000 --- a/libdimension/tests/modules/base.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the base module in isolation. - */ - -#include "dimension/base.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/canvas.c b/libdimension/tests/modules/canvas.c deleted file mode 100644 index e9e0471..0000000 --- a/libdimension/tests/modules/canvas.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the canvas module in isolation. - */ - -#include "dimension/canvas.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/color.c b/libdimension/tests/modules/color.c deleted file mode 100644 index 6c9e63f..0000000 --- a/libdimension/tests/modules/color.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the color module in isolation. - */ - -#include "dimension/color.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/concurrency.c b/libdimension/tests/modules/concurrency.c deleted file mode 100644 index 16bb305..0000000 --- a/libdimension/tests/modules/concurrency.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the concurrency module in isolation. - */ - -#include "dimension/concurrency.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/math.c b/libdimension/tests/modules/math.c deleted file mode 100644 index 5bf3ac1..0000000 --- a/libdimension/tests/modules/math.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the math module in isolation. - */ - -#include "dimension/math.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/model.c b/libdimension/tests/modules/model.c deleted file mode 100644 index 9bd5f00..0000000 --- a/libdimension/tests/modules/model.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the model module in isolation. - */ - -#include "dimension/model.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/pattern.c b/libdimension/tests/modules/pattern.c deleted file mode 100644 index 46bb99e..0000000 --- a/libdimension/tests/modules/pattern.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the pattern module in isolation. - */ - -#include "dimension/pattern.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/platform.c b/libdimension/tests/modules/platform.c deleted file mode 100644 index b0fc978..0000000 --- a/libdimension/tests/modules/platform.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the platform module in isolation. - */ - -#include "dimension/platform.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/modules/render.c b/libdimension/tests/modules/render.c deleted file mode 100644 index a508c9f..0000000 --- a/libdimension/tests/modules/render.c +++ /dev/null @@ -1,29 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Test the render module in isolation. - */ - -#include "dimension/render.h" - -#ifndef DMNSN_INLINE -#error "Missing #include " -#endif diff --git a/libdimension/tests/png.c b/libdimension/tests/png.c deleted file mode 100644 index 7311e43..0000000 --- a/libdimension/tests/png.c +++ /dev/null @@ -1,111 +0,0 @@ -/************************************************************************* - * Copyright (C) 2009-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -#include "tests.h" -#include -#include - -int -main(void) -{ - int ret = EXIT_SUCCESS; - - // Treat warnings as errors for tests - dmnsn_die_on_warnings(true); - - // Allocate our canvas - dmnsn_pool *pool = dmnsn_new_pool(); - dmnsn_canvas *canvas = dmnsn_new_canvas(pool, 768, 480); - - // Optimize the canvas for PNG export - if (dmnsn_png_optimize_canvas(pool, canvas) != 0) { - fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n"); - ret = EXIT_FAILURE; - goto exit; - } - - // Paint the test pattern - dmnsn_paint_test_canvas(canvas); - - // Write the image to PNG - - printf("Writing scene to PNG\n"); - FILE *ofile = fopen("png1.png", "wb"); - if (!ofile) { - fprintf(stderr, "--- Couldn't open 'png1.png' for writing! ---\n"); - ret = EXIT_FAILURE; - goto exit; - } - - if (dmnsn_png_write_canvas(canvas, ofile) != 0) { - fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); - fclose(ofile); - ret = EXIT_FAILURE; - goto exit; - } - - fclose(ofile); - - /* - * 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"); - ret = EXIT_FAILURE; - goto exit; - } - - canvas = dmnsn_png_read_canvas(pool, ifile); - if (!canvas) { - fprintf(stderr, "--- Reading canvas from PNG failed! ---\n"); - fclose(ifile); - ret = EXIT_FAILURE; - goto exit; - } - - 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"); - ret = EXIT_FAILURE; - goto exit; - } - - if (dmnsn_png_write_canvas(canvas, ofile) != 0) { - fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); - fclose(ofile); - ret = EXIT_FAILURE; - goto exit; - } - - fclose(ofile); - - exit: - dmnsn_delete_pool(pool); - return ret; -} diff --git a/libdimension/tests/polynomial.c b/libdimension/tests/polynomial.c deleted file mode 100644 index a983bd4..0000000 --- a/libdimension/tests/polynomial.c +++ /dev/null @@ -1,222 +0,0 @@ -/************************************************************************* - * Copyright (C) 2010-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Basic tests of the polynomial root-finder. - */ - -#include "../math/polynomial.c" -#include "tests.h" -#include - -#define DMNSN_CLOSE_ENOUGH 1.0e-6 - -static void -dmnsn_assert_roots(const double poly[], size_t degree, size_t nroots_ex, ...) -{ - double roots[degree]; - size_t nroots = dmnsn_polynomial_solve(poly, degree, roots); - ck_assert_int_eq(nroots, nroots_ex); - - va_list ap; - va_start(ap, nroots_ex); - for (size_t i = 0; i < nroots; ++i) { - double root_ex = va_arg(ap, double); - bool found = false; - for (size_t j = 0; j < nroots; ++j) { - double root = roots[j]; - if (fabs(root_ex - root) >= dmnsn_epsilon) { - continue; - } - - double evroot = dmnsn_polynomial_evaluate(poly, degree, root); - ck_assert(fabs(evroot) < DMNSN_CLOSE_ENOUGH); - - double evmin = dmnsn_polynomial_evaluate(poly, degree, root - dmnsn_epsilon); - double evmax = dmnsn_polynomial_evaluate(poly, degree, root + dmnsn_epsilon); - ck_assert(fabs(evroot) <= fabs(evmin) && fabs(evroot) <= fabs(evmax)); - - found = true; - break; - } - - if (!found) { - for (size_t j = 0; j < nroots; ++j) { - fprintf(stderr, "roots[%zu] == %.17g\n", j, roots[j]); - } - fprintf(stderr, "----\n"); - ck_abort_msg("Expected root %.17g not found", root_ex); - } - } - va_end(ap); -} - - -DMNSN_TEST(linear, no_positive_roots) -{ - // poly[] = x + 1 - static const double poly[] = { - [1] = 1.0, - [0] = 1.0, - }; - dmnsn_assert_roots(poly, 1, 0); -} - -DMNSN_TEST(linear, one_root) -{ - // poly[] = x - 1 - static const double poly[] = { - [1] = 1.0, - [0] = -1.0, - }; - dmnsn_assert_roots(poly, 1, 1, 1.0); -} - - -DMNSN_TEST(quadratic, no_roots) -{ - // poly[] = x^2 + 1 - static const double poly[] = { - [2] = 1.0, - [1] = 0.0, - [0] = 1.0, - }; - dmnsn_assert_roots(poly, 2, 0); -} - -DMNSN_TEST(quadratic, no_positive_roots) -{ - // poly[] = (x + 1)^2 - static const double poly[] = { - [2] = 1.0, - [1] = 2.0, - [0] = 1.0, - }; - dmnsn_assert_roots(poly, 2, 0); -} - -DMNSN_TEST(quadratic, one_positive_root) -{ - // poly[] = (x + 1)*(x - 1) - static const double poly[] = { - [2] = 1.0, - [1] = 0.0, - [0] = -1.0, - }; - dmnsn_assert_roots(poly, 2, 1, 1.0); -} - -DMNSN_TEST(quadratic, two_roots) -{ - // poly[] = (x - 1.2345)*(x - 2.3456) - static const double poly[] = { - [2] = 1.0, - [1] = -3.5801, - [0] = 2.8956432, - }; - dmnsn_assert_roots(poly, 2, 2, 1.2345, 2.3456); -} - - -DMNSN_TEST(cubic, no_positive_roots) -{ - // poly[] = x^3 + 1 - static const double poly[] = { - [3] = 1.0, - [2] = 0.0, - [1] = 0.0, - [0] = 1.0, - }; - dmnsn_assert_roots(poly, 3, 0); -} - -DMNSN_TEST(cubic, one_root) -{ - // poly[] = x^3 - 1 - static const double poly[] = { - [3] = 1.0, - [2] = 0.0, - [1] = 0.0, - [0] = -1.0, - }; - dmnsn_assert_roots(poly, 3, 1, 1.0); -} - -DMNSN_TEST(cubic, two_roots) -{ - // poly[] = (x - 1)*(x - 4)^2 - static const double poly[] = { - [3] = 1.0, - [2] = -9.0, - [1] = 24.0, - [0] = -16.0, - }; - dmnsn_assert_roots(poly, 3, 2, 1.0, 4.0); -} - -DMNSN_TEST(cubic, three_roots) -{ - // poly[] = (x - 1.2345)*(x - 2.3456)*(x - 100) - static const double poly[] = { - [3] = 1.0, - [2] = -103.5801, - [1] = 360.9056432, - [0] = -289.56432, - }; - dmnsn_assert_roots(poly, 3, 3, 1.2345, 2.3456, 100.0); -} - - -DMNSN_TEST(quintic, four_roots) -{ - // poly[] = 2*(x + 1)*(x - 1.2345)*(x - 2.3456)*(x - 5)*(x - 100) - static const double poly[] = { - [5] = 2.0, - [4] = -215.1602, - [3] = 1540.4520864, - [2] = -2430.5727856, - [1] = -1292.541872, - [0] = 2895.6432, - }; - dmnsn_assert_roots(poly, 5, 4, 1.2345, 2.3456, 5.0, 100.0); -} - -// repeated_root[] = (x - 1)^6 -static const double repeated_root[7] = { - [6] = 1.0, - [5] = -6.0, - [4] = 15.0, - [3] = -20.0, - [2] = 15.0, - [1] = -6.0, - [0] = 1.0, -}; - -DMNSN_TEST(stability, equal_bounds) -{ - double root = dmnsn_bisect_root(repeated_root, 6, 1.0, 1.0); - ck_assert_msg(root == 1.0, "root == %.17g", root); -} - -DMNSN_TEST(stability, equal_values_at_bounds) -{ - double root = dmnsn_bisect_root(repeated_root, 6, 0.9, 1.1); - ck_assert_msg(fabs(root - 1.0) < dmnsn_epsilon, "root == %.17g", root); -} diff --git a/libdimension/tests/pool.c b/libdimension/tests/pool.c deleted file mode 100644 index 6090c36..0000000 --- a/libdimension/tests/pool.c +++ /dev/null @@ -1,87 +0,0 @@ -/************************************************************************* - * Copyright (C) 2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Tests for memory pools. - */ - -#include "../concurrency/future.c" -#include "../concurrency/threads.c" -#include "../internal.h" -#include "tests.h" - -static dmnsn_pool *pool; - -DMNSN_TEST_SETUP(pool) -{ - pool = dmnsn_new_pool(); -} - -DMNSN_TEST_TEARDOWN(pool) -{ - dmnsn_delete_pool(pool); -} - -DMNSN_TEST(pool, simple) -{ - for (int i = 0; i < 10000; ++i) { - int *p = DMNSN_PALLOC(pool, int); - *p = i; - } - - // Leak checking will tell us if something bad happened -} - -static int counter = 0; - -static void -callback(void *ptr) -{ - ++counter; -} - -DMNSN_TEST(pool, callback) -{ - DMNSN_PALLOC(pool, int); - DMNSN_PALLOC_TIDY(pool, int, callback); - DMNSN_PALLOC_TIDY(pool, int, callback); - DMNSN_PALLOC(pool, int); - - dmnsn_delete_pool(pool); - pool = NULL; - - ck_assert_int_eq(counter, 2); -} - -static int -alloc_thread(void *ptr, unsigned int thread, unsigned int nthreads) -{ - for (unsigned int i = thread; i < 10000; i += nthreads) { - int *p = DMNSN_PALLOC(pool, int); - *p = i; - } - return 0; -} - -DMNSN_TEST(pool, threaded) -{ - int ret = dmnsn_execute_concurrently(NULL, alloc_thread, NULL, 64); - ck_assert_int_eq(ret, 0); -} diff --git a/libdimension/tests/prtree.c b/libdimension/tests/prtree.c deleted file mode 100644 index 8824e93..0000000 --- a/libdimension/tests/prtree.c +++ /dev/null @@ -1,115 +0,0 @@ -/************************************************************************* - * Copyright (C) 2010-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -/** - * @file - * Basic tests of PR-trees. - */ - -#include "../platform/platform.c" -#include "../concurrency/threads.c" -#include "../concurrency/future.c" -#include "../bvh/bvh.c" -#include "../bvh/prtree.c" -#include -#include - -unsigned int calls = 0; - -static bool -dmnsn_fake_intersection_fn(const dmnsn_object *object, dmnsn_ray ray, - dmnsn_intersection *intersection) -{ - intersection->t = (object->aabb.min.z - ray.x0.z)/ray.n.z; - intersection->normal = dmnsn_x; - ++calls; - return true; -} - -static void -dmnsn_randomize_aabb(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->aabb.min = dmnsn_vector_min(a, b); - object->aabb.max = dmnsn_vector_max(a, b); -} - -static const dmnsn_object_vtable dmnsn_fake_vtable = { - .intersection_fn = dmnsn_fake_intersection_fn, -}; - -static dmnsn_object * -dmnsn_new_fake_object(dmnsn_pool *pool) -{ - dmnsn_object *object = dmnsn_new_object(pool); - dmnsn_randomize_aabb(object); - object->vtable = &dmnsn_fake_vtable; - object->trans_inv = dmnsn_identity_matrix(); - return object; -} - -int -main(void) -{ - // Treat warnings as errors for tests - dmnsn_die_on_warnings(true); - - dmnsn_pool *pool = dmnsn_new_pool(); - - const size_t nobjects = 128; - dmnsn_array *objects = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); - - for (size_t i = 0; i < nobjects; ++i) { - dmnsn_object *object = dmnsn_new_fake_object(pool); - dmnsn_array_push(objects, &object); - } - - dmnsn_bvh *bvh = dmnsn_new_bvh(objects, DMNSN_BVH_PRTREE); - - dmnsn_intersection intersection; - dmnsn_ray ray = dmnsn_new_ray( - dmnsn_new_vector(0.0, 0.0, -2.0), - dmnsn_new_vector(0.0, 0.0, 1.0) - ); - - if (!dmnsn_bvh_intersection(bvh, 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_bvh(bvh); - dmnsn_delete_pool(pool); - return EXIT_SUCCESS; -} diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c deleted file mode 100644 index fd1e96d..0000000 --- a/libdimension/tests/render.c +++ /dev/null @@ -1,406 +0,0 @@ -/************************************************************************* - * Copyright (C) 2010-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -#include "tests.h" -#include -#include -#include - -static void -dmnsn_test_scene_set_defaults(dmnsn_pool *pool, dmnsn_scene *scene) -{ - // Default texture - scene->default_texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_black)); - dmnsn_finish *default_finish = &scene->default_texture->finish; - default_finish->ambient = dmnsn_new_ambient( - pool, dmnsn_color_from_sRGB(dmnsn_color_mul(0.1, dmnsn_white)) - ); - default_finish->diffuse = dmnsn_new_lambertian(pool, dmnsn_sRGB_inverse_gamma(0.7)); -} - -static void -dmnsn_test_scene_add_canvas(dmnsn_pool *pool, dmnsn_scene *scene) -{ - scene->canvas = dmnsn_new_canvas(pool, 768, 480); -} - -static void -dmnsn_test_scene_add_camera(dmnsn_pool *pool, dmnsn_scene *scene) -{ - // 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(pool); - scene->camera->trans = trans; -} - -static void -dmnsn_test_scene_add_background(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_pattern *sky_gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y); - dmnsn_map *sky_gradient_pigment_map = dmnsn_new_pigment_map(pool); - - dmnsn_canvas *png_canvas = NULL; - dmnsn_pigment *png_pigment; - FILE *png = fopen("png2.png", "rb"); - if (png) { - png_canvas = dmnsn_png_read_canvas(pool, png); - fclose(png); - } - if (png_canvas) { - png_pigment = dmnsn_new_canvas_pigment(pool, png_canvas); - png_pigment->trans = dmnsn_rotation_matrix( - dmnsn_new_vector(0.0, dmnsn_radians(53.0), 0.0) - ); - } else { - // Loading png2.png failed, possibly compiled with --disable-png - fprintf(stderr, "--- WARNING: Couldn't open or read png2.png! ---\n"); - png_pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_orange)); - } - dmnsn_map_add_entry(sky_gradient_pigment_map, 0.0, &png_pigment); - - dmnsn_color background = dmnsn_color_from_sRGB( - dmnsn_new_color(0.0, 0.1, 0.2) - ); - dmnsn_tcolor tbackground = dmnsn_new_tcolor(background, 0.1, 0.0); - dmnsn_pigment *bkgpigment = dmnsn_new_solid_pigment(pool, tbackground); - dmnsn_map_add_entry(sky_gradient_pigment_map, 0.35, &bkgpigment); - - scene->background = dmnsn_new_pigment_map_pigment( - pool, sky_gradient, sky_gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB - ); -} - -static void -dmnsn_test_scene_add_lights(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_light *light = dmnsn_new_point_light( - pool, - dmnsn_new_vector(-15.0, 20.0, 10.0), - dmnsn_white - ); - dmnsn_array_push(scene->lights, &light); -} - -static void -dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_object *cube = dmnsn_new_cube(pool); - cube->trans = dmnsn_rotation_matrix( - dmnsn_new_vector(dmnsn_radians(45.0), 0.0, 0.0) - ); - - cube->texture = dmnsn_new_texture(pool); - dmnsn_tcolor cube_color = dmnsn_new_tcolor(dmnsn_blue, 0.75, 1.0/3.0); - cube->texture->pigment = dmnsn_new_solid_pigment(pool, cube_color); - - dmnsn_color reflect = dmnsn_color_from_sRGB(dmnsn_color_mul(0.5, dmnsn_white)); - cube->texture->finish.reflection = dmnsn_new_basic_reflection(pool, dmnsn_black, reflect, 1.0); - - cube->interior = dmnsn_new_interior(pool); - cube->interior->ior = 1.1; - - dmnsn_object *sphere = dmnsn_new_sphere(pool); - sphere->texture = dmnsn_new_texture(pool); - sphere->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_green)); - sphere->texture->finish.specular = dmnsn_new_phong(pool, dmnsn_sRGB_inverse_gamma(0.2), 40.0); - sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); - - dmnsn_object *hollow_cube = dmnsn_new_csg_difference(pool, cube, sphere); - dmnsn_array_push(scene->objects, &hollow_cube); -} - -#define dmnsn_pigment_map_add_color(map, n, color) \ - do { \ - dmnsn_tcolor tcolor = DMNSN_TCOLOR(color); \ - dmnsn_pigment *pigment = dmnsn_new_solid_pigment(pool, tcolor); \ - dmnsn_map_add_entry(map, n, &pigment); \ - } while (0) - -static void -dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_array *arrow_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); - - dmnsn_object *cylinder = dmnsn_new_cone(pool, 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(pool, 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(pool, arrow_array); - dmnsn_pattern *gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y); - dmnsn_map *gradient_pigment_map = dmnsn_new_pigment_map(pool); - dmnsn_pigment_map_add_color(gradient_pigment_map, 0.0, dmnsn_red); - dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0/6.0, dmnsn_orange); - dmnsn_pigment_map_add_color(gradient_pigment_map, 2.0/6.0, dmnsn_yellow); - dmnsn_pigment_map_add_color(gradient_pigment_map, 3.0/6.0, dmnsn_green); - dmnsn_pigment_map_add_color(gradient_pigment_map, 4.0/6.0, dmnsn_blue); - dmnsn_pigment_map_add_color(gradient_pigment_map, 5.0/6.0, dmnsn_magenta); - dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0, dmnsn_red); - arrow->texture = dmnsn_new_texture(pool); - arrow->texture->pigment = dmnsn_new_pigment_map_pigment( - pool, gradient, gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB - ); - - 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 *torus_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); - - dmnsn_object *torus1 = dmnsn_new_torus(pool, 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(pool, 0.15, 0.05); - dmnsn_array_push(torus_array, &torus2); - - dmnsn_object *torus3 = dmnsn_new_torus(pool, 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(pool, torus_array); - torii->texture = dmnsn_new_texture(pool); - torii->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_blue)); - torii->texture->finish.ambient = dmnsn_new_ambient(pool, dmnsn_white); - - dmnsn_array *spike_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); - dmnsn_array_push(spike_array, &arrow); - dmnsn_array_push(spike_array, &torii); - dmnsn_object *spike = dmnsn_new_csg_union(pool, spike_array); - spike->trans = dmnsn_rotation_matrix( - dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0) - ); - dmnsn_array_push(scene->objects, &spike); -} - -static void -dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_array *strip_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); - dmnsn_vector vertices[] = { - dmnsn_zero, - dmnsn_new_vector(0.0, sqrt(3.0)/2.0, 0.5), - dmnsn_z, - }; - dmnsn_texture *strip_textures[3] = { - dmnsn_new_texture(pool), - dmnsn_new_texture(pool), - dmnsn_new_texture(pool), - }; - strip_textures[0]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_red)); - strip_textures[1]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_orange)); - strip_textures[2]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_yellow)); - for (unsigned int i = 0; i < 128; ++i) { - dmnsn_object *triangle = dmnsn_new_triangle(pool, vertices); - triangle->texture = strip_textures[i%3]; - dmnsn_array_push(strip_array, &triangle); - - vertices[0] = vertices[1]; - vertices[1] = vertices[2]; - vertices[2] = dmnsn_vector_add(vertices[0], dmnsn_z); - } - - dmnsn_object *strip = dmnsn_new_csg_union(pool, strip_array); - strip->trans = dmnsn_translation_matrix(dmnsn_new_vector(5.0, -2.0, -4.0)); - dmnsn_array_push(scene->objects, &strip); -} - -static void -dmnsn_test_scene_add_ground(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_object *plane = dmnsn_new_plane(pool, dmnsn_new_vector(0.0, 1.0, 0.0)); - plane->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -2.0, 0.0)); - dmnsn_pattern *checker = dmnsn_new_checker_pattern(pool); - dmnsn_map *small_map = dmnsn_new_pigment_map(pool); - dmnsn_pigment_map_add_color(small_map, 0.0, dmnsn_black); - dmnsn_pigment_map_add_color(small_map, 1.0, dmnsn_white); - dmnsn_pigment *small_pigment = dmnsn_new_pigment_map_pigment( - pool, checker, small_map, DMNSN_PIGMENT_MAP_REGULAR - ); - small_pigment->trans = - dmnsn_scale_matrix(dmnsn_new_vector(1.0/3.0, 1.0/3.0, 1.0/3.0)); - dmnsn_map *big_map = dmnsn_new_pigment_map(pool); - dmnsn_pigment_map_add_color(big_map, 0.0, dmnsn_white); - dmnsn_map_add_entry(big_map, 1.0, &small_pigment); - plane->texture = dmnsn_new_texture(pool); - plane->texture->pigment = dmnsn_new_pigment_map_pigment( - pool, checker, big_map, DMNSN_PIGMENT_MAP_REGULAR - ); - plane->texture->pigment->quick_color = DMNSN_TCOLOR( - dmnsn_color_from_sRGB( - dmnsn_new_color(1.0, 0.5, 0.75) - ) - ); - dmnsn_array_push(scene->objects, &plane); -} - -static void -dmnsn_test_scene_add_objects(dmnsn_pool *pool, dmnsn_scene *scene) -{ - dmnsn_test_scene_add_hollow_cube(pool, scene); - dmnsn_test_scene_add_spike(pool, scene); - dmnsn_test_scene_add_triangle_strip(pool, scene); - dmnsn_test_scene_add_ground(pool, scene); -} - -/// Test scene. -static dmnsn_scene * -dmnsn_new_test_scene(dmnsn_pool *pool) -{ - dmnsn_scene *scene = dmnsn_new_scene(pool); - dmnsn_test_scene_set_defaults(pool, scene); - dmnsn_test_scene_add_canvas(pool, scene); - dmnsn_test_scene_add_camera(pool, scene); - dmnsn_test_scene_add_background(pool, scene); - dmnsn_test_scene_add_lights(pool, scene); - dmnsn_test_scene_add_objects(pool, scene); - return scene; -} - -int -main(void) -{ - int ret = EXIT_FAILURE; - - // Treat warnings as errors for tests - dmnsn_die_on_warnings(true); - - dmnsn_display *display = NULL; - - // Create the test scene - dmnsn_pool *pool = dmnsn_new_pool(); - dmnsn_scene *scene = dmnsn_new_test_scene(pool); - - // Optimize the canvas for PNG export - bool have_png = true; - errno = 0; - if (dmnsn_png_optimize_canvas(pool, scene->canvas) != 0) { - if (errno == ENOSYS) { - have_png = false; - } else { - fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n"); - goto exit; - } - } - - // Optimize the canvas for GL drawing - bool have_gl = true; - errno = 0; - if (dmnsn_gl_optimize_canvas(pool, scene->canvas) != 0) { - if (errno == ENOSYS) { - have_gl = false; - } else { - fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n"); - goto exit; - } - } - - dmnsn_canvas_clear(scene->canvas, DMNSN_TCOLOR(dmnsn_black)); - - // Create a new glX display - 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_future *future = dmnsn_render_async(scene); - - // Display the scene as it's rendered - if (display) { - while (!dmnsn_future_is_done(future)) { - dmnsn_future_pause(future); - if (dmnsn_gl_write_canvas(scene->canvas) != 0) { - fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); - goto exit; - } - dmnsn_future_resume(future); - - dmnsn_display_flush(display); - } - } - - if (dmnsn_future_join(future) != 0) { - fprintf(stderr, "--- Raytracing failed! ---\n"); - goto exit; - } - - // Make sure we show the completed rendering - if (display) { - printf("Drawing to OpenGL\n"); - if (dmnsn_gl_write_canvas(scene->canvas) != 0) { - fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); - goto exit; - } - dmnsn_display_flush(display); - } - - if (have_png) { - printf("Writing scene to PNG\n"); - FILE *ofile = fopen("render.png", "wb"); - if (!ofile) { - fprintf(stderr, "--- Couldn't open 'render.png' for writing! ---\n"); - goto exit; - } - - if (dmnsn_png_write_canvas(scene->canvas, ofile) != 0) { - fclose(ofile); - fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); - goto exit; - } - - fclose(ofile); - } - - ret = EXIT_SUCCESS; - exit: - dmnsn_delete_display(display); - dmnsn_delete_pool(pool); - return ret; -} diff --git a/libdimension/tests/render/render.c b/libdimension/tests/render/render.c new file mode 100644 index 0000000..fd1e96d --- /dev/null +++ b/libdimension/tests/render/render.c @@ -0,0 +1,406 @@ +/************************************************************************* + * Copyright (C) 2010-2014 Tavian Barnes * + * * + * 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 . * + *************************************************************************/ + +#include "tests.h" +#include +#include +#include + +static void +dmnsn_test_scene_set_defaults(dmnsn_pool *pool, dmnsn_scene *scene) +{ + // Default texture + scene->default_texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_black)); + dmnsn_finish *default_finish = &scene->default_texture->finish; + default_finish->ambient = dmnsn_new_ambient( + pool, dmnsn_color_from_sRGB(dmnsn_color_mul(0.1, dmnsn_white)) + ); + default_finish->diffuse = dmnsn_new_lambertian(pool, dmnsn_sRGB_inverse_gamma(0.7)); +} + +static void +dmnsn_test_scene_add_canvas(dmnsn_pool *pool, dmnsn_scene *scene) +{ + scene->canvas = dmnsn_new_canvas(pool, 768, 480); +} + +static void +dmnsn_test_scene_add_camera(dmnsn_pool *pool, dmnsn_scene *scene) +{ + // 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(pool); + scene->camera->trans = trans; +} + +static void +dmnsn_test_scene_add_background(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_pattern *sky_gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y); + dmnsn_map *sky_gradient_pigment_map = dmnsn_new_pigment_map(pool); + + dmnsn_canvas *png_canvas = NULL; + dmnsn_pigment *png_pigment; + FILE *png = fopen("png2.png", "rb"); + if (png) { + png_canvas = dmnsn_png_read_canvas(pool, png); + fclose(png); + } + if (png_canvas) { + png_pigment = dmnsn_new_canvas_pigment(pool, png_canvas); + png_pigment->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(0.0, dmnsn_radians(53.0), 0.0) + ); + } else { + // Loading png2.png failed, possibly compiled with --disable-png + fprintf(stderr, "--- WARNING: Couldn't open or read png2.png! ---\n"); + png_pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_orange)); + } + dmnsn_map_add_entry(sky_gradient_pigment_map, 0.0, &png_pigment); + + dmnsn_color background = dmnsn_color_from_sRGB( + dmnsn_new_color(0.0, 0.1, 0.2) + ); + dmnsn_tcolor tbackground = dmnsn_new_tcolor(background, 0.1, 0.0); + dmnsn_pigment *bkgpigment = dmnsn_new_solid_pigment(pool, tbackground); + dmnsn_map_add_entry(sky_gradient_pigment_map, 0.35, &bkgpigment); + + scene->background = dmnsn_new_pigment_map_pigment( + pool, sky_gradient, sky_gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB + ); +} + +static void +dmnsn_test_scene_add_lights(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_light *light = dmnsn_new_point_light( + pool, + dmnsn_new_vector(-15.0, 20.0, 10.0), + dmnsn_white + ); + dmnsn_array_push(scene->lights, &light); +} + +static void +dmnsn_test_scene_add_hollow_cube(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_object *cube = dmnsn_new_cube(pool); + cube->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(dmnsn_radians(45.0), 0.0, 0.0) + ); + + cube->texture = dmnsn_new_texture(pool); + dmnsn_tcolor cube_color = dmnsn_new_tcolor(dmnsn_blue, 0.75, 1.0/3.0); + cube->texture->pigment = dmnsn_new_solid_pigment(pool, cube_color); + + dmnsn_color reflect = dmnsn_color_from_sRGB(dmnsn_color_mul(0.5, dmnsn_white)); + cube->texture->finish.reflection = dmnsn_new_basic_reflection(pool, dmnsn_black, reflect, 1.0); + + cube->interior = dmnsn_new_interior(pool); + cube->interior->ior = 1.1; + + dmnsn_object *sphere = dmnsn_new_sphere(pool); + sphere->texture = dmnsn_new_texture(pool); + sphere->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_green)); + sphere->texture->finish.specular = dmnsn_new_phong(pool, dmnsn_sRGB_inverse_gamma(0.2), 40.0); + sphere->trans = dmnsn_scale_matrix(dmnsn_new_vector(1.25, 1.25, 1.25)); + + dmnsn_object *hollow_cube = dmnsn_new_csg_difference(pool, cube, sphere); + dmnsn_array_push(scene->objects, &hollow_cube); +} + +#define dmnsn_pigment_map_add_color(map, n, color) \ + do { \ + dmnsn_tcolor tcolor = DMNSN_TCOLOR(color); \ + dmnsn_pigment *pigment = dmnsn_new_solid_pigment(pool, tcolor); \ + dmnsn_map_add_entry(map, n, &pigment); \ + } while (0) + +static void +dmnsn_test_scene_add_spike(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_array *arrow_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); + + dmnsn_object *cylinder = dmnsn_new_cone(pool, 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(pool, 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(pool, arrow_array); + dmnsn_pattern *gradient = dmnsn_new_gradient_pattern(pool, dmnsn_y); + dmnsn_map *gradient_pigment_map = dmnsn_new_pigment_map(pool); + dmnsn_pigment_map_add_color(gradient_pigment_map, 0.0, dmnsn_red); + dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0/6.0, dmnsn_orange); + dmnsn_pigment_map_add_color(gradient_pigment_map, 2.0/6.0, dmnsn_yellow); + dmnsn_pigment_map_add_color(gradient_pigment_map, 3.0/6.0, dmnsn_green); + dmnsn_pigment_map_add_color(gradient_pigment_map, 4.0/6.0, dmnsn_blue); + dmnsn_pigment_map_add_color(gradient_pigment_map, 5.0/6.0, dmnsn_magenta); + dmnsn_pigment_map_add_color(gradient_pigment_map, 1.0, dmnsn_red); + arrow->texture = dmnsn_new_texture(pool); + arrow->texture->pigment = dmnsn_new_pigment_map_pigment( + pool, gradient, gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB + ); + + 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 *torus_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); + + dmnsn_object *torus1 = dmnsn_new_torus(pool, 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(pool, 0.15, 0.05); + dmnsn_array_push(torus_array, &torus2); + + dmnsn_object *torus3 = dmnsn_new_torus(pool, 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(pool, torus_array); + torii->texture = dmnsn_new_texture(pool); + torii->texture->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_blue)); + torii->texture->finish.ambient = dmnsn_new_ambient(pool, dmnsn_white); + + dmnsn_array *spike_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); + dmnsn_array_push(spike_array, &arrow); + dmnsn_array_push(spike_array, &torii); + dmnsn_object *spike = dmnsn_new_csg_union(pool, spike_array); + spike->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(dmnsn_radians(-45.0), 0.0, 0.0) + ); + dmnsn_array_push(scene->objects, &spike); +} + +static void +dmnsn_test_scene_add_triangle_strip(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_array *strip_array = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *); + dmnsn_vector vertices[] = { + dmnsn_zero, + dmnsn_new_vector(0.0, sqrt(3.0)/2.0, 0.5), + dmnsn_z, + }; + dmnsn_texture *strip_textures[3] = { + dmnsn_new_texture(pool), + dmnsn_new_texture(pool), + dmnsn_new_texture(pool), + }; + strip_textures[0]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_red)); + strip_textures[1]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_orange)); + strip_textures[2]->pigment = dmnsn_new_solid_pigment(pool, DMNSN_TCOLOR(dmnsn_yellow)); + for (unsigned int i = 0; i < 128; ++i) { + dmnsn_object *triangle = dmnsn_new_triangle(pool, vertices); + triangle->texture = strip_textures[i%3]; + dmnsn_array_push(strip_array, &triangle); + + vertices[0] = vertices[1]; + vertices[1] = vertices[2]; + vertices[2] = dmnsn_vector_add(vertices[0], dmnsn_z); + } + + dmnsn_object *strip = dmnsn_new_csg_union(pool, strip_array); + strip->trans = dmnsn_translation_matrix(dmnsn_new_vector(5.0, -2.0, -4.0)); + dmnsn_array_push(scene->objects, &strip); +} + +static void +dmnsn_test_scene_add_ground(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_object *plane = dmnsn_new_plane(pool, dmnsn_new_vector(0.0, 1.0, 0.0)); + plane->trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -2.0, 0.0)); + dmnsn_pattern *checker = dmnsn_new_checker_pattern(pool); + dmnsn_map *small_map = dmnsn_new_pigment_map(pool); + dmnsn_pigment_map_add_color(small_map, 0.0, dmnsn_black); + dmnsn_pigment_map_add_color(small_map, 1.0, dmnsn_white); + dmnsn_pigment *small_pigment = dmnsn_new_pigment_map_pigment( + pool, checker, small_map, DMNSN_PIGMENT_MAP_REGULAR + ); + small_pigment->trans = + dmnsn_scale_matrix(dmnsn_new_vector(1.0/3.0, 1.0/3.0, 1.0/3.0)); + dmnsn_map *big_map = dmnsn_new_pigment_map(pool); + dmnsn_pigment_map_add_color(big_map, 0.0, dmnsn_white); + dmnsn_map_add_entry(big_map, 1.0, &small_pigment); + plane->texture = dmnsn_new_texture(pool); + plane->texture->pigment = dmnsn_new_pigment_map_pigment( + pool, checker, big_map, DMNSN_PIGMENT_MAP_REGULAR + ); + plane->texture->pigment->quick_color = DMNSN_TCOLOR( + dmnsn_color_from_sRGB( + dmnsn_new_color(1.0, 0.5, 0.75) + ) + ); + dmnsn_array_push(scene->objects, &plane); +} + +static void +dmnsn_test_scene_add_objects(dmnsn_pool *pool, dmnsn_scene *scene) +{ + dmnsn_test_scene_add_hollow_cube(pool, scene); + dmnsn_test_scene_add_spike(pool, scene); + dmnsn_test_scene_add_triangle_strip(pool, scene); + dmnsn_test_scene_add_ground(pool, scene); +} + +/// Test scene. +static dmnsn_scene * +dmnsn_new_test_scene(dmnsn_pool *pool) +{ + dmnsn_scene *scene = dmnsn_new_scene(pool); + dmnsn_test_scene_set_defaults(pool, scene); + dmnsn_test_scene_add_canvas(pool, scene); + dmnsn_test_scene_add_camera(pool, scene); + dmnsn_test_scene_add_background(pool, scene); + dmnsn_test_scene_add_lights(pool, scene); + dmnsn_test_scene_add_objects(pool, scene); + return scene; +} + +int +main(void) +{ + int ret = EXIT_FAILURE; + + // Treat warnings as errors for tests + dmnsn_die_on_warnings(true); + + dmnsn_display *display = NULL; + + // Create the test scene + dmnsn_pool *pool = dmnsn_new_pool(); + dmnsn_scene *scene = dmnsn_new_test_scene(pool); + + // Optimize the canvas for PNG export + bool have_png = true; + errno = 0; + if (dmnsn_png_optimize_canvas(pool, scene->canvas) != 0) { + if (errno == ENOSYS) { + have_png = false; + } else { + fprintf(stderr, "--- Couldn't optimize canvas for PNG! ---\n"); + goto exit; + } + } + + // Optimize the canvas for GL drawing + bool have_gl = true; + errno = 0; + if (dmnsn_gl_optimize_canvas(pool, scene->canvas) != 0) { + if (errno == ENOSYS) { + have_gl = false; + } else { + fprintf(stderr, "--- Couldn't optimize canvas for GL! ---\n"); + goto exit; + } + } + + dmnsn_canvas_clear(scene->canvas, DMNSN_TCOLOR(dmnsn_black)); + + // Create a new glX display + 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_future *future = dmnsn_render_async(scene); + + // Display the scene as it's rendered + if (display) { + while (!dmnsn_future_is_done(future)) { + dmnsn_future_pause(future); + if (dmnsn_gl_write_canvas(scene->canvas) != 0) { + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + goto exit; + } + dmnsn_future_resume(future); + + dmnsn_display_flush(display); + } + } + + if (dmnsn_future_join(future) != 0) { + fprintf(stderr, "--- Raytracing failed! ---\n"); + goto exit; + } + + // Make sure we show the completed rendering + if (display) { + printf("Drawing to OpenGL\n"); + if (dmnsn_gl_write_canvas(scene->canvas) != 0) { + fprintf(stderr, "--- Drawing to OpenGL failed! ---\n"); + goto exit; + } + dmnsn_display_flush(display); + } + + if (have_png) { + printf("Writing scene to PNG\n"); + FILE *ofile = fopen("render.png", "wb"); + if (!ofile) { + fprintf(stderr, "--- Couldn't open 'render.png' for writing! ---\n"); + goto exit; + } + + if (dmnsn_png_write_canvas(scene->canvas, ofile) != 0) { + fclose(ofile); + fprintf(stderr, "--- Writing canvas to PNG failed! ---\n"); + goto exit; + } + + fclose(ofile); + } + + ret = EXIT_SUCCESS; + exit: + dmnsn_delete_display(display); + dmnsn_delete_pool(pool); + return ret; +} diff --git a/libdimension/tests/warning-as-error.c b/libdimension/tests/warning-as-error.c deleted file mode 100644 index 7d0fdb7..0000000 --- a/libdimension/tests/warning-as-error.c +++ /dev/null @@ -1,33 +0,0 @@ -/************************************************************************* - * Copyright (C) 2009-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -// Make sure warnings kill us in strict mode - this test should fail - -#include "dimension.h" -#include -#include - -int -main(void) -{ - dmnsn_die_on_warnings(true); - errno = ENOMEM; - dmnsn_warning("This warning is expected."); - return EXIT_SUCCESS; -} diff --git a/libdimension/tests/warning.c b/libdimension/tests/warning.c deleted file mode 100644 index cc1aed1..0000000 --- a/libdimension/tests/warning.c +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************************************* - * Copyright (C) 2009-2014 Tavian Barnes * - * * - * 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 . * - *************************************************************************/ - -// Make sure warnings don't kill us - this test should pass - -#include "dimension.h" -#include -#include - -int -main(void) -{ - errno = ENOMEM; - dmnsn_warning("This warning is expected."); - return EXIT_SUCCESS; -} -- cgit v1.2.3