Code

Fix version check for wheel selector.
[inkscape.git] / src / svg / svg-color-test.h
1 #include <cxxtest/TestSuite.h>
2 #include <cassert>
3 #include <cstdlib>
5 #include "preferences.h"
6 #include "svg/svg-color.h"
7 #include "svg/svg-icc-color.h"
9 class SVGColorTest : public CxxTest::TestSuite
10 {
11     struct simpleIccCase {
12         unsigned numEntries;
13         bool shouldPass;
14         char const* name;
15         char const* str;
16     };
18 public:
19     void check_rgb24(unsigned const rgb24)
20     {
21         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
22         char css[8];
23         prefs->setBool("/options/svgoutput/usenamedcolors", false);
24         sp_svg_write_color(css, sizeof(css), rgb24 << 8);
25         TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
26                          rgb24 << 8);
27         prefs->setBool("/options/svgoutput/usenamedcolors", true);
28         sp_svg_write_color(css, sizeof(css), rgb24 << 8);
29         TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
30                          rgb24 << 8);
31     }
33 // createSuite and destroySuite get us per-suite setup and teardown
34 // without us having to worry about static initialization order, etc.
35     static SVGColorTest *createSuite() { return new SVGColorTest(); }
36     static void destroySuite( SVGColorTest *suite ) { delete suite; }
38     void testWrite()
39     {
40         unsigned const components[] = {0, 0x80, 0xff, 0xc0, 0x77};
41         unsigned const nc = G_N_ELEMENTS(components);
42         for (unsigned i = nc*nc*nc; i--;) {
43             unsigned tmp = i;
44             unsigned rgb24 = 0;
45             for (unsigned c = 0; c < 3; ++c) {
46                 unsigned const component = components[tmp % nc];
47                 rgb24 = (rgb24 << 8) | component;
48                 tmp /= nc;
49             }
50             assert( tmp == 0 );
51             check_rgb24(rgb24);
52         }
54         /* And a few completely random ones. */
55         for (unsigned i = 500; i--;) {  /* Arbitrary number of iterations. */
56             unsigned const rgb24 = (std::rand() >> 4) & 0xffffff;
57             check_rgb24(rgb24);
58         }
59     }
61     void testReadColor()
62     {
63         gchar const* val[] = {"#f0f", "#ff00ff", "rgb(255,0,255)", "fuchsia"};
64         size_t const n = sizeof(val)/sizeof(*val);
65         for(size_t i=0; i<n; i++) {
66             gchar const* end = 0;
67             guint32 result = sp_svg_read_color( val[i], &end, 0x3 );
68             TS_ASSERT_EQUALS( result, 0xff00ff00 );
69             TS_ASSERT_LESS_THAN( val[i], end );
70         }
71     }
73     void testIccColor()
74     {
75         simpleIccCase cases[] = {
76             {1, true, "named", "icc-color(named, 3)"},
77             {0, false, "", "foodle"},
78             {1, true, "a", "icc-color(a, 3)"},
79             {4, true, "named", "icc-color(named, 3, 0, 0.1, 2.5)"},
80             {0, false, "", "icc-color(named, 3"},
81             {0, false, "", "icc-color(space named, 3)"},
82             {0, false, "", "icc-color(tab\tnamed, 3)"},
83             {0, false, "", "icc-color(0name, 3)"},
84             {0, false, "", "icc-color(-name, 3)"},
85             {1, true, "positive", "icc-color(positive, +3)"},
86             {1, true, "negative", "icc-color(negative, -3)"},
87             {1, true, "positive", "icc-color(positive, +0.1)"},
88             {1, true, "negative", "icc-color(negative, -0.1)"},
89             {0, false, "", "icc-color(named, value)"},
90             {1, true, "hyphen-name", "icc-color(hyphen-name, 1)"},
91             {1, true, "under_name", "icc-color(under_name, 1)"},
92         };
94         for ( size_t i = 0; i < G_N_ELEMENTS(cases); i++ ) {
95             SVGICCColor tmp;
96             gchar const* str = cases[i].str;
97             gchar const* result = 0;
99             std::string testDescr( cases[i].str );
101             bool parseRet = sp_svg_read_icc_color( str, &result, &tmp );
102             TSM_ASSERT_EQUALS( testDescr, parseRet, cases[i].shouldPass );
103             TSM_ASSERT_EQUALS( testDescr, tmp.colors.size(), cases[i].numEntries );
104             if ( cases[i].shouldPass ) {
105                 TSM_ASSERT_DIFFERS( testDescr, str, result );
106                 TSM_ASSERT_EQUALS( testDescr, tmp.colorProfile, std::string(cases[i].name) );
107             } else {
108                 TSM_ASSERT_EQUALS( testDescr, str, result );
109                 TSM_ASSERT( testDescr, tmp.colorProfile.empty() );
110             }
111         }
112     }
114 };
116 /*
117   Local Variables:
118   mode:c++
119   c-file-style:"stroustrup"
120   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
121   indent-tabs-mode:nil
122   fill-column:99
123   End:
124 */
125 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :