1 /*
2  * Copyright 2011 © Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #include "fake-symbols.h"
24 #include <xf86Wacom.h>
25 
26 /**
27  * NOTE: this file may not contain tests that require static variables. The
28  * current compiler undef static magic makes them local variables and thus
29  * change the behaviour.
30  */
31 
32 static void
test_get_scroll_delta(void)33 test_get_scroll_delta(void)
34 {
35 	int test_table[][5] = {
36 		{ 100,  25, 0, 0,  75}, { 25,  100, 0, 0, -75},
37 		{-100, -25, 0, 0, -75}, {-25, -100, 0, 0,  75},
38 		{ 100, -25, 0, 0, 125}, {-25,  100, 0, 0,-125},
39 		{ 100, 100, 0, 0,   0}, {-25,  -25, 0, 0,   0},
40 
41 		{23, 0, 50, 0,  23}, {0, 23, 50, 0, -23},
42 		{24, 0, 50, 0,  24}, {0, 24, 50, 0, -24},
43 		{25, 0, 50, 0,  25}, {0, 25, 50, 0, -25},
44 		{26, 0, 50, 0, -25}, {0, 26, 50, 0,  25},
45 		{27, 0, 50, 0, -24}, {0, 27, 50, 0,  24},
46 		{28, 0, 50, 0, -23}, {0, 28, 50, 0,  23},
47 
48 		{1024, 0, 0, AXIS_BITWISE, 11}, {0, 1024, 0, AXIS_BITWISE, -11},
49 
50 		{  0, 4, 256, AXIS_BITWISE, -3}, {4,   0, 256, AXIS_BITWISE,  3},
51 		{  1, 4, 256, AXIS_BITWISE, -2}, {4,   1, 256, AXIS_BITWISE,  2},
52 		{  2, 4, 256, AXIS_BITWISE, -1}, {4,   2, 256, AXIS_BITWISE,  1},
53 		{  4, 4, 256, AXIS_BITWISE,  0}, {4,   4, 256, AXIS_BITWISE,  0},
54 		{  8, 4, 256, AXIS_BITWISE,  1}, {4,   8, 256, AXIS_BITWISE, -1},
55 		{ 16, 4, 256, AXIS_BITWISE,  2}, {4,  16, 256, AXIS_BITWISE, -2},
56 		{ 32, 4, 256, AXIS_BITWISE,  3}, {4,  32, 256, AXIS_BITWISE, -3},
57 		{ 64, 4, 256, AXIS_BITWISE,  4}, {4,  64, 256, AXIS_BITWISE, -4},
58 		{128, 4, 256, AXIS_BITWISE,  5}, {4, 128, 256, AXIS_BITWISE, -5},
59 		{256, 4, 256, AXIS_BITWISE, -4}, {4, 256, 256, AXIS_BITWISE,  4}
60 	};
61 	int i;
62 
63 	for (i = 0; i < ARRAY_SIZE(test_table); i++)
64 	{
65 		int delta;
66 		int current, old, wrap, flags;
67 		current = test_table[i][0];
68 		old     = test_table[i][1];
69 		wrap    = test_table[i][2];
70 		flags   = test_table[i][3];
71 
72 		delta = getScrollDelta(current, old, wrap, flags);
73 		assert(delta == test_table[i][4]);
74 
75 		flags |= AXIS_INVERT;
76 		delta = getScrollDelta(current, old, wrap, flags);
77 		assert(delta == -1 * test_table[i][4]);
78 	}
79 }
80 
81 static void
test_get_wheel_button(void)82 test_get_wheel_button(void)
83 {
84 	int delta;
85 	int action_up, action_dn;
86 
87 	action_up = 300;
88 	action_dn = 400;
89 
90 	for (delta = -32; delta <= 32; delta++)
91 	{
92 		int action;
93 		action = getWheelButton(delta, action_up, action_dn);
94 		if (delta < 0)
95 		{
96 			assert(action == action_dn);
97 		}
98 		else if (delta == 0)
99 		{
100 			assert(action == -1);
101 		}
102 		else
103 		{
104 			assert(action == action_up);
105 		}
106 	}
107 }
108 
109 /**
110  * Test refcounting of the common struct.
111  */
112 static void
test_common_ref(void)113 test_common_ref(void)
114 {
115 	WacomCommonPtr common;
116 	WacomCommonPtr second;
117 
118 	common = wcmNewCommon();
119 	assert(common);
120 	assert(common->refcnt == 1);
121 
122 	second = wcmRefCommon(common);
123 
124 	assert(second == common);
125 	assert(second->refcnt == 2);
126 
127 	wcmFreeCommon(&second);
128 	assert(common);
129 	assert(!second);
130 	assert(common->refcnt == 1);
131 
132 	second = wcmRefCommon(NULL);
133 	assert(common != second);
134 	assert(second->refcnt == 1);
135 	assert(common->refcnt == 1);
136 
137 	wcmFreeCommon(&second);
138 	wcmFreeCommon(&common);
139 	assert(!second && !common);
140 }
141 
142 
143 static void
test_rebase_pressure(void)144 test_rebase_pressure(void)
145 {
146 	WacomDeviceRec priv = {0};
147 	WacomDeviceRec base = {0};
148 	WacomDeviceState ds = {0};
149 	int pressure;
150 
151 	priv.minPressure = 4;
152 	ds.pressure = 10;
153 
154 	/* Pressure in out-of-proximity means get new preloaded pressure */
155 	priv.oldState.proximity = 0;
156 
157 	/* make sure we don't touch priv, not really needed, the compiler should
158 	 * honor the consts but... */
159 	base = priv;
160 
161 	pressure = rebasePressure(&priv, &ds);
162 	assert(pressure == ds.pressure);
163 
164 	assert(memcmp(&priv, &base, sizeof(priv)) == 0);
165 
166 	/* Pressure in-proximity means rebase to new minimum */
167 	priv.oldState.proximity = 1;
168 
169 	base = priv;
170 
171 	pressure = rebasePressure(&priv, &ds);
172 	assert(pressure == priv.minPressure);
173 	assert(memcmp(&priv, &base, sizeof(priv)) == 0);
174 }
175 
176 static void
test_normalize_pressure(void)177 test_normalize_pressure(void)
178 {
179 	InputInfoRec pInfo = {0};
180 	WacomDeviceRec priv = {0};
181 	WacomCommonRec common = {0};
182 	int normalized_max = 65536;
183 	int pressure, prev_pressure = -1;
184 	int i, j, k;
185 
186 	priv.common = &common;
187 	priv.pInfo = &pInfo;
188 	pInfo.name = strdup("Wacom test device");
189 	common.wcmPressureRecalibration = 1;
190 
191 	priv.minPressure = 0;
192 
193 	/* Check various maxCurve values */
194 	for (k = 512; k <= normalized_max; k += 239) {
195 		priv.maxCurve = k;
196 
197 		/* Some random loop to check various maxZ pressure values. Starting at
198 		 * 1, because if wcmMaxZ is 0 we have other problems. */
199 		for (j = 1; j <= 256; j += 17)
200 		{
201 			common.wcmMaxZ = j;
202 			prev_pressure = -1;
203 
204 			for (i = 0; i <= common.wcmMaxZ; i++)
205 			{
206 				pressure = i;
207 
208 				pressure = normalizePressure(&priv, pressure);
209 				assert(pressure >= 0);
210 				assert(pressure <= k);
211 
212 				/* we count up, so assume normalised pressure goes up too */
213 				assert(prev_pressure < pressure);
214 				prev_pressure = pressure;
215 			}
216 
217 			assert(pressure == k);
218 		}
219 	}
220 
221 	/* If minPressure is higher than ds->pressure, normalizePressure takes
222 	 * minPressure and ignores actual pressure. This would be a bug in the
223 	 * driver code, but we might as well test for it. */
224 	priv.minPressure = 10;
225 	priv.maxCurve = normalized_max;
226 
227 	prev_pressure = normalizePressure(&priv, 0);
228 	for (i = 0; i < priv.minPressure; i++)
229 	{
230 
231 		pressure = normalizePressure(&priv, i);
232 
233 		assert(pressure >= 0);
234 		assert(pressure < normalized_max);
235 
236 		/* we count up, so assume normalised pressure goes up too */
237 		assert(prev_pressure == pressure);
238 	}
239 	free(pInfo.name);
240 }
241 
242 /**
243  * After a call to wcmInitialToolSize, the min/max and resolution must be
244  * set up correctly.
245  *
246  * wcmInitialToolSize takes the data from the common rec, so test that the
247  * priv has all the values of the common.
248  */
249 static void
test_initial_size(void)250 test_initial_size(void)
251 {
252 	InputInfoRec info = {0};
253 	WacomDeviceRec priv = {0};
254 	WacomCommonRec common = {0};
255 
256 	/* pin to some numbers */
257 	int xres = 1920, yres = 1600;
258 	int minx, maxx = 2 * xres, miny, maxy = 2 * yres;
259 
260 	info.private = &priv;
261 	priv.common = &common;
262 
263 	/* FIXME: we currently assume min of 0 in the driver. we cannot cope
264 	 * with non-zero devices */
265 	minx = miny = 0;
266 
267 	common.wcmMaxX = maxx;
268 	common.wcmMaxY = maxy;
269 	common.wcmResolX = xres;
270 	common.wcmResolY = yres;
271 
272 	wcmInitialToolSize(&info);
273 
274 	assert(priv.topX == minx);
275 	assert(priv.topY == minx);
276 	assert(priv.bottomX == maxx);
277 	assert(priv.bottomY == maxy);
278 	assert(priv.resolX == xres);
279 	assert(priv.resolY == yres);
280 
281 	/* Same thing for a touch-enabled device */
282 	memset(&common, 0, sizeof(common));
283 
284 	priv.flags = TOUCH_ID;
285 	assert(IsTouch(&priv));
286 
287 	common.wcmMaxTouchX = maxx;
288 	common.wcmMaxTouchY = maxy;
289 	common.wcmTouchResolX = xres;
290 	common.wcmTouchResolY = yres;
291 
292 	wcmInitialToolSize(&info);
293 
294 	assert(priv.topX == minx);
295 	assert(priv.topY == minx);
296 	assert(priv.bottomX == maxx);
297 	assert(priv.bottomY == maxy);
298 	assert(priv.resolX == xres);
299 	assert(priv.resolY == yres);
300 
301 }
302 
303 static void
test_suppress(void)304 test_suppress(void)
305 {
306 	enum WacomSuppressMode rc;
307 	WacomCommonRec common = {0};
308 	WacomDeviceState old = {0},
309 			 new = {0};
310 
311 	common.wcmSuppress = 2;
312 
313 	rc = wcmCheckSuppress(&common, &old, &new);
314 	assert(rc == SUPPRESS_ALL);
315 
316 	/* proximity, buttons and strip send for any change */
317 
318 #define test_any_suppress(field) \
319 	old.field = 1; \
320 	rc = wcmCheckSuppress(&common, &old, &new); \
321 	assert(rc == SUPPRESS_NONE); \
322 	new.field = old.field;
323 
324 	test_any_suppress(proximity);
325 	test_any_suppress(buttons);
326 	test_any_suppress(stripx);
327 	test_any_suppress(stripy);
328 
329 #undef test_any_suppress
330 
331 	/* pressure, capacity, throttle, rotation, abswheel only when
332 	 * difference is above suppress */
333 
334 	/* test negative and positive transition */
335 #define test_above_suppress(field) \
336 	old.field = common.wcmSuppress; \
337 	rc = wcmCheckSuppress(&common, &old, &new); \
338 	assert(rc == SUPPRESS_ALL); \
339 	old.field = common.wcmSuppress + 1; \
340 	rc = wcmCheckSuppress(&common, &old, &new); \
341 	assert(rc == SUPPRESS_NONE); \
342 	old.field = -common.wcmSuppress; \
343 	rc = wcmCheckSuppress(&common, &old, &new); \
344 	assert(rc == SUPPRESS_ALL); \
345 	old.field = -common.wcmSuppress - 1; \
346 	rc = wcmCheckSuppress(&common, &old, &new); \
347 	assert(rc == SUPPRESS_NONE); \
348 	new.field = old.field;
349 
350 	test_above_suppress(pressure);
351 	test_above_suppress(throttle);
352 	test_above_suppress(rotation);
353 	test_above_suppress(abswheel);
354 
355 #undef test_above_suppress
356 
357 	/* any movement on relwheel counts */
358 	new.relwheel = 1;
359 	rc = wcmCheckSuppress(&common, &old, &new);
360 	assert(rc == SUPPRESS_NONE);
361 	new.relwheel = 0;
362 
363 	/* x axis movement */
364 
365 	/* not enough movement */
366 	new.x = common.wcmSuppress;
367 	rc = wcmCheckSuppress(&common, &old, &new);
368 	assert(rc == SUPPRESS_ALL);
369 	assert(old.x == new.x);
370 	assert(old.y == new.y);
371 
372 	/* only x axis above thresh */
373 	new.x = common.wcmSuppress + 1;
374 	rc = wcmCheckSuppress(&common, &old, &new);
375 	assert(rc == SUPPRESS_NON_MOTION);
376 
377 	/* x and other field above thres */
378 	new.pressure = ~old.pressure;
379 	rc = wcmCheckSuppress(&common, &old, &new);
380 	assert(rc == SUPPRESS_NONE);
381 
382 	new.pressure = old.pressure;
383 	new.x = old.x;
384 
385 	/* y axis movement */
386 	new.y = common.wcmSuppress;
387 	rc = wcmCheckSuppress(&common, &old, &new);
388 	assert(rc == SUPPRESS_ALL);
389 	assert(old.x == new.x);
390 	assert(old.y == new.y);
391 
392 	new.y = common.wcmSuppress + 1;
393 	rc = wcmCheckSuppress(&common, &old, &new);
394 	assert(rc == SUPPRESS_NON_MOTION);
395 
396 	new.pressure = ~old.pressure;
397 	rc = wcmCheckSuppress(&common, &old, &new);
398 	assert(rc == SUPPRESS_NONE);
399 	new.pressure = old.pressure;
400 }
401 
402 static void
test_tilt_to_rotation(void)403 test_tilt_to_rotation(void)
404 {
405 #if 0
406 	This table below was generated from wcmTilt2R with the following code
407 
408 	for (angle = 0; angle < 360; angle++)
409 	{
410 		double rad = angle * M_PI / 180.0;
411 		double x, y;
412 		x = sin(rad);
413 		y = cos(rad);
414 
415 		/* wcmTilt2R only uses it for the angle anyway, let's try to
416 		get as precise as possible */
417 		ds.tiltx = x * 1000;
418 		ds.tilty = y * 1000;
419 		ds.rotation = 0;
420 
421 		wcmTilt2R(&ds);
422 
423 		printf("{ %d, %d, %d},\n", ds.tiltx, ds.tilty, ds.rotation);
424 	}
425 #endif
426 
427 	int rotation_table[][3] = {
428 		{ 17, 999, 20}, { 34, 999, 15}, { 52, 998, 10}, { 69, 997, 5}, { 87, 996, 0},
429 		{ 104, 994, -5}, { 121, 992, -10}, { 139, 990, -15}, { 156, 987, -20}, { 173, 984, -25},
430 		{ 190, 981, -30}, { 207, 978, -35}, { 224, 974, -40}, { 241, 970, -45}, { 258, 965, -50},
431 		{ 275, 961, -55}, { 292, 956, -60}, { 309, 951, -65}, { 325, 945, -70}, { 342, 939, -75},
432 		{ 358, 933, -80}, { 374, 927, -85}, { 390, 920, -90}, { 406, 913, -95}, { 422, 906, -100},
433 		{ 438, 898, -105}, { 453, 891, -110}, { 469, 882, -115}, { 484, 874, -120}, { 499, 866, -125},
434 		{ 515, 857, -130}, { 529, 848, -135}, { 544, 838, -140}, { 559, 829, -145}, { 573, 819, -150},
435 		{ 587, 809, -155}, { 601, 798, -160}, { 615, 788, -165}, { 629, 777, -170}, { 642, 766, -175},
436 		{ 656, 754, -180}, { 669, 743, -185}, { 681, 731, -190}, { 694, 719, -195}, { 707, 707, -200},
437 		{ 719, 694, -205}, { 731, 681, -210}, { 743, 669, -215}, { 754, 656, -220}, { 766, 642, -225},
438 		{ 777, 629, -230}, { 788, 615, -235}, { 798, 601, -240}, { 809, 587, -245}, { 819, 573, -250},
439 		{ 829, 559, -255}, { 838, 544, -260}, { 848, 529, -265}, { 857, 515, -270}, { 866, 500, -275},
440 		{ 874, 484, -280}, { 882, 469, -285}, { 891, 453, -290}, { 898, 438, -295}, { 906, 422, -300},
441 		{ 913, 406, -305}, { 920, 390, -310}, { 927, 374, -315}, { 933, 358, -320}, { 939, 342, -325},
442 		{ 945, 325, -330}, { 951, 309, -335}, { 956, 292, -340}, { 961, 275, -345}, { 965, 258, -350},
443 		{ 970, 241, -355}, { 974, 224, -360}, { 978, 207, -365}, { 981, 190, -370}, { 984, 173, -375},
444 		{ 987, 156, -380}, { 990, 139, -385}, { 992, 121, -390}, { 994, 104, -395}, { 996, 87, -400},
445 		{ 997, 69, -405}, { 998, 52, -410}, { 999, 34, -415}, { 999, 17, -420}, { 1000, 0, -425},
446 		{ 999, -17, -430}, { 999, -34, -435}, { 998, -52, -440}, { 997, -69, -445}, { 996, -87, -450},
447 		{ 994, -104, -455}, { 992, -121, -460}, { 990, -139, -465}, { 987, -156, -470}, { 984, -173, -475},
448 		{ 981, -190, -480}, { 978, -207, -485}, { 974, -224, -490}, { 970, -241, -495}, { 965, -258, -500},
449 		{ 961, -275, -505}, { 956, -292, -510}, { 951, -309, -515}, { 945, -325, -520}, { 939, -342, -525},
450 		{ 933, -358, -530}, { 927, -374, -535}, { 920, -390, -540}, { 913, -406, -545}, { 906, -422, -550},
451 		{ 898, -438, -555}, { 891, -453, -560}, { 882, -469, -565}, { 874, -484, -570}, { 866, -499, -575},
452 		{ 857, -515, -580}, { 848, -529, -585}, { 838, -544, -590}, { 829, -559, -595}, { 819, -573, -600},
453 		{ 809, -587, -605}, { 798, -601, -610}, { 788, -615, -615}, { 777, -629, -620}, { 766, -642, -625},
454 		{ 754, -656, -630}, { 743, -669, -635}, { 731, -681, -640}, { 719, -694, -645}, { 707, -707, -650},
455 		{ 694, -719, -655}, { 681, -731, -660}, { 669, -743, -665}, { 656, -754, -670}, { 642, -766, -675},
456 		{ 629, -777, -680}, { 615, -788, -685}, { 601, -798, -690}, { 587, -809, -695}, { 573, -819, -700},
457 		{ 559, -829, -705}, { 544, -838, -710}, { 529, -848, -715}, { 515, -857, -720}, { 499, -866, -725},
458 		{ 484, -874, -730}, { 469, -882, -735}, { 453, -891, -740}, { 438, -898, -745}, { 422, -906, -750},
459 		{ 406, -913, -755}, { 390, -920, -760}, { 374, -927, -765}, { 358, -933, -770}, { 342, -939, -775},
460 		{ 325, -945, -780}, { 309, -951, -785}, { 292, -956, -790}, { 275, -961, -795}, { 258, -965, -800},
461 		{ 241, -970, -805}, { 224, -974, -810}, { 207, -978, -815}, { 190, -981, -820}, { 173, -984, -825},
462 		{ 156, -987, -830}, { 139, -990, -835}, { 121, -992, -840}, { 104, -994, -845}, { 87, -996, -850},
463 		{ 69, -997, -855}, { 52, -998, -860}, { 34, -999, -865}, { 17, -999, -870}, { 0, -1000, -875},
464 		{ -17, -999, -880}, { -34, -999, -885}, { -52, -998, -890}, { -69, -997, -895}, { -87, -996, -900},
465 		{ -104, -994, 895}, { -121, -992, 890}, { -139, -990, 885}, { -156, -987, 880}, { -173, -984, 875},
466 		{ -190, -981, 870}, { -207, -978, 865}, { -224, -974, 860}, { -241, -970, 855}, { -258, -965, 850},
467 		{ -275, -961, 845}, { -292, -956, 840}, { -309, -951, 835}, { -325, -945, 830}, { -342, -939, 825},
468 		{ -358, -933, 820}, { -374, -927, 815}, { -390, -920, 810}, { -406, -913, 805}, { -422, -906, 800},
469 		{ -438, -898, 795}, { -453, -891, 790}, { -469, -882, 785}, { -484, -874, 780}, { -500, -866, 775},
470 		{ -515, -857, 770}, { -529, -848, 765}, { -544, -838, 760}, { -559, -829, 755}, { -573, -819, 750},
471 		{ -587, -809, 745}, { -601, -798, 740}, { -615, -788, 735}, { -629, -777, 730}, { -642, -766, 725},
472 		{ -656, -754, 720}, { -669, -743, 715}, { -681, -731, 710}, { -694, -719, 705}, { -707, -707, 700},
473 		{ -719, -694, 695}, { -731, -681, 690}, { -743, -669, 685}, { -754, -656, 680}, { -766, -642, 675},
474 		{ -777, -629, 670}, { -788, -615, 665}, { -798, -601, 660}, { -809, -587, 655}, { -819, -573, 650},
475 		{ -829, -559, 645}, { -838, -544, 640}, { -848, -529, 635}, { -857, -515, 630}, { -866, -500, 625},
476 		{ -874, -484, 620}, { -882, -469, 615}, { -891, -453, 610}, { -898, -438, 605}, { -906, -422, 600},
477 		{ -913, -406, 595}, { -920, -390, 590}, { -927, -374, 585}, { -933, -358, 580}, { -939, -342, 575},
478 		{ -945, -325, 570}, { -951, -309, 565}, { -956, -292, 560}, { -961, -275, 555}, { -965, -258, 550},
479 		{ -970, -241, 545}, { -974, -224, 540}, { -978, -207, 535}, { -981, -190, 530}, { -984, -173, 525},
480 		{ -987, -156, 520}, { -990, -139, 515}, { -992, -121, 510}, { -994, -104, 505}, { -996, -87, 500},
481 		{ -997, -69, 495}, { -998, -52, 490}, { -999, -34, 485}, { -999, -17, 480}, { -1000, 0, 475},
482 		{ -999, 17, 470}, { -999, 34, 465}, { -998, 52, 460}, { -997, 69, 455}, { -996, 87, 450},
483 		{ -994, 104, 445}, { -992, 121, 440}, { -990, 139, 435}, { -987, 156, 430}, { -984, 173, 425},
484 		{ -981, 190, 420}, { -978, 207, 415}, { -974, 224, 410}, { -970, 241, 405}, { -965, 258, 400},
485 		{ -961, 275, 395}, { -956, 292, 390}, { -951, 309, 385}, { -945, 325, 380}, { -939, 342, 375},
486 		{ -933, 358, 370}, { -927, 374, 365}, { -920, 390, 360}, { -913, 406, 355}, { -906, 422, 350},
487 		{ -898, 438, 345}, { -891, 453, 340}, { -882, 469, 335}, { -874, 484, 330}, { -866, 500, 325},
488 		{ -857, 515, 320}, { -848, 529, 315}, { -838, 544, 310}, { -829, 559, 305}, { -819, 573, 300},
489 		{ -809, 587, 295}, { -798, 601, 290}, { -788, 615, 285}, { -777, 629, 280}, { -766, 642, 275},
490 		{ -754, 656, 270}, { -743, 669, 265}, { -731, 681, 260}, { -719, 694, 255}, { -707, 707, 250},
491 		{ -694, 719, 245}, { -681, 731, 240}, { -669, 743, 235}, { -656, 754, 230}, { -642, 766, 225},
492 		{ -629, 777, 220}, { -615, 788, 215}, { -601, 798, 210}, { -587, 809, 205}, { -573, 819, 200},
493 		{ -559, 829, 195}, { -544, 838, 190}, { -529, 848, 185}, { -515, 857, 180}, { -500, 866, 175},
494 		{ -484, 874, 170}, { -469, 882, 165}, { -453, 891, 160}, { -438, 898, 155}, { -422, 906, 150},
495 		{ -406, 913, 145}, { -390, 920, 140}, { -374, 927, 135}, { -358, 933, 130}, { -342, 939, 125},
496 		{ -325, 945, 120}, { -309, 951, 115}, { -292, 956, 110}, { -275, 961, 105}, { -258, 965, 100},
497 		{ -241, 970, 95}, { -224, 974, 90}, { -207, 978, 85}, { -190, 981, 80}, { -173, 984, 75},
498 		{ -156, 987, 70}, { -139, 990, 65}, { -121, 992, 60}, { -104, 994, 55}, { -87, 996, 50},
499 		{ -69, 997, 45}, { -52, 998, 40}, { -34, 999, 35}, { -17, 999, 30},
500 	};
501 	int i;
502 
503 	for (i = 0; i < ARRAY_SIZE(rotation_table); i++)
504 	{
505 		int rotation;
506 		int x, y;
507 		x = rotation_table[i][0];
508 		y = rotation_table[i][1];
509 		rotation = wcmTilt2R(x, y, INTUOS4_CURSOR_ROTATION_OFFSET);
510 		assert(rotation == rotation_table[i][2]);
511 	}
512 }
513 
514 
515 static void
test_mod_buttons(void)516 test_mod_buttons(void)
517 {
518 	int i;
519 	for (i = 0; i < sizeof(int) * 8; i++)
520 	{
521 		int buttons = mod_buttons(0, i, 1);
522 		assert(buttons == (1 << i));
523 		buttons = mod_buttons(0, i, 0);
524 		assert(buttons == 0);
525 	}
526 
527 	assert(mod_buttons(0, sizeof(int) * 8, 1) == 0);
528 }
529 
test_set_type(void)530 static void test_set_type(void)
531 {
532 	InputInfoRec info = {0};
533 	WacomDeviceRec priv = {0};
534 	WacomTool tool = {0};
535 	WacomCommonRec common = {0};
536 	int rc;
537 
538 #define reset(_info, _priv, _tool, _common) \
539 	memset(&(_info), 0, sizeof(_info)); \
540 	memset(&(_priv), 0, sizeof(_priv)); \
541 	memset(&(_tool), 0, sizeof(_tool)); \
542 	(_info).private = &(_priv); \
543 	(_priv).tool = &(_tool); \
544 	(_priv).common = &(_common);
545 
546 
547 	reset(info, priv, tool, common);
548 	rc = wcmSetType(&info, NULL);
549 	assert(rc == 0);
550 
551 	reset(info, priv, tool, common);
552 	rc = wcmSetType(&info, "stylus");
553 	assert(rc == 1);
554 	assert(is_absolute(&info));
555 	assert(IsStylus(&priv));
556 	assert(!IsTouch(&priv));
557 	assert(!IsEraser(&priv));
558 	assert(!IsCursor(&priv));
559 	assert(!IsPad(&priv));
560 
561 	reset(info, priv, tool, common);
562 	rc = wcmSetType(&info, "touch");
563 	assert(rc == 1);
564 	/* only some touch screens are absolute */
565 	assert(!is_absolute(&info));
566 	assert(!IsStylus(&priv));
567 	assert(IsTouch(&priv));
568 	assert(!IsEraser(&priv));
569 	assert(!IsCursor(&priv));
570 	assert(!IsPad(&priv));
571 
572 	reset(info, priv, tool, common);
573 	rc = wcmSetType(&info, "eraser");
574 	assert(rc == 1);
575 	assert(is_absolute(&info));
576 	assert(!IsStylus(&priv));
577 	assert(!IsTouch(&priv));
578 	assert(IsEraser(&priv));
579 	assert(!IsCursor(&priv));
580 	assert(!IsPad(&priv));
581 
582 	reset(info, priv, tool, common);
583 	rc = wcmSetType(&info, "cursor");
584 	assert(rc == 1);
585 	assert(!is_absolute(&info));
586 	assert(!IsStylus(&priv));
587 	assert(!IsTouch(&priv));
588 	assert(!IsEraser(&priv));
589 	assert(IsCursor(&priv));
590 	assert(!IsPad(&priv));
591 
592 	reset(info, priv, tool, common);
593 	rc = wcmSetType(&info, "pad");
594 	assert(rc == 1);
595 	assert(is_absolute(&info));
596 	assert(!IsStylus(&priv));
597 	assert(!IsTouch(&priv));
598 	assert(!IsEraser(&priv));
599 	assert(!IsCursor(&priv));
600 	assert(IsPad(&priv));
601 
602 	reset(info, priv, tool, common);
603 	rc = wcmSetType(&info, "foobar");
604 	assert(rc == 0);
605 
606 #undef reset
607 }
608 
test_flag_set(void)609 static void test_flag_set(void)
610 {
611 	int i;
612 	unsigned int flags = 0;
613 
614 	for (i = 0; i < sizeof(flags); i++)
615 	{
616 		int mask = 1 << i;
617 		flags = 0;
618 
619 		assert(!MaskIsSet(flags, mask));
620 		MaskSet(flags, mask);
621 		assert(flags != 0);
622 		assert(MaskIsSet(flags, mask));
623 		MaskClear(flags, mask);
624 		assert(!MaskIsSet(flags, mask));
625 		assert(flags == 0);
626 	}
627 }
628 
main(int argc,char ** argv)629 int main(int argc, char** argv)
630 {
631 	test_common_ref();
632 	test_rebase_pressure();
633 	test_normalize_pressure();
634 	test_suppress();
635 	test_initial_size();
636 	test_tilt_to_rotation();
637 	test_mod_buttons();
638 	test_set_type();
639 	test_flag_set();
640 	test_get_scroll_delta();
641 	test_get_wheel_button();
642 	return 0;
643 }
644 
645 /* vim: set noexpandtab tabstop=8 shiftwidth=8: */
646