diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/display/bezier-utils-test.h | 347 | ||||
| -rw-r--r-- | src/helper/units-test.h | 88 | ||||
| -rw-r--r-- | src/xml/quote-test.h | 59 | ||||
| -rw-r--r-- | src/xml/repr-action-test.h | 74 |
4 files changed, 566 insertions, 2 deletions
diff --git a/src/display/bezier-utils-test.h b/src/display/bezier-utils-test.h new file mode 100644 index 000000000..efed5ea4b --- /dev/null +++ b/src/display/bezier-utils-test.h @@ -0,0 +1,347 @@ +#include <cxxtest/TestSuite.h>
+
+#include <glib.h>
+#include <libnr/nr-macros.h> /* NR_DF_TEST_CLOSE */
+#include <sstream>
+
+/* mental disclaims all responsibility for this evil idea for testing
+ static functions. The main disadvantages are that we retain the
+ #define's and `using' directives of the included file. */
+#include "bezier-utils.cpp"
+
+using NR::Point;
+
+/* (Returns false if NaN encountered.) */
+static bool range_approx_equal(double const a[], double const b[], unsigned const len) {
+ for (unsigned i = 0; i < len; ++i) {
+ if (!( fabs( a[i] - b[i] ) < 1e-4 )) {
+ return false;
+ }
+ }
+ return true;
+}
+
+static inline bool point_approx_equal(NR::Point const &a, NR::Point const &b, double const eps)
+{
+ using NR::X; using NR::Y;
+ return ( NR_DF_TEST_CLOSE(a[X], b[X], eps) &&
+ NR_DF_TEST_CLOSE(a[Y], b[Y], eps) );
+}
+
+static inline double square(double const x) {
+ return x * x;
+}
+
+/** Determine whether the found control points are the same as previously found on some developer's
+ machine. Doesn't call utest__fail, just writes a message to stdout for diagnostic purposes:
+ the most important test is that the root-mean-square of errors in the estimation are low rather
+ than that the control points found are the same.
+**/
+static void compare_ctlpts(Point const est_b[], Point const exp_est_b[])
+{
+ unsigned diff_mask = 0;
+ for (unsigned i = 0; i < 4; ++i) {
+ for (unsigned d = 0; d < 2; ++d) {
+ if ( fabs( est_b[i][d] - exp_est_b[i][d] ) > 1.1e-5 ) {
+ diff_mask |= 1 << ( i * 2 + d );
+ }
+ }
+ }
+ if ( diff_mask != 0 ) {
+ std::stringstream msg;
+ msg << "Got different control points from previously-coded (diffs=0x" << std::hex << diff_mask << "\n";
+ msg << " Previous:";
+ for (unsigned i = 0; i < 4; ++i) {
+ msg << " (" << exp_est_b[i][0] << ", " << exp_est_b[i][1] << ")"; // localizing ok
+ }
+ msg << "\n";
+ msg << " Found: ";
+ for (unsigned i = 0; i < 4; ++i) {
+ msg << " (" << est_b[i][0] << ", " << est_b[i][1] << ")"; // localizing ok
+ }
+ msg << "\n";
+ TS_WARN(msg.str().c_str());
+ }
+}
+
+static void compare_rms(Point const est_b[], double const t[], Point const d[], unsigned const n,
+ double const exp_rms_error)
+{
+ double sum_errsq = 0.0;
+ for (unsigned i = 0; i < n; ++i) {
+ Point const fit_pt = bezier_pt(3, est_b, t[i]);
+ Point const diff = fit_pt - d[i];
+ sum_errsq += dot(diff, diff);
+ }
+ double const rms_error = sqrt( sum_errsq / n );
+ TS_ASSERT_LESS_THAN_EQUALS( rms_error , exp_rms_error + 1.1e-6 );
+ if ( rms_error < exp_rms_error - 1.1e-6 ) {
+ /* The fitter code appears to have improved [or the floating point calculations differ
+ on this machine from the machine where exp_rms_error was calculated]. */
+ char msg[200];
+ sprintf(msg, "N.B. rms_error regression requirement can be decreased: have rms_error=%g.", rms_error); // localizing ok
+ TS_TRACE(msg);
+ }
+}
+
+class BezierUtilsTest : public CxxTest::TestSuite {
+public:
+ static Point const c[4];
+ static double const t[24];
+ static unsigned const n;
+ Point d[24];
+ static Point const src_b[4];
+ static Point const tHat1;
+ static Point const tHat2;
+
+ BezierUtilsTest()
+ {
+ /* Feed it some points that can be fit exactly with a single bezier segment, and see how
+ well it manages. */
+ for (unsigned i = 0; i < n; ++i) {
+ d[i] = bezier_pt(3, src_b, t[i]);
+ }
+ }
+ virtual ~BezierUtilsTest() {}
+
+ void testCopyWithoutNansOrAdjacentDuplicates()
+ {
+ NR::Point const src[] = {
+ Point(2., 3.),
+ Point(2., 3.),
+ Point(0., 0.),
+ Point(2., 3.),
+ Point(2., 3.),
+ Point(1., 9.),
+ Point(1., 9.)
+ };
+ Point const exp_dest[] = {
+ Point(2., 3.),
+ Point(0., 0.),
+ Point(2., 3.),
+ Point(1., 9.)
+ };
+ g_assert( G_N_ELEMENTS(src) == 7 );
+ Point dest[7];
+ struct tst {
+ unsigned src_ix0;
+ unsigned src_len;
+ unsigned exp_dest_ix0;
+ unsigned exp_dest_len;
+ } const test_data[] = {
+ /* src start ix, src len, exp_dest start ix, exp dest len */
+ {0, 0, 0, 0},
+ {2, 1, 1, 1},
+ {0, 1, 0, 1},
+ {0, 2, 0, 1},
+ {0, 3, 0, 2},
+ {1, 3, 0, 3},
+ {0, 5, 0, 3},
+ {0, 6, 0, 4},
+ {0, 7, 0, 4}
+ };
+ for (unsigned i = 0 ; i < G_N_ELEMENTS(test_data) ; ++i) {
+ tst const &t = test_data[i];
+ TS_ASSERT_EQUALS( t.exp_dest_len,
+ copy_without_nans_or_adjacent_duplicates(src + t.src_ix0,
+ t.src_len,
+ dest) );
+ TS_ASSERT_SAME_DATA(dest,
+ exp_dest + t.exp_dest_ix0,
+ t.exp_dest_len);
+ }
+ }
+
+ void testBezierPt1()
+ {
+ Point const a[] = {Point(2.0, 4.0),
+ Point(1.0, 8.0)};
+ TS_ASSERT_EQUALS( bezier_pt(1, a, 0.0) , a[0] );
+ TS_ASSERT_EQUALS( bezier_pt(1, a, 1.0) , a[1] );
+ TS_ASSERT_EQUALS( bezier_pt(1, a, 0.5) , Point(1.5, 6.0) );
+ double const t[] = {0.5, 0.25, 0.3, 0.6};
+ for (unsigned i = 0; i < G_N_ELEMENTS(t); ++i) {
+ double const ti = t[i], si = 1.0 - ti;
+ TS_ASSERT_EQUALS( bezier_pt(1, a, ti) , si * a[0] + ti * a[1] );
+ }
+ }
+
+ void testBezierPt2()
+ {
+ Point const b[] = {Point(1.0, 2.0),
+ Point(8.0, 4.0),
+ Point(3.0, 1.0)};
+ TS_ASSERT_EQUALS( bezier_pt(2, b, 0.0) , b[0] );
+ TS_ASSERT_EQUALS( bezier_pt(2, b, 1.0) , b[2] );
+ TS_ASSERT_EQUALS( bezier_pt(2, b, 0.5) , Point(5.0, 2.75) );
+ double const t[] = {0.5, 0.25, 0.3, 0.6};
+ for (unsigned i = 0; i < G_N_ELEMENTS(t); ++i) {
+ double const ti = t[i], si = 1.0 - ti;
+ Point const exp_pt( si*si * b[0] + 2*si*ti * b[1] + ti*ti * b[2] );
+ Point const pt(bezier_pt(2, b, ti));
+ TS_ASSERT(point_approx_equal(pt, exp_pt, 1e-11));
+ }
+ }
+
+ void testBezierPt3()
+ {
+ TS_ASSERT_EQUALS( bezier_pt(3, c, 0.0) , c[0] );
+ TS_ASSERT_EQUALS( bezier_pt(3, c, 1.0) , c[3] );
+ TS_ASSERT_EQUALS( bezier_pt(3, c, 0.5) , Point(4.0, 13.0/8.0) );
+ double const t[] = {0.5, 0.25, 0.3, 0.6};
+ for (unsigned i = 0; i < G_N_ELEMENTS(t); ++i) {
+ double const ti = t[i], si = 1.0 - ti;
+ TS_ASSERT( LInfty( bezier_pt(3, c, ti)
+ - ( si*si*si * c[0] +
+ 3*si*si*ti * c[1] +
+ 3*si*ti*ti * c[2] +
+ ti*ti*ti * c[3] ) )
+ < 1e-4 );
+ }
+ }
+
+ void testComputeMaxErrorRatio()
+ {
+ struct Err_tst {
+ Point pt;
+ double u;
+ double err;
+ } const err_tst[] = {
+ {c[0], 0.0, 0.0},
+ {Point(4.0, 13.0/8.0), 0.5, 0.0},
+ {Point(4.0, 2.0), 0.5, 9.0/64.0},
+ {Point(3.0, 2.0), 0.5, 1.0 + 9.0/64.0},
+ {Point(6.0, 2.0), 0.5, 4.0 + 9.0/64.0},
+ {c[3], 1.0, 0.0},
+ };
+ Point d[G_N_ELEMENTS(err_tst)];
+ double u[G_N_ELEMENTS(err_tst)];
+ for (unsigned i = 0; i < G_N_ELEMENTS(err_tst); ++i) {
+ Err_tst const &t = err_tst[i];
+ d[i] = t.pt;
+ u[i] = t.u;
+ }
+ g_assert( G_N_ELEMENTS(u) == G_N_ELEMENTS(d) );
+ unsigned max_ix = ~0u;
+ double const err_ratio = compute_max_error_ratio(d, u, G_N_ELEMENTS(d), c, 1.0, &max_ix);
+ TS_ASSERT_LESS_THAN( fabs( sqrt(err_tst[4].err) - err_ratio ) , 1e-12 );
+ TS_ASSERT_EQUALS( max_ix , 4 );
+ }
+
+ void testChordLengthParameterize()
+ {
+ /* n == 2 */
+ {
+ Point const d[] = {Point(2.9415, -5.8149),
+ Point(23.021, 4.9814)};
+ double u[G_N_ELEMENTS(d)];
+ double const exp_u[] = {0.0, 1.0};
+ g_assert( G_N_ELEMENTS(u) == G_N_ELEMENTS(exp_u) );
+ chord_length_parameterize(d, u, G_N_ELEMENTS(d));
+ TS_ASSERT_SAME_DATA(u, exp_u, G_N_ELEMENTS(exp_u));
+ }
+
+ /* Straight line. */
+ {
+ double const exp_u[] = {0.0, 0.1829, 0.2105, 0.2105, 0.619, 0.815, 0.999, 1.0};
+ unsigned const n = G_N_ELEMENTS(exp_u);
+ Point d[n];
+ double u[n];
+ Point const a(-23.985, 4.915), b(4.9127, 5.203);
+ for (unsigned i = 0; i < n; ++i) {
+ double bi = exp_u[i], ai = 1.0 - bi;
+ d[i] = ai * a + bi * b;
+ }
+ chord_length_parameterize(d, u, n);
+ TS_ASSERT(range_approx_equal(u, exp_u, n));
+ }
+ }
+
+ void testGenerateBezier()
+ {
+ Point est_b[4];
+ generate_bezier(est_b, d, t, n, tHat1, tHat2, 1.0);
+
+ compare_ctlpts(est_b, src_b);
+
+ /* We're being unfair here in using our t[] rather than best t[] for est_b: we
+ may over-estimate RMS of errors. */
+ compare_rms(est_b, t, d, n, 1e-8);
+ }
+
+ void testSpBezierFitCubicFull()
+ {
+ Point est_b[4];
+ int splitpoints[2];
+ gint const succ = sp_bezier_fit_cubic_full(est_b, splitpoints, d, n, tHat1, tHat2, square(1.2), 1);
+ TS_ASSERT_EQUALS( succ , 1 );
+
+ Point const exp_est_b[4] = {
+ Point(5.000000, -3.000000),
+ Point(7.5753, -0.4247),
+ Point(4.77533, 1.22467),
+ Point(3, 3)
+ };
+ compare_ctlpts(est_b, exp_est_b);
+
+ /* We're being unfair here in using our t[] rather than best t[] for est_b: we
+ may over-estimate RMS of errors. */
+ compare_rms(est_b, t, d, n, .307911);
+ }
+
+ void testSpBezierFitCubic()
+ {
+ Point est_b[4];
+ gint const succ = sp_bezier_fit_cubic(est_b, d, n, square(1.2));
+ TS_ASSERT_EQUALS( succ , 1 );
+
+ Point const exp_est_b[4] = {
+ Point(5.000000, -3.000000),
+ Point(7.57134, -0.423509),
+ Point(4.77929, 1.22426),
+ Point(3, 3)
+ };
+ compare_ctlpts(est_b, exp_est_b);
+
+#if 1 /* A change has been made to right_tangent. I believe that usually this change
+ will result in better fitting, but it won't do as well for this example where
+ we happen to be feeding a t=0.999 point to the fitter. */
+ TS_WARN("TODO: Update this test case for revised right_tangent implementation.");
+ /* In particular, have a test case to show whether the new implementation
+ really is likely to be better on average. */
+#else
+ /* We're being unfair here in using our t[] rather than best t[] for est_b: we
+ may over-estimate RMS of errors. */
+ compare_rms(est_b, t, d, n, .307983);
+#endif
+ }
+};
+
+// This is not very neat, but since we know this header is only included by the generated CxxTest file it shouldn't give any problems
+Point const BezierUtilsTest::c[4] = {
+ Point(1.0, 2.0),
+ Point(8.0, 4.0),
+ Point(3.0, 1.0),
+ Point(-2.0, -4.0)};
+double const BezierUtilsTest::t[24] = {
+ 0.0, .001, .03, .05, .09, .13, .18, .25, .29, .33, .39, .44,
+ .51, .57, .62, .69, .75, .81, .91, .93, .97, .98, .999, 1.0};
+unsigned const BezierUtilsTest::n = G_N_ELEMENTS(BezierUtilsTest::t);
+Point const BezierUtilsTest::src_b[4] = {
+ Point(5., -3.),
+ Point(8., 0.),
+ Point(4., 2.),
+ Point(3., 3.)};
+Point const BezierUtilsTest::tHat1(unit_vector( BezierUtilsTest::src_b[1] - BezierUtilsTest::src_b[0] ));
+Point const BezierUtilsTest::tHat2(unit_vector( BezierUtilsTest::src_b[2] - BezierUtilsTest::src_b[3] ));
+
+/*
+ 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/helper/units-test.h b/src/helper/units-test.h new file mode 100644 index 000000000..f704b988a --- /dev/null +++ b/src/helper/units-test.h @@ -0,0 +1,88 @@ +#include <cxxtest/TestSuite.h>
+
+#include <helper/units.h>
+#include <glibmm/i18n.h>
+#include <math.h>
+
+
+/* N.B. Wrongly returns false if both near 0. (Not a problem for current users.) */
+static bool
+approx_equal(double const x, double const y)
+{
+ return fabs(x / y - 1) < 1e-15;
+}
+
+static double
+sp_units_get_points(double const x, SPUnit const &unit)
+{
+ SPUnit const &pt_unit = sp_unit_get_by_id(SP_UNIT_PT);
+ double const px = sp_units_get_pixels(x, unit);
+ return sp_pixels_get_units(px, pt_unit);
+}
+
+static double
+sp_points_get_units(double const pts, SPUnit const &unit)
+{
+ SPUnit const &pt_unit = sp_unit_get_by_id(SP_UNIT_PT);
+ double const px = sp_units_get_pixels(pts, pt_unit);
+ return sp_pixels_get_units(px, unit);
+}
+
+class UnitsTest : public CxxTest::TestSuite {
+public:
+
+ UnitsTest()
+ {
+ }
+ virtual ~UnitsTest() {}
+
+ void testConversions()
+ {
+ struct Case { double x; char const *abbr; double pts; } const tests[] = {
+ { 1.0, "pt", 1.0 },
+ { 5.0, "pt", 5.0 },
+ { 1.0, "in", 72.0 },
+ { 2.0, "in", 144.0 },
+ { 254., "mm", 720.0 },
+ { 254., "cm", 7200. },
+ { 254., "m", 720000. },
+ { 1.5, "mm", (15 * 72. / 254) }
+ };
+ for (unsigned i = 0; i < G_N_ELEMENTS(tests); ++i) {
+ Case const &c = tests[i];
+ SPUnit const &unit = *sp_unit_get_by_abbreviation(N_(c.abbr));
+
+ double const calc_pts = sp_units_get_points(c.x, unit);
+ TS_ASSERT(approx_equal(calc_pts, c.pts));
+
+ double const calc_x = sp_points_get_units(c.pts, unit);
+ TS_ASSERT(approx_equal(calc_x, c.x));
+
+ double tmp = c.x;
+ bool const converted_to_pts = sp_convert_distance(&tmp, &unit, SP_PS_UNIT);
+ TS_ASSERT(converted_to_pts);
+ TS_ASSERT(approx_equal(tmp, c.pts));
+
+ tmp = c.pts;
+ bool const converted_from_pts = sp_convert_distance(&tmp, SP_PS_UNIT, &unit);
+ TS_ASSERT(converted_from_pts);
+ TS_ASSERT(approx_equal(tmp, c.x));
+ }
+ }
+
+ void testUnitTable()
+ {
+ TS_ASSERT(sp_units_table_sane());
+ }
+};
+
+/*
+ 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/xml/quote-test.h b/src/xml/quote-test.h index 39f7e6167..7f5372844 100644 --- a/src/xml/quote-test.h +++ b/src/xml/quote-test.h @@ -1,5 +1,25 @@ #include <cxxtest/TestSuite.h> +/* Initial author: Peter Moulder. + Hereby released into the Public Domain. */ + +#include <cstring> +#include <functional> + +/* mental disclaims all responsibility for this evil idea for testing + static functions. The main disadvantages are that we retain any + #define's and `using' directives of the included file. */ +#include "quote.cpp" + +struct streq_free2 { + bool operator()(char const *exp, char *got) const + { + bool const ret = (strcmp(exp, got) == 0); + g_free(got); + return ret; + } +}; + class XmlQuoteTest : public CxxTest::TestSuite { public: @@ -14,8 +34,45 @@ public: static XmlQuoteTest *createSuite() { return new XmlQuoteTest(); } static void destroySuite( XmlQuoteTest *suite ) { delete suite; } - void testFoo() + void testXmlQuotedStrlen() + { + struct { + char const *s; + size_t len; + } cases[] = { + {"", 0}, + {"x", 1}, + {"Foo", 3}, + {"\"", 6}, + {"&", 5}, + {"<", 4}, + {">", 4}, + {"a\"b", 8}, + {"a\"b<c>d;!@#$%^*(\\)?", 30} + }; + for(size_t i=0; i<G_N_ELEMENTS(cases); i++) { + TS_ASSERT_EQUALS( xml_quoted_strlen(cases[i].s) , cases[i].len ); + } + } + + void testXmlQuoteStrdup() { + struct { + char const * s1; + char const * s2; + } cases[] = { + {"", ""}, + {"x", "x"}, + {"Foo", "Foo"}, + {"\"", """}, + {"&", "&"}, + {"<", "<"}, + {">", ">"}, + {"a\"b<c>d;!@#$%^*(\\)?", "a"b<c>d;!@#$%^*(\\)?"} + }; + for(size_t i=0; i<G_N_ELEMENTS(cases); i++) { + TS_ASSERT_RELATION( streq_free2, cases[i].s2, xml_quote_strdup(cases[i].s1) ); + } } }; diff --git a/src/xml/repr-action-test.h b/src/xml/repr-action-test.h index 10f7d52f1..8fe1327af 100644 --- a/src/xml/repr-action-test.h +++ b/src/xml/repr-action-test.h @@ -1,11 +1,30 @@ #include <cxxtest/TestSuite.h> +#include <cstdlib> +#include <glib.h> + +#include "repr.h" +#include "event-fns.h" + +static void * const null_ptr = 0; + class XmlReprActionTest : public CxxTest::TestSuite { + Inkscape::XML::Document *document; + Inkscape::XML::Node *a, *b, *c, *root; + public: XmlReprActionTest() { + Inkscape::GC::init(); + + document = sp_repr_document_new("test"); + root = document->root(); + + a = document->createElement("a"); + b = document->createElement("b"); + c = document->createElement("c"); } virtual ~XmlReprActionTest() {} @@ -14,9 +33,62 @@ public: static XmlReprActionTest *createSuite() { return new XmlReprActionTest(); } static void destroySuite( XmlReprActionTest *suite ) { delete suite; } - void testFoo() + void testRollbackOfNodeAddition() + { + sp_repr_begin_transaction(document); + TS_ASSERT_EQUALS(sp_repr_parent(a) , null_ptr); + + root->appendChild(a); + TS_ASSERT_EQUALS(sp_repr_parent(a) , root); + + sp_repr_rollback(document); + TS_ASSERT_EQUALS(sp_repr_parent(a) , null_ptr); + } + + void testRollbackOfNodeRemoval() + { + root->appendChild(a); + + sp_repr_begin_transaction(document); + TS_ASSERT_EQUALS(sp_repr_parent(a) , root); + + sp_repr_unparent(a); + TS_ASSERT_EQUALS(sp_repr_parent(a) , null_ptr); + + sp_repr_rollback(document); + TS_ASSERT_EQUALS(sp_repr_parent(a) , root); + + sp_repr_unparent(a); + } + + void testRollbackOfNodeReordering() { + root->appendChild(a); + root->appendChild(b); + root->appendChild(c); + + sp_repr_begin_transaction(document); + TS_ASSERT_EQUALS(sp_repr_next(a) , b); + TS_ASSERT_EQUALS(sp_repr_next(b) , c); + TS_ASSERT_EQUALS(sp_repr_next(c) , null_ptr); + + root->changeOrder(b, c); + TS_ASSERT_EQUALS(sp_repr_next(a) , c); + TS_ASSERT_EQUALS(sp_repr_next(b) , null_ptr); + TS_ASSERT_EQUALS(sp_repr_next(c) , b); + + sp_repr_rollback(document); + TS_ASSERT_EQUALS(sp_repr_next(a) , b); + TS_ASSERT_EQUALS(sp_repr_next(b) , c); + TS_ASSERT_EQUALS(sp_repr_next(c) , null_ptr); + + sp_repr_unparent(a); + sp_repr_unparent(b); + sp_repr_unparent(c); } + + /* lots more tests needed ... */ + }; /* |
