From 7f43eabc54033ee929b618ac0a726a67f860a37b Mon Sep 17 00:00:00 2001 From: jaspervdg Date: Thu, 3 Jul 2008 13:20:00 +0000 Subject: [PATCH] Tests for svg-affine and svg-length (the latter is not much more than a stub) and a CxxTest version of the list-container tests. --- build.xml | 11 ++ src/svg/svg-affine-test.h | 219 +++++++++++++++++++++++++++++ src/svg/svg-length-test.h | 46 +++++++ src/util/list-container-test.h | 245 +++++++++++++++++++++++++++++++++ 4 files changed, 521 insertions(+) create mode 100644 src/svg/svg-affine-test.h create mode 100644 src/svg/svg-length-test.h create mode 100644 src/util/list-container-test.h diff --git a/build.xml b/build.xml index 0355b98bc..2ab7104e2 100644 --- a/build.xml +++ b/build.xml @@ -245,10 +245,18 @@ + + + + + + + @@ -414,6 +422,7 @@ + @@ -566,6 +575,7 @@ + @@ -827,6 +837,7 @@ + diff --git a/src/svg/svg-affine-test.h b/src/svg/svg-affine-test.h new file mode 100644 index 000000000..4180fcf3d --- /dev/null +++ b/src/svg/svg-affine-test.h @@ -0,0 +1,219 @@ +#include + +#include "svg/svg.h" +#include <2geom/matrix.h> +#include +#include +#include +#include +#include + +struct streq_free2 { + bool operator()(char const *exp, char const *got) const + { + bool const ret = (strcmp(exp, got) == 0); + g_free((void*)got); + return ret; + } +}; + +struct approx_equal { + bool operator()(Geom::Matrix const &ref, Geom::Matrix const &cm) const + { + double maxabsdiff = 0; + for(size_t i=0; i<6; i++) { + maxabsdiff = std::max(std::abs(ref[i]-cm[i]), maxabsdiff); + } + return maxabsdiff < 1e-14; + } +}; + +class SvgAffineTest : public CxxTest::TestSuite +{ +private: + struct test_t { + char const * str; + Geom::Matrix matrix; + }; + static test_t const read_matrix_tests[3]; + static test_t const read_translate_tests[3]; + static test_t const read_scale_tests[3]; + static test_t const read_rotate_tests[4]; + static test_t const read_skew_tests[3]; + static test_t const write_matrix_tests[2]; + static test_t const write_translate_tests[3]; + static test_t const write_scale_tests[2]; + static test_t const write_rotate_tests[2]; + static test_t const write_skew_tests[3]; +public: + SvgAffineTest() { + } + + void testReadIdentity() + { + char const* strs[] = { + //0, + "", + "matrix(1,0,0,1,0,0)", + "translate(0,0)", + "scale(1,1)", + "rotate(0,0,0)", + "skewX(0)", + "skewY(0)"}; + size_t n = G_N_ELEMENTS(strs); + for(size_t i=0; i + +#include "svg/svg-length.h" +#include +#include + +class SvgLengthTest : public CxxTest::TestSuite +{ +private: +public: + SvgLengthTest() { + } + + void testRead() + { + struct test_t { + char const* str; float computed; + test_t(char const* str, float computed) : str(str), computed(computed) {} + }; + test_t tests[] = { + test_t("0",0), + test_t("1",1), + test_t("1.00001",1.00001), + test_t("1px",1), + test_t(".1px",0.1)}; + size_t n = G_N_ELEMENTS(tests); + for(size_t i=0; i + +#include +#include "util/list-container.h" + +using Inkscape::Util::ListContainer; + +#define ARRAY_RANGE(array) (array), (array)+sizeof((array))/sizeof((array)[0]) + +static bool check_values(ListContainer const &c, unsigned n_values, ...) { + bool ret = true; + va_list args; + va_start(args, n_values); + ListContainer::const_iterator iter(c.begin()); + while ( n_values && iter != c.end() ) { + int const value = va_arg(args, int); + if ( value != *iter ) { + ret = false; + } + if ( n_values == 1 && &c.back() != &*iter ) { + ret = false; + } + n_values--; + ++iter; + } + va_end(args); + return ret && n_values == 0 && iter == c.end(); +} + +class ListContainerTest : public CxxTest::TestSuite { +public: + ListContainerTest() + { + Inkscape::GC::init(); + } + virtual ~ListContainerTest() {} + + void testRangeConstructor() + { + int const values[]={1,2,3,4}; + int const * const values_end=values+4; + ListContainer container(values, values_end); + + ListContainer::iterator container_iter=container.begin(); + int const * values_iter=values; + + while ( values_iter != values_end && container_iter != container.end() ) { + TS_ASSERT_EQUALS(*values_iter , *container_iter); + ++values_iter; + ++container_iter; + } + + TS_ASSERT_EQUALS(values_iter , values_end); + TS_ASSERT_EQUALS(container_iter , container.end()); + } + + void testEqualityTests() + { + int const a[] = { 1, 2, 3, 4 }; + int const b[] = { 1, 2, 3, 4 }; + int const c[] = { 1, 2, 3 }; + int const d[] = { 1, 2, 3, 5 }; + ListContainer c_a(ARRAY_RANGE(a)); + ListContainer c_b(ARRAY_RANGE(b)); + ListContainer c_c(ARRAY_RANGE(c)); + ListContainer c_d(ARRAY_RANGE(d)); + + TS_ASSERT(c_a == c_b); + TS_ASSERT(!( c_a != c_b )); + TS_ASSERT(!( c_a == c_c )); + TS_ASSERT(c_a != c_c); + TS_ASSERT(!( c_a == c_d )); + TS_ASSERT(c_a != c_d); + } + + void testLessThan() + { + int const a[] = { 1, 2, 3, 4 }; + int const b[] = { 1, 2, 2, 4 }; + int const c[] = { 1, 2, 4, 4 }; + int const d[] = { 1, 2, 3 }; + ListContainer c_a(ARRAY_RANGE(a)); + ListContainer c_b(ARRAY_RANGE(b)); + ListContainer c_c(ARRAY_RANGE(c)); + ListContainer c_d(ARRAY_RANGE(d)); + TS_ASSERT(c_a >= c_b); + TS_ASSERT(!( c_a < c_b )); + TS_ASSERT(!( c_a >= c_c )); + TS_ASSERT(c_a < c_c); + TS_ASSERT(!( c_a < c_d )); + TS_ASSERT(c_a >= c_d); + TS_ASSERT(c_d < c_a); + } + + void testAssignmentOperator() + { + int const a[] = { 1, 2, 3, 4 }; + ListContainer c_a(ARRAY_RANGE(a)); + ListContainer c_c; + TS_ASSERT(c_a != c_c); + c_c = c_a; + TS_ASSERT(c_a == c_c); + c_c = c_a; + TS_ASSERT(c_a == c_c); + } + + void testFillConstructor() + { + ListContainer filled((std::size_t)3, 2); + TS_ASSERT(check_values(filled, 3, 2, 2, 2)); + } + + void testContainerSize() + { + ListContainer empty; + TS_ASSERT(empty.empty()); + TS_ASSERT_EQUALS(empty.size() , 0); + int const a[] = { 1, 2, 3 }; + ListContainer c_a(ARRAY_RANGE(a)); + TS_ASSERT(!c_a.empty()); + TS_ASSERT_EQUALS(c_a.size() , 3); + + TS_ASSERT_LESS_THAN(0 , empty.max_size()); + } + + void testAppending() + { + ListContainer c; + c.push_back(1); + TS_ASSERT(check_values(c, 1, 1)); + c.push_back(2); + TS_ASSERT(check_values(c, 2, 1, 2)); + c.push_back(3); + TS_ASSERT(check_values(c, 3, 1, 2, 3)); + } + + void testBulkAppending() + { + int const a[] = { 1, 2, 3, 4 }; + int const b[] = { 5, 6, 7 }; + ListContainer c_a(ARRAY_RANGE(a)); + ListContainer c_b(ARRAY_RANGE(b)); + c_a.insert(c_a.end(), c_b.begin(), c_b.end()); + TS_ASSERT(check_values(c_a, 7, 1, 2, 3, 4, 5, 6, 7)); + } + + void testPrepending() + { + ListContainer c; + c.push_front(1); + TS_ASSERT(check_values(c, 1, 1)); + c.push_front(2); + TS_ASSERT(check_values(c, 2, 2, 1)); + c.push_front(3); + TS_ASSERT(check_values(c, 3, 3, 2, 1)); + } + + void testSingleValueInsertion() + { + ListContainer c; + + c.insert(c.begin(), 1); + TS_ASSERT(check_values(c, 1, 1)); + + c.insert(c.end(), 2); + TS_ASSERT(check_values(c, 2, 1, 2)); + + c.insert(c.begin(), 3); + TS_ASSERT(check_values(c, 3, 3, 1, 2)); + + ListContainer::iterator pos=c.begin(); + ++pos; + c.insert(pos, 4); + TS_ASSERT(check_values(c, 4, 3, 4, 1, 2)); + } + + void testSingleValueErasure() + { + int const values[] = { 1, 2, 3, 4 }; + ListContainer c(ARRAY_RANGE(values)); + + c.erase(c.begin()); + TS_ASSERT(check_values(c, 3, 2, 3, 4)); + + ListContainer::iterator pos=c.begin(); + ++pos; + c.erase(pos); + TS_ASSERT(check_values(c, 2, 2, 4)); + + pos=c.begin(); + ++pos; + c.erase(pos); + TS_ASSERT(check_values(c, 1, 2)); + + c.erase(c.begin()); + TS_ASSERT(check_values(c, 0)); + } + + void testPopFront() + { + int const full_ary[] = { 1, 2, 3 }; + ListContainer t(ARRAY_RANGE(full_ary)); + TS_ASSERT(check_values(t, 3, 1, 2, 3)); + TS_ASSERT_EQUALS(t.back() , 3); + t.pop_front(); + TS_ASSERT(check_values(t, 2, 2, 3)); + TS_ASSERT_EQUALS(t.back() , 3); + t.push_back(23); + TS_ASSERT(check_values(t, 3, 2, 3, 23)); + TS_ASSERT_EQUALS(t.back() , 23); + t.pop_front(); + TS_ASSERT(check_values(t, 2, 3, 23)); + TS_ASSERT_EQUALS(t.back() , 23); + t.pop_front(); + TS_ASSERT(check_values(t, 1, 23)); + TS_ASSERT_EQUALS(t.back() , 23); + t.pop_front(); + TS_ASSERT(check_values(t, 0)); + t.push_back(42); + TS_ASSERT(check_values(t, 1, 42)); + TS_ASSERT_EQUALS(t.back() , 42); + } + + void testEraseAfter() + { + int const full_ary[] = { 1, 2, 3, 4 }; + int const exp_ary[] = { 1, 3, 4 }; + ListContainer full_list(ARRAY_RANGE(full_ary)); + ListContainer exp_list(ARRAY_RANGE(exp_ary)); + TS_ASSERT(full_list != exp_list); + full_list.erase_after(full_list.begin()); + TS_ASSERT(full_list == exp_list); + } +}; + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : -- 2.30.2