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.) */
13
approx_equal(double const x, double const y)
15
return fabs(x / y - 1) < 1e-15;
19
sp_units_get_points(double const x, SPUnit const &unit)
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);
27
sp_points_get_units(double const pts, SPUnit const &unit)
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);
37
utest_start("sp_units_get_pixels, sp_pixels_get_units");
39
struct Case { double x; char const *abbr; double pts; } const tests[] = {
44
{ 254., "mm", 720.0 },
45
{ 254., "cm", 7200. },
46
{ 254., "m", 720000. },
47
{ 1.5, "mm", (15 * 72. / 254) }
49
for (unsigned i = 0; i < G_N_ELEMENTS(tests); ++i) {
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);
57
UTEST_ASSERT(approx_equal(calc_pts, c.pts));
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);
63
UTEST_ASSERT(approx_equal(calc_x, 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);
70
UTEST_ASSERT(converted_to_pts);
71
UTEST_ASSERT(approx_equal(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);
78
UTEST_ASSERT(converted_from_pts);
79
UTEST_ASSERT(approx_equal(tmp, c.x));
88
utest_start("unit table");
89
UTEST_TEST("sp_units_table_sane") {
90
UTEST_ASSERT(sp_units_table_sane());
96
main(int argc, char *argv[])
98
int const ret = ( ( test_conversions()
99
&& test_unit_table() )
109
c-file-style:"stroustrup"
110
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
115
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :