summaryrefslogtreecommitdiffstats
path: root/src/svg/svg-color-test.h
blob: b1723cb38439288a33efcec3a475f5ba9c2e2d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * TODO: insert short description here
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2010 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include <cxxtest/TestSuite.h>
#include <cassert>
#include <cstdlib>

#include "preferences.h"
#include "svg/svg-color.h"
#include "svg/svg-icc-color.h"

class SVGColorTest : public CxxTest::TestSuite
{
    struct simpleIccCase {
        unsigned numEntries;
        bool shouldPass;
        char const* name;
        char const* str;
    };

public:
    void check_rgb24(unsigned const rgb24)
    {
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
        char css[8];
        prefs->setBool("/options/svgoutput/usenamedcolors", false);
        sp_svg_write_color(css, sizeof(css), rgb24 << 8);
        TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
                         rgb24 << 8);
        prefs->setBool("/options/svgoutput/usenamedcolors", true);
        sp_svg_write_color(css, sizeof(css), rgb24 << 8);
        TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
                         rgb24 << 8);
    }

// createSuite and destroySuite get us per-suite setup and teardown
// without us having to worry about static initialization order, etc.
    static SVGColorTest *createSuite() { return new SVGColorTest(); }
    static void destroySuite( SVGColorTest *suite ) { delete suite; }

    void testWrite()
    {
        unsigned const components[] = {0, 0x80, 0xff, 0xc0, 0x77};
        unsigned const nc = G_N_ELEMENTS(components);
        for (unsigned i = nc*nc*nc; i--;) {
            unsigned tmp = i;
            unsigned rgb24 = 0;
            for (unsigned c = 0; c < 3; ++c) {
                unsigned const component = components[tmp % nc];
                rgb24 = (rgb24 << 8) | component;
                tmp /= nc;
            }
            assert( tmp == 0 );
            check_rgb24(rgb24);
        }

        /* And a few completely random ones. */
        for (unsigned i = 500; i--;) {  /* Arbitrary number of iterations. */
            unsigned const rgb24 = (std::rand() >> 4) & 0xffffff;
            check_rgb24(rgb24);
        }
    }

    void testReadColor()
    {
        gchar const* val[] = {"#f0f", "#ff00ff", "rgb(255,0,255)", "fuchsia"};
        size_t const n = sizeof(val)/sizeof(*val);
        for(size_t i=0; i<n; i++) {
            gchar const* end = 0;
            guint32 result = sp_svg_read_color( val[i], &end, 0x3 );
            TS_ASSERT_EQUALS( result, 0xff00ff00 );
            TS_ASSERT_LESS_THAN( val[i], end );
        }
    }

    void testIccColor()
    {
        simpleIccCase cases[] = {
            {1, true, "named", "icc-color(named, 3)"},
            {0, false, "", "foodle"},
            {1, true, "a", "icc-color(a, 3)"},
            {4, true, "named", "icc-color(named, 3, 0, 0.1, 2.5)"},
            {0, false, "", "icc-color(named, 3"},
            {0, false, "", "icc-color(space named, 3)"},
            {0, false, "", "icc-color(tab\tnamed, 3)"},
            {0, false, "", "icc-color(0name, 3)"},
            {0, false, "", "icc-color(-name, 3)"},
            {1, true, "positive", "icc-color(positive, +3)"},
            {1, true, "negative", "icc-color(negative, -3)"},
            {1, true, "positive", "icc-color(positive, +0.1)"},
            {1, true, "negative", "icc-color(negative, -0.1)"},
            {0, false, "", "icc-color(named, value)"},
            {1, true, "hyphen-name", "icc-color(hyphen-name, 1)"},
            {1, true, "under_name", "icc-color(under_name, 1)"},
        };

        for ( size_t i = 0; i < G_N_ELEMENTS(cases); i++ ) {
            SVGICCColor tmp;
            gchar const* str = cases[i].str;
            gchar const* result = 0;

            std::string testDescr( cases[i].str );

            bool parseRet = sp_svg_read_icc_color( str, &result, &tmp );
            TSM_ASSERT_EQUALS( testDescr, parseRet, cases[i].shouldPass );
            TSM_ASSERT_EQUALS( testDescr, tmp.colors.size(), cases[i].numEntries );
            if ( cases[i].shouldPass ) {
                TSM_ASSERT_DIFFERS( testDescr, str, result );
                TSM_ASSERT_EQUALS( testDescr, tmp.colorProfile, std::string(cases[i].name) );
            } else {
                TSM_ASSERT_EQUALS( testDescr, str, result );
                TSM_ASSERT( testDescr, tmp.colorProfile.empty() );
            }
        }
    }

};

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :