Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8f0a55ad96bc50fa3953c6d1ae208a0b67e42257 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2005, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
 *								bug 320170
 *								bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
 *******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
import org.eclipse.jdt.internal.compiler.flow.UnconditionalFlowInfo;
import org.eclipse.jdt.internal.compiler.flow.UnconditionalFlowInfo.AssertionFailedException;
import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;

/**
 * A tests series especially meant to validate the internals of our null
 * reference analysis. See NullReferenceTest for tests targetted at
 * the source code compiler behavior level.
 */
public class NullReferenceImplTests extends NullReferenceTest {
	// Static initializer to specify tests subset using TESTS_* static variables
  	// All specified tests which does not belong to the class are skipped...
  	// Only the highest compliance level is run; add the VM argument
  	// -Dcompliance=1.4 (for example) to lower it if needed
  	static {
//    	TESTS_NAMES = new String[] { "test2050" };
//    	TESTS_NUMBERS = new int[] { 2061 };
//    	TESTS_NUMBERS = new int[] { 2999 };
//    	TESTS_RANGE = new int[] { 2050, -1 };
  	}

/**
 * A class to hold states as seen by the low level validation tests and machinery.
 * State provides:
 * - singletons for all possible states given the number of bits for the said
 *   states;
 * - semantic names for known states;
 * - printable representation of states as bit fields;
 * - coordination with other classes to perform transitive closure analysis, etc.
 */
	/*
		This is a tabular definition for states. It can be completed/leveraged by
		the Generator class so as to smoothen the transition between differing encodings
		of the states.
	// STATES DEFINITION START
			000000	start
			000001
			000010
			000011
			000100	pot. unknown
			000101
			000110
			000111
			001000	pot. non null
			001001
			001010
			001011
			001100	pot. nn & pot. un
			001101
			001110
			001111
			010000	pot. null
			010001
			010010
			010011
			010100	pot. n & pot. un
			010101
			010110
			010111
			011000	pot. n & pot. nn
			011001
			011010
			011011
			011100  pot. n & pot. nn & pot. un
			011101
			011110
			011111
			100000
			100001
			100010
			100011
			100100	def. unknown
			100101
			100110
			100111
			101000	def. non null
			101001
			101010
			101011
			101100	pot. nn & prot. nn
			101101
			101110
			101111
			110000	def. null
			110001
			110010
			110011
			110100	pot. n & prot. n
			110101
			110110
			110111
			111000	prot. null
			111001
			111010
			111011
			111100	prot. non null
			111101
			111110
			111111
	// STATES DEFINITION END
	*/
	public static class State implements Comparable {
		// PREMATURE consider moving initialization to test setup/dispose
		public final static State[] states = {
			// STATES INITIALIZER START
			new State(0, "start"), // 000000
			new State(1), // 000001
			new State(2), // 000010
			new State(3), // 000011
			new State(4, "pot. unknown"), // 000100
			new State(5), // 000101
			new State(6), // 000110
			new State(7), // 000111
			new State(8, "pot. non null"), // 001000
			new State(9), // 001001
			new State(10), // 001010
			new State(11), // 001011
			new State(12, "pot. nn & pot. un"), // 001100
			new State(13), // 001101
			new State(14), // 001110
			new State(15), // 001111
			new State(16, "pot. null"), // 010000
			new State(17), // 010001
			new State(18), // 010010
			new State(19), // 010011
			new State(20, "pot. n & pot. un"), // 010100
			new State(21), // 010101
			new State(22), // 010110
			new State(23), // 010111
			new State(24, "pot. n & pot. nn"), // 011000
			new State(25), // 011001
			new State(26), // 011010
			new State(27), // 011011
			new State(28), // 011100
			new State(29), // 011101
			new State(30), // 011110
			new State(31), // 011111
			new State(32), // 100000
			new State(33), // 100001
			new State(34), // 100010
			new State(35), // 100011
			new State(36, "def. unknown"), // 100100
			new State(37), // 100101
			new State(38), // 100110
			new State(39), // 100111
			new State(40, "def. non null"), // 101000
			new State(41), // 101001
			new State(42), // 101010
			new State(43), // 101011
			new State(44, "pot. nn & prot. nn"), // 101100
			new State(45), // 101101
			new State(46), // 101110
			new State(47), // 101111
			new State(48, "def. null"), // 110000
			new State(49), // 110001
			new State(50), // 110010
			new State(51), // 110011
			new State(52, "pot. n & prot. n"), // 110100
			new State(53), // 110101
			new State(54), // 110110
			new State(55), // 110111
			new State(56, "prot. null"), // 111000
			new State(57), // 111001
			new State(58), // 111010
			new State(59), // 111011
			new State(60, "prot. non null"), // 111100
			new State(61), // 111101
			new State(62), // 111110
			new State(63), // 111111
			// STATES INITIALIZER END
		};
		public final static State start = states[0];
		public static final int
			stateMaxValue = 0x3F,
			stateWidth = 6,
			statesNb = stateMaxValue + 1;
		String name, printableBitsField, hexString;
		public byte value;
		boolean symbolic;
	private State() {
	}
	private State(int numericValue) {
		this(numericValue, null);
	}
	private State(int numericValue, String publicName) {
		if (numericValue > stateMaxValue) {
			throw new IllegalArgumentException("state value overflow");
		}
		this.value = (byte) numericValue;
		StringBuffer printableValue = new StringBuffer(6);
		for (int i = stateWidth - 1; i >= 0; i--) {
			printableValue.append((numericValue >>> i & 1) != 0 ? '1' : '0');
		}
		this.printableBitsField = printableValue.toString();
		if (this.value > 0xF) {
			this.hexString = "0x" + Integer.toHexString(this.value).toUpperCase();
		}
		else {
			this.hexString = "0x0" + Integer.toHexString(this.value).toUpperCase();
		}
		if (publicName != null) {
			this.name = publicName;
			this.symbolic = true;
		}
		else {
			this.name = this.printableBitsField;
		}
	}
	private State(String commentLine) {
		char current = ' '; // keep the initialization status quiet
		int cursor, length;
		for (cursor = 0, length = commentLine.length();
			cursor < length;
			cursor++) {
			if ((current = commentLine.charAt(cursor)) == '0' ||
					current == '1') {
				break;
			}
		}
		if (cursor == length) {
			throw new RuntimeException("bad state definition format (missing bits field): " + commentLine);
			// PREMATURE adopt consistent error policy
		}
		int valueDigits;
		for (valueDigits = 1; cursor < (length - 1) && valueDigits < stateWidth; valueDigits++) {
			this.value = (byte) ((this.value << 1) + (current - '0'));
			if ((current = commentLine.charAt(++cursor)) != '0' &&
					current != '1') {
				throw new RuntimeException("bad state definition format (inappropriate character in bits field): " + commentLine);
				// PREMATURE adopt consistent error policy
			}
		}
		if (valueDigits < stateWidth) {
			throw new RuntimeException("bad state definition format (bits field is too short): " + commentLine);
			// PREMATURE adopt consistent error policy
		}
		this.value = (byte) ((this.value << 1) + (current - '0'));
		this.printableBitsField = commentLine.substring(cursor - stateWidth + 1, cursor + 1);
		if (this.value > 0xF) {
			this.hexString = "0x" + Integer.toHexString(this.value).toUpperCase();
		}
		else {
			this.hexString = "0x0" + Integer.toHexString(this.value).toUpperCase();
		}
		while (++cursor < length && Character.isWhitespace(current = commentLine.charAt(++cursor)) && current != '\n') {
			// loop
		}
		if (cursor < length && current != '\n') {
			this.name = commentLine.substring(cursor, length);
		}
		if (this.name == null) {
			this.name = this.printableBitsField;
		} else {
			this.symbolic = true;
		}
	}
	private String asInitializer() {
		StringBuffer result = new StringBuffer(70);
		result.append("		new State(");
		result.append(this.value);
		char first;
		boolean nameIsSymbolic = (first = this.name.charAt(0)) != '0'
			&& first != '1';
		if (nameIsSymbolic) {
			result.append(", \"");
			result.append(this.name);
			result.append('"');
		}
		result.append("), // ");
		result.append(this.printableBitsField);
		return result.toString();
	}
	long [] asLongArray() {
		long[] result = new long[stateWidth];
		for (int i = 0; i < stateWidth; i++) {
			result[i] = ((this.value >> (stateWidth - i - 1)) & 1) == 0 ? 0 : 1;
		}
		return result;
	}
	private String asSourceComment() {
		StringBuffer result = new StringBuffer(70);
		result.append("\t\t");
		result.append(this.printableBitsField);
		char first;
		boolean nameIsSymbolic = (first = this.name.charAt(0)) != '0'
			&& first != '1';
		if (nameIsSymbolic) {
			result.append('\t');
			result.append(this.name);
		}
		return result.toString();
	}
	public int compareTo(Object o) {
		return this.value - ((State) o).value;
	}
	static State fromLongValues(long bit1, long bit2, long bit3, long bit4, long bit5, long bit6) {
		// PREMATURE consider taking an UnconditionalFlowInfo in parameter
		return states[(int)(
			(bit6 & 1) +
				2 * ((bit5 & 1) +
					2 * ((bit4 & 1) +
						2 * ((bit3 & 1) +
							2 * ((bit2 & 1) +
								2 * (bit1 & 1))))))];
	}
	private static Map namesIndex;
	static State fromSymbolicName (String name) {
		if (namesIndex == null) {
			namesIndex = new HashMap(states.length);
			for (int i = 0; i < states.length; i++) {
				if (states[i].name != null) {
					namesIndex.put(states[i].name, states[i]);
				}
			}
		}
		return (State) namesIndex.get(name);
	}
	private static void grabDefinitionFromComment(BufferedReader input) {
		String line;
		State current;
	// use when the initializer is incomplete, hence needs to be reinitialized
	//	states = new State[stateMaxValue + 1];
	// use when the states field is final, with the appropriate size:
		for (int i = 0; i <= stateMaxValue; i++) {
			states[i] = null;
		}
		try {
			while ((line = input.readLine()) != null && line.indexOf(definitionEndMarker) == -1) {
				current = new State(line);
				if (states[current.value] != null) {
					throw new RuntimeException("duplicate state for index: " + current.value);
				}
				else {
					states[current.value] = current;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		for (int i = 0; i < stateMaxValue; i++) {
			if (states[i] == null) {
				states[i] = new State(i);
			}
		}
	}
	// PREMATURE may decide to remove
	//private static void printAsInitializer() {
	//	int i, length;
	//	System.out.println(initializerStartMarker);
	//	for (i = 0, length = states.length; i < length; i++) {
	//		System.out.println(states[i].asInitializer());
	//	}
	//	for (/* continue */; i <= stateMaxValue; i++) {
	//		System.out.println((new State(i)).asInitializer() + " CHECK");
	//	}
	//	System.out.println(initializerEndMarker);
	//}
	// PREMATURE may decide to remove
	//private static void printAsSourceComment() {
	//	int i, length;
	//	System.out.println("/*");
	//	System.out.println(definitionStartMarker);
	//	for (i = 0, length = states.length; i < length; i++) {
	//		System.out.println(states[i].asSourceComment());
	//	}
	//	for (/* continue */; i <= stateMaxValue; i++) {
	//		System.out.println((new State(i)).asSourceComment());
	//	}
	//	System.out.println(definitionEndMarker);
	//	System.out.println("*/");
	//}
	private final static String
		definitionStartMarker = "// STATES " + CodeAnalysis.definitionStartMarker,
		definitionEndMarker = "// STATES " + CodeAnalysis.definitionEndMarker,
		initializerStartMarker = "// STATES " + CodeAnalysis.initializerStartMarker,
		initializerEndMarker = "// STATES " + CodeAnalysis.initializerEndMarker;
	static void reinitializeFromComment(BufferedReader input, BufferedWriter output) {
		String line, tab = "";
		int cursor;
		char c;
		try {
			while ((line = input.readLine()) != null) {
				output.write(line);
				output.write('\n');
				if ((cursor = line.indexOf(definitionStartMarker)) != -1) {
					// check the line format
					boolean reachedStart = true;
					for (int i = 0; i < cursor; i++) {
						if (!Character.isWhitespace(c = line.charAt(i))) {
							reachedStart = false;
							break;
						}
						else {
							tab += c;
						}
					}
					if (reachedStart) {
						grabDefinitionFromComment(input); // consumes up to the END line
						int i, length;
						for (i = 0, length = states.length; i < length; i++) {
							output.write(states[i].asSourceComment());
							output.write('\n');
						}
						output.write(tab + definitionEndMarker + "\n");
					}
				}
				if ((cursor = line.indexOf(initializerStartMarker)) != -1) {
					// check the line format
					boolean reachedStart = true;
					tab = "";
					for (int i = 0; i < cursor; i++) {
						if (!Character.isWhitespace(c = line.charAt(i))) {
							reachedStart = false;
							break;
						}
						else {
							tab += c;
						}
					}
					if (reachedStart) {
						while ((line = input.readLine()) != null &&
								line.indexOf(initializerEndMarker) == -1) {
							// loop
						}
						int i, length;
						for (i = 0, length = states.length; i < length; i++) {
							output.write(states[i].asInitializer());
							output.write('\n');
						}
						output.write(tab + initializerEndMarker + "\n");
					}
				}
			}
			output.flush();
			namesIndex = null;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	static Iterator symbolicStates() {
		return new Iterator() {
			int nextSymbolic = -1;
			public boolean hasNext() {
				if (this.nextSymbolic == -1) {
					for (this.nextSymbolic = 0; this.nextSymbolic < states.length; this.nextSymbolic++) {
						if (states[this.nextSymbolic].symbolic) {
							break;
						}
					}
				} else {
					for (; this.nextSymbolic < states.length; this.nextSymbolic++) {
						if (states[this.nextSymbolic].symbolic) {
							break;
						}
					}
				}
				return this.nextSymbolic < states.length;
			}
			public Object next() {
				State result = null;
				if (this.nextSymbolic < states.length) {
					result = states[this.nextSymbolic];
					this.nextSymbolic++;
				}
				return result;
			}
			public void remove() {
				throw new RuntimeException("unimplemented");
			}
		};
	}
	public String toString() {
		return this.name;
	}
	}

public NullReferenceImplTests(String name) {
    super(name);
}

  	// Tests tuning
	// private static final boolean skipHighOrderBits = false; // define to true when tuning encoding
	private static final int COMBINATION_TESTS_LOOP_NB = 1; // define to 10000s to measure performances
	private static final boolean MEASURE_PERFORMANCES = COMBINATION_TESTS_LOOP_NB > 1;

public static Test suite() {
	// we do not want to run for 1.3, 1.4, 1.5 but once only
    Class clazz = testClass();
    TestSuite all = new TestSuite(clazz.getName());
    List tests = buildTestsList(testClass());
    for (int i = 0, length = tests.size(); i < length; i++) {
    	all.addTest((Test) tests.get(i));
    }
	return all;
}

public static Class testClass() {
    return NullReferenceImplTests.class;
}

public void test2050_markAsComparedEqualToNonNull() {
	int failures = NullReferenceImplTransformations.markAsComparedEqualToNonNull.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2051_markAsComparedEqualToNull() {
	int failures = NullReferenceImplTransformations.markAsComparedEqualToNull.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2055_markAsDefinitelyNonNull() {
	int failures = NullReferenceImplTransformations.markAsDefinitelyNonNull.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2056_markAsDefinitelyNull() {
	int failures = NullReferenceImplTransformations.markAsDefinitelyNull.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2057_markAsDefinitelyUnknown() {
	int failures = NullReferenceImplTransformations.markAsDefinitelyUnknown.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2060_addInitializationsFrom() {
	int failures = NullReferenceImplTransformations.addInitializationsFrom.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2061_addPotentialInitializationsFrom() {
	int failures = NullReferenceImplTransformations.addPotentialInitializationsFrom.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2062_mergedWith() {
	int failures = NullReferenceImplTransformations.mergedWith.test();
	assertTrue("nb of failures: " + failures, failures == 0);
}

// PREMATURE rewrite from scratch
//public void _test2058_recode() {
//	long [][][] testData = transitionsTablesData[recode];
//	int failures = 0;
//	long start;
//	if (combinationTestsloopsNb > 1) {
//		start = System.currentTimeMillis();
//	}
//	String header = "recode failures: ";
//	for (int l = 0; l < combinationTestsloopsNb ; l++) {
//		for (int i = 0; i < testData.length; i++) {
//			UnconditionalFlowInfoTestHarness result;
//			result = UnconditionalFlowInfoTestHarness.
//						testUnconditionalFlowInfo(testData[i][0]);
//			result.encode();
//			result.decode();
//
//			if (!result.testEquals(UnconditionalFlowInfoTestHarness.
//						testUnconditionalFlowInfo(testData[i][0]))) {
//				if (failures == 0) {
//					System.out.println(header);
//				}
//				failures++;
//				System.out.println("\t\t{" + result.testString() +
//					"}, // instead of: " + testStringValueOf(testData[i][0]));
//			}
//		}
//	}
//	if (combinationTestsloopsNb > 1) {
//		System.out.println("mergedWith\t\t\t" + combinationTestsloopsNb + "\t" +
//				(System.currentTimeMillis() - start));
//	}
//	for (int i = 0; i < testData.length; i++) {
//		UnconditionalFlowInfoTestHarness result;
//		result = UnconditionalFlowInfoTestHarness.
//					testUnconditionalFlowInfo(testData[i][0], 64);
//		result.encode();
//		result.decode();
//
//		if (!result.testEquals(UnconditionalFlowInfoTestHarness.
//					testUnconditionalFlowInfo(testData[i][0], 64))) {
//			if (failures == 0) {
//				System.out.println(header);
//			}
//			failures++;
//			System.out.println("\t\t{" + result.testString() +
//				"}, // (64) - instead of: " + testStringValueOf(testData[i][0]));
//		}
//	}
//	assertTrue("nb of failures: " + failures, failures == 0);
//}

public void test2400_state_consistency() {
	int failures = 0;
	long start;
	if (MEASURE_PERFORMANCES) {
		start = System.currentTimeMillis();
	}
	String header = "state consistency failures: ";
	for (int l = 0; l < COMBINATION_TESTS_LOOP_NB ; l++) {
		for (int i = 0; i < State.states.length; i++) {
			if (State.states[i].symbolic) {
				UnconditionalFlowInfoTestHarness
					state = UnconditionalFlowInfoTestHarness.
							testUnconditionalFlowInfo(State.states[i]);
				boolean
					isDefinitelyNonNull = state.isDefinitelyNonNull(TestLocalVariableBinding.local0),
					isDefinitelyNull = state.isDefinitelyNull(TestLocalVariableBinding.local0),
					isDefinitelyUnknown = state.isDefinitelyUnknown(TestLocalVariableBinding.local0),
					isPotentiallyNonNull = state.isPotentiallyNonNull(TestLocalVariableBinding.local0),
					isPotentiallyNull = state.isPotentiallyNull(TestLocalVariableBinding.local0),
					isPotentiallyUnknown = state.isPotentiallyUnknown(TestLocalVariableBinding.local0),
					isProtectedNonNull = state.isProtectedNonNull(TestLocalVariableBinding.local0),
					isProtectedNull = state.isProtectedNull(TestLocalVariableBinding.local0),
					cannotBeDefinitelyNullOrNonNull = state.cannotBeDefinitelyNullOrNonNull(TestLocalVariableBinding.local0),
					cannotBeNull = state.cannotBeNull(TestLocalVariableBinding.local0),
					canOnlyBeNull = state.canOnlyBeNull(TestLocalVariableBinding.local0);
				if (isDefinitelyNonNull
							&& (isDefinitelyNull || isDefinitelyUnknown
									|| isPotentiallyNull
									|| isProtectedNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage for definitely non null state " + State.states[i].name);
				}
				if (isDefinitelyNull
							&& (isDefinitelyNonNull || isDefinitelyUnknown
									|| isPotentiallyUnknown || isProtectedNonNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage for definitely null state " + State.states[i].name);
				}
				if (isDefinitelyUnknown
							&& (isDefinitelyNonNull || isDefinitelyNull
									|| isPotentiallyNull || isProtectedNonNull
									|| isProtectedNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage for definitely unknown state " + State.states[i].name);
				}
				if (isProtectedNonNull && !isDefinitelyNonNull
						|| isProtectedNull && !isDefinitelyNull
						|| i > 0 // not start
							&& !State.states[i].name.equals("pot. non null")
							&& !(isDefinitelyNonNull || isDefinitelyNull
									|| isDefinitelyUnknown || isPotentiallyNull
									|| isPotentiallyUnknown || isProtectedNonNull
									|| isProtectedNull)
						|| cannotBeDefinitelyNullOrNonNull !=
							(isPotentiallyUnknown ||
								isPotentiallyNull && isPotentiallyNonNull)
						|| cannotBeNull != (isProtectedNonNull ||
								isDefinitelyNonNull)
						|| canOnlyBeNull != (isProtectedNull ||
								isDefinitelyNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage for " + State.states[i].name);
				}
			}
		}
	}
	if (MEASURE_PERFORMANCES) {
		System.out.println("mergedWith\t\t\t" + COMBINATION_TESTS_LOOP_NB + "\t" +
				(System.currentTimeMillis() - start));
	}
	for (int i = 0; i < State.states.length; i++) {
		if (State.states[i].symbolic) {
			UnconditionalFlowInfoTestHarness state;
			state = UnconditionalFlowInfoTestHarness.
						testUnconditionalFlowInfo(State.states[i], 64);
			boolean
				isDefinitelyNonNull = state.isDefinitelyNonNull(TestLocalVariableBinding.local64),
				isDefinitelyNull = state.isDefinitelyNull(TestLocalVariableBinding.local64),
				isDefinitelyUnknown = state.isDefinitelyUnknown(TestLocalVariableBinding.local64),
				isPotentiallyNonNull = state.isPotentiallyNonNull(TestLocalVariableBinding.local64),
				isPotentiallyNull = state.isPotentiallyNull(TestLocalVariableBinding.local64),
				isPotentiallyUnknown = state.isPotentiallyUnknown(TestLocalVariableBinding.local64),
				isProtectedNonNull = state.isProtectedNonNull(TestLocalVariableBinding.local64),
				isProtectedNull = state.isProtectedNull(TestLocalVariableBinding.local64),
				cannotBeDefinitelyNullOrNonNull = state.cannotBeDefinitelyNullOrNonNull(TestLocalVariableBinding.local64),
				cannotBeNull = state.cannotBeNull(TestLocalVariableBinding.local64),
				canOnlyBeNull = state.canOnlyBeNull(TestLocalVariableBinding.local64);
				if (isDefinitelyNonNull
							&& (isDefinitelyNull || isDefinitelyUnknown
									|| isPotentiallyNull
									|| isProtectedNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage (64) for definitely non null state " + State.states[i].name);
				}
				if (isDefinitelyNull
							&& (isDefinitelyNonNull || isDefinitelyUnknown
									|| isPotentiallyUnknown || isProtectedNonNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage (64) for definitely null state " + State.states[i].name);
				}
				if (isDefinitelyUnknown
							&& (isDefinitelyNonNull || isDefinitelyNull
									|| isPotentiallyNull || isProtectedNonNull
									|| isProtectedNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage (64) for definitely unknown state " + State.states[i].name);
				}
				if (isProtectedNonNull && !isDefinitelyNonNull
						|| isProtectedNull && !isDefinitelyNull
						|| i > 0 // not start
							&& !State.states[i].name.equals("pot. non null")
							&& !(isDefinitelyNonNull || isDefinitelyNull
									|| isDefinitelyUnknown || isPotentiallyNull
									|| isPotentiallyUnknown || isProtectedNonNull
									|| isProtectedNull)
									|| cannotBeDefinitelyNullOrNonNull !=
										(isPotentiallyUnknown ||
											isPotentiallyNull &&
												isPotentiallyNonNull)
									|| cannotBeNull != (isProtectedNonNull ||
											isDefinitelyNonNull)
									|| canOnlyBeNull != (isProtectedNull ||
											isDefinitelyNull)) {
					if (failures == 0) {
						System.out.println(header);
					}
					failures++;
					System.out.println("\t\tconsistency breakage (64) for " + State.states[i].name);
				}
		}
	}
	assertTrue("nb of failures: " + failures, failures == 0);
}

public void test2500_addInitializationsFrom_for_definites() {
	// when an added initialization is a def. something, it should
	// affect the left hand term as the markAsDefinite* method would
	// do
	int failures = 0;
	for (int i = 0; i < State.states.length; i++) {
		if (State.states[i].symbolic) {
			UnconditionalFlowInfoTestHarness source1, source2, result1, result2;
			source1 = UnconditionalFlowInfoTestHarness.
				testUnconditionalFlowInfo(State.states[i]);
			for (int j = 0; j < State.states.length; j++) {
				if (State.states[j].symbolic) {
					source2 = UnconditionalFlowInfoTestHarness.
						testUnconditionalFlowInfo(State.states[j]);
					result1 = (UnconditionalFlowInfoTestHarness) source1.copy();
					result2 = (UnconditionalFlowInfoTestHarness) source1.copy();
					if (source2.isDefinitelyNonNull(TestLocalVariableBinding.local0)) {
						if (! source2.isProtectedNonNull(TestLocalVariableBinding.local0)) {
							result1.markAsDefinitelyNonNull(TestLocalVariableBinding.local0);
						} else {
							continue;
						}
					}
					else if (source2.isDefinitelyNull(TestLocalVariableBinding.local0)) {
						if (! source2.isProtectedNull(TestLocalVariableBinding.local0)) {
							result1.markAsDefinitelyNull(TestLocalVariableBinding.local0);
						} else {
							continue;
						}
					}
					else if (source2.isDefinitelyUnknown(TestLocalVariableBinding.local0)) {
						result1.markAsDefinitelyUnknown(TestLocalVariableBinding.local0);
					}
					else if (source2.nullBit1 != 0) {
						if (failures == 0) {
							System.out.println("addInitializationsFrom_for_definites failures: "); //$NON-NLS-1$
						}
						failures++;
						System.out.println("\t\t" + State.states[j].name +
							" should answer true to at least one isDefinite* query");
						// PREMATURE move to specific queries test case
					}
					else {
						continue;
					}
					result2.addInitializationsFrom(source2);
					if (!result1.testEquals(result2)) {
						if (failures == 0) {
							System.out.println("addInitializationsFrom_for_definites failures: "); //$NON-NLS-1$
						}
						failures++;
						System.out.println("\t\t" + State.states[i].name +
							" + " + State.states[j].name +
							" => " + result2.asState().name +
							" instead of: " + result1.asState().name);
					}
				}
			}
		}
	}
	assertTrue("nb of failures: " + failures, failures == 0);
}

// Use for coverage tests only. Needs specific instrumentation of code,
// that is controled by UnconditionalFlowInfo#coverageTestFlag.
// Note: coverage tests tend to fill the console with messages, and the
//       instrumented code is slower, so never release code with active
//       coverage tests.
private static int coveragePointsNb = 41;

// PREMATURE reactivate coverage tests
// Coverage by state transition tables methods.
public void test2998_coverage() {
	if (UnconditionalFlowInfo.COVERAGE_TEST_FLAG) {
		// sanity check: need to be sure that the tests execute properly when not
		// trying to check coverage
		UnconditionalFlowInfo.CoverageTestId = 0;
		test0053_array();
		test0070_type_reference();
		test2050_markAsComparedEqualToNonNull();
		test2051_markAsComparedEqualToNull();
		test2055_markAsDefinitelyNonNull();
		test2056_markAsDefinitelyNull();
		test2057_markAsDefinitelyUnknown();
		test2060_addInitializationsFrom();
		test2061_addPotentialInitializationsFrom();
		test2062_mergedWith();
		// coverage check
		int failuresNb = 0;
		for (int i = 1; i <= coveragePointsNb; i++) {
			if (i == 11 || i == 12 || i == 14) {
				continue;
				// these can only be reached via a direct call to addPotentialNullInfoFrom,
				// which is not implemented in low level tests - all those go through
				// addPotentialInitsFrom
			}
			try {
				UnconditionalFlowInfo.CoverageTestId = i;
				test0053_array();
				test0070_type_reference();
				test2050_markAsComparedEqualToNonNull();
				test2051_markAsComparedEqualToNull();
				test2055_markAsDefinitelyNonNull();
				test2056_markAsDefinitelyNull();
				test2057_markAsDefinitelyUnknown();
				test2060_addInitializationsFrom();
				test2061_addPotentialInitializationsFrom();
				test2062_mergedWith();
			}
			catch (AssertionFailedError e) {
				continue;
			}
			catch (AssertionFailedException e) {
				continue;
			}
			failuresNb++;
			System.out.println("Missing coverage point: " + i);
		}
		UnconditionalFlowInfo.CoverageTestId = 0; // reset for other tests
		assertEquals(failuresNb + " missing coverage point(s)", failuresNb, 0);
	}
}

// Coverage by code samples.
public void test2999_coverage() {
	if (UnconditionalFlowInfo.COVERAGE_TEST_FLAG) {
		// sanity check: need to be sure that the tests execute properly when not
		// trying to check coverage
		UnconditionalFlowInfo.CoverageTestId = 0;
		test0001_simple_local();
		test0053_array();
		test0070_type_reference();
		test0327_if_else();
		test0401_while();
		test0420_while();
		test0509_try_finally_embedded();
		test2000_flow_info();
		test2004_flow_info();
		test2008_flow_info();
		test2011_flow_info();
		test2013_flow_info();
		test2018_flow_info();
		test2019_flow_info();
		test2020_flow_info();
		// coverage check
		int failuresNb = 0;
		for (int i = 1; i <= coveragePointsNb; i++) {
			if (i < 3
				|| 4 < i && i < 11
				|| 11 < i && i < 16
				|| 16 < i && i < 20
				|| i == 21
				|| 23 < i && i < 27
				|| 29 < i && i < 33
				|| 36 < i) { // TODO (maxime) complete coverage tests
				continue;
			}
			try {
				UnconditionalFlowInfo.CoverageTestId = i;
				test0001_simple_local();
				test0053_array();
				test0070_type_reference();
				test0327_if_else();
				test0401_while();
				test0420_while();
				test0509_try_finally_embedded();
				test2000_flow_info();
				test2004_flow_info();
				test2008_flow_info();
				test2011_flow_info();
				test2013_flow_info();
				test2018_flow_info();
				test2019_flow_info();
				test2020_flow_info();
			}
			catch (AssertionFailedError e) {
				continue;
			}
			catch (AssertionFailedException e) {
				continue;
			}
			failuresNb++;
			System.out.println("Missing coverage point: " + i);
		}
		UnconditionalFlowInfo.CoverageTestId = 0; // reset for other tests
		assertEquals(failuresNb + " missing coverage point(s)", failuresNb, 0);
	}
}

// only works for info coded on bit 0 - least significant
String testCodedValueOf(long[] data) {
	int length;
	StringBuffer result = new StringBuffer(length = data.length);
	for (int i = 0; i < length; i++) {
		result.append(data[i] == 0 ? '0' : '1');
	}
	return result.toString();
}

static String testStringValueOf(long[] data) {
	int length;
	StringBuffer result = new StringBuffer((length = data.length) * 2 + 1);
	result.append('{');
	for (int i = 0; i < length; i++) {
		if (i > 0) {
			result.append(',');
		}
		result.append(data[i]);
	}
	result.append('}');
	return result.toString();
}
}

/**
 * A specific extension of LocalVariableBinding suitable for flow info
 * manipulation at an implementation level.
 */
class TestLocalVariableBinding extends LocalVariableBinding {
	static class TestTypeBinding extends TypeBinding {
		public TestTypeBinding() {
			this.tagBits = 0L;
		}
		public char[] constantPoolName() {
			return null;
		}
		public PackageBinding getPackage() {
			return null;
		}
		public boolean isCompatibleWith(TypeBinding right) {
			return false;
		}
		public char[] qualifiedSourceName() {
			return null;
		}
		public char[] sourceName() {
			return null;
		}
		public char[] readableName() {
			return null;
		}
	}
	final static TypeBinding testTypeBinding = new TestTypeBinding();
	final static char [] testName = {'t', 'e', 's', 't'};
	TestLocalVariableBinding(int id) {
		super(testName, testTypeBinding, 0, false);
		this.id = id;
	}
	public Constant constant() {
		return Constant.NotAConstant;
	}
	static final TestLocalVariableBinding
		local0 = new TestLocalVariableBinding(0),
		local64 = new TestLocalVariableBinding(64),
		local128 = new TestLocalVariableBinding(128);
}

/**
 * A class meant to augment
 * @link{org.eclipse.jdt.internal.compiler.flow.UnconditionalFlowInfo} with
 * capabilities in the test domain. It especially provides factories to build
 * fake flow info instances for use in state transitions validation.
 */
/*
 * Moreover, this class defines the implementation of key operations for the
 * benefit of itself and NullInfoRegistryTestHarness. Given the fact that the
 * latter could not extend UnconditionalFlowInfoTestHarness and
 * NullInfoRegistry, the code is factorized into static methods.
 */
class UnconditionalFlowInfoTestHarness extends UnconditionalFlowInfo {
	int testPosition;
	// Interface
/**
 * Return the state represented by this.
 * @return the state represented by this
 */
NullReferenceImplTests.State asState() {
	return asState(this, 0);
}

/**
 * Return the state represented by this for a variable encoded at a given position.
 * @param position - int the position of the considered variable
 * @return the state represented by this for a variable encoded at a given position
 */
NullReferenceImplTests.State asState(int position) {
	return asState(this, position);
}

public FlowInfo copy() {
	UnconditionalFlowInfoTestHarness copy =
		new UnconditionalFlowInfoTestHarness();
	copy.testPosition = this.testPosition;
	copy(this, copy);
	return copy;
}

public void markAsDefinitelyNonNull(LocalVariableBinding local) {
	grow(local.id + this.maxFieldCount);
	super.markAsDefinitelyNonNull(local);
}

public void markAsDefinitelyNull(LocalVariableBinding local) {
	grow(local.id + this.maxFieldCount);
	super.markAsDefinitelyNull(local);
}

public void markAsDefinitelyUnknown(LocalVariableBinding local) {
	grow(local.id + this.maxFieldCount);
	super.markAsDefinitelyUnknown(local);
}

/**
 * Return a fake unconditional flow info which bit fields represent the given
 * null bits for a local variable of id 0 within a class that would have no
 * field.
 * @param nullBits the bits that must be set, given in the same order as the
 *        nullAssignment* fields in UnconditionalFlowInfo definition; use 0
 *        for a bit that is not set, 1 else
 * @return a fake unconditional flow info which bit fields represent the
 *         null bits given in parameter
 */
public static UnconditionalFlowInfoTestHarness testUnconditionalFlowInfo(
		long [] nullBits) {
	return testUnconditionalFlowInfo(nullBits, 0);
}

/**
 * Return a fake unconditional flow info which bit fields represent the given
 * null bits for a local variable of id position within a class that would have
 * no field.
 * @param nullBits the bits that must be set, given in the same order as the
 *        nullAssignment* fields in UnconditionalFlowInfo definition; use 0
 *        for a bit that is not set, 1 else
 * @param position the position of the variable within the bit fields; use
 *        various values to test different parts of the bit fields, within
 *        or beyond BitCacheSize
 * @return a fake unconditional flow info which bit fields represent the
 *         null bits given in parameter
 */
public static UnconditionalFlowInfoTestHarness testUnconditionalFlowInfo(
		long [] nullBits, int position) {
 	UnconditionalFlowInfoTestHarness result =
 		new UnconditionalFlowInfoTestHarness();
	result.testPosition = position;
	init(result, nullBits, position);
	return result;
}

/**
 * Return a fake unconditional flow info which bit fields represent the given
 * state for a local variable of id 0 within a class that would have
 * no field.
 * @param state - State the desired state for the variable
 * @return a fake unconditional flow info which bit fields represent the
 *         state given in parameter
 */
public static UnconditionalFlowInfoTestHarness testUnconditionalFlowInfo(NullReferenceImplTests.State state) {
	return testUnconditionalFlowInfo(state, 0);
}

/**
 * Return a fake unconditional flow info which bit fields represent the given
 * state for a local variable of id position within a class that would have
 * no field.
 * @param state - State the desired state for the variable
 * @param position the position of the variable within the bit fields; use
 *        various values to test different parts of the bit fields, within
 *        or beyond BitCacheSize
 * @return a fake unconditional flow info which bit fields represent the
 *         state given in parameter
 */
public static UnconditionalFlowInfoTestHarness testUnconditionalFlowInfo(
		NullReferenceImplTests.State state, int position) {
 	UnconditionalFlowInfoTestHarness result =
 		new UnconditionalFlowInfoTestHarness();
 	long[] nullBits = state.asLongArray();
	result.testPosition = position;
	init(result, nullBits, position);
	return result;
}

/**
 * Return true iff this flow info can be considered as equal to the one passed
 * in parameter.
 * @param other the flow info to compare to
 * @return true iff this flow info compares equal to other
 */
public boolean testEquals(UnconditionalFlowInfo other) {
	return testEquals(this, other);
}

/**
 * Return true iff this flow info can be considered as equal to the one passed
 * in parameter in respect with a single local variable which id would be
 * position in a class with no field.
 * @param other the flow info to compare to
 * @param position the position of the local to consider
 * @return true iff this flow info compares equal to other for a given local
 */
public boolean testEquals(UnconditionalFlowInfo other, int position) {
	return testEquals(this, other, position);
}

/**
 * Return a string suitable for use as a representation of this flow info
 * within test series.
 * @return a string suitable for use as a representation of this flow info
 */
public String testString() {
	if (this == DEAD_END) {
		return "FlowInfo.DEAD_END"; //$NON-NLS-1$
	}
	return testString(this, this.testPosition);
}

/**
 * Return a string suitable for use as a representation of this flow info
 * within test series.
 * @param position a position to consider instead of this flow info default
 *                 test position
 * @return a string suitable for use as a representation of this flow info
 */
public String testString(int position) {
	return testString(this, position);
}

	// Factorized implementation
static NullReferenceImplTests.State asState(UnconditionalFlowInfo zis, int position) {
	if ((zis.tagBits & NULL_FLAG_MASK) == 0) {
		return NullReferenceImplTests.State.start;
	}
	if (position < BitCacheSize) {
		return NullReferenceImplTests.State.fromLongValues(
				(zis.nullBit1 >> position) & 1,
				(zis.nullBit2 >> position) & 1,
				(zis.nullBit3 >> position) & 1,
				(zis.nullBit4 >> position) & 1,
				0,
				0);
	}
 	else {
		int vectorIndex = (position / BitCacheSize) - 1;
        position %= BitCacheSize;
        if (vectorIndex >= zis.extra[2].length) {
        	return NullReferenceImplTests.State.start;
        }
		return NullReferenceImplTests.State.fromLongValues(
				(zis.extra[2][vectorIndex] >> position) & 1,
				(zis.extra[3][vectorIndex] >> position) & 1,
				(zis.extra[4][vectorIndex] >> position) & 1,
				(zis.extra[5][vectorIndex] >> position) & 1,
				0 //(zis.extra[6][vectorIndex] >> position) & 1,
				, 0 //(zis.extra[7][vectorIndex] >> position) & 1
				);
	}
}

static void copy(UnconditionalFlowInfo source, UnconditionalFlowInfo target) {
	target.definiteInits = source.definiteInits;
	target.potentialInits = source.potentialInits;
	boolean hasNullInfo = (source.tagBits & NULL_FLAG_MASK) != 0;
	if (hasNullInfo) {
		target.nullBit1 = source.nullBit1;
		target.nullBit2 = source.nullBit2;
		target.nullBit3 = source.nullBit3;
		target.nullBit4 = source.nullBit4;
//		target.nullBit5 = source.nullBit5;
//		target.nullBit6 = source.nullBit6;
	}
	target.tagBits = source.tagBits;
	target.maxFieldCount = source.maxFieldCount;
	if (source.extra != null) {
		int length;
        target.extra = new long[extraLength][];
		System.arraycopy(source.extra[0], 0,
			(target.extra[0] = new long[length = source.extra[0].length]), 0, length);
		System.arraycopy(source.extra[1], 0,
			(target.extra[1] = new long[length]), 0, length);
		if (hasNullInfo) {
            for (int j = 0; j < extraLength; j++) {
			    System.arraycopy(source.extra[j], 0,
				    (target.extra[j] = new long[length]), 0, length);
            }
		}
		else {
            for (int j = 0; j < extraLength; j++) {
			    target.extra[j] = new long[length];
            }
		}
	}
}

public void grow(int position) {
	int vectorIndex = ((position) / BitCacheSize) - 1;
	int length = vectorIndex + 1, oldLength;
	if (this.extra == null) {
		this.extra = new long[extraLength][];
		for (int j = 0; j < extraLength; j++) {
			this.extra[j] = new long[length];
		}
	} else if (length > (oldLength = this.extra[2].length)) {
		for (int j = 0; j < extraLength; j++) {
			System.arraycopy(this.extra[j], 0,
				this.extra[j] = new long[length], 0, oldLength);
		}
	}
}

static void init(UnconditionalFlowInfo zis, long [] nullBits, int position) {
	if (position < BitCacheSize) {
		zis.nullBit1 = nullBits[0] << position;
		zis.nullBit2 = nullBits[1] << position;
		zis.nullBit3 = nullBits[2] << position;
		zis.nullBit4 = nullBits[3] << position;
//		zis.nullBit5 = nullBits[4] << position;
//		zis.nullBit6 = nullBits[5] << position;
	}
 	else {
		int vectorIndex = (position / BitCacheSize) - 1,
			length = vectorIndex + 1;
        position %= BitCacheSize;
        zis.extra = new long[extraLength][];
		zis.extra[0] = new long[length];
		zis.extra[1] = new long[length];
        for (int j = 2; j < extraLength; j++) {
		    zis.extra[j] = new long[length];
		    zis.extra[j][vectorIndex] = nullBits[j - 2] << position;
        }
	}
	if (nullBits[0] != 0 || nullBits[1] != 0
	        || nullBits[2] != 0 || nullBits[3] != 0
	        || nullBits[4] != 0 || nullBits[5] != 0) {
		// cascade better than nullBits[0] | nullBits[1] | nullBits[2] | nullBits[3]
		// by 10%+
		// TODO (maxime) run stats to determine which is the better order
		zis.tagBits |= NULL_FLAG_MASK;
	}
	zis.maxFieldCount = 0;
}

static boolean testEquals(UnconditionalFlowInfo zis, UnconditionalFlowInfo other) {
	if (zis.tagBits != other.tagBits) {
		return false;
	}
	if (zis.nullBit1 != other.nullBit1
			|| zis.nullBit2 != other.nullBit2
			|| zis.nullBit3 != other.nullBit3
			|| zis.nullBit4 != other.nullBit4
			/* || zis.nullBit5 != other.nullBit5
			|| zis.nullBit6 != other.nullBit6 */) {
		return false;
	}
	int left = zis.extra == null ? 0 : zis.extra[2].length,
			right = other.extra == null ? 0 : other.extra[2].length,
			both = 0, i;
	if (left > right) {
		both = right;
	}
	else {
		both = left;
	}
	for (i = 0; i < both ; i++) {
		for (int j = 2; j < extraLength; j++) {
			if (zis.extra[j][i] !=
					other.extra[j][i]) {
				return false;
			}
		}
	}
	for (; i < left; i++) {
		for (int j = 2; j < extraLength; j++) {
			if (zis.extra[j][i] != 0) {
				return false;
			}
		}
	}
	for (; i < right; i++) {
		for (int j = 2; j < extraLength; j++) {
			if (other.extra[j][i] != 0) {
				return false;
			}
		}
	}
	return true;
}

static boolean testEquals(UnconditionalFlowInfo zis, UnconditionalFlowInfo other,
		int position) {
	int vectorIndex = position / BitCacheSize - 1;
	if ((zis.tagBits & other.tagBits & NULL_FLAG_MASK) == 0) {
		return true;
	}
	long mask;
	if (vectorIndex < 0) {
		return ((zis.nullBit1 & (mask = (1L << position))) ^
					(other.nullBit1 & mask)) == 0 &&
				((zis.nullBit2 & mask) ^
					(other.nullBit2 & mask)) == 0 &&
				((zis.nullBit3 & mask) ^
					(other.nullBit3 & mask)) == 0 &&
				((zis.nullBit4 & mask) ^
					(other.nullBit4 & mask)) == 0 /* &&
				((zis.nullBit5 & mask) ^
					(other.nullBit5 & mask)) == 0 &&
				((zis.nullBit6 & mask) ^
					(other.nullBit6 & mask)) == 0 */;
	}
	else {
		int left = zis.extra == null ?
				0 :
				zis.extra[0].length;
		int right = other.extra == null ?
				0 :
				other.extra[0].length;
		int both = left < right ? left : right;
		if (vectorIndex < both) {
			mask = (1L << (position % BitCacheSize));
			for (int j = 2; j < extraLength; j++) {
				if (((zis.extra[j][vectorIndex] & mask)
						^ (other.extra[j][vectorIndex] & mask)) != 0) {
					return false;
				}
			}
			return true;
		}
		if (vectorIndex < left) {
			return ((zis.extra[2][vectorIndex] |
					zis.extra[3][vectorIndex] |
					zis.extra[4][vectorIndex] |
					zis.extra[5][vectorIndex] |
					zis.extra[6][vectorIndex] |
					zis.extra[7][vectorIndex]) &
					(1L << (position % BitCacheSize))) == 0;
		}
		return ((other.extra[2][vectorIndex] |
				other.extra[3][vectorIndex] |
				other.extra[4][vectorIndex] |
				other.extra[5][vectorIndex] |
				other.extra[6][vectorIndex] |
				other.extra[7][vectorIndex]) &
				(1L << (position % BitCacheSize))) == 0;
	}
}

static String testString(UnconditionalFlowInfo zis, int position) {
	if (zis == DEAD_END) {
		return "FlowInfo.DEAD_END"; //$NON-NLS-1$
	}
	if (position < BitCacheSize) {
		return "{" + (zis.nullBit1 >> position) //$NON-NLS-1$
					+ "," + (zis.nullBit2 >> position) //$NON-NLS-1$
					+ "," + (zis.nullBit3 >> position) //$NON-NLS-1$
					+ "," + (zis.nullBit4 >> position) //$NON-NLS-1$
//					+ "," + (zis.nullBit5 >> position) //$NON-NLS-1$
//					+ "," + (zis.nullBit6 >> position) //$NON-NLS-1$
					+ "}"; //$NON-NLS-1$
	}
	else {
		int vectorIndex = position / BitCacheSize - 1,
			shift = position % BitCacheSize;
			return "{" + (zis.extra[2][vectorIndex] //$NON-NLS-1$
			               >> shift)
						+ "," + (zis.extra[3][vectorIndex] //$NON-NLS-1$
						   >> shift)
						+ "," + (zis.extra[4][vectorIndex] //$NON-NLS-1$
						   >> shift)
						+ "," + (zis.extra[5][vectorIndex] //$NON-NLS-1$
						   >> shift)
//						+ "," + (zis.extra[6][vectorIndex] //$NON-NLS-1$
//						   >> shift)
//						+ "," + (zis.extra[7][vectorIndex] //$NON-NLS-1$
//						   >> shift)
						+ "}"; //$NON-NLS-1$
	}
}
}
interface CodeAnalysis {
	public static final String
		definitionStartMarker = "DEFINITION START",
		definitionEndMarker = "DEFINITION END",
		initializerStartMarker = "INITIALIZER START",
		initializerEndMarker = "INITIALIZER END";
}
class TransitiveClosureHolder {
static class Element {
	NullReferenceImplTests.State value;
	boolean alreadyKnown;
	Element(NullReferenceImplTests.State value) {
		if (value == null) {
			throw new IllegalArgumentException("not a valid element");
		}
		this.value = value;
	}
}
Map elements = new TreeMap();
public TransitiveClosureHolder() {
	Element start = new Element(NullReferenceImplTests.State.start);
	this.elements.put(start.value, start);
}
void add(NullReferenceImplTests.State value) {
	if (value == null) {
		throw new IllegalArgumentException("not a valid state");
	}
	if (! this.elements.containsKey(value)) {
		this.elements.put(value, new Element(value));
	}
}
void add(NullReferenceImplTests.State[] values) {
	if (values == null) {
		throw new IllegalArgumentException("not a valid states set");
	}
	for (int i = 0, length = values.length; i < length; i++) {
		add(values[i]);
	}
}
NullReferenceImplTests.State[] asArray() {
	int length;
	NullReferenceImplTests.State[] result = new NullReferenceImplTests.State[length = this.elements.size()];
	Iterator elementsIterator = this.elements.keySet().iterator();
	for (int j = 0; j < length; j++) {
		result[j] = (NullReferenceImplTests.State) elementsIterator.next();
	}
	return result;
}
NullReferenceImplTests.State[] notAlreadyKnowns() {
	List resultAccumulator = new ArrayList(this.elements.size());
	Iterator i = this.elements.values().iterator();
	Element current;
	while (i.hasNext()) {
		if (! (current = (Element) i.next()).alreadyKnown) {
			resultAccumulator.add(current.value);
		}
	}
	int length;
	NullReferenceImplTests.State[] result = new NullReferenceImplTests.State[length = resultAccumulator.size()];
	for (int j = 0; j < length; j++) {
		result[j] = (NullReferenceImplTests.State) resultAccumulator.get(j);
	}
	return result;
}
void markAllAsAlreadyKnown() {
	Iterator i = this.elements.values().iterator();
	while (i.hasNext()) {
		((Element) i.next()).alreadyKnown = true;
	}
}
public String toString() {
	StringBuffer output = new StringBuffer();
	output.append("Transitive closure:\n");
	SortedMap sorted = new TreeMap(this.elements);
	Iterator i = sorted.keySet().iterator();
	while (i.hasNext()) {
		output.append(i.next().toString());
		output.append('\n');
	}
	return output.toString();
}
}

// PREMATURE segregate pure tooling into a separate project, keep tests only here
/**
 * The Generator class is meant to generate the tabular data needed by the
 * flow information implementation level tests. While the tests should ensure
 * non regression by leveraging their initialization tables only, any change
 * into the flow information logic or encoding is due to yield considerable
 * changes into the literal values sets of the initializers themselves.
 * Tooling the production of those literals buys us flexibility.
 * {@link #printHelp printHelp} for details.
 */
class Generator {
static NullReferenceImplTests.State[] computeTransitiveClosure() {
	TransitiveClosureHolder transitiveClosure = new TransitiveClosureHolder();
	NullReferenceImplTests.State[] unknowns;
	unknowns = transitiveClosure.notAlreadyKnowns();
	while (unknowns.length != 0) {
		transitiveClosure.markAllAsAlreadyKnown();
		for (int i = 0, length = NullReferenceImplTransformations.transformations.length;	i < length; i ++) {
			transitiveClosure.add(
				NullReferenceImplTransformations.transformations[i].
					computeOutputs(transitiveClosure.asArray()));
		}
		unknowns = transitiveClosure.notAlreadyKnowns();
	}
	return transitiveClosure.asArray();
}
public static void main(String[] args) {
	if (args.length == 0) {
		printHelp(false);
		System.exit(1);
	}
	switch (args.length) {
		case 1:
			if (args[0].equals("--help")) {
				printHelp(true);
				System.exit(0);
			}
			else {
				printHelp(false);
				System.exit(1);
			}
			break;
		case 2:
			if (args[0].equals("--printTruthTables")) {
				File outputDir = new File(args[1]);
				if (outputDir.isDirectory()) {
					for (int i = 0, length = NullReferenceImplTransformations.transformations.length; i < length; i++) {
						NullReferenceImplTransformations.transformations[i].printTruthTables(outputDir);
					}
				}
				else {
					// PREMATURE error handling
				}
				System.exit(0);
			}
			else {
				printHelp(false);
				System.exit(1);
			}
			break;
		case 3:
			if (args[0].equals("--reinitializeFromComputedValues")) {
				reinitializeFromComputedValues(args[1], args[2]);
				System.out.println("Generator generated new file into " + args[2]);
				System.exit(0);
			}
			//$FALL-THROUGH$
		case 5:
			if (args[0].equals("--reinitializeFromComments")) {
				reinitializeFromComments(args[1], args[2], args[3], args[4]);
				System.out.println("Generator generated new files into " + args[2]
					+ " and " + args[4]);
				System.exit(0);
			}
			//$FALL-THROUGH$
		default:
			printHelp(false);
			System.exit(1);
	}
}

private static void reinitializeFromComments(
		String statesSource, String statesTarget,
		String transformationsSource, String transformationsTarget) {
	if (statesSource.equals(transformationsSource) ||
			statesTarget.equals(transformationsTarget)) {
		throw new RuntimeException();
	}
	try {
		BufferedReader in;
		BufferedWriter out;
		NullReferenceImplTests.State.reinitializeFromComment(
			in = new BufferedReader(
				new FileReader(statesSource)),
			out = new BufferedWriter(new FileWriter(statesTarget)));
		in.close();
		out.close();
		File[] tempFiles = new File[2];
		tempFiles[0] = File.createTempFile("generator", "java");
		tempFiles[1] = File.createTempFile("generator", "java");
		NullReferenceImplTransformations.transformations[0].reinitializeFromComments(
			in = new BufferedReader(
				new FileReader(transformationsSource)),
			out = new BufferedWriter(new FileWriter(tempFiles[0])));
		in.close();
		out.close();
		int i, length;
		for (i = 1, length = NullReferenceImplTransformations.transformations.length - 1; i < length; i++) {
			NullReferenceImplTransformations.transformations[i].reinitializeFromComments(
				in = new BufferedReader(
					new FileReader(tempFiles[(i + 1) % 2])),
				out = new BufferedWriter(new FileWriter(tempFiles[i % 2])));
			in.close();
			out.close();
		}
		NullReferenceImplTransformations.transformations[i].reinitializeFromComments(
			in = new BufferedReader(
				new FileReader(tempFiles[(i + 1) % 2])),
			out = new BufferedWriter(new FileWriter(transformationsTarget)));
		in.close();
		out.close();
	} catch (Throwable t) {
		System.err.println("Generator error:");
		t.printStackTrace(System.err);
		System.exit(2);
	}
}

private static void reinitializeFromComputedValues(String source, String target) {
	for (int i = 0, length = NullReferenceImplTransformations.transformations.length;
			i < length; i++) {
		NullReferenceImplTransformations.transformations[i].hydrate();
	}
	NullReferenceImplTests.State[] transitiveClosure = computeTransitiveClosure();
	try {
		BufferedReader in;
		BufferedWriter out;
		File[] tempFiles = new File[2];
		tempFiles[0] = File.createTempFile("generator", "java");
		tempFiles[1] = File.createTempFile("generator", "java");
		NullReferenceImplTransformations.transformations[0].reinitializeFromComputedValues(
			in = new BufferedReader(
				new FileReader(source)),
			out = new BufferedWriter(new FileWriter(tempFiles[0])),
			transitiveClosure);
		in.close();
		out.close();
		int i, length;
		for (i = 1, length = NullReferenceImplTransformations.transformations.length - 1; i < length; i++) {
			NullReferenceImplTransformations.transformations[i].reinitializeFromComputedValues(
				in = new BufferedReader(
					new FileReader(tempFiles[(i + 1) % 2])),
				out = new BufferedWriter(new FileWriter(tempFiles[i % 2])),
				transitiveClosure);
			in.close();
			out.close();
		}
		NullReferenceImplTransformations.transformations[i].reinitializeFromComputedValues(
			in = new BufferedReader(
				new FileReader(tempFiles[(i + 1) % 2])),
			out = new BufferedWriter(new FileWriter(target)),
			transitiveClosure);
		in.close();
		out.close();
	} catch (Throwable t) {
		System.err.println("Generator error:");
		t.printStackTrace(System.err);
		System.exit(2);
	}
}

private static void printHelp(boolean longText) {
	if (longText) {
		System.out.println(
			"Generator use cases\n" +
			" - when a brand new logic is experimented for the transitions, the best\n" +
			"   way to go is to write explicit (inefficient) transformation code within\n" +
			"   UnconditionalFlowInfo, then generate the literal initializers from\n" +
			"   there; use the command\n" +
			"   --reinitializeFromComputedValues <source file> <target file>\n" +
			"   to this effect; in case of inconsistencies or errors, messages are\n" +
			"   printed to the error output stream and the result should be considered as non reliable;\n" +
			" - when only a few changes are made to state names or a specific\n" +
			"   transitions, it should be possible to get the test initializers fixed\n" +
			"   before UnconditionalFlowInfo implements those changes; use the command\n" +
	        "   --reinitializeFromComments <states source file> <states target file> <transformations source file> <transformations target file>\n" +
	        "   to this effect;\n" +
	        " - the same command can be used when, while the semantics of the system\n" +
	        "   are unchanged, the encoding is modified; it should then produce the\n" +
	        "   initializers according to the new encoding, as defined by the comment\n" +
	        "   for State.states, and the transformations as defined by their\n" +
	        "   respective comment;\n" +
	        " - when a given encoding is retained, its optimization may leverage truth\n" +
	        "   tables; use the --printTruthTables command to this effect.\n" +
	        "   \n\n");
		printHelp(false);
	}
	else {
		System.out.println(
	        "Usage:\n" +
	        "Generator --help\n" +
	        "  prints a more detailed help message\n" +
	        "Generator --printTruthTables\n" +
	        "  prints the truth tables of the transformations\n" +
	        "Generator --reinitializeFromComments <source file> <target file>\n" +
	        "  generates into target file a copy of source file into which\n" +
	        "  transformations initializers have been reset from their definitions\n" +
			"Generator --reinitializeFromComputedValues <source file> <target file>\n"  +
	        "  generates into target file a copy of source file into which\n" +
	        "  transformations definitions and initializers have been reset\n" +
	        "  according to the behavior of the current UnconditionalFlowInfo\n"
	        );
	}
}
}

Back to the top