1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
|
/*
* Copyright © 2004 Red Hat, Inc.
* Copyright © 2008 Chris Wilson
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of
* Red Hat, Inc. not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Red Hat, Inc. makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Carl D. Worth <cworth@cworth.org>
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#define _GNU_SOURCE 1 /* for feenableexcept() et al */
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#if HAVE_FEENABLEEXCEPT
#include <fenv.h>
#endif
#include <assert.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>
#if HAVE_FCFINI
#include <fontconfig/fontconfig.h>
#endif
#if HAVE_PTHREAD_H
#include <pthread.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_VALGRIND
#include <valgrind.h>
#else
#define RUNNING_ON_VALGRIND 0
#endif
#if HAVE_MEMFAULT
#include <memfault.h>
#define MF(x) x
#else
#define MF(x)
#endif
#include "cairo-test-private.h"
#include "buffer-diff.h"
#ifdef _MSC_VER
#include <crtdbg.h>
#define vsnprintf _vsnprintf
#define access _access
#define F_OK 0
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE !FALSE
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(A) (sizeof(A) / sizeof (A[0]))
#endif
#if ! HAVE_ALARM
#define alarm(X);
#endif
static const cairo_user_data_key_t _cairo_test_context_key;
static void
_xunlink (const cairo_test_context_t *ctx, const char *pathname);
static const char *fail_face = "", *xfail_face="", *normal_face = "";
static cairo_bool_t print_fail_on_stdout;
static int cairo_test_timeout = 60;
#define NUM_DEVICE_OFFSETS 2
static char *
_cairo_test_fixup_name (const char *original)
{
int len = strlen (original);
char *name, *s;
name = xmalloc (len + 1);
s = memcpy (name, original, len + 1);
while ((s = strchr (s, '_')) != NULL)
*s++ = '-';
return name;
}
char *
cairo_test_get_name (const cairo_test_t *test)
{
return _cairo_test_fixup_name (test->name);
}
static void
_cairo_test_init (cairo_test_context_t *ctx,
const cairo_test_context_t *parent,
const cairo_test_t *test,
const char *test_name)
{
char *log_name;
MF (MEMFAULT_DISABLE_FAULTS ());
#if HAVE_FEENABLEEXCEPT
feenableexcept (FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif
ctx->test = test;
ctx->test_name = _cairo_test_fixup_name (test_name);
ctx->malloc_failure = 0;
#if HAVE_MEMFAULT
if (getenv ("CAIRO_TEST_MALLOC_FAILURE"))
ctx->malloc_failure = atoi (getenv ("CAIRO_TEST_MALLOC_FAILURE"));
if (ctx->malloc_failure && ! RUNNING_ON_MEMFAULT ())
ctx->malloc_failure = 0;
#endif
ctx->timeout = cairo_test_timeout;
if (getenv ("CAIRO_TEST_TIMEOUT"))
ctx->timeout = atoi (getenv ("CAIRO_TEST_TIMEOUT"));
xasprintf (&log_name, "%s%s", ctx->test_name, CAIRO_TEST_LOG_SUFFIX);
_xunlink (NULL, log_name);
ctx->log_file = fopen (log_name, "a");
if (ctx->log_file == NULL) {
fprintf (stderr, "Error opening log file: %s\n", log_name);
ctx->log_file = stderr;
}
free (log_name);
ctx->ref_name = NULL;
ctx->ref_image = NULL;
ctx->ref_image_flattened = NULL;
ctx->thread = 0;
if (parent != NULL) {
ctx->targets_to_test = parent->targets_to_test;
ctx->num_targets = parent->num_targets;
ctx->limited_targets = parent->limited_targets;
ctx->own_targets = FALSE;
ctx->srcdir = parent->srcdir;
ctx->refdir = parent->refdir;
} else {
int tmp_num_targets;
cairo_bool_t tmp_limited_targets;
ctx->targets_to_test = cairo_boilerplate_get_targets (&tmp_num_targets, &tmp_limited_targets);
ctx->num_targets = tmp_num_targets;
ctx->limited_targets = tmp_limited_targets;
ctx->own_targets = TRUE;
ctx->srcdir = getenv ("srcdir");
if (ctx->srcdir == NULL)
ctx->srcdir = ".";
ctx->refdir = getenv ("CAIRO_REF_DIR");
}
#ifdef HAVE_UNISTD_H
if (*fail_face == '\0' && isatty (2)) {
fail_face = "\033[41;37;1m";
xfail_face = "\033[43;37;1m";
normal_face = "\033[m";
if (isatty (1))
print_fail_on_stdout = FALSE;
}
#endif
printf ("\nTESTING %s\n", ctx->test_name);
}
void
_cairo_test_context_init_for_test (cairo_test_context_t *ctx,
const cairo_test_context_t *parent,
const cairo_test_t *test)
{
_cairo_test_init (ctx, parent, test, test->name);
}
void
cairo_test_init (cairo_test_context_t *ctx,
const char *test_name)
{
_cairo_test_init (ctx, NULL, NULL, test_name);
}
static void
cairo_test_init_thread (cairo_test_context_t *ctx,
cairo_test_context_t *master,
int thread)
{
MF (MEMFAULT_DISABLE_FAULTS ());
*ctx = *master;
ctx->thread = thread;
}
void
cairo_test_fini (cairo_test_context_t *ctx)
{
if (ctx->thread != 0)
return;
if (ctx->log_file == NULL)
return;
if (ctx->log_file != stderr)
fclose (ctx->log_file);
ctx->log_file = NULL;
if (ctx->ref_name != NULL)
free (ctx->ref_name);
cairo_surface_destroy (ctx->ref_image);
cairo_surface_destroy (ctx->ref_image_flattened);
if (ctx->test_name != NULL)
free ((char *) ctx->test_name);
if (ctx->own_targets)
cairo_boilerplate_free_targets (ctx->targets_to_test);
cairo_debug_reset_static_data ();
#if HAVE_FCFINI
FcFini ();
#endif
}
void
cairo_test_logv (const cairo_test_context_t *ctx,
const char *fmt, va_list va)
{
FILE *file = ctx && ctx->log_file ? ctx->log_file : stderr;
vfprintf (file, fmt, va);
}
void
cairo_test_log (const cairo_test_context_t *ctx, const char *fmt, ...)
{
va_list va;
FILE *file = ctx && ctx->log_file ? ctx->log_file : stderr;
va_start (va, fmt);
vfprintf (file, fmt, va);
va_end (va);
}
void
cairo_test_log_path (const cairo_test_context_t *ctx,
const cairo_path_t *path)
{
int i;
for (i = 0; i < path->num_data; i += path->data[i].header.length) {
cairo_path_data_t *data = &path->data[i];
switch (data->header.type) {
case CAIRO_PATH_MOVE_TO:
cairo_test_log (ctx,
" cairo_move_to (cr, %g, %g);\n",
data[1].point.x, data[1].point.y);
break;
case CAIRO_PATH_LINE_TO:
cairo_test_log (ctx,
" cairo_line_to (cr, %g, %g);\n",
data[1].point.x, data[1].point.y);
break;
case CAIRO_PATH_CURVE_TO:
cairo_test_log (ctx,
" cairo_curve_to (cr, %g, %g, %g, %g, %g, %g);\n",
data[1].point.x, data[1].point.y,
data[2].point.x, data[2].point.y,
data[3].point.x, data[3].point.y);
break;
case CAIRO_PATH_CLOSE_PATH:
cairo_test_log (ctx,
" cairo_close_path (cr);\n\n");
break;
default:
assert (0);
}
}
}
static void
_xunlink (const cairo_test_context_t *ctx, const char *pathname)
{
if (unlink (pathname) < 0 && errno != ENOENT) {
cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
pathname, strerror (errno));
exit (1);
}
}
char *
cairo_test_reference_filename (const cairo_test_context_t *ctx,
const char *base_name,
const char *test_name,
const char *target_name,
const char *base_target_name,
const char *format,
const char *suffix,
const char *extension)
{
char *ref_name = NULL;
/* First look for a previous build for comparison. */
if (ctx->refdir != NULL) {
xasprintf (&ref_name, "%s/%s%s%s",
ctx->refdir,
base_name,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
}
if (target_name != NULL) {
/* Next look for a target/format-specific reference image. */
xasprintf (&ref_name, "%s/%s.%s.%s%s%s",
ctx->srcdir,
test_name,
target_name,
format,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
/* Next, look for target-specific reference image. */
xasprintf (&ref_name, "%s/%s.%s%s%s",
ctx->srcdir,
test_name,
target_name,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
}
if (base_target_name != NULL) {
/* Next look for a base/format-specific reference image. */
xasprintf (&ref_name, "%s/%s.%s.%s%s%s",
ctx->srcdir,
test_name,
base_target_name,
format,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
/* Next, look for base-specific reference image. */
xasprintf (&ref_name, "%s/%s.%s%s%s",
ctx->srcdir,
test_name,
base_target_name,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
}
/* Next, look for format-specific reference image. */
xasprintf (&ref_name, "%s/%s.%s%s%s",
ctx->srcdir,
test_name,
format,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
/* Finally, look for the standard reference image. */
xasprintf (&ref_name, "%s/%s%s%s", ctx->srcdir,
test_name,
suffix,
extension);
if (access (ref_name, F_OK) != 0)
free (ref_name);
else
goto done;
ref_name = NULL;
done:
return ref_name;
}
cairo_bool_t
cairo_test_target_has_similar (const cairo_test_context_t *ctx,
const cairo_boilerplate_target_t *target)
{
cairo_surface_t *surface;
cairo_bool_t has_similar;
cairo_t * cr;
cairo_surface_t *similar;
cairo_status_t status;
void *closure;
/* ignore image intermediate targets */
if (target->expected_type == CAIRO_SURFACE_TYPE_IMAGE)
return FALSE;
if (getenv ("CAIRO_TEST_IGNORE_SIMILAR"))
return FALSE;
do {
do {
surface = (target->create_surface) (ctx->test->name,
target->content,
ctx->test->width,
ctx->test->height,
ctx->test->width + 25 * NUM_DEVICE_OFFSETS,
ctx->test->height + 25 * NUM_DEVICE_OFFSETS,
CAIRO_BOILERPLATE_MODE_TEST,
0,
&closure);
if (surface == NULL)
return FALSE;
} while (cairo_test_malloc_failure (ctx, cairo_surface_status (surface)));
if (cairo_surface_status (surface))
return FALSE;
cr = cairo_create (surface);
cairo_push_group_with_content (cr,
cairo_boilerplate_content (target->content));
similar = cairo_get_group_target (cr);
status = cairo_surface_status (similar);
has_similar = cairo_surface_get_type (similar) == cairo_surface_get_type (surface);
cairo_destroy (cr);
cairo_surface_destroy (surface);
if (target->cleanup)
target->cleanup (closure);
} while (cairo_test_malloc_failure (ctx, status));
return has_similar;
}
static cairo_surface_t *
_cairo_test_flatten_reference_image (cairo_test_context_t *ctx,
cairo_bool_t flatten)
{
cairo_surface_t *surface;
cairo_t *cr;
if (! flatten)
return ctx->ref_image;
if (ctx->ref_image_flattened != NULL)
return ctx->ref_image_flattened;
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
cairo_image_surface_get_width (ctx->ref_image),
cairo_image_surface_get_height (ctx->ref_image));
cr = cairo_create (surface);
cairo_surface_destroy (surface);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr);
cairo_set_source_surface (cr, ctx->ref_image, 0, 0);
cairo_paint (cr);
surface = cairo_surface_reference (cairo_get_target (cr));
cairo_destroy (cr);
if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
ctx->ref_image_flattened = surface;
return surface;
}
cairo_surface_t *
cairo_test_get_reference_image (cairo_test_context_t *ctx,
const char *filename,
cairo_bool_t flatten)
{
cairo_surface_t *surface;
int len;
if (ctx->ref_name != NULL) {
if (strcmp (ctx->ref_name, filename) == 0)
return _cairo_test_flatten_reference_image (ctx, flatten);
cairo_surface_destroy (ctx->ref_image);
ctx->ref_image = NULL;
cairo_surface_destroy (ctx->ref_image_flattened);
ctx->ref_image_flattened = NULL;
free (ctx->ref_name);
ctx->ref_name = NULL;
}
surface = cairo_image_surface_create_from_png (filename);
if (cairo_surface_status (surface))
return surface;
len = strlen (filename);
ctx->ref_name = xmalloc (len + 1);
memcpy (ctx->ref_name, filename, len + 1);
ctx->ref_image = surface;
return _cairo_test_flatten_reference_image (ctx, flatten);
}
static cairo_bool_t
cairo_test_file_is_older (const char *filename,
char **ref_filenames,
int num_ref_filenames)
{
#if HAVE_SYS_STAT_H
struct stat st;
if (stat (filename, &st) < 0)
return FALSE;
while (num_ref_filenames--) {
struct stat ref;
char *ref_filename = *ref_filenames++;
if (ref_filename == NULL)
continue;
if (stat (ref_filename++, &ref) < 0)
continue;
if (st.st_mtime <= ref.st_mtime)
return TRUE;
}
#endif
return FALSE;
}
static cairo_bool_t
cairo_test_files_equal (const char *test_filename,
const char *pass_filename)
{
FILE *test, *pass;
int t, p;
if (test_filename == NULL || pass_filename == NULL)
return FALSE;
test = fopen (test_filename, "rb");
if (test == NULL)
return FALSE;
pass = fopen (pass_filename, "rb");
if (pass == NULL) {
fclose (test);
return FALSE;
}
/* as simple as it gets */
do {
t = getc (test);
p = getc (pass);
if (t != p)
break;
} while (t != EOF && p != EOF);
fclose (pass);
fclose (test);
return t == p; /* both EOF */
}
static cairo_bool_t
cairo_test_copy_file (const char *src_filename,
const char *dst_filename)
{
FILE *src, *dst;
int c;
#if HAVE_LINK
if (link (src_filename, dst_filename) == 0)
return TRUE;
unlink (dst_filename);
#endif
src = fopen (src_filename, "rb");
if (src == NULL)
return FALSE;
dst = fopen (dst_filename, "wb");
if (dst == NULL) {
fclose (src);
return FALSE;
}
/* as simple as it gets */
while ((c = getc (src)) != EOF)
putc (c, dst);
fclose (src);
fclose (dst);
return TRUE;
}
static cairo_bool_t
_cairo_test_mkdir (const char *path)
{
#if ! HAVE_MKDIR
return FALSE;
#elif HAVE_MKDIR == 1
if (mkdir (path) == 0)
return TRUE;
#elif HAVE_MKDIR == 2
if (mkdir (path, 0770) == 0)
return TRUE;
#else
#error Bad value for HAVE_MKDIR
#endif
return errno == EEXIST;
}
static cairo_test_status_t
cairo_test_for_target (cairo_test_context_t *ctx,
const cairo_boilerplate_target_t *target,
int dev_offset,
cairo_bool_t similar)
{
cairo_test_status_t status;
cairo_surface_t *surface = NULL;
cairo_t *cr;
const char *empty_str = "";
char *offset_str, *thread_str;
char *base_name, *base_path;
char *out_png_path;
char *ref_path = NULL, *ref_png_path, *cmp_png_path = NULL;
char *new_path = NULL, *new_png_path;
char *xfail_path = NULL, *xfail_png_path;
char *base_ref_png_path;
char *base_new_png_path;
char *base_xfail_png_path;
char *diff_png_path;
char *test_filename = NULL, *pass_filename = NULL, *fail_filename = NULL;
cairo_test_status_t ret;
cairo_content_t expected_content;
cairo_font_options_t *font_options;
const char *format;
cairo_bool_t have_output = FALSE;
cairo_bool_t have_result = FALSE;
void *closure;
double width, height;
cairo_bool_t have_output_dir;
#if HAVE_MEMFAULT
int malloc_failure_iterations = ctx->malloc_failure;
int last_fault_count = 0;
#endif
/* Get the strings ready that we'll need. */
format = cairo_boilerplate_content_name (target->content);
if (dev_offset)
xasprintf (&offset_str, ".%d", dev_offset);
else
offset_str = (char *) empty_str;
if (ctx->thread)
xasprintf (&thread_str, ".thread%d", ctx->thread);
else
thread_str = (char *) empty_str;
xasprintf (&base_name, "%s.%s.%s%s%s%s",
ctx->test_name,
target->name,
format,
similar ? ".similar" : "",
offset_str,
thread_str);
if (offset_str != empty_str)
free (offset_str);
if (thread_str != empty_str)
free (thread_str);
ref_png_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
target->name,
target->basename,
format,
CAIRO_TEST_REF_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
new_png_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
target->name,
target->basename,
format,
CAIRO_TEST_NEW_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
xfail_png_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
target->name,
target->basename,
format,
CAIRO_TEST_XFAIL_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
base_ref_png_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
NULL, NULL,
format,
CAIRO_TEST_REF_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
base_new_png_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
NULL, NULL,
format,
CAIRO_TEST_NEW_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
base_xfail_png_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
NULL, NULL,
format,
CAIRO_TEST_XFAIL_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
if (target->file_extension != NULL) {
ref_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
target->name,
target->basename,
format,
CAIRO_TEST_REF_SUFFIX,
target->file_extension);
new_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
target->name,
target->basename,
format,
CAIRO_TEST_NEW_SUFFIX,
target->file_extension);
xfail_path = cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
target->name,
target->basename,
format,
CAIRO_TEST_XFAIL_SUFFIX,
target->file_extension);
}
have_output_dir = _cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR);
xasprintf (&base_path, "%s/%s",
have_output_dir ? CAIRO_TEST_OUTPUT_DIR : ".",
base_name);
xasprintf (&out_png_path, "%s" CAIRO_TEST_OUT_PNG, base_path);
xasprintf (&diff_png_path, "%s" CAIRO_TEST_DIFF_PNG, base_path);
if (ctx->test->requirements != NULL) {
const char *required;
required = target->is_vector ? "target=raster" : "target=vector";
if (strstr (ctx->test->requirements, required) != NULL) {
cairo_test_log (ctx, "Error: Skipping for %s target %s\n",
target->is_vector ? "vector" : "raster",
target->name);
ret = CAIRO_TEST_UNTESTED;
goto UNWIND_STRINGS;
}
required = target->is_meta ? "target=!meta" : "target=meta";
if (strstr (ctx->test->requirements, required) != NULL) {
cairo_test_log (ctx, "Error: Skipping for %s target %s\n",
target->is_meta ? "meta" : "non-meta",
target->name);
ret = CAIRO_TEST_UNTESTED;
goto UNWIND_STRINGS;
}
}
width = ctx->test->width;
height = ctx->test->height;
if (width && height) {
width += dev_offset;
height += dev_offset;
}
#if HAVE_MEMFAULT
REPEAT:
MEMFAULT_CLEAR_FAULTS ();
MEMFAULT_RESET_LEAKS ();
ctx->last_fault_count = 0;
last_fault_count = MEMFAULT_COUNT_FAULTS ();
/* Pre-initialise fontconfig so that the configuration is loaded without
* malloc failures (our primary goal is to test cairo fault tolerance).
*/
#if HAVE_FCINIT
FcInit ();
#endif
MEMFAULT_ENABLE_FAULTS ();
#endif
have_output = FALSE;
have_result = FALSE;
/* Run the actual drawing code. */
ret = CAIRO_TEST_SUCCESS;
surface = (target->create_surface) (base_path,
target->content,
width, height,
ctx->test->width + 25 * NUM_DEVICE_OFFSETS,
ctx->test->height + 25 * NUM_DEVICE_OFFSETS,
CAIRO_BOILERPLATE_MODE_TEST,
ctx->thread,
&closure);
if (surface == NULL) {
cairo_test_log (ctx, "Error: Failed to set %s target\n", target->name);
ret = CAIRO_TEST_UNTESTED;
goto UNWIND_STRINGS;
}
#if HAVE_MEMFAULT
if (ctx->malloc_failure &&
MEMFAULT_COUNT_FAULTS () - last_fault_count > 0 &&
cairo_surface_status (surface) == CAIRO_STATUS_NO_MEMORY)
{
goto REPEAT;
}
#endif
if (cairo_surface_status (surface)) {
MF (MEMFAULT_PRINT_FAULTS ());
cairo_test_log (ctx, "Error: Created an error surface: %s\n",
cairo_status_to_string (cairo_surface_status (surface)));
ret = CAIRO_TEST_FAILURE;
goto UNWIND_STRINGS;
}
/* Check that we created a surface of the expected type. */
if (cairo_surface_get_type (surface) != target->expected_type) {
MF (MEMFAULT_PRINT_FAULTS ());
cairo_test_log (ctx, "Error: Created surface is of type %d (expected %d)\n",
cairo_surface_get_type (surface), target->expected_type);
ret = CAIRO_TEST_UNTESTED;
goto UNWIND_SURFACE;
}
/* Check that we created a surface of the expected content,
* (ignore the artificial CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED value).
*/
expected_content = cairo_boilerplate_content (target->content);
if (cairo_surface_get_content (surface) != expected_content) {
MF (MEMFAULT_PRINT_FAULTS ());
cairo_test_log (ctx, "Error: Created surface has content %d (expected %d)\n",
cairo_surface_get_content (surface), expected_content);
ret = CAIRO_TEST_FAILURE;
goto UNWIND_SURFACE;
}
cairo_surface_set_user_data (surface,
&cairo_boilerplate_output_basename_key,
base_path,
NULL);
cairo_surface_set_device_offset (surface, dev_offset, dev_offset);
cr = cairo_create (surface);
if (cairo_set_user_data (cr, &_cairo_test_context_key, (void*) ctx, NULL)) {
#if HAVE_MEMFAULT
cairo_destroy (cr);
cairo_surface_destroy (surface);
if (target->cleanup)
target->cleanup (closure);
goto REPEAT;
#else
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
#endif
}
if (similar)
cairo_push_group_with_content (cr, expected_content);
/* Clear to transparent (or black) depending on whether the target
* surface supports alpha. */
cairo_save (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint (cr);
cairo_restore (cr);
/* Set all components of font_options to avoid backend differences
* and reduce number of needed reference images. */
font_options = cairo_font_options_create ();
cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_ON);
cairo_font_options_set_antialias (font_options, CAIRO_ANTIALIAS_GRAY);
cairo_set_font_options (cr, font_options);
cairo_font_options_destroy (font_options);
cairo_save (cr);
alarm (ctx->timeout);
status = (ctx->test->draw) (cr, ctx->test->width, ctx->test->height);
alarm (0);
cairo_restore (cr);
if (similar) {
cairo_pop_group_to_source (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
}
#if HAVE_MEMFAULT
MEMFAULT_DISABLE_FAULTS ();
/* repeat test after malloc failure injection */
if (ctx->malloc_failure &&
MEMFAULT_COUNT_FAULTS () - last_fault_count > 0 &&
(status == CAIRO_TEST_NO_MEMORY ||
cairo_status (cr) == CAIRO_STATUS_NO_MEMORY ||
cairo_surface_status (surface) == CAIRO_STATUS_NO_MEMORY))
{
cairo_destroy (cr);
cairo_surface_destroy (surface);
if (target->cleanup)
target->cleanup (closure);
if (ctx->thread == 0) {
cairo_debug_reset_static_data ();
#if HAVE_FCFINI
FcFini ();
#endif
if (MEMFAULT_COUNT_LEAKS () > 0) {
MEMFAULT_PRINT_FAULTS ();
MEMFAULT_PRINT_LEAKS ();
}
}
goto REPEAT;
}
#endif
/* Then, check all the different ways it could fail. */
if (status) {
cairo_test_log (ctx, "Error: Function under test failed\n");
ret = status;
goto UNWIND_CAIRO;
}
#if HAVE_MEMFAULT
if (MEMFAULT_COUNT_FAULTS () - last_fault_count > 0 &&
MEMFAULT_HAS_FAULTS ())
{
VALGRIND_PRINTF ("Unreported memfaults...");
MEMFAULT_PRINT_FAULTS ();
}
#endif
if (target->finish_surface != NULL) {
#if HAVE_MEMFAULT
/* We need to re-enable faults as most meta-surface processing
* is done during cairo_surface_finish().
*/
MEMFAULT_CLEAR_FAULTS ();
last_fault_count = MEMFAULT_COUNT_FAULTS ();
MEMFAULT_ENABLE_FAULTS ();
#endif
/* also check for infinite loops whilst replaying */
alarm (ctx->timeout);
status = target->finish_surface (surface);
alarm (0);
#if HAVE_MEMFAULT
MEMFAULT_DISABLE_FAULTS ();
if (ctx->malloc_failure &&
MEMFAULT_COUNT_FAULTS () - last_fault_count > 0 &&
status == CAIRO_STATUS_NO_MEMORY)
{
cairo_destroy (cr);
cairo_surface_destroy (surface);
if (target->cleanup)
target->cleanup (closure);
if (ctx->thread == 0) {
cairo_debug_reset_static_data ();
#if HAVE_FCFINI
FcFini ();
#endif
if (MEMFAULT_COUNT_LEAKS () > 0) {
MEMFAULT_PRINT_FAULTS ();
MEMFAULT_PRINT_LEAKS ();
}
}
goto REPEAT;
}
#endif
if (status) {
cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
cairo_status_to_string (status));
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
}
/* Skip image check for tests with no image (width,height == 0,0) */
if (ctx->test->width != 0 && ctx->test->height != 0) {
cairo_surface_t *ref_image;
cairo_surface_t *test_image;
cairo_surface_t *diff_image;
buffer_diff_result_t result;
cairo_status_t diff_status;
if (ref_png_path == NULL) {
cairo_test_log (ctx, "Error: Cannot find reference image for %s\n",
base_name);
/* we may be running this test to generate reference images */
_xunlink (ctx, out_png_path);
/* be more generous as we may need to use external renderers */
alarm (4 * ctx->timeout);
test_image = target->get_image_surface (surface, 0,
ctx->test->width,
ctx->test->height);
alarm (0);
diff_status = cairo_surface_write_to_png (test_image, out_png_path);
cairo_surface_destroy (test_image);
if (diff_status) {
if (cairo_surface_status (test_image) == CAIRO_STATUS_INVALID_STATUS)
ret = CAIRO_TEST_CRASHED;
else
ret = CAIRO_TEST_FAILURE;
cairo_test_log (ctx,
"Error: Failed to write output image: %s\n",
cairo_status_to_string (diff_status));
}
have_output = TRUE;
ret = CAIRO_TEST_XFAILURE;
goto UNWIND_CAIRO;
}
if (target->file_extension != NULL) { /* compare vector surfaces */
char *filenames[] = {
ref_png_path,
ref_path,
new_png_path,
new_path,
xfail_png_path,
xfail_path,
base_ref_png_path,
base_new_png_path,
base_xfail_png_path,
};
xasprintf (&test_filename, "%s.out%s",
base_path, target->file_extension);
xasprintf (&pass_filename, "%s.pass%s",
base_path, target->file_extension);
xasprintf (&fail_filename, "%s.fail%s",
base_path, target->file_extension);
if (cairo_test_file_is_older (pass_filename,
filenames,
ARRAY_SIZE (filenames)))
{
_xunlink (ctx, pass_filename);
}
if (cairo_test_file_is_older (fail_filename,
filenames,
ARRAY_SIZE (filenames)))
{
_xunlink (ctx, fail_filename);
}
if (cairo_test_files_equal (out_png_path, ref_path)) {
cairo_test_log (ctx, "Vector surface matches reference.\n");
have_output = FALSE;
ret = CAIRO_TEST_SUCCESS;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, new_path)) {
cairo_test_log (ctx, "Vector surface matches current failure.\n");
have_output = FALSE;
ret = CAIRO_TEST_NEW;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, xfail_path)) {
cairo_test_log (ctx, "Vector surface matches known failure.\n");
have_output = FALSE;
ret = CAIRO_TEST_XFAILURE;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (test_filename, pass_filename)) {
/* identical output as last known PASS */
cairo_test_log (ctx, "Vector surface matches last pass.\n");
have_output = TRUE;
ret = CAIRO_TEST_SUCCESS;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (test_filename, fail_filename)) {
/* identical output as last known FAIL, fail */
cairo_test_log (ctx, "Vector surface matches last fail.\n");
have_result = TRUE; /* presume these were kept around as well */
have_output = TRUE;
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
}
/* be more generous as we may need to use external renderers */
alarm (4 * ctx->timeout);
test_image = target->get_image_surface (surface, 0,
ctx->test->width,
ctx->test->height);
alarm (0);
if (cairo_surface_status (test_image)) {
cairo_test_log (ctx, "Error: Failed to extract image: %s\n",
cairo_status_to_string (cairo_surface_status (test_image)));
if (cairo_surface_status (test_image) == CAIRO_STATUS_INVALID_STATUS)
ret = CAIRO_TEST_CRASHED;
else
ret = CAIRO_TEST_FAILURE;
cairo_surface_destroy (test_image);
goto UNWIND_CAIRO;
}
_xunlink (ctx, out_png_path);
diff_status = cairo_surface_write_to_png (test_image, out_png_path);
if (diff_status) {
cairo_test_log (ctx, "Error: Failed to write output image: %s\n",
cairo_status_to_string (diff_status));
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
have_output = TRUE;
/* binary compare png files (no decompression) */
if (target->file_extension == NULL) {
char *filenames[] = {
ref_png_path,
new_png_path,
xfail_png_path,
base_ref_png_path,
base_new_png_path,
base_xfail_png_path,
};
xasprintf (&test_filename, "%s", out_png_path);
xasprintf (&pass_filename, "%s.pass.png", base_path);
xasprintf (&fail_filename, "%s.fail.png", base_path);
if (cairo_test_file_is_older (pass_filename,
filenames,
ARRAY_SIZE (filenames)))
{
_xunlink (ctx, pass_filename);
}
if (cairo_test_file_is_older (fail_filename,
filenames,
ARRAY_SIZE (filenames)))
{
_xunlink (ctx, fail_filename);
}
if (cairo_test_files_equal (test_filename, pass_filename)) {
cairo_test_log (ctx, "PNG file exactly matches last pass.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_SUCCESS;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, ref_png_path)) {
cairo_test_log (ctx, "PNG file exactly reference image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_SUCCESS;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, new_png_path)) {
cairo_test_log (ctx, "PNG file exactly current failure image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_NEW;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, xfail_png_path)) {
cairo_test_log (ctx, "PNG file exactly known failure image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_XFAILURE;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (test_filename, fail_filename)) {
cairo_test_log (ctx, "PNG file exactly matches last fail.\n");
have_result = TRUE; /* presume these were kept around as well */
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
} else {
if (cairo_test_files_equal (out_png_path, ref_png_path)) {
cairo_test_log (ctx, "PNG file exactly matches reference image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_SUCCESS;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, new_png_path)) {
cairo_test_log (ctx, "PNG file exactly matches current failure image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_NEW;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, xfail_png_path)) {
cairo_test_log (ctx, "PNG file exactly matches known failure image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_XFAILURE;
goto UNWIND_CAIRO;
}
}
if (cairo_test_files_equal (out_png_path, base_ref_png_path)) {
cairo_test_log (ctx, "PNG file exactly reference image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_SUCCESS;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, base_new_png_path)) {
cairo_test_log (ctx, "PNG file exactly current failure image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_NEW;
goto UNWIND_CAIRO;
}
if (cairo_test_files_equal (out_png_path, base_xfail_png_path)) {
cairo_test_log (ctx, "PNG file exactly known failure image.\n");
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_XFAILURE;
goto UNWIND_CAIRO;
}
/* first compare against the ideal reference */
ref_image = cairo_test_get_reference_image (ctx, base_ref_png_path,
target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
if (cairo_surface_status (ref_image)) {
cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
base_ref_png_path,
cairo_status_to_string (cairo_surface_status (ref_image)));
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
ctx->test->width,
ctx->test->height);
cmp_png_path = base_ref_png_path;
diff_status = image_diff (ctx,
test_image, ref_image, diff_image,
&result);
_xunlink (ctx, diff_png_path);
if (diff_status ||
(result.pixels_changed &&
result.max_diff > target->error_tolerance))
{
/* that failed, so check against the specific backend */
ref_image = cairo_test_get_reference_image (ctx, ref_png_path,
target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
if (cairo_surface_status (ref_image)) {
cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
ref_png_path,
cairo_status_to_string (cairo_surface_status (ref_image)));
cairo_surface_destroy (test_image);
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
cmp_png_path = ref_png_path;
diff_status = image_diff (ctx,
test_image, ref_image,
diff_image,
&result);
if (diff_status)
{
cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
cairo_status_to_string (diff_status));
ret = CAIRO_TEST_FAILURE;
}
else if (result.pixels_changed &&
result.max_diff > target->error_tolerance)
{
ret = CAIRO_TEST_FAILURE;
diff_status = cairo_surface_write_to_png (diff_image,
diff_png_path);
if (diff_status) {
cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
cairo_status_to_string (diff_status));
} else {
have_result = TRUE;
}
cairo_test_copy_file (test_filename, fail_filename);
}
else
{ /* success */
cairo_test_copy_file (test_filename, pass_filename);
}
}
else
{ /* success */
cairo_test_copy_file (test_filename, pass_filename);
}
/* If failed, compare against the current image output,
* and attempt to detect systematic failures.
*/
if (ret == CAIRO_TEST_FAILURE) {
char *image_out_path;
image_out_path =
cairo_test_reference_filename (ctx,
base_name,
ctx->test_name,
"image",
"image",
format,
CAIRO_TEST_OUT_SUFFIX,
CAIRO_TEST_PNG_EXTENSION);
if (image_out_path != NULL) {
if (cairo_test_files_equal (out_png_path,
image_out_path))
{
ret = CAIRO_TEST_XFAILURE;
}
else
{
ref_image =
cairo_image_surface_create_from_png (image_out_path);
if (cairo_surface_status (ref_image) == CAIRO_STATUS_SUCCESS)
{
diff_status = image_diff (ctx,
test_image, ref_image,
diff_image,
&result);
if (diff_status == CAIRO_STATUS_SUCCESS &&
(result.pixels_changed == 0 ||
result.max_diff > target->error_tolerance))
{
ret = CAIRO_TEST_XFAILURE;
}
cairo_surface_destroy (ref_image);
}
}
free (image_out_path);
}
}
cairo_surface_destroy (test_image);
cairo_surface_destroy (diff_image);
}
if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
cairo_test_log (ctx, "Error: Function under test left cairo status in an error state: %s\n",
cairo_status_to_string (cairo_status (cr)));
ret = CAIRO_TEST_FAILURE;
goto UNWIND_CAIRO;
}
UNWIND_CAIRO:
if (test_filename != NULL) {
free (test_filename);
test_filename = NULL;
}
if (fail_filename != NULL) {
free (fail_filename);
fail_filename = NULL;
}
if (pass_filename != NULL) {
free (pass_filename);
pass_filename = NULL;
}
#if HAVE_MEMFAULT
if (ret == CAIRO_TEST_FAILURE)
MEMFAULT_PRINT_FAULTS ();
#endif
cairo_destroy (cr);
UNWIND_SURFACE:
cairo_surface_destroy (surface);
if (target->cleanup)
target->cleanup (closure);
#if HAVE_MEMFAULT
if (ctx->thread == 0) {
cairo_debug_reset_static_data ();
#if HAVE_FCFINI
FcFini ();
#endif
if (MEMFAULT_COUNT_LEAKS () > 0) {
if (ret != CAIRO_TEST_FAILURE)
MEMFAULT_PRINT_FAULTS ();
MEMFAULT_PRINT_LEAKS ();
}
}
if (ret == CAIRO_TEST_SUCCESS && --malloc_failure_iterations > 0)
goto REPEAT;
#endif
if (ctx->thread == 0) {
if (have_output)
cairo_test_log (ctx, "OUTPUT: %s\n", out_png_path);
if (have_result) {
if (cmp_png_path == NULL) {
/* XXX presume we matched the normal ref last time */
cmp_png_path = ref_png_path;
}
cairo_test_log (ctx,
"REFERENCE: %s\nDIFFERENCE: %s\n",
cmp_png_path, diff_png_path);
}
}
UNWIND_STRINGS:
if (out_png_path)
free (out_png_path);
if (ref_png_path)
free (ref_png_path);
if (base_ref_png_path)
free (base_ref_png_path);
if (ref_path)
free (ref_path);
if (new_png_path)
free (new_png_path);
if (base_new_png_path)
free (base_new_png_path);
if (new_path)
free (new_path);
if (xfail_png_path)
free (xfail_png_path);
if (base_xfail_png_path)
free (base_xfail_png_path);
if (xfail_path)
free (xfail_path);
if (diff_png_path)
free (diff_png_path);
if (base_path)
free (base_path);
if (base_name)
free (base_name);
return ret;
}
#if defined(HAVE_SIGNAL_H) && defined(HAVE_SETJMP_H)
#include <signal.h>
#include <setjmp.h>
/* Used to catch crashes in a test, so that we report it as such and
* continue testing, although one crasher may already have corrupted memory in
* an nonrecoverable fashion. */
static jmp_buf jmpbuf;
static void
segfault_handler (int signal)
{
longjmp (jmpbuf, signal);
}
#endif
cairo_test_status_t
_cairo_test_context_run_for_target (cairo_test_context_t *ctx,
const cairo_boilerplate_target_t *target,
cairo_bool_t similar,
int dev_offset)
{
cairo_test_status_t status;
if (target->get_image_surface == NULL)
return CAIRO_TEST_UNTESTED;
if (similar && ! cairo_test_target_has_similar (ctx, target))
return CAIRO_TEST_UNTESTED;
cairo_test_log (ctx,
"Testing %s with %s%s target (dev offset %d)\n",
ctx->test_name,
similar ? " (similar) " : "",
target->name,
dev_offset);
if (ctx->thread == 0) {
printf ("%s.%s.%s [%d]%s:\t", ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content),
dev_offset,
similar ? " (similar)": "");
fflush (stdout);
}
#if defined(HAVE_SIGNAL_H) && defined(HAVE_SETJMP_H)
if (ctx->thread == 0 && ! RUNNING_ON_VALGRIND) {
void (* volatile old_segfault_handler)(int);
void (* volatile old_segfpe_handler)(int);
void (* volatile old_sigpipe_handler)(int);
void (* volatile old_sigabrt_handler)(int);
void (* volatile old_sigalrm_handler)(int);
/* Set up a checkpoint to get back to in case of segfaults. */
#ifdef SIGSEGV
old_segfault_handler = signal (SIGSEGV, segfault_handler);
#endif
#ifdef SIGFPE
old_segfpe_handler = signal (SIGFPE, segfault_handler);
#endif
#ifdef SIGPIPE
old_sigpipe_handler = signal (SIGPIPE, segfault_handler);
#endif
#ifdef SIGABRT
old_sigabrt_handler = signal (SIGABRT, segfault_handler);
#endif
#ifdef SIGALRM
old_sigalrm_handler = signal (SIGALRM, segfault_handler);
#endif
if (0 == setjmp (jmpbuf))
status = cairo_test_for_target (ctx, target, dev_offset, similar);
else
status = CAIRO_TEST_CRASHED;
#ifdef SIGSEGV
signal (SIGSEGV, old_segfault_handler);
#endif
#ifdef SIGFPE
signal (SIGFPE, old_segfpe_handler);
#endif
#ifdef SIGPIPE
signal (SIGPIPE, old_sigpipe_handler);
#endif
#ifdef SIGABRT
signal (SIGABRT, old_sigabrt_handler);
#endif
#ifdef SIGALRM
signal (SIGALRM, old_sigalrm_handler);
#endif
} else {
status = cairo_test_for_target (ctx, target, dev_offset, similar);
}
#else
status = cairo_test_for_target (ctx, target, dev_offset, similar);
#endif
if (ctx->thread == 0) {
cairo_test_log (ctx,
"TEST: %s TARGET: %s FORMAT: %s OFFSET: %d SIMILAR: %d RESULT: ",
ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content),
dev_offset, similar);
switch (status) {
case CAIRO_TEST_SUCCESS:
printf ("PASS\n");
cairo_test_log (ctx, "PASS\n");
break;
case CAIRO_TEST_UNTESTED:
printf ("UNTESTED\n");
cairo_test_log (ctx, "UNTESTED\n");
break;
default:
case CAIRO_TEST_CRASHED:
if (print_fail_on_stdout && ctx->thread == 0) {
printf ("!!!CRASHED!!!\n");
} else {
/* eat the test name */
printf ("\r");
fflush (stdout);
}
cairo_test_log (ctx, "CRASHED\n");
fprintf (stderr, "%s.%s.%s [%d]%s:\t%s!!!CRASHED!!!%s\n",
ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content), dev_offset, similar ? " (similar)" : "",
fail_face, normal_face);
break;
case CAIRO_TEST_XFAILURE:
if (print_fail_on_stdout && ctx->thread == 0) {
printf ("XFAIL\n");
} else {
/* eat the test name */
printf ("\r");
fflush (stdout);
}
fprintf (stderr, "%s.%s.%s [%d]%s:\t%sXFAIL%s\n",
ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content), dev_offset, similar ? " (similar)" : "",
xfail_face, normal_face);
cairo_test_log (ctx, "XFAIL\n");
break;
case CAIRO_TEST_NEW:
if (print_fail_on_stdout && ctx->thread == 0) {
printf ("NEW\n");
} else {
/* eat the test name */
printf ("\r");
fflush (stdout);
}
fprintf (stderr, "%s.%s.%s [%d]%s:\t%sNEW%s\n",
ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content), dev_offset, similar ? " (similar)" : "",
fail_face, normal_face);
cairo_test_log (ctx, "NEW\n");
break;
case CAIRO_TEST_NO_MEMORY:
case CAIRO_TEST_FAILURE:
if (print_fail_on_stdout && ctx->thread == 0) {
printf ("FAIL\n");
} else {
/* eat the test name */
printf ("\r");
fflush (stdout);
}
fprintf (stderr, "%s.%s.%s [%d]%s:\t%sFAIL%s\n",
ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content), dev_offset, similar ? " (similar)" : "",
fail_face, normal_face);
cairo_test_log (ctx, "FAIL\n");
break;
}
fflush (stdout);
} else {
#if HAVE_FLOCKFILE && HAVE_FUNLOCKFILE
flockfile (stdout);
#endif
printf ("%s.%s.%s %d [%d]:\t",
ctx->test_name, target->name,
cairo_boilerplate_content_name (target->content),
ctx->thread,
dev_offset);
switch (status) {
case CAIRO_TEST_SUCCESS:
printf ("PASS\n");
break;
case CAIRO_TEST_UNTESTED:
printf ("UNTESTED\n");
break;
default:
case CAIRO_TEST_CRASHED:
printf ("!!!CRASHED!!!\n");
break;
case CAIRO_TEST_XFAILURE:
printf ("XFAIL\n");
break;
case CAIRO_TEST_NEW:
printf ("NEW\n");
break;
case CAIRO_TEST_NO_MEMORY:
case CAIRO_TEST_FAILURE:
printf ("FAIL\n");
break;
}
fflush (stdout);
#if HAVE_FLOCKFILE && HAVE_FUNLOCKFILE
funlockfile (stdout);
#endif
}
return status;
}
cairo_test_status_t
_cairo_test_context_run (cairo_test_context_t *ctx)
{
size_t i, j;
cairo_test_status_t ret;
ret = CAIRO_TEST_UNTESTED;
if (ctx->test->preamble != NULL)
ret = ctx->test->preamble (ctx);
if (ctx->test->draw == NULL)
return ret;
/* The intended logic here is that we return overall SUCCESS
* iff. there is at least one tested backend and that all tested
* backends return SUCCESS, OR, there's backends were manually
* limited, and none were tested.
* In other words:
*
* if backends limited and no backend tested
* -> SUCCESS
* else if any backend not SUCCESS
* -> FAILURE
* else if all backends UNTESTED
* -> FAILURE
* else (== some backend SUCCESS)
* -> SUCCESS
*
* Also, on a crash, run no further tests.
*/
for (i = 0; i < ctx->num_targets && ret != CAIRO_TEST_CRASHED; i++) {
const cairo_boilerplate_target_t *target = ctx->targets_to_test[(i + ctx->thread) % ctx->num_targets];
for (j = 0; j < NUM_DEVICE_OFFSETS; j++) {
int dev_offset = ((j + ctx->thread) % NUM_DEVICE_OFFSETS) * 25;
int similar, has_similar;
has_similar = cairo_test_target_has_similar (ctx, target);
for (similar = 0; similar <= has_similar ; similar++) {
cairo_status_t status;
status = _cairo_test_context_run_for_target (ctx,
target,
similar,
dev_offset);
if (ret == CAIRO_TEST_UNTESTED)
ret = status;
}
}
}
return ret;
}
#if HAVE_PTHREAD_H
typedef struct _cairo_test_thread {
pthread_t thread;
cairo_test_context_t *ctx;
size_t id;
} cairo_test_thread_t;
static void *
cairo_test_run_threaded (void *closure)
{
cairo_test_thread_t *arg = closure;
cairo_test_context_t ctx;
cairo_test_status_t ret;
cairo_test_init_thread (&ctx, arg->ctx, arg->id);
ret = _cairo_test_context_run (&ctx);
cairo_test_fini (&ctx);
return (void *) ret;
}
#endif
static cairo_test_status_t
cairo_test_expecting (const cairo_test_t *test)
{
cairo_test_context_t ctx;
cairo_test_status_t ret = CAIRO_TEST_SUCCESS;
size_t num_threads;
_cairo_test_init (&ctx, NULL, test, test->name);
printf ("%s\n", test->description);
#if HAVE_PTHREAD_H
num_threads = 0;
if (getenv ("CAIRO_TEST_NUM_THREADS"))
num_threads = atoi (getenv ("CAIRO_TEST_NUM_THREADS"));
if (num_threads > 1) {
cairo_test_thread_t *threads;
size_t n;
threads = xmalloc (sizeof (cairo_test_thread_t) * num_threads);
for (n = 0; n < num_threads; n++) {
threads[n].ctx = &ctx;
threads[n].id = n + 1;
pthread_create (&threads[n].thread, NULL,
cairo_test_run_threaded, &threads[n]);
}
for (n = 0; n < num_threads; n++) {
void *tmp;
pthread_join (threads[n].thread, &tmp);
if (ret == CAIRO_TEST_SUCCESS)
ret = (cairo_test_status_t) tmp;
}
free (threads);
}
if (ret == CAIRO_TEST_SUCCESS)
#endif
ret = _cairo_test_context_run (&ctx);
if (ret != CAIRO_TEST_SUCCESS)
printf ("Check %s%s out for more information.\n", ctx.test_name, CAIRO_TEST_LOG_SUFFIX);
cairo_test_fini (&ctx);
return ret;
}
cairo_test_status_t
cairo_test (const cairo_test_t *test)
{
#ifdef _MSC_VER
/* We don't want an assert dialog, we want stderr */
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
#endif
return cairo_test_expecting (test);
}
const cairo_test_context_t *
cairo_test_get_context (cairo_t *cr)
{
return cairo_get_user_data (cr, &_cairo_test_context_key);
}
cairo_surface_t *
cairo_test_create_surface_from_png (const cairo_test_context_t *ctx,
const char *filename)
{
cairo_surface_t *image;
cairo_status_t status;
image = cairo_image_surface_create_from_png (filename);
status = cairo_surface_status (image);
if (status == CAIRO_STATUS_FILE_NOT_FOUND) {
/* expect not found when running with srcdir != builddir
* such as when 'make distcheck' is run
*/
if (ctx->srcdir) {
char *srcdir_filename;
xasprintf (&srcdir_filename, "%s/%s", ctx->srcdir, filename);
cairo_surface_destroy (image);
image = cairo_image_surface_create_from_png (srcdir_filename);
free (srcdir_filename);
}
}
return image;
}
cairo_pattern_t *
cairo_test_create_pattern_from_png (const cairo_test_context_t *ctx,
const char *filename)
{
cairo_surface_t *image;
cairo_pattern_t *pattern;
image = cairo_test_create_surface_from_png (ctx, filename);
pattern = cairo_pattern_create_for_surface (image);
cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
cairo_surface_destroy (image);
return pattern;
}
static cairo_surface_t *
_draw_check (int width, int height)
{
cairo_surface_t *surface;
cairo_t *cr;
surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 12, 12);
cr = cairo_create (surface);
cairo_surface_destroy (surface);
cairo_set_source_rgb (cr, 0.75, 0.75, 0.75); /* light gray */
cairo_paint (cr);
cairo_set_source_rgb (cr, 0.25, 0.25, 0.25); /* dark gray */
cairo_rectangle (cr, width / 2, 0, width / 2, height / 2);
cairo_rectangle (cr, 0, height / 2, width / 2, height / 2);
cairo_fill (cr);
surface = cairo_surface_reference (cairo_get_target (cr));
cairo_destroy (cr);
return surface;
}
void
cairo_test_paint_checkered (cairo_t *cr)
{
cairo_surface_t *check;
check = _draw_check (12, 12);
cairo_save (cr);
cairo_set_source_surface (cr, check, 0, 0);
cairo_surface_destroy (check);
cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
cairo_paint (cr);
cairo_restore (cr);
}
cairo_bool_t
cairo_test_is_target_enabled (const cairo_test_context_t *ctx,
const char *target)
{
size_t i;
for (i = 0; i < ctx->num_targets; i++) {
const cairo_boilerplate_target_t *t = ctx->targets_to_test[i];
if (strcmp (t->name, target) == 0) {
/* XXX ask the target whether is it possible to run?
* e.g. the xlib backend could check whether it is able to connect
* to the Display.
*/
return t->get_image_surface != NULL;
}
}
return FALSE;
}
cairo_bool_t
cairo_test_malloc_failure (const cairo_test_context_t *ctx,
cairo_status_t status)
{
if (! ctx->malloc_failure)
return FALSE;
if (status != CAIRO_STATUS_NO_MEMORY)
return FALSE;
#if HAVE_MEMFAULT
{
int n_faults;
/* prevent infinite loops... */
n_faults = MEMFAULT_COUNT_FAULTS ();
if (n_faults == ctx->last_fault_count)
return FALSE;
((cairo_test_context_t *) ctx)->last_fault_count = n_faults;
}
#endif
return TRUE;
}
cairo_test_status_t
cairo_test_status_from_status (const cairo_test_context_t *ctx,
cairo_status_t status)
{
if (status == CAIRO_STATUS_SUCCESS)
return CAIRO_TEST_SUCCESS;
if (cairo_test_malloc_failure (ctx, status))
return CAIRO_TEST_NO_MEMORY;
return CAIRO_TEST_FAILURE;
}
|