~unity-api-team/thumbnailer/trusty

« back to all changes in this revision

Viewing changes to tests/unique_gobj.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-02-18 23:01:31 UTC
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20140218230131-8bxr5ucdqo8wums2
Tags: upstream-1.0+14.04.20140218
ImportĀ upstreamĀ versionĀ 1.0+14.04.20140218

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Jussi Pakkanen <jussi.pakkanen@canonical.com>
 
17
 */
 
18
 
 
19
#include<internal/gobj_memory.h>
 
20
#include<gdk-pixbuf/gdk-pixbuf.h>
 
21
#include<cassert>
 
22
 
 
23
using namespace std;
 
24
 
 
25
void trivial_test() {
 
26
    unique_gobj<GdkPixbuf> basic(gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480));
 
27
    assert(basic);
 
28
    assert(gdk_pixbuf_get_width(basic.get()) == 640);
 
29
    assert(gdk_pixbuf_get_height(basic.get()) == 480);
 
30
}
 
31
 
 
32
void compare_test() {
 
33
    GdkPixbuf *pb1 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
34
    GdkPixbuf *pb2 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
35
    if(pb1 > pb2) {
 
36
        std::swap(pb1, pb2);
 
37
    }
 
38
    assert(pb1 < pb2);
 
39
    unique_gobj<GdkPixbuf> u1(pb1);
 
40
    unique_gobj<GdkPixbuf> u2(pb2);
 
41
 
 
42
    assert(!(u1 == nullptr));
 
43
    assert(u1 != nullptr);
 
44
    assert(u1 != u2);
 
45
    assert(!(u1 == u2));
 
46
    assert(u1 < u2);
 
47
    assert(!(u2 < u1));
 
48
    assert(!(u1 == u2));
 
49
    assert(!(u2 == u1));
 
50
    assert(u1 <= u2);
 
51
    assert(!(u2 <= u1));
 
52
}
 
53
 
 
54
// This is its own thing due to need to avoid double release.
 
55
 
 
56
void equality_test() {
 
57
    GdkPixbuf *pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
58
    unique_gobj<GdkPixbuf> u1(pb);
 
59
    g_object_ref(G_OBJECT(pb));
 
60
    unique_gobj<GdkPixbuf> u2(pb);
 
61
    assert(u1 == u2);
 
62
    assert(u2 == u1);
 
63
    assert(!(u1 != u2));
 
64
    assert(!(u2 != u1));
 
65
}
 
66
 
 
67
void release_test() {
 
68
    GdkPixbuf *pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
69
    unique_gobj<GdkPixbuf> u(pb);
 
70
    assert(u);
 
71
    assert(u.get() != nullptr);
 
72
    assert(pb == u.release());
 
73
    assert(!u);
 
74
    assert(u.get() == nullptr);
 
75
    g_object_unref(pb);
 
76
}
 
77
 
 
78
void sub_func(GdkPixbuf *pb) {
 
79
    assert(G_OBJECT(pb)->ref_count == 2);
 
80
    unique_gobj<GdkPixbuf> u(pb);
 
81
    assert(G_OBJECT(pb)->ref_count == 2);
 
82
    // Now it dies and refcount is reduced.
 
83
}
 
84
 
 
85
void refcount_test() {
 
86
    GdkPixbuf *pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
87
    assert(G_OBJECT(pb)->ref_count == 1);
 
88
    g_object_ref(G_OBJECT(pb));
 
89
    sub_func(pb);
 
90
    assert(G_OBJECT(pb)->ref_count == 1);
 
91
    g_object_unref(G_OBJECT(pb));
 
92
}
 
93
 
 
94
void swap_test() {
 
95
    GdkPixbuf *pb1 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
96
    GdkPixbuf *pb2 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
97
    unique_gobj<GdkPixbuf> u1(pb1);
 
98
    unique_gobj<GdkPixbuf> u2(pb2);
 
99
 
 
100
    u1.swap(u2);
 
101
    assert(u1.get() == pb2);
 
102
    assert(u2.get() == pb1);
 
103
 
 
104
    std::swap(u1, u2);
 
105
    assert(u1.get() == pb1);
 
106
    assert(u2.get() == pb2);
 
107
}
 
108
 
 
109
void floating_test() {
 
110
    GdkPixbuf *pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
111
    bool got_exception = false;
 
112
    g_object_force_floating(G_OBJECT(pb));
 
113
    try {
 
114
        unique_gobj<GdkPixbuf> u(pb);
 
115
    } catch(const invalid_argument &c) {
 
116
        got_exception = true;
 
117
    }
 
118
    g_object_unref(G_OBJECT(pb));
 
119
    assert(got_exception);
 
120
}
 
121
 
 
122
void move_test() {
 
123
    GdkPixbuf *pb1 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
124
    GdkPixbuf *pb2 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
125
    g_object_ref(G_OBJECT(pb1));
 
126
    unique_gobj<GdkPixbuf> u1(pb1);
 
127
    unique_gobj<GdkPixbuf> u2(pb2);
 
128
    u1 = std::move(u2);
 
129
    assert(u1.get() == pb2);
 
130
    assert(!u2);
 
131
    assert(G_OBJECT(pb1)->ref_count == 1);
 
132
    g_object_unref(G_OBJECT(pb1));
 
133
}
 
134
 
 
135
void null_test() {
 
136
    GdkPixbuf *pb1 = NULL;
 
137
    GdkPixbuf *pb3 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
138
    unique_gobj<GdkPixbuf> u1(pb1);
 
139
    unique_gobj<GdkPixbuf> u2(nullptr);
 
140
    unique_gobj<GdkPixbuf> u3(pb3);
 
141
 
 
142
    assert(!u1);
 
143
    assert(!u2);
 
144
    u3 = nullptr;
 
145
    assert(!u3);
 
146
}
 
147
 
 
148
void reset_test() {
 
149
    GdkPixbuf *pb1 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
150
    GdkPixbuf *pb2 = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 640, 480);
 
151
    unique_gobj<GdkPixbuf> u(pb1);
 
152
 
 
153
    u.reset(pb2);
 
154
    assert(u.get() == pb2);
 
155
    u.reset(nullptr);
 
156
    assert(!u);
 
157
}
 
158
 
 
159
void sizeof_test() {
 
160
    assert(sizeof(GdkPixbuf*) == sizeof(unique_gobj<GdkPixbuf>));
 
161
}
 
162
 
 
163
void deleter_test() {
 
164
    unique_gobj<GdkPixbuf> u1;
 
165
    assert(u1.get_deleter() == g_object_unref);
 
166
}
 
167
 
 
168
int main() {
 
169
    g_type_init(); // Still needed in precise.
 
170
#ifdef NDEBUG
 
171
    fprintf(stderr, "NDEBUG defined, tests will not work.\n");
 
172
    return 1;
 
173
#else
 
174
    trivial_test();
 
175
    compare_test();
 
176
    equality_test();
 
177
    release_test();
 
178
    refcount_test();
 
179
    move_test();
 
180
    swap_test();
 
181
    floating_test();
 
182
    null_test();
 
183
    reset_test();
 
184
    sizeof_test();
 
185
    deleter_test();
 
186
    return 0;
 
187
#endif
 
188
}