1 | #include <gtk/gtk.h> |
2 | #include <cairo-gobject.h> |
3 | |
4 | static void |
5 | test_rectangle_equal (void) |
6 | { |
7 | GdkRectangle a = { 0, 0, 1, 1 }; |
8 | GdkRectangle b = { 1, 1, 2, 2 }; |
9 | GdkRectangle c = { 0, 0, 2, 2 }; |
10 | GdkRectangle d = { 0, 0, 1, 1 }; |
11 | GdkRectangle e = { 0, 0, 0, 0 }; |
12 | GdkRectangle f = { 1, 1, 0, 0 }; |
13 | |
14 | g_assert_true (!gdk_rectangle_equal (&a, &b)); |
15 | g_assert_true (!gdk_rectangle_equal (&a, &c)); |
16 | g_assert_true (!gdk_rectangle_equal (&b, &c)); |
17 | g_assert_true ( gdk_rectangle_equal (&a, &d)); |
18 | g_assert_true (!gdk_rectangle_equal (&e, &f)); |
19 | } |
20 | |
21 | static void |
22 | test_rectangle_intersect (void) |
23 | { |
24 | GdkRectangle a = { 0, 0, 10, 10 }; |
25 | GdkRectangle b = { 5, 5, 10, 10 }; |
26 | GdkRectangle c = { 0, 0, 0, 0 }; |
27 | GdkRectangle d = { 5, 5, 5, 5 }; |
28 | GdkRectangle e = { 0, 0, 10, 10 }; |
29 | GdkRectangle f = { 20, 20, 10, 10 }; |
30 | GdkRectangle g = { 0, 0, 0, 0 }; |
31 | GdkRectangle h = { 10, 10, 0, 0 }; |
32 | gboolean res; |
33 | |
34 | res = gdk_rectangle_intersect (src1: &a, src2: &b, dest: &c); |
35 | g_assert_true (res); |
36 | g_assert_true (gdk_rectangle_equal (&c, &d)); |
37 | |
38 | /* non-empty, non-intersecting rectangles */ |
39 | res = gdk_rectangle_intersect (src1: &e, src2: &f, dest: &f); |
40 | g_assert_cmpint (f.width, ==, 0); |
41 | g_assert_cmpint (f.height, ==, 0); |
42 | |
43 | /* empty rectangles */ |
44 | res = gdk_rectangle_intersect (src1: &g, src2: &h, NULL); |
45 | g_assert_true (!res); |
46 | } |
47 | |
48 | static void |
49 | test_rectangle_union (void) |
50 | { |
51 | GdkRectangle a = { 0, 0, 10, 10 }; |
52 | GdkRectangle b = { 5, 5, 10, 10 }; |
53 | GdkRectangle c = { 0, 0, 0, 0 }; |
54 | GdkRectangle d = { 0, 0, 15, 15 }; |
55 | GdkRectangle e = { 0, 0, 0, 0 }; |
56 | GdkRectangle f = { 50, 50, 0, 0 }; |
57 | GdkRectangle g = { 0, 0, 50, 50 }; |
58 | |
59 | gdk_rectangle_union (src1: &a, src2: &b, dest: &c); |
60 | g_assert_true (gdk_rectangle_equal (&c, &d)); |
61 | |
62 | gdk_rectangle_union (src1: &a, src2: &b, dest: &b); |
63 | g_assert_true (gdk_rectangle_equal (&b, &d)); |
64 | |
65 | gdk_rectangle_union (src1: &e, src2: &f, dest: &f); |
66 | g_assert_true (gdk_rectangle_equal (&f, &g)); |
67 | } |
68 | |
69 | /* Test that GdkRectangle works as a boxed type */ |
70 | |
71 | static void |
72 | test_rectangle_type (void) |
73 | { |
74 | GValue value = G_VALUE_INIT; |
75 | GdkRectangle b = { 5, 5, 10, 10 }; |
76 | GdkRectangle *c; |
77 | GValue value2 = G_VALUE_INIT; |
78 | cairo_rectangle_int_t *c2; |
79 | |
80 | g_value_init (value: &value, GDK_TYPE_RECTANGLE); |
81 | g_value_set_boxed (value: &value, v_boxed: &b); |
82 | c = g_value_get_boxed (value: &value); |
83 | |
84 | g_assert_true (gdk_rectangle_equal (&b, c)); |
85 | |
86 | g_assert_true (g_value_type_transformable (GDK_TYPE_RECTANGLE, CAIRO_GOBJECT_TYPE_RECTANGLE_INT)); |
87 | g_value_init (value: &value2, CAIRO_GOBJECT_TYPE_RECTANGLE_INT); |
88 | g_assert_true (g_value_transform (&value, &value2)); |
89 | c2 = g_value_get_boxed (value: &value2); |
90 | g_assert_cmpint (c->x, ==, c2->x); |
91 | g_assert_cmpint (c->y, ==, c2->y); |
92 | g_assert_cmpint (c->width, ==, c2->width); |
93 | g_assert_cmpint (c->height, ==, c2->height); |
94 | |
95 | g_value_unset (value: &value); |
96 | g_value_unset (value: &value2); |
97 | } |
98 | |
99 | static void |
100 | test_rectangle_contains (void) |
101 | { |
102 | GdkRectangle b = { 5, 5, 5, 5 }; |
103 | |
104 | g_assert_true (gdk_rectangle_contains_point (&b, 5, 5)); |
105 | g_assert_true (gdk_rectangle_contains_point (&b, 9, 9)); |
106 | g_assert_false (gdk_rectangle_contains_point (&b, 4, 8)); |
107 | g_assert_false (gdk_rectangle_contains_point (&b, 10, 6)); |
108 | } |
109 | |
110 | int |
111 | main (int argc, char *argv[]) |
112 | { |
113 | (g_test_init) (argc: &argc, argv: &argv, NULL); |
114 | |
115 | gtk_init (); |
116 | |
117 | g_test_add_func (testpath: "/rectangle/equal" , test_func: test_rectangle_equal); |
118 | g_test_add_func (testpath: "/rectangle/intersect" , test_func: test_rectangle_intersect); |
119 | g_test_add_func (testpath: "/rectangle/union" , test_func: test_rectangle_union); |
120 | g_test_add_func (testpath: "/rectangle/type" , test_func: test_rectangle_type); |
121 | g_test_add_func (testpath: "/rectangle/contains" , test_func: test_rectangle_contains); |
122 | |
123 | return g_test_run (); |
124 | } |
125 | |