1#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include <X11/Xutil.h> /* for XDestroyImage */
7
8#include "test.h"
9
10static void draw_string(struct test_display *t, Drawable d, uint8_t alu,
11			int x, int y, uint32_t fg, uint32_t bg, int s, int fill)
12{
13	const char *strings[] = {
14		"Hello",
15		"World",
16		"Cairo's twin is Giza",
17	};
18	XGCValues val;
19	GC gc;
20
21	val.function = alu;
22	val.foreground = fg;
23	val.background = bg;
24
25	gc = XCreateGC(t->dpy, d, GCForeground | GCBackground | GCFunction, &val);
26	if (fill)
27		XDrawImageString(t->dpy, d, gc, x, y, strings[s%3], strlen(strings[s%3]));
28	else
29		XDrawString(t->dpy, d, gc, x, y, strings[s%3], strlen(strings[s%3]));
30	XFreeGC(t->dpy, gc);
31}
32
33static void clear(struct test_display *dpy, struct test_target *tt)
34{
35	XRenderColor render_color = {0};
36	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
37			     0, 0, tt->width, tt->height);
38}
39
40static void string_tests(struct test *t, int reps, int sets, enum target target)
41{
42	struct test_target out, ref;
43	int r, s;
44
45	printf("Testing general (%s): ", test_target_name(target));
46	fflush(stdout);
47
48	test_target_create_render(&t->out, target, &out);
49	clear(&t->out, &out);
50
51	test_target_create_render(&t->ref, target, &ref);
52	clear(&t->ref, &ref);
53
54	for (s = 0; s < sets; s++) {
55		for (r = 0; r < reps; r++) {
56			int x = rand() % (2*out.width) - out.width;
57			int y = rand() % (2*out.height) - out.height;
58			uint8_t alu = rand() % (GXset + 1);
59			uint32_t fg = rand();
60			uint32_t bg = rand();
61			int str = rand();
62			int fill = rand() & 1;
63
64			draw_string(&t->out, out.draw, alu, x, y, fg, bg, str, fill);
65			draw_string(&t->ref, ref.draw, alu, x, y, fg, bg, str, fill);
66		}
67
68		test_compare(t,
69			     out.draw, out.format,
70			     ref.draw, ref.format,
71			     0, 0, out.width, out.height,
72			     "");
73	}
74
75	printf("passed [%d iterations x %d]\n", reps, sets);
76
77	test_target_destroy_render(&t->out, &out);
78	test_target_destroy_render(&t->ref, &ref);
79}
80
81int main(int argc, char **argv)
82{
83	struct test test;
84	int i;
85
86	test_init(&test, argc, argv);
87
88	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
89		int reps = REPS(i), sets = SETS(i);
90		enum target t;
91
92		for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
93			string_tests(&test, reps, sets, t);
94		}
95	}
96
97	return 0;
98}
99