summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJasper van de Gronde <jasper.vandegronde@gmail.com>2008-07-03 13:20:00 +0000
committerjaspervdg <jaspervdg@users.sourceforge.net>2008-07-03 13:20:00 +0000
commit240e04d9c670a1c3898545845a60a91a512b8f70 (patch)
tree66c6f687080bb2e72ecd6c0212a8657d076d87ee /src
parentadded new LPE and remove obsolete entries (diff)
downloadinkscape-240e04d9c670a1c3898545845a60a91a512b8f70.tar.gz
inkscape-240e04d9c670a1c3898545845a60a91a512b8f70.zip
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.
(bzr r6132)
Diffstat (limited to 'src')
-rw-r--r--src/svg/svg-affine-test.h219
-rw-r--r--src/svg/svg-length-test.h46
-rw-r--r--src/util/list-container-test.h245
3 files changed, 510 insertions, 0 deletions
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 <cxxtest/TestSuite.h>
+
+#include "svg/svg.h"
+#include <2geom/matrix.h>
+#include <algorithm>
+#include <glib.h>
+#include <iostream>
+#include <math.h>
+#include <utility>
+
+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<n; i++) {
+ Geom::Matrix cm;
+ TSM_ASSERT(strs[i] , sp_svg_transform_read(strs[i], &cm));
+ TSM_ASSERT_EQUALS(strs[i] , Geom::identity() , cm);
+ }
+ }
+
+ void testWriteIdentity()
+ {
+ TS_ASSERT_EQUALS(sp_svg_transform_write(Geom::identity()) , (void*)0)
+ }
+
+ void testReadMatrix()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(read_matrix_tests); i++) {
+ Geom::Matrix cm;
+ TSM_ASSERT(read_matrix_tests[i].str , sp_svg_transform_read(read_matrix_tests[i].str, &cm));
+ TSM_ASSERT_RELATION(read_matrix_tests[i].str , approx_equal , read_matrix_tests[i].matrix , cm);
+ }
+ }
+
+ void testReadTranslate()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(read_translate_tests); i++) {
+ Geom::Matrix cm;
+ TSM_ASSERT(read_translate_tests[i].str , sp_svg_transform_read(read_translate_tests[i].str, &cm));
+ TSM_ASSERT_RELATION(read_translate_tests[i].str , approx_equal , read_translate_tests[i].matrix , cm);
+ }
+ }
+
+ void testReadScale()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(read_scale_tests); i++) {
+ Geom::Matrix cm;
+ TSM_ASSERT(read_scale_tests[i].str , sp_svg_transform_read(read_scale_tests[i].str, &cm));
+ TSM_ASSERT_RELATION(read_scale_tests[i].str , approx_equal , read_scale_tests[i].matrix , cm);
+ }
+ }
+
+ void testReadRotate()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(read_rotate_tests); i++) {
+ Geom::Matrix cm;
+ TSM_ASSERT(read_rotate_tests[i].str , sp_svg_transform_read(read_rotate_tests[i].str, &cm));
+ TSM_ASSERT_RELATION(read_rotate_tests[i].str , approx_equal , read_rotate_tests[i].matrix , cm);
+ }
+ }
+
+ void testReadSkew()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(read_skew_tests); i++) {
+ Geom::Matrix cm;
+ TSM_ASSERT(read_skew_tests[i].str , sp_svg_transform_read(read_skew_tests[i].str, &cm));
+ TSM_ASSERT_RELATION(read_skew_tests[i].str , approx_equal , read_skew_tests[i].matrix , cm);
+ }
+ }
+
+ void testWriteMatrix()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(write_matrix_tests); i++) {
+ TS_ASSERT_RELATION(streq_free2 , sp_svg_transform_write(write_matrix_tests[i].matrix) , write_matrix_tests[i].str);
+ }
+ }
+
+ void testWriteTranslate()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(write_translate_tests); i++) {
+ TS_ASSERT_RELATION(streq_free2 , sp_svg_transform_write(write_translate_tests[i].matrix) , write_translate_tests[i].str);
+ }
+ }
+
+ void testWriteScale()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(write_scale_tests); i++) {
+ TS_ASSERT_RELATION(streq_free2 , sp_svg_transform_write(write_scale_tests[i].matrix) , write_scale_tests[i].str);
+ }
+ }
+
+ void testWriteRotate()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(write_rotate_tests); i++) {
+ TS_ASSERT_RELATION(streq_free2 , sp_svg_transform_write(write_rotate_tests[i].matrix) , write_rotate_tests[i].str);
+ }
+ }
+
+ void testWriteSkew()
+ {
+ for(size_t i=0; i<G_N_ELEMENTS(write_skew_tests); i++) {
+ TS_ASSERT_RELATION(streq_free2 , sp_svg_transform_write(write_skew_tests[i].matrix) , write_skew_tests[i].str);
+ }
+ }
+
+ void testReadConcatenation()
+ {
+ char const * str = "skewY(17)skewX(9)translate(7,13)scale(2)rotate(13)translate(3,5)";
+ Geom::Matrix ref(2.0199976232558053, 1.0674773585906016, -0.14125199392774669, 1.9055550612095459, 14.412730624347654, 28.499820929377454); // Precomputed using Mathematica
+ Geom::Matrix cm;
+ TS_ASSERT(sp_svg_transform_read(str, &cm));
+ TS_ASSERT_RELATION(approx_equal , ref , cm);
+ }
+
+ // TODO: Perhaps check faulty transforms (like "translate(1,2,3)", or "matrix(1,2,3,4,5)", or ...)
+};
+
+static double const DEGREE = M_PI/180.;
+
+SvgAffineTest::test_t const SvgAffineTest::read_matrix_tests[3] = {
+ {"matrix(0,0,0,0,0,0)",Geom::Matrix(0,0,0,0,0,0)},
+ {"matrix(1,2,3,4,5,6)",Geom::Matrix(1,2,3,4,5,6)},
+ {"matrix(1 2 -3,-4,5e6,-6e-7)",Geom::Matrix(1,2,-3,-4,5e6,-6e-7)}};
+SvgAffineTest::test_t const SvgAffineTest::read_translate_tests[3] = {
+ {"translate(1)",Geom::Matrix(1,0,0,1,1,0)},
+ {"translate(1,1)",Geom::Matrix(1,0,0,1,1,1)},
+ {"translate(-1e3 .123e2)",Geom::Matrix(1,0,0,1,-1e3,.123e2)}};
+SvgAffineTest::test_t const SvgAffineTest::read_scale_tests[3] = {
+ {"scale(2)",Geom::Matrix(2,0,0,2,0,0)},
+ {"scale(2,3)",Geom::Matrix(2,0,0,3,0,0)},
+ {"scale(0.1e-2 -.475e0)",Geom::Matrix(0.1e-2,0,0,-.475e0,0,0)}};
+SvgAffineTest::test_t const SvgAffineTest::read_rotate_tests[4] = {
+ {"rotate(13 )",Geom::Matrix(cos(13.*DEGREE),sin(13.*DEGREE),-sin(13.*DEGREE),cos(13.*DEGREE),0,0)},
+ {"rotate(-13)",Geom::Matrix(cos(-13.*DEGREE),sin(-13.*DEGREE),-sin(-13.*DEGREE),cos(-13.*DEGREE),0,0)},
+ {"rotate(373)",Geom::Matrix(cos(13.*DEGREE),sin(13.*DEGREE),-sin(13.*DEGREE),cos(13.*DEGREE),0,0)},
+ {"rotate(13,7,11)",Geom::Matrix(cos(13.*DEGREE),sin(13.*DEGREE),-sin(13.*DEGREE),cos(13.*DEGREE),(1-cos(13.*DEGREE))*7+sin(13.*DEGREE)*11,(1-cos(13.*DEGREE))*11-sin(13.*DEGREE)*7)}};
+SvgAffineTest::test_t const SvgAffineTest::read_skew_tests[3] = {
+ {"skewX( 30)",Geom::Matrix(1,0,tan(30.*DEGREE),1,0,0)},
+ {"skewX(-30)",Geom::Matrix(1,0,tan(-30.*DEGREE),1,0,0)},
+ {"skewY(390)",Geom::Matrix(1,tan(30.*DEGREE),0,1,0,0)}};
+
+SvgAffineTest::test_t const SvgAffineTest::write_matrix_tests[2] = {
+ {"matrix(1,2,3,4,5,6)",Geom::Matrix(1,2,3,4,5,6)},
+ {"matrix(-1,2123,3,0.4,1e-8,1e20)",Geom::Matrix(-1,2.123e3,3+1e-14,0.4,1e-8,1e20)}};
+SvgAffineTest::test_t const SvgAffineTest::write_translate_tests[3] = {
+ {"translate(1,1)",Geom::Matrix(1,0,0,1,1,1)},
+ {"translate(1)",Geom::Matrix(1,0,0,1,1,0)},
+ {"translate(-1345,0.123)",Geom::Matrix(1,0,0,1,-1.345e3,.123)}};
+SvgAffineTest::test_t const SvgAffineTest::write_scale_tests[2] = {
+ {"scale(0)",Geom::Matrix(0,0,0,0,0,0)},
+ {"scale(2,3)",Geom::Matrix(2,0,0,3,0,0)}};
+SvgAffineTest::test_t const SvgAffineTest::write_rotate_tests[2] = {
+ {"rotate(13)",Geom::Matrix(cos(13.*DEGREE),sin(13.*DEGREE),-sin(13.*DEGREE),cos(13.*DEGREE),0,0)},
+ {"rotate(-13,7,11)",Geom::Matrix(cos(-13.*DEGREE),sin(-13.*DEGREE),-sin(-13.*DEGREE),cos(-13.*DEGREE),(1-cos(-13.*DEGREE))*7+sin(-13.*DEGREE)*11,(1-cos(-13.*DEGREE))*11-sin(-13.*DEGREE)*7)}};
+SvgAffineTest::test_t const SvgAffineTest::write_skew_tests[3] = {
+ {"skewX(30)",Geom::Matrix(1,0,tan(30.*DEGREE),1,0,0)},
+ {"skewX(-30)",Geom::Matrix(1,0,tan(-30.*DEGREE),1,0,0)},
+ {"skewY(390)",Geom::Matrix(1,tan(30.*DEGREE),0,1,0,0)}};
+
+/*
+ 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 :
diff --git a/src/svg/svg-length-test.h b/src/svg/svg-length-test.h
new file mode 100644
index 000000000..0f628ae7c
--- /dev/null
+++ b/src/svg/svg-length-test.h
@@ -0,0 +1,46 @@
+#include <cxxtest/TestSuite.h>
+
+#include "svg/svg-length.h"
+#include <glib.h>
+#include <utility>
+
+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<n; i++) {
+ SVGLength l;
+ TSM_ASSERT(tests[i].str , l.read(tests[i].str));
+ TSM_ASSERT_EQUALS(tests[i].str , l.computed , tests[i].computed);
+ }
+ }
+
+ // TODO: More tests
+};
+
+/*
+ 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 :
diff --git a/src/util/list-container-test.h b/src/util/list-container-test.h
new file mode 100644
index 000000000..9d8507f65
--- /dev/null
+++ b/src/util/list-container-test.h
@@ -0,0 +1,245 @@
+#include <cxxtest/TestSuite.h>
+
+#include <stdarg.h>
+#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<int> const &c, unsigned n_values, ...) {
+ bool ret = true;
+ va_list args;
+ va_start(args, n_values);
+ ListContainer<int>::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<int> container(values, values_end);
+
+ ListContainer<int>::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<int> c_a(ARRAY_RANGE(a));
+ ListContainer<int> c_b(ARRAY_RANGE(b));
+ ListContainer<int> c_c(ARRAY_RANGE(c));
+ ListContainer<int> 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<int> c_a(ARRAY_RANGE(a));
+ ListContainer<int> c_b(ARRAY_RANGE(b));
+ ListContainer<int> c_c(ARRAY_RANGE(c));
+ ListContainer<int> 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<int> c_a(ARRAY_RANGE(a));
+ ListContainer<int> 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<int> filled((std::size_t)3, 2);
+ TS_ASSERT(check_values(filled, 3, 2, 2, 2));
+ }
+
+ void testContainerSize()
+ {
+ ListContainer<int> empty;
+ TS_ASSERT(empty.empty());
+ TS_ASSERT_EQUALS(empty.size() , 0);
+ int const a[] = { 1, 2, 3 };
+ ListContainer<int> 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<int> 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<int> c_a(ARRAY_RANGE(a));
+ ListContainer<int> 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<int> 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<int> 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<int>::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<int> c(ARRAY_RANGE(values));
+
+ c.erase(c.begin());
+ TS_ASSERT(check_values(c, 3, 2, 3, 4));
+
+ ListContainer<int>::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<int> 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<int> full_list(ARRAY_RANGE(full_ary));
+ ListContainer<int> 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 :