basic-string.c revision 03b705cf
103b705cfSriastradh#include <stdint.h>
203b705cfSriastradh#include <stdio.h>
303b705cfSriastradh#include <stdlib.h>
403b705cfSriastradh#include <string.h>
503b705cfSriastradh
603b705cfSriastradh#include <X11/Xutil.h> /* for XDestroyImage */
703b705cfSriastradh
803b705cfSriastradh#include "test.h"
903b705cfSriastradh
1003b705cfSriastradhstatic void draw_string(struct test_display *t, Drawable d, uint8_t alu,
1103b705cfSriastradh			int x, int y, uint32_t fg, uint32_t bg, int s, int fill)
1203b705cfSriastradh{
1303b705cfSriastradh	const char *strings[] = {
1403b705cfSriastradh		"Hello",
1503b705cfSriastradh		"World",
1603b705cfSriastradh		"Cairo's twin is Giza",
1703b705cfSriastradh	};
1803b705cfSriastradh	XGCValues val;
1903b705cfSriastradh	GC gc;
2003b705cfSriastradh
2103b705cfSriastradh	val.function = alu;
2203b705cfSriastradh	val.foreground = fg;
2303b705cfSriastradh	val.background = bg;
2403b705cfSriastradh
2503b705cfSriastradh	gc = XCreateGC(t->dpy, d, GCForeground | GCBackground | GCFunction, &val);
2603b705cfSriastradh	if (fill)
2703b705cfSriastradh		XDrawImageString(t->dpy, d, gc, x, y, strings[s%3], strlen(strings[s%3]));
2803b705cfSriastradh	else
2903b705cfSriastradh		XDrawString(t->dpy, d, gc, x, y, strings[s%3], strlen(strings[s%3]));
3003b705cfSriastradh	XFreeGC(t->dpy, gc);
3103b705cfSriastradh}
3203b705cfSriastradh
3303b705cfSriastradhstatic void clear(struct test_display *dpy, struct test_target *tt)
3403b705cfSriastradh{
3503b705cfSriastradh	XRenderColor render_color = {0};
3603b705cfSriastradh	XRenderFillRectangle(dpy->dpy, PictOpClear, tt->picture, &render_color,
3703b705cfSriastradh			     0, 0, tt->width, tt->height);
3803b705cfSriastradh}
3903b705cfSriastradh
4003b705cfSriastradhstatic void string_tests(struct test *t, int reps, int sets, enum target target)
4103b705cfSriastradh{
4203b705cfSriastradh	struct test_target real, ref;
4303b705cfSriastradh	int r, s;
4403b705cfSriastradh
4503b705cfSriastradh	printf("Testing general (%s): ", test_target_name(target));
4603b705cfSriastradh	fflush(stdout);
4703b705cfSriastradh
4803b705cfSriastradh	test_target_create_render(&t->real, target, &real);
4903b705cfSriastradh	clear(&t->real, &real);
5003b705cfSriastradh
5103b705cfSriastradh	test_target_create_render(&t->ref, target, &ref);
5203b705cfSriastradh	clear(&t->ref, &ref);
5303b705cfSriastradh
5403b705cfSriastradh	for (s = 0; s < sets; s++) {
5503b705cfSriastradh		for (r = 0; r < reps; r++) {
5603b705cfSriastradh			int x = rand() % (2*real.width) - real.width;
5703b705cfSriastradh			int y = rand() % (2*real.height) - real.height;
5803b705cfSriastradh			uint8_t alu = rand() % (GXset + 1);
5903b705cfSriastradh			uint32_t fg = rand();
6003b705cfSriastradh			uint32_t bg = rand();
6103b705cfSriastradh			int str = rand();
6203b705cfSriastradh			int fill = rand() & 1;
6303b705cfSriastradh
6403b705cfSriastradh			draw_string(&t->real, real.draw, alu, x, y, fg, bg, str, fill);
6503b705cfSriastradh			draw_string(&t->ref, ref.draw, alu, x, y, fg, bg, str, fill);
6603b705cfSriastradh		}
6703b705cfSriastradh
6803b705cfSriastradh		test_compare(t,
6903b705cfSriastradh			     real.draw, real.format,
7003b705cfSriastradh			     ref.draw, ref.format,
7103b705cfSriastradh			     0, 0, real.width, real.height,
7203b705cfSriastradh			     "");
7303b705cfSriastradh	}
7403b705cfSriastradh
7503b705cfSriastradh	printf("passed [%d iterations x %d]\n", reps, sets);
7603b705cfSriastradh
7703b705cfSriastradh	test_target_destroy_render(&t->real, &real);
7803b705cfSriastradh	test_target_destroy_render(&t->ref, &ref);
7903b705cfSriastradh}
8003b705cfSriastradh
8103b705cfSriastradhint main(int argc, char **argv)
8203b705cfSriastradh{
8303b705cfSriastradh	struct test test;
8403b705cfSriastradh	int i;
8503b705cfSriastradh
8603b705cfSriastradh	test_init(&test, argc, argv);
8703b705cfSriastradh
8803b705cfSriastradh	for (i = 0; i <= DEFAULT_ITERATIONS; i++) {
8903b705cfSriastradh		int reps = 1 << i;
9003b705cfSriastradh		int sets = 1 << (12 - i);
9103b705cfSriastradh		enum target t;
9203b705cfSriastradh
9303b705cfSriastradh		if (sets < 2)
9403b705cfSriastradh			sets = 2;
9503b705cfSriastradh
9603b705cfSriastradh		for (t = TARGET_FIRST; t <= TARGET_LAST; t++) {
9703b705cfSriastradh			string_tests(&test, reps, sets, t);
9803b705cfSriastradh		}
9903b705cfSriastradh	}
10003b705cfSriastradh
10103b705cfSriastradh	return 0;
10203b705cfSriastradh}
103