Code

Switched pen and pencil toobars to stock GTK+ toolbars
[inkscape.git] / src / helper / units-test.cpp
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 #include <math.h>
6 #include <glibmm/i18n.h>
7 #include <helper/units.h>
8 #include <utest/utest.h>
11 /* N.B. Wrongly returns false if both near 0.  (Not a problem for current users.) */
12 static bool
13 approx_equal(double const x, double const y)
14 {
15     return fabs(x / y - 1) < 1e-15;
16 }
18 static double
19 sp_units_get_points(double const x, SPUnit const &unit)
20 {
21     SPUnit const &pt_unit = sp_unit_get_by_id(SP_UNIT_PT);
22     double const px = sp_units_get_pixels(x, unit);
23     return sp_pixels_get_units(px, pt_unit);
24 }
26 static double
27 sp_points_get_units(double const pts, SPUnit const &unit)
28 {
29     SPUnit const &pt_unit = sp_unit_get_by_id(SP_UNIT_PT);
30     double const px = sp_units_get_pixels(pts, pt_unit);
31     return sp_pixels_get_units(px, unit);
32 }
34 static bool
35 test_conversions()
36 {
37     utest_start("sp_units_get_pixels, sp_pixels_get_units");
39     struct Case { double x; char const *abbr; double pts; } const tests[] = {
40         { 1.0, "pt", 1.0 },
41         { 5.0, "pt", 5.0 },
42         { 1.0, "in", 72.0 },
43         { 2.0, "in", 144.0 },
44         { 254., "mm", 720.0 },
45         { 254., "cm", 7200. },
46         { 254., "m", 720000. },
47         { 1.5, "mm", (15 * 72. / 254) }
48     };
49     for (unsigned i = 0; i < G_N_ELEMENTS(tests); ++i) {
50         char name[80];
51         Case const &c = tests[i];
52         SPUnit const &unit = *sp_unit_get_by_abbreviation(N_(c.abbr));
54         double const calc_pts = sp_units_get_points(c.x, unit);
55         snprintf(name, sizeof(name), "%.1f %s -> %.1f pt", c.x, c.abbr, c.pts);
56         UTEST_TEST(name) {
57             UTEST_ASSERT(approx_equal(calc_pts, c.pts));
58         }
60         double const calc_x = sp_points_get_units(c.pts, unit);
61         snprintf(name, sizeof(name), "%.1f pt -> %.1f %s", c.pts, c.x, c.abbr);
62         UTEST_TEST(name) {
63             UTEST_ASSERT(approx_equal(calc_x, c.x));
64         }
66         double tmp = c.x;
67         bool const converted_to_pts = sp_convert_distance(&tmp, &unit, SP_PS_UNIT);
68         snprintf(name, sizeof(name), "convert %.1f %s -> %.1f pt", c.x, c.abbr, c.pts);
69         UTEST_TEST(name) {
70             UTEST_ASSERT(converted_to_pts);
71             UTEST_ASSERT(approx_equal(tmp, c.pts));
72         }
74         tmp = c.pts;
75         bool const converted_from_pts = sp_convert_distance(&tmp, SP_PS_UNIT, &unit);
76         snprintf(name, sizeof(name), "convert %.1f pt -> %.1f %s", c.pts, c.x, c.abbr);
77         UTEST_TEST(name) {
78             UTEST_ASSERT(converted_from_pts);
79             UTEST_ASSERT(approx_equal(tmp, c.x));
80         }
81     }
82     return utest_end();
83 }
85 static bool
86 test_unit_table()
87 {
88     utest_start("unit table");
89     UTEST_TEST("sp_units_table_sane") {
90         UTEST_ASSERT(sp_units_table_sane());
91     }
92     return utest_end();
93 }
95 int
96 main(int argc, char *argv[])
97 {
98     int const ret = ( ( test_conversions()
99                         && test_unit_table() )
100                       ? EXIT_SUCCESS
101                       : EXIT_FAILURE );
102     return ret;
106 /*
107   Local Variables:
108   mode:c++
109   c-file-style:"stroustrup"
110   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
111   indent-tabs-mode:nil
112   fill-column:99
113   End:
114 */
115 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :