~valavanisalex/ubuntu/precise/inkscape/fix-943984

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/svg/svg-color-test.h

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cxxtest/TestSuite.h>
 
2
#include <cassert>
 
3
#include <cstdlib>
 
4
 
 
5
#include "preferences.h"
 
6
#include "svg/svg-color.h"
 
7
#include "svg/svg-icc-color.h"
 
8
 
 
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
    };
 
17
 
 
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
    }
 
32
 
 
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; }
 
37
 
 
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
        }
 
53
 
 
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
    }
 
60
 
 
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
    }
 
72
 
 
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
        };
 
91
 
 
92
        for ( size_t i = 0; i < G_N_ELEMENTS(cases); i++ ) {
 
93
            SVGICCColor tmp;
 
94
            gchar const* str = cases[i].str;
 
95
            gchar const* result = 0;
 
96
 
 
97
            std::string testDescr( cases[i].str );
 
98
 
 
99
            bool parseRet = sp_svg_read_icc_color( str, &result, &tmp );
 
100
            TSM_ASSERT_EQUALS( testDescr, parseRet, cases[i].shouldPass );
 
101
            TSM_ASSERT_EQUALS( testDescr, tmp.colors.size(), cases[i].numEntries );
 
102
            if ( cases[i].shouldPass ) {
 
103
                TSM_ASSERT_DIFFERS( testDescr, str, result );
 
104
                TSM_ASSERT_EQUALS( testDescr, tmp.colorProfile, std::string(cases[i].name) );
 
105
            } else {
 
106
                TSM_ASSERT_EQUALS( testDescr, str, result );
 
107
                TSM_ASSERT( testDescr, tmp.colorProfile.empty() );
 
108
            }
 
109
        }
 
110
    }
 
111
 
 
112
};
 
113
 
 
114
/*
 
115
  Local Variables:
 
116
  mode:c++
 
117
  c-file-style:"stroustrup"
 
118
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
119
  indent-tabs-mode:nil
 
120
  fill-column:99
 
121
  End:
 
122
*/
 
123
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :