2 #include <cxxtest/TestSuite.h>
3 #include <cassert>
5 #include "svg/svg-color.h"
6 #include "svg/svg-icc-color.h"
8 class SVGColorTest : public CxxTest::TestSuite
9 {
10 struct simpleIccCase {
11 int numEntries;
12 bool shouldPass;
13 char const* name;
14 char const* str;
15 };
17 public:
18 void check_rgb24(unsigned const rgb24)
19 {
20 char css[8];
21 sp_svg_write_color(css, sizeof(css), rgb24 << 8);
22 TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
23 rgb24 << 8);
24 }
26 void testWrite()
27 {
28 unsigned const components[] = {0, 0x80, 0xff, 0xc0, 0x77};
29 unsigned const nc = G_N_ELEMENTS(components);
30 for (unsigned i = nc*nc*nc; i--;) {
31 unsigned tmp = i;
32 unsigned rgb24 = 0;
33 for (unsigned c = 0; c < 3; ++c) {
34 unsigned const component = components[tmp % nc];
35 rgb24 = (rgb24 << 8) | component;
36 tmp /= nc;
37 }
38 assert( tmp == 0 );
39 check_rgb24(rgb24);
40 }
42 /* And a few completely random ones. */
43 for (unsigned i = 500; i--;) { /* Arbitrary number of iterations. */
44 unsigned const rgb24 = (rand() >> 4) & 0xffffff;
45 check_rgb24(rgb24);
46 }
47 }
49 void testReadColor()
50 {
51 gchar const* val="#f0f";
52 gchar const* end = 0;
53 guint32 result = sp_svg_read_color( val, &end, 0x3 );
54 TS_ASSERT_EQUALS( result, 0xff00ff00 );
55 TS_ASSERT_LESS_THAN( val, end );
56 }
58 void testIccColor()
59 {
60 simpleIccCase cases[] = {
61 {1, true, "named", "icc-color(named, 3)"},
62 {0, false, "", "foodle"},
63 {1, true, "a", "icc-color(a, 3)"},
64 {4, true, "named", "icc-color(named, 3, 0, 0.1, 2.5)"},
65 {0, false, "", "icc-color(named, 3"},
66 {0, false, "", "icc-color(space named, 3)"},
67 {0, false, "", "icc-color(tab\tnamed, 3)"},
68 {0, false, "", "icc-color(0name, 3)"},
69 {0, false, "", "icc-color(-name, 3)"},
70 {1, true, "positive", "icc-color(positive, +3)"},
71 {1, true, "negative", "icc-color(negative, -3)"},
72 {1, true, "positive", "icc-color(positive, +0.1)"},
73 {1, true, "negative", "icc-color(negative, -0.1)"},
74 {0, false, "", "icc-color(named, value)"},
75 };
77 for ( size_t i = 0; i < G_N_ELEMENTS(cases); i++ ) {
78 SVGICCColor tmp;
79 gchar const* str = cases[i].str;
80 gchar const* result = 0;
82 std::string testDescr( cases[i].str );
84 bool parseRet = sp_svg_read_icc_color( str, &result, &tmp );
85 TSM_ASSERT_EQUALS( testDescr, parseRet, cases[i].shouldPass );
86 TSM_ASSERT_EQUALS( testDescr, tmp.colors.size(), cases[i].numEntries );
87 if ( cases[i].shouldPass ) {
88 TSM_ASSERT_DIFFERS( testDescr, str, result );
89 TSM_ASSERT_EQUALS( testDescr, tmp.colorProfile, std::string(cases[i].name) );
90 } else {
91 TSM_ASSERT_EQUALS( testDescr, str, result );
92 TSM_ASSERT( testDescr, tmp.colorProfile.empty() );
93 }
94 }
95 }
97 };
99 /*
100 Local Variables:
101 mode:c++
102 c-file-style:"stroustrup"
103 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
104 indent-tabs-mode:nil
105 fill-column:99
106 End:
107 */
108 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :