File: CallbacksTest.java

package info (click to toggle)
libjna-java 5.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,828 kB
  • sloc: java: 90,222; ansic: 4,994; xml: 3,713; makefile: 433; sh: 299
file content (1594 lines) | stat: -rw-r--r-- 66,257 bytes parent folder | download
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
/* Copyright (c) 2007-2013 Timothy Wall, All Rights Reserved
 *
 * The contents of this file is dual-licensed under 2
 * alternative Open Source/Free licenses: LGPL 2.1 or later and
 * Apache License 2.0. (starting with JNA version 4.0.0).
 *
 * You can freely decide which license you want to apply to
 * the project.
 *
 * You may obtain a copy of the LGPL License at:
 *
 * http://www.gnu.org/licenses/licenses.html
 *
 * A copy is also included in the downloadable source code package
 * containing JNA, in file "LGPL2.1".
 *
 * You may obtain a copy of the Apache License at:
 *
 * http://www.apache.org/licenses/
 *
 * A copy is also included in the downloadable source code package
 * containing JNA, in file "AL2.0".
 */
package com.sun.jna;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

import com.sun.jna.Callback.UncaughtExceptionHandler;
import com.sun.jna.CallbacksTest.TestLibrary.CbCallback;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.W32APIOptions;

import junit.framework.TestCase;

/** Exercise callback-related functionality.
 *
 * @author twall@users.sf.net
 */
//@SuppressWarnings("unused")
public class CallbacksTest extends TestCase implements Paths {

    // On OSX, on Oracle JVM 1.8+, pthread cleanup thinks the native thread is
    // not attached, and the JVM never unmaps the defunct native thread.  In
    // order to avoid this situation causing tests to time out, we need to
    // explicitly detach the native thread after our Java code is done with it.
    // Also reproducible on Ubuntu 6 (x86-64), Java 6
    private static final boolean THREAD_DETACH_BUG = Platform.isMac() || (Platform.isLinux() && Platform.is64Bit());

    private static final String UNICODE = "[\u0444]";

    private static final double DOUBLE_MAGIC = -118.625d;
    private static final float FLOAT_MAGIC = -118.625f;

    private static final int THREAD_TIMEOUT = 5000;

    protected void waitFor(Thread thread) {
        long start = System.currentTimeMillis();
        while (thread.isAlive()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
            if (System.currentTimeMillis() - start > THREAD_TIMEOUT) {
                fail("Timed out waiting for native thread " + thread
                        + " to detach and terminate");
            }
        }
    }

    public static class SmallTestStructure extends Structure {
        public static final List<String> FIELDS = createFieldsOrder("value");
        public double value;
        public static int allocations = 0;
        @Override
        protected void allocateMemory(int size) {
            super.allocateMemory(size);
            ++allocations;
        }
        public SmallTestStructure() { }
        public SmallTestStructure(Pointer p) { super(p); read(); }
        @Override
        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }
    public static class TestStructure extends Structure {
        public static class ByValue extends TestStructure implements Structure.ByValue { }
        public static interface TestCallback extends Callback {
            TestStructure.ByValue callback(TestStructure.ByValue s);
        }

        public static final List<String> FIELDS = createFieldsOrder("c", "s", "i", "j", "inner");
        public byte c;
        public short s;
        public int i;
        public long j;
        public SmallTestStructure inner;
        @Override
        protected List<String> getFieldOrder() {
            return FIELDS;
        }
    }
    public static interface TestLibrary extends Library {
        interface NoMethodCallback extends Callback {
        }
        interface CustomMethodCallback extends Callback {
            void invoke();
        }
        interface TooManyMethodsCallback extends Callback {
            void invoke();
            void invoke2();
        }
        interface MultipleMethodsCallback extends Callback {
            void invoke();
            void callback();
        }
        interface VoidCallback extends Callback {
            void callback();
        }
        void callVoidCallback(VoidCallback c);
        void callVoidCallbackThreaded(VoidCallback c, int count, int ms, String name, int stacksize);
        interface VoidCallbackCustom extends Callback {
            void customMethodName();
        }
        abstract class VoidCallbackCustomAbstract implements VoidCallbackCustom {
            @Override
            public void customMethodName() { }
        }
        class VoidCallbackCustomDerived extends VoidCallbackCustomAbstract { }
        void callVoidCallback(VoidCallbackCustom c);
        interface BooleanCallback extends Callback {
            boolean callback(boolean arg, boolean arg2);
        }
        boolean callBooleanCallback(BooleanCallback c, boolean arg, boolean arg2);
        interface ByteCallback extends Callback {
            byte callback(byte arg, byte arg2);
        }
        byte callInt8Callback(ByteCallback c, byte arg, byte arg2);
        interface ShortCallback extends Callback {
            short callback(short arg, short arg2);
        }
        short callInt16Callback(ShortCallback c, short arg, short arg2);
        interface Int32Callback extends Callback {
            int callback(int arg, int arg2);
        }
        int callInt32Callback(Int32Callback c, int arg, int arg2);
        interface NativeLongCallback extends Callback {
            NativeLong callback(NativeLong arg, NativeLong arg2);
        }
        NativeLong callNativeLongCallback(NativeLongCallback c, NativeLong arg, NativeLong arg2);
        interface Int64Callback extends Callback {
            long callback(long arg, long arg2);
        }
        long callInt64Callback(Int64Callback c, long arg, long arg2);
        interface FloatCallback extends Callback {
            float callback(float arg, float arg2);
        }
        float callFloatCallback(FloatCallback c, float arg, float arg2);
        interface DoubleCallback extends Callback {
            double callback(double arg, double arg2);
        }
        double callDoubleCallback(DoubleCallback c, double arg, double arg2);
        interface StructureCallback extends Callback {
            SmallTestStructure callback(SmallTestStructure arg);
        }
        SmallTestStructure callStructureCallback(StructureCallback c, SmallTestStructure arg);
        interface StringCallback extends Callback {
            String callback(String arg, String arg2);
        }
        String callStringCallback(StringCallback c, String arg, String arg2);
        interface WideStringCallback extends Callback {
            WString callback(WString arg, WString arg2);
        }
        WString callWideStringCallback(WideStringCallback c, WString arg, WString arg2);
        interface CopyArgToByReference extends Callback {
            int callback(int arg, IntByReference result);
        }
        interface StringArrayCallback extends Callback {
            String[] callback(String[] arg);
        }
        Pointer callStringArrayCallback(StringArrayCallback c, String[] arg);
        int callCallbackWithByReferenceArgument(CopyArgToByReference cb, int arg, IntByReference result);
        TestStructure.ByValue callCallbackWithStructByValue(TestStructure.TestCallback callback, TestStructure.ByValue cbstruct);
        interface CbCallback extends Callback {
            CbCallback callback(CbCallback arg);
        }
        CbCallback callCallbackWithCallback(CbCallback cb);

        interface Int32CallbackX extends Callback {
            public int callback(int arg);
        }
        Int32CallbackX returnCallback();
        Int32CallbackX returnCallbackArgument(Int32CallbackX cb);

        interface CustomCallback extends Callback {
            Custom callback(Custom arg1, Custom arg2);
        }
        int callInt32Callback(CustomCallback cb, int arg1, int arg2);

        class CbStruct extends Structure {
            public Callback cb;
            @Override
            protected List<String> getFieldOrder() {
                return Arrays.asList(new String[] { "cb" });
            }
        }
        void callCallbackInStruct(CbStruct cbstruct);

        // Union (by value)
        class TestUnion extends Union implements Structure.ByValue {
            public String f1;
            public int f2;
        }
        interface UnionCallback extends Callback {
            TestUnion invoke(TestUnion arg);
        }
        TestUnion testUnionByValueCallbackArgument(UnionCallback cb, TestUnion arg);
    }

    TestLibrary lib;

    @Override
    protected void setUp() {
        lib = Native.load("testlib", TestLibrary.class);
    }

    @Override
    protected void tearDown() {
        lib = null;
    }

    public static class Custom implements NativeMapped {
        private int value;
        public Custom() { }
        public Custom(int value) {
            this.value = value;
        }
        @Override
        public Object fromNative(Object nativeValue, FromNativeContext context) {
            return new Custom(((Integer)nativeValue).intValue());
        }
        @Override
        public Class<?> nativeType() {
            return Integer.class;
        }
        @Override
        public Object toNative() {
            return Integer.valueOf(value);
        }
        @Override
        public boolean equals(Object o) {
            return o instanceof Custom && ((Custom)o).value == value;
        }
    }

    public void testLookupNullCallback() {
        assertNull("NULL pointer should result in null callback",
                   CallbackReference.getCallback(null, null));
        try {
            CallbackReference.getCallback(TestLibrary.VoidCallback.class, new Pointer(0));
            fail("Null pointer lookup should fail");
        } catch(NullPointerException e) {
            // expected
        }
    }

    public void testLookupNonCallbackClass() {
        try {
            CallbackReference.getCallback(String.class, new Pointer(0));
            fail("Request for non-Callback class should fail");
        }
        catch(IllegalArgumentException e) {
        }
    }

    public void testAcceptMultiplyMappedCallbacks() {
        Pointer p = new Pointer(getName().hashCode());

        Callback cbV1 = CallbackReference.getCallback(TestLibrary.VoidCallback.class, p);
        Callback cbB1 = CallbackReference.getCallback(TestLibrary.ByteCallback.class, p);
        Callback cbV2 = CallbackReference.getCallback(TestLibrary.VoidCallback.class, p);
        Callback cbB2 = CallbackReference.getCallback(TestLibrary.ByteCallback.class, p);

        assertSame(cbV1, cbV2);
        assertSame(cbB1, cbB2);
    }

    public void testNoMethodCallback() {
        try {
            CallbackReference.getCallback(TestLibrary.NoMethodCallback.class, new Pointer(getName().hashCode()));
            fail("Callback with no callback method should fail");
        }
        catch(IllegalArgumentException e) {
        }
    }

    public void testCustomMethodCallback() {
        CallbackReference.getCallback(TestLibrary.CustomMethodCallback.class, new Pointer(getName().hashCode()));
    }

    public void testTooManyMethodsCallback() {
        try {
            CallbackReference.getCallback(TestLibrary.TooManyMethodsCallback.class, new Pointer(getName().hashCode()));
            fail("Callback lookup with too many methods should fail");
        }
        catch(IllegalArgumentException e) {
        }
    }

    public void testMultipleMethodsCallback() {
        CallbackReference.getCallback(TestLibrary.MultipleMethodsCallback.class, new Pointer(getName().hashCode()));
    }

    public void testNativeFunctionPointerStringValue() {
        Callback cb = CallbackReference.getCallback(TestLibrary.VoidCallback.class, new Pointer(getName().hashCode()));
        Class<?> cls = CallbackReference.findCallbackClass(cb.getClass());
        assertTrue("toString should include Java Callback type: " + cb + " ("
                   + cls + ")", cb.toString().indexOf(cls.getName()) != -1);
    }

    public void testLookupSameCallback() {
        Callback cb = CallbackReference.getCallback(TestLibrary.VoidCallback.class, new Pointer(getName().hashCode()));
        Callback cb2 = CallbackReference.getCallback(TestLibrary.VoidCallback.class, new Pointer(getName().hashCode()));

        assertEquals("Callback lookups for same pointer should return same Callback object", cb, cb2);
    }

    // Allow direct tests to override
    protected Map<Callback, CallbackReference> callbackCache() {
        return CallbackReference.callbackMap;
    }

    // Fails on OpenJDK(linux/ppc), probably finalize not run
    public void testGCCallbackOnFinalize() throws Exception {
        final boolean[] called = { false };
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                called[0] = true;
            }
        };
        lib.callVoidCallback(cb);
        assertTrue("Callback not called", called[0]);

        Map<Callback, CallbackReference> refs = new WeakHashMap<>(callbackCache());
        assertTrue("Callback not cached", refs.containsKey(cb));
        CallbackReference ref = refs.get(cb);
        refs = callbackCache();
        Pointer cbstruct = ref.cbstruct;

        cb = null;
        System.gc();
        for (int i = 0; i < 100 && (ref.get() != null || refs.containsValue(ref)); ++i) {
            Thread.sleep(10); // Give the GC a chance to run
            System.gc();
        }
        assertNull("Callback not GC'd", ref.get());
        assertFalse("Callback still in map", refs.containsValue(ref));

        ref = null;
        System.gc();
        for (int i = 0; i < 100 && (cbstruct.peer != 0 || refs.size() > 0); ++i) {
            // Flush weak hash map
            refs.size();
            try {
                Thread.sleep(10); // Give the GC a chance to run
                System.gc();
            } finally {}
        }
        assertEquals("Callback trampoline not freed", 0, cbstruct.peer);
    }

    public void testFindCallbackInterface() {
        TestLibrary.Int32Callback cb = new TestLibrary.Int32Callback() {
            @Override
            public int callback(int arg, int arg2) {
                return arg + arg2;
            }
        };
        assertEquals("Wrong callback interface",
                     TestLibrary.Int32Callback.class,
                     CallbackReference.findCallbackClass(cb.getClass()));
    }

    public void testCallVoidCallback() {
        final boolean[] called = { false };
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                called[0] = true;
            }
        };
        lib.callVoidCallback(cb);
        assertTrue("Callback not called", called[0]);
    }

    public void testCallInt32Callback() {
        final int MAGIC = 0x11111111;
        final boolean[] called = { false };
        TestLibrary.Int32Callback cb = new TestLibrary.Int32Callback() {
            @Override
            public int callback(int arg, int arg2) {
                called[0] = true;
                return arg + arg2;
            }
        };
        final int EXPECTED = MAGIC*3;
        int value = lib.callInt32Callback(cb, MAGIC, MAGIC*2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong callback value", Integer.toHexString(EXPECTED),
                     Integer.toHexString(value));

        value = lib.callInt32Callback(cb, -1, -2);
        assertEquals("Wrong callback return", -3, value);
    }

    public void testCallInt64Callback() {
        final long MAGIC = 0x1111111111111111L;
        final boolean[] called = { false };
        TestLibrary.Int64Callback cb = new TestLibrary.Int64Callback() {
            @Override
            public long callback(long arg, long arg2) {
                called[0] = true;
                return arg + arg2;
            }
        };
        final long EXPECTED = MAGIC*3;
        long value = lib.callInt64Callback(cb, MAGIC, MAGIC*2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong callback value", Long.toHexString(EXPECTED),
                     Long.toHexString(value));

        value = lib.callInt64Callback(cb, -1, -2);
        assertEquals("Wrong callback return", -3, value);
    }

    public void testCallFloatCallback() {
        final boolean[] called = { false };
        final float[] args = { 0, 0 };
        TestLibrary.FloatCallback cb = new TestLibrary.FloatCallback() {
            @Override
            public float callback(float arg, float arg2) {
                called[0] = true;
                args[0] = arg;
                args[1] = arg2;
                return arg + arg2;
            }
        };
        final float EXPECTED = FLOAT_MAGIC*3;
        float value = lib.callFloatCallback(cb, FLOAT_MAGIC, FLOAT_MAGIC*2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first argument", FLOAT_MAGIC, args[0], 0);
        assertEquals("Wrong second argument", FLOAT_MAGIC*2, args[1], 0);
        assertEquals("Wrong callback value", EXPECTED, value, 0);

        value = lib.callFloatCallback(cb, -1f, -2f);
        assertEquals("Wrong callback return", -3f, value, 0);
    }

    public void testCallDoubleCallback() {
        final boolean[] called = { false };
        final double[] args = { 0, 0 };
        TestLibrary.DoubleCallback cb = new TestLibrary.DoubleCallback() {
            @Override
            public double callback(double arg, double arg2) {
                called[0] = true;
                args[0] = arg;
                args[1] = arg2;
                return arg + arg2;
            }
        };
        final double EXPECTED = DOUBLE_MAGIC*3;
        double value = lib.callDoubleCallback(cb, DOUBLE_MAGIC, DOUBLE_MAGIC*2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first argument", DOUBLE_MAGIC, args[0], 0);
        assertEquals("Wrong second argument", DOUBLE_MAGIC*2, args[1], 0);
        assertEquals("Wrong callback value", EXPECTED, value, 0);

        value = lib.callDoubleCallback(cb, -1d, -2d);
        assertEquals("Wrong callback return", -3d, value, 0);
    }

    public void testCallStructureCallback() {
        final boolean[] called = {false};
        final Pointer[] cbarg = { null };
        final SmallTestStructure s = new SmallTestStructure();
        final double MAGIC = 118.625;
        TestLibrary.StructureCallback cb = new TestLibrary.StructureCallback() {
            @Override
            public SmallTestStructure callback(SmallTestStructure arg) {
                called[0] = true;
                cbarg[0] = arg.getPointer();
                arg.value = MAGIC;
                return arg;
            }
        };
        SmallTestStructure.allocations = 0;
        SmallTestStructure value = lib.callStructureCallback(cb, s);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong argument passed to callback", s.getPointer(), cbarg[0]);
        assertEquals("Structure argument not synched on callback return",
                     MAGIC, s.value, 0d);
        assertEquals("Wrong structure return", s.getPointer(), value.getPointer());
        assertEquals("Structure return not synched",
                     MAGIC, value.value, 0d);
        // All structures involved should be created from pointers, with no
        // memory allocation at all.
        assertEquals("No structure memory should be allocated", 0, SmallTestStructure.allocations);
    }

    public void testCallStructureArrayCallback() {
        final SmallTestStructure s = new SmallTestStructure();
        final SmallTestStructure[] array = (SmallTestStructure[])s.toArray(2);
        final double MAGIC = 118.625;
        TestLibrary.StructureCallback cb = new TestLibrary.StructureCallback() {
            @Override
            public SmallTestStructure callback(SmallTestStructure arg) {
                SmallTestStructure[] array =
                    (SmallTestStructure[])arg.toArray(2);
                array[0].value = MAGIC;
                array[1].value = MAGIC*2;
                return arg;
            }
        };
        SmallTestStructure value = lib.callStructureCallback(cb, s);
        assertEquals("Structure array element 0 not synched on callback return",
                     MAGIC, array[0].value, 0d);
        assertEquals("Structure array element 1 not synched on callback return",
                     MAGIC*2, array[1].value, 0d);
    }

    public void testCallBooleanCallback() {
        final boolean[] called = {false};
        final boolean[] cbargs = { false, false };
        TestLibrary.BooleanCallback cb = new TestLibrary.BooleanCallback() {
            @Override
            public boolean callback(boolean arg, boolean arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return arg && arg2;
            }
        };
        boolean value = lib.callBooleanCallback(cb, true, false);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first callback argument", true, cbargs[0]);
        assertEquals("Wrong second callback argument", false, cbargs[1]);
        assertFalse("Wrong boolean return", value);
    }

    public void testCallInt8Callback() {
        final boolean[] called = {false};
        final byte[] cbargs = { 0, 0 };
        TestLibrary.ByteCallback cb = new TestLibrary.ByteCallback() {
            @Override
            public byte callback(byte arg, byte arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return (byte)(arg + arg2);
            }
        };
        final byte MAGIC = 0x11;
        byte value = lib.callInt8Callback(cb, MAGIC, (byte)(MAGIC*2));
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first callback argument",
                     Integer.toHexString(MAGIC),
                     Integer.toHexString(cbargs[0]));
        assertEquals("Wrong second callback argument",
                     Integer.toHexString(MAGIC*2),
                     Integer.toHexString(cbargs[1]));
        assertEquals("Wrong byte return",
                     Integer.toHexString(MAGIC*3),
                     Integer.toHexString(value));

        value = lib.callInt8Callback(cb, (byte)-1, (byte)-2);
        assertEquals("Wrong byte return (hi bit)", (byte)-3, value);
    }

    public void testCallInt16Callback() {
        final boolean[] called = {false};
        final short[] cbargs = { 0, 0 };
        TestLibrary.ShortCallback cb = new TestLibrary.ShortCallback() {
            @Override
            public short callback(short arg, short arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return (short)(arg + arg2);
            }
        };
        final short MAGIC = 0x1111;
        short value = lib.callInt16Callback(cb, MAGIC, (short)(MAGIC*2));
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first callback argument",
                     Integer.toHexString(MAGIC),
                     Integer.toHexString(cbargs[0]));
        assertEquals("Wrong second callback argument",
                     Integer.toHexString(MAGIC*2),
                     Integer.toHexString(cbargs[1]));
        assertEquals("Wrong short return",
                     Integer.toHexString(MAGIC*3),
                     Integer.toHexString(value));

        value = lib.callInt16Callback(cb, (short)-1, (short)-2);
        assertEquals("Wrong short return (hi bit)", (short)-3, value);
    }

    public void testCallNativeLongCallback() {
        final boolean[] called = {false};
        final NativeLong[] cbargs = { null, null};
        TestLibrary.NativeLongCallback cb = new TestLibrary.NativeLongCallback() {
            @Override
            public NativeLong callback(NativeLong arg, NativeLong arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return new NativeLong(arg.intValue() +  arg2.intValue());
            }
        };
        NativeLong value = lib.callNativeLongCallback(cb, new NativeLong(1), new NativeLong(2));
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first callback argument", new NativeLong(1), cbargs[0]);
        assertEquals("Wrong second callback argument", new NativeLong(2), cbargs[1]);
        assertEquals("Wrong boolean return", new NativeLong(3), value);
    }

    public void testCallNativeMappedCallback() {
        final boolean[] called = {false};
        final Custom[] cbargs = { null, null};
        TestLibrary.CustomCallback cb = new TestLibrary.CustomCallback() {
            @Override
            public Custom callback(Custom arg, Custom arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return new Custom(arg.value + arg2.value);
            }
        };
        int value = lib.callInt32Callback(cb, 1, 2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first callback argument", new Custom(1), cbargs[0]);
        assertEquals("Wrong second callback argument", new Custom(2), cbargs[1]);
        assertEquals("Wrong NativeMapped return", 3, value);
    }

    public void testCallStringCallback() {
        final boolean[] called = {false};
        final String[] cbargs = { null, null };
        TestLibrary.StringCallback cb = new TestLibrary.StringCallback() {
            @Override
            public String callback(String arg, String arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return arg + arg2;
            }
        };
        Charset charset = Charset.forName(Native.getDefaultStringEncoding());
        final String VALUE = "value" + charset.decode(charset.encode(UNICODE));
        final String VALUE2 = getName() + charset.decode(charset.encode(UNICODE));
        String value = lib.callStringCallback(cb, VALUE, VALUE2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong String callback argument 0", VALUE, cbargs[0]);
        assertEquals("Wrong String callback argument 1", VALUE2, cbargs[1]);
        assertEquals("Wrong String return", VALUE + VALUE2, value);
    }

    public void testStringCallbackMemoryReclamation() throws InterruptedException {
        TestLibrary.StringCallback cb = new TestLibrary.StringCallback() {
            @Override
            public String callback(String arg, String arg2) {
                return arg + arg2;
            }
        };

        // A little internal groping
        Map<?, ?> m = CallbackReference.allocations;
        m.clear();

        Charset charset = Charset.forName(Native.getDefaultStringEncoding());
        String arg = getName() + "1" + charset.decode(charset.encode(UNICODE));
        String arg2 = getName() + "2" + charset.decode(charset.encode(UNICODE));
        String value = lib.callStringCallback(cb, arg, arg2);
        WeakReference<Object> ref = new WeakReference<>(value);

        arg = null;
        value = null;
        System.gc();
        for (int i = 0; i < 100 && (ref.get() != null || m.size() > 0); ++i) {
            try {
                Thread.sleep(10); // Give the GC a chance to run
                System.gc();
            } finally {}
        }
        assertNull("NativeString reference not GC'd", ref.get());
        assertEquals("NativeString reference still held: " + m.values(), 0, m.size());
    }

    public void testCallWideStringCallback() {
        final boolean[] called = {false};
        final WString[] cbargs = { null, null };
        TestLibrary.WideStringCallback cb = new TestLibrary.WideStringCallback() {
            @Override
            public WString callback(WString arg, WString arg2) {
                called[0] = true;
                cbargs[0] = arg;
                cbargs[1] = arg2;
                return new WString(arg.toString() + arg2.toString());
            }
        };
        final WString VALUE = new WString("magic" + UNICODE);
        final WString VALUE2 = new WString(getName() + UNICODE);
        WString value = lib.callWideStringCallback(cb, VALUE, VALUE2);
        assertTrue("Callback not called", called[0]);
        assertEquals("Wrong first callback argument", VALUE, cbargs[0]);
        assertEquals("Wrong second callback argument", VALUE2, cbargs[1]);
        assertEquals("Wrong wide string return", new WString(VALUE.toString() + VALUE2.toString()), value);
    }

    public void testCallStringArrayCallback() {
        final boolean[] called = {false};
        final String[][] cbargs = { null };
        TestLibrary.StringArrayCallback cb = new TestLibrary.StringArrayCallback() {
            @Override
            public String[] callback(String[] arg) {
                called[0] = true;
                cbargs[0] = arg;
                return arg;
            }
        };
        Charset charset = Charset.forName(Native.getDefaultStringEncoding());
        final String VALUE = "value" + charset.decode(charset.encode(UNICODE));
        final String[] VALUE_ARRAY = { VALUE, null };
        Pointer value = lib.callStringArrayCallback(cb, VALUE_ARRAY);
        assertTrue("Callback not called", called[0]);
        assertEquals("String[] array should not be modified",
                     VALUE, VALUE_ARRAY[0]);
        assertEquals("Terminating null should be removed from incoming arg",
                     VALUE_ARRAY.length-1, cbargs[0].length);
        assertEquals("String[] argument index 0 mismatch",
                     VALUE_ARRAY[0], cbargs[0][0]);
        String[] result = value.getStringArray(0);
        assertEquals("Wrong String[] return", VALUE_ARRAY[0], result[0]);
        assertEquals("Terminating null should be removed from return value",
                     VALUE_ARRAY.length-1, result.length);
    }

    public void testCallCallbackWithByReferenceArgument() {
        final boolean[] called = {false};
        TestLibrary.CopyArgToByReference cb = new TestLibrary.CopyArgToByReference() {
            @Override
            public int callback(int arg, IntByReference result) {
                called[0] = true;
                result.setValue(arg);
                return result.getValue();
            }
        };
        final int VALUE = 0;
        IntByReference ref = new IntByReference(~VALUE);
        int value = lib.callCallbackWithByReferenceArgument(cb, VALUE, ref);
        assertEquals("Wrong value returned", VALUE, value);
        assertEquals("Wrong value in by reference memory", VALUE, ref.getValue());
    }

    public void testCallCallbackWithStructByValue() throws Exception {
        final boolean[] called = { false };
        final TestStructure.ByValue[] arg = { null };
        final TestStructure.ByValue s = new TestStructure.ByValue();
        TestStructure.TestCallback cb = new TestStructure.TestCallback() {
            @Override
            public TestStructure.ByValue callback(TestStructure.ByValue s) {
                // Copy the argument value for later comparison
                called[0] = true;
                return arg[0] = s;
            }
        };
        s.c = (byte)0x11;
        s.s = 0x2222;
        s.i = 0x33333333;
        s.j = 0x4444444444444444L;
        s.inner.value = 5;

        TestStructure result = lib.callCallbackWithStructByValue(cb, s);
        assertTrue("Callback not called", called[0]);
        assertTrue("ByValue argument should own its own memory, instead was "
                   + arg[0].getPointer(), arg[0].getPointer() instanceof Memory);
        assertTrue("ByValue result should own its own memory, instead was "
                   + result.getPointer(), result.getPointer() instanceof Memory);
        if (!s.dataEquals(arg[0], true)) {
            System.out.println("Mismatch: " + s);
            System.out.println("  versus: " + arg[0]);
        }
        assertTrue("Wrong value for callback argument", s.dataEquals(arg[0], true));
        if (!s.dataEquals(result, true)) {
            System.out.println("Mismatch: " + s);
            System.out.println("  versus: " + result);
        }
        assertTrue("Wrong value for callback result", s.dataEquals(result, true));
    }

    public void testUnionByValueCallbackArgument() throws Exception{
        TestLibrary.TestUnion arg = new TestLibrary.TestUnion();
        arg.setType(String.class);
        Charset charset = Charset.forName(arg.getStringEncoding());
        final String VALUE = getName() + charset.decode(charset.encode(UNICODE));
        arg.f1 = VALUE;
        final boolean[] called = { false };
        final TestLibrary.TestUnion[] cbvalue = { null };
        TestLibrary.TestUnion result = lib.testUnionByValueCallbackArgument(new TestLibrary.UnionCallback() {
            @Override
            public TestLibrary.TestUnion invoke(TestLibrary.TestUnion v) {
                called[0] = true;
                v.setType(String.class);
                v.read();
                cbvalue[0] = v;
                return v;
            }
        }, arg);
        assertTrue("Callback not called", called[0]);
        assertTrue("ByValue argument should have its own allocated memory, instead was "
                   + cbvalue[0].getPointer(),
                   cbvalue[0].getPointer() instanceof Memory);
        assertEquals("Wrong value for callback argument", VALUE, cbvalue[0].f1);
        assertEquals("Wrong value for callback result", VALUE, result.getTypedValue(String.class));
    }

    public void testCallCallbackWithCallbackArgumentAndResult() {
        TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
            @Override
            public CbCallback callback(CbCallback arg) {
                return arg;
            }
        };
        TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
        assertEquals("Callback reference should be reused", cb, cb2);
    }

    public void testDefaultCallbackExceptionHandler() {
        final RuntimeException ERROR = new RuntimeException(getName());
        PrintStream ps = System.err;
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        System.setErr(new PrintStream(s));
        try {
            TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
                @Override
                public CbCallback callback(CbCallback arg) {
                    throw ERROR;
                }
            };
            TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
            String output = s.toString();
            assertTrue("Default handler not called", output.length() > 0);
        }
        finally {
            System.setErr(ps);
        }
    }

    // Most Callbacks are wrapped in DefaultCallbackProxy, which catches their
    // exceptions.
    public void testCallbackExceptionHandler() {
        final RuntimeException ERROR = new RuntimeException(getName());
        final Throwable CAUGHT[] = { null };
        final Callback CALLBACK[] = { null };
        UncaughtExceptionHandler old = Native.getCallbackExceptionHandler();
        UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Callback cb, Throwable e) {
                CALLBACK[0] = cb;
                CAUGHT[0] = e;
            }
        };
        Native.setCallbackExceptionHandler(handler);
        try {
            TestLibrary.CbCallback cb = new TestLibrary.CbCallback() {
                @Override
                public CbCallback callback(CbCallback arg) {
                    throw ERROR;
                }
            };
            TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
            assertNotNull("Exception handler not called", CALLBACK[0]);
            assertEquals("Wrong callback argument to handler", cb, CALLBACK[0]);
            assertEquals("Wrong exception passed to handler",
                         ERROR, CAUGHT[0]);
        }
        finally {
            Native.setCallbackExceptionHandler(old);
        }
    }

    // CallbackProxy is called directly from native.
    public void testCallbackExceptionHandlerWithCallbackProxy() throws Throwable {
        final RuntimeException ERROR = new RuntimeException(getName());
        final Throwable CAUGHT[] = { null };
        final Callback CALLBACK[] = { null };
        UncaughtExceptionHandler old = Native.getCallbackExceptionHandler();
        UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Callback cb, Throwable e) {
                CALLBACK[0] = cb;
                CAUGHT[0] = e;
            }
        };
        Native.setCallbackExceptionHandler(handler);
        try {
            class TestProxy implements CallbackProxy, TestLibrary.CbCallback {
                @Override
                public CbCallback callback(CbCallback arg) {
                    throw new Error("Should never be called");
                }
                @Override
                public Object callback(Object[] args) {
                    throw ERROR;
                }
                @Override
                public Class<?>[] getParameterTypes() {
                    return new Class[] { CbCallback.class };
                }
                @Override
                public Class<?> getReturnType() {
                    return CbCallback.class;
                }
            };
            TestLibrary.CbCallback cb = new TestProxy();
            TestLibrary.CbCallback cb2 = lib.callCallbackWithCallback(cb);
            assertNotNull("Exception handler not called", CALLBACK[0]);
            assertEquals("Wrong callback argument to handler", cb, CALLBACK[0]);
            assertEquals("Wrong exception passed to handler",
                         ERROR, CAUGHT[0]);
        }
        finally {
            Native.setCallbackExceptionHandler(old);
        }
    }

    public void testResetCallbackExceptionHandler() {
        Native.setCallbackExceptionHandler(null);
        assertNotNull("Should not be able to set callback EH null",
                      Native.getCallbackExceptionHandler());
    }

    public void testInvokeCallback() {
        TestLibrary.Int32CallbackX cb = lib.returnCallback();
        assertNotNull("Callback should not be null", cb);
        assertEquals("Callback should be callable", 1, cb.callback(1));

        TestLibrary.Int32CallbackX cb2 = new TestLibrary.Int32CallbackX() {
            @Override
            public int callback(int arg) {
                return 0;
            }
        };
        assertSame("Java callback should be looked up",
                   cb2, lib.returnCallbackArgument(cb2));
        assertSame("Existing native function wrapper should be reused",
                   cb, lib.returnCallbackArgument(cb));
    }

    public void testCallCallbackInStructure() {
        final boolean[] flag = {false};
        final TestLibrary.CbStruct s = new TestLibrary.CbStruct();
        s.cb = new Callback() {
            public void callback() {
                flag[0] = true;
            }
        };
        lib.callCallbackInStruct(s);
        assertTrue("Callback not invoked", flag[0]);
    }

    public void testCustomCallbackMethodName() {
        final boolean[] called = {false};
        TestLibrary.VoidCallbackCustom cb = new TestLibrary.VoidCallbackCustom() {
            @Override
            public void customMethodName() {
                called[0] = true;
            }
            @Override
            public String toString() {
                return "Some debug output";
            }
        };
        lib.callVoidCallback(cb);
        assertTrue("Callback with custom method name not called", called[0]);
    }

    public void testDisallowDetachFromJVMThread() {
        final boolean[] called = {false};
        final boolean[] exceptionThrown = {true};
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                called[0] = true;
                try {
                    Native.detach(true);
                }
                catch(IllegalStateException e) {
                }
            }
        };
        lib.callVoidCallback(cb);
        assertTrue("Callback not called", called[0]);
        assertTrue("Native.detach(true) should throw IllegalStateException when called from JVM thread", exceptionThrown[0]);
    }

    public void testCustomCallbackVariedInheritance() {
        final boolean[] called = {false};
        TestLibrary.VoidCallbackCustom cb =
            new TestLibrary.VoidCallbackCustomDerived();
        lib.callVoidCallback(cb);
    }

    protected static class CallbackTypeMapper extends DefaultTypeMapper {
        public int fromNativeConversions = 0;
        public int toNativeConversions = 0;
        public void clear() {
            fromNativeConversions = 0;
            toNativeConversions = 0;
        }
        {
            // Convert java doubles into native integers and back
            TypeConverter converter = new TypeConverter() {
                @Override
                public Object fromNative(Object value, FromNativeContext context) {
                    ++fromNativeConversions;
                    return Double.valueOf(((Integer)value).intValue());
                }
                @Override
                public Class<?> nativeType() {
                    return Integer.class;
                }
                @Override
                public Object toNative(Object value, ToNativeContext ctx) {
                    ++toNativeConversions;
                    return Integer.valueOf(((Double)value).intValue());
                }
            };
            addTypeConverter(double.class, converter);
            converter = new TypeConverter() {
                @Override
                public Object fromNative(Object value, FromNativeContext context) {
                    ++fromNativeConversions;
                    return Float.valueOf(((Long)value).intValue());
                }
                @Override
                public Class<?> nativeType() {
                    return Long.class;
                }
                @Override
                public Object toNative(Object value, ToNativeContext ctx) {
                    ++toNativeConversions;
                    return Long.valueOf(((Float)value).longValue());
                }
            };
            addTypeConverter(float.class, converter);
            converter = new TypeConverter() {
                @Override
                public Object fromNative(Object value, FromNativeContext context) {
                    ++fromNativeConversions;
                    if (value == null) {
                        return null;
                    }
                    if (value instanceof Pointer) {
                        return ((Pointer)value).getWideString(0);
                    }
                    return value.toString();
                }
                @Override
                public Class<?> nativeType() {
                    return WString.class;
                }
                @Override
                public Object toNative(Object value, ToNativeContext ctx) {
                    ++toNativeConversions;
                    return new WString(value.toString());
                }
            };
            addTypeConverter(String.class, converter);
        }
    }

    public static interface CallbackTestLibrary extends Library {
        final CallbackTypeMapper _MAPPER = new CallbackTypeMapper();
        final Map<String, ?> _OPTIONS = Collections.singletonMap(Library.OPTION_TYPE_MAPPER, _MAPPER);

        interface DoubleCallback extends Callback {
            double callback(double arg, double arg2);
        }
        double callInt32Callback(DoubleCallback c, double arg, double arg2);
        interface FloatCallback extends Callback {
            float callback(float arg, float arg2);
        }
        float callInt64Callback(FloatCallback c, float arg, float arg2);
        interface WStringCallback extends Callback {
            String callback(String arg, String arg2);
        }
        String callWideStringCallback(WStringCallback c, String arg, String arg2);
    }

    protected CallbackTestLibrary loadCallbackTestLibrary() {
        return Native.load("testlib", CallbackTestLibrary.class, CallbackTestLibrary._OPTIONS);
    }

    /** This test is here instead of NativeTest in order to facilitate running
        the exact same test on a direct-mapped library without the tests
        interfering with one another due to persistent/cached state in library
        loading.
    */
    public void testCallbackUsesTypeMapper() throws Exception {
        CallbackTestLibrary lib = loadCallbackTestLibrary();
        CallbackTestLibrary._MAPPER.clear();

        final double[] ARGS = new double[2];

        CallbackTestLibrary.DoubleCallback cb = new CallbackTestLibrary.DoubleCallback() {
            @Override
            public double callback(double arg, double arg2) {
                ARGS[0] = arg;
                ARGS[1] = arg2;
                return arg + arg2;
            }
        };
        assertEquals("Wrong type mapper for callback class", CallbackTestLibrary._MAPPER,
                     Native.getTypeMapper(CallbackTestLibrary.DoubleCallback.class));
        assertEquals("Wrong type mapper for callback object", CallbackTestLibrary._MAPPER,
                     Native.getTypeMapper(cb.getClass()));

        double result = lib.callInt32Callback(cb, -1, -2);
        assertEquals("Wrong first callback argument", -1, ARGS[0], 0);
        assertEquals("Wrong second callback argument", -2, ARGS[1], 0);
        assertEquals("Incorrect result of callback invocation", -3, result, 0);

        // Once per argument, then again for return value (convert native int->Java double)
        assertEquals("Type mapper not called for arguments", 3, CallbackTestLibrary._MAPPER.fromNativeConversions);
        // Once per argument, then again for return value (convert Java double->native int)
        assertEquals("Type mapper not called for result", 3, CallbackTestLibrary._MAPPER.toNativeConversions);
    }

    public void testTypeMapperWithWideStrings() throws Exception {
        CallbackTestLibrary lib = loadCallbackTestLibrary();
        CallbackTestLibrary._MAPPER.clear();

        final String[] ARGS = new String[2];

        CallbackTestLibrary.WStringCallback cb = new CallbackTestLibrary.WStringCallback() {
            @Override
            public String callback(String arg, String arg2) {
                ARGS[0] = arg;
                ARGS[1] = arg2;
                return arg + arg2;
            }
        };
        assertEquals("Wrong type mapper for callback class", CallbackTestLibrary._MAPPER,
                     Native.getTypeMapper(CallbackTestLibrary.WStringCallback.class));
        assertEquals("Wrong type mapper for callback object", CallbackTestLibrary._MAPPER,
                     Native.getTypeMapper(cb.getClass()));

        final String[] EXPECTED = { "magic" + UNICODE, getName() + UNICODE };
        String result = lib.callWideStringCallback(cb, EXPECTED[0], EXPECTED[1]);
        assertEquals("Wrong first callback argument", EXPECTED[0], ARGS[0]);
        assertEquals("Wrong second callback argument", EXPECTED[1], ARGS[1]);
        assertEquals("Incorrect result of callback invocation", EXPECTED[0] + EXPECTED[1], result);

        // Once per argument, then again for return value (convert const wchar_t*->Java String)
        assertEquals("Type mapper not called for arguments", 3, CallbackTestLibrary._MAPPER.fromNativeConversions);
        // Once per argument, then again for return value (convert Java String->const wchar_t*)
        assertEquals("Type mapper not called for result", 3, CallbackTestLibrary._MAPPER.toNativeConversions);
    }

    public void testCallbackUsesTypeMapperWithDifferentReturnTypeSize() throws Exception {
        CallbackTestLibrary lib = loadCallbackTestLibrary();

        final float[] ARGS = new float[2];

        CallbackTestLibrary.FloatCallback cb = new CallbackTestLibrary.FloatCallback() {
            @Override
            public float callback(float arg, float arg2) {
                ARGS[0] = arg;
                ARGS[1] = arg2;
                return arg + arg2;
            }
        };
        assertEquals("Wrong type mapper for callback class", CallbackTestLibrary._MAPPER,
                     Native.getTypeMapper(CallbackTestLibrary.FloatCallback.class));
        assertEquals("Wrong type mapper for callback object", CallbackTestLibrary._MAPPER,
                     Native.getTypeMapper(cb.getClass()));

        float result = lib.callInt64Callback(cb, -1, -2);
        assertEquals("Wrong first callback argument", -1, ARGS[0], 0);
        assertEquals("Wrong second callback argument", -2, ARGS[1], 0);
        assertEquals("Incorrect result of callback invocation", -3, result, 0);
    }

    protected void callThreadedCallback(TestLibrary.VoidCallback cb,
                                        CallbackThreadInitializer cti,
                                        int repeat, int sleepms,
                                        int[] called) throws Exception {
        callThreadedCallback(cb, cti, repeat, sleepms, called, repeat);
    }

    protected void callThreadedCallback(TestLibrary.VoidCallback cb,
                                        CallbackThreadInitializer cti,
                                        int repeat, int sleepms,
                                        int[] called, int returnAfter) throws Exception {
        if (cti != null) {
            Native.setCallbackThreadInitializer(cb, cti);
        }
        lib.callVoidCallbackThreaded(cb, repeat, sleepms, getName(), 0);

        long start = System.currentTimeMillis();
        while (called[0] < returnAfter) {
            Thread.sleep(10);
            if (System.currentTimeMillis() - start > THREAD_TIMEOUT) {
                fail("Timed out waiting for callback, invoked " + called[0] + " times so far");
            }
        }
    }

    public void testCallbackThreadDefaults() throws Exception {
        final int[] called = {0};
        final boolean[] daemon = {false};
        final String[] name = { null };
        final ThreadGroup[] group = { null };
        final Thread[] t = { null };

        ThreadGroup testGroup = new ThreadGroup(getName() + UNICODE);
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                Thread thread = Thread.currentThread();
                daemon[0] = thread.isDaemon();
                name[0] = thread.getName();
                group[0] = thread.getThreadGroup();
                t[0] = thread;
                ++called[0];
            }
        };
        callThreadedCallback(cb, null, 1, 100, called);

        assertFalse("Callback thread default should not be attached as daemon", daemon[0]);
        // thread name and group are not defined
    }

    public void testCustomizeCallbackThread() throws Exception {
        final int[] called = {0};
        final boolean[] daemon = {false};
        final String[] name = { null };
        final ThreadGroup[] group = { null };
        final Thread[] t = { null };
        // Ensure unicode is properly handled
        final String tname = "NAME: " + getName() + UNICODE;
        final boolean[] alive = {false};

        ThreadGroup testGroup = new ThreadGroup("Thread group for " + getName());
        CallbackThreadInitializer init = new CallbackThreadInitializer(true, false, tname, testGroup);
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                Thread thread = Thread.currentThread();
                daemon[0] = thread.isDaemon();
                name[0] = thread.getName();
                group[0] = thread.getThreadGroup();
                t[0] = thread;
                if (thread.isAlive()) {
                    // NOTE: older phoneME incorrectly reports thread "alive" status
                    alive[0] = true;
                }

                ++called[0];
                if (THREAD_DETACH_BUG && called[0] == 2) {
                    Native.detach(true);
                }
            }
        };
        callThreadedCallback(cb, init, 2, 2000, called, 1);

        assertTrue("Callback thread not attached as daemon", daemon[0]);
        assertEquals("Callback thread name not applied", tname, name[0]);
        assertEquals("Callback thread group not applied", testGroup, group[0]);
        // NOTE: older phoneME incorrectly reports thread "alive" status
        if (!alive[0]) {
            throw new Error("VM incorrectly reports Thread.isAlive() == false within callback");
        }
        assertTrue("Thread should still be alive", t[0].isAlive());

        long start = System.currentTimeMillis();
        while (called[0] < 2) {
            Thread.sleep(10);
            if (System.currentTimeMillis() - start > THREAD_TIMEOUT) {
                fail("Timed out waiting for second callback invocation, which indicates detach");
            }
        }

        waitFor(t[0]);
    }

    public void testSmallStackCallback() throws Exception {
        // This test runs the callback in a thread, that is allocated a very
        // small size. It was observed on linux amd64, that a library allocated
        // a stack size of 64kB, this prevented the JVM to attach to that
        // thread. The JNIEnv pointer was not checked and this lead to a
        // hard crash of the JVM.
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                System.out.println("Callback called");
            }
        };

        lib.callVoidCallbackThreaded(cb, 1, 0, "Test Callback", 64 * 1024);

        // Give the JVM enough time to run the call back
        Thread.sleep(1 * 1000);
    }

    // Detach preference is indicated by the initializer.  Thread is attached
    // as daemon to avoid VM having to wait for it.
    public void testCallbackThreadPersistence() throws Exception {
        final int[] called = {0};
        final Set<Thread> threads = new HashSet<>();

        final int COUNT = 5;
        CallbackThreadInitializer init = new CallbackThreadInitializer(true, false) {
            @Override
            public String getName(Callback cb) {
                return "Test thread (native) for " + CallbacksTest.this.getName() + " (call count: " + called[0] + ")";
            }
        };
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                threads.add(Thread.currentThread());
                ++called[0];
                if (THREAD_DETACH_BUG && called[0] == COUNT) {
                    Native.detach(true);
                }
            }
        };
        callThreadedCallback(cb, init, COUNT, 100, called);

        assertEquals("Multiple callbacks on a given native thread should use the same Thread mapping: " + threads,
                     1, threads.size());

        waitFor(threads.iterator().next());
    }

    // Thread object is never GC'd on linux-amd64 and darwin-amd64 (w/openjdk7)
    public void testCleanupUndetachedThreadOnThreadExit() throws Exception {
        final Set<Reference<Thread>> threads = new HashSet<>();
        final int[] called = { 0 };
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                threads.add(new WeakReference<>(Thread.currentThread()));
                if (++called[0] == 1) {
                    Thread.currentThread().setName(getName() + " (Thread to be cleaned up)");
                }
                Native.detach(false);
            }
        };
        // Always attach as daemon to ensure tests will exit
        CallbackThreadInitializer asDaemon = new CallbackThreadInitializer(true) {
            @Override
            public String getName(Callback cb) {
                return "Test thread (native) for " + CallbacksTest.this.getName();
            }
        };
        callThreadedCallback(cb, asDaemon, 2, 100, called);
        // Wait for it to start up
        long start = System.currentTimeMillis();
        while (threads.size() == 0 && called[0] == 0) {
            Thread.sleep(10L);
            if (System.currentTimeMillis() - start > THREAD_TIMEOUT) {
                fail("Timed out waiting for thread to detach and terminate");
            }
        }
        start = System.currentTimeMillis();
        Reference<Thread> ref = threads.iterator().next();
        while (ref.get() != null) {
            System.gc();
            Thread.sleep(100);
            Thread[] remaining = new Thread[Thread.activeCount()];
            Thread.enumerate(remaining);
            if (System.currentTimeMillis() - start > THREAD_TIMEOUT) {
                Thread t = ref.get();
                Pointer terminationFlag = Native.getTerminationFlag(t);
                assertNotNull("Native thread termination flag is missing", terminationFlag);
                if (terminationFlag.getInt(0) == 0) {
                    fail("Timed out waiting for native attached thread to be GC'd: " + t + " alive: "
                         + t.isAlive() + " daemon: " + t.isDaemon() + "\n" + Arrays.asList(remaining));
                }
                System.err.println("Warning: JVM did not GC Thread mapping after native thread terminated");
                break;
            }
        }
    }

    // Callback indicates detach preference (instead of
    // CallbackThreadInitializer); thread is non-daemon (default),
    // but callback explicitly detaches it on final invocation.
    public void testCallbackIndicatedThreadDetach() throws Exception {
        final int[] called = {0};
        final Set<Thread> threads = new HashSet<>();
        final int COUNT = 5;
        TestLibrary.VoidCallback cb = new TestLibrary.VoidCallback() {
            @Override
            public void callback() {
                threads.add(Thread.currentThread());
                // detach on final invocation
                int count = called[0] + 1;
                if (count == 1) {
                    Thread.currentThread().setName("Native thread for " + getName());
                    Native.detach(false);
                }
                else if (count == COUNT) {
                    Native.detach(true);
                }
                called[0] = count;
            }
        };
        callThreadedCallback(cb, null, COUNT, 100, called);

        assertEquals("Multiple callbacks in the same native thread should use the same Thread mapping: "
                     + threads, 1, threads.size());

        waitFor(threads.iterator().next());
    }

    public void testDLLCallback() throws Exception {
        if (!Platform.HAS_DLL_CALLBACKS) {
            return;
        }

        final boolean[] called = { false };
        class TestCallback implements TestLibrary.VoidCallback, com.sun.jna.win32.DLLCallback {
            @Override
            public void callback() {
                called[0] = true;
            }
        }

        TestCallback cb = new TestCallback();
        lib.callVoidCallback(cb);
        assertTrue("Callback not called", called[0]);

        // Check module information
        Pointer fp = CallbackReference.getFunctionPointer(cb);
        NativeLibrary kernel32 = NativeLibrary.getInstance("kernel32", W32APIOptions.DEFAULT_OPTIONS);
        Function f = kernel32.getFunction("GetModuleHandleExW");
        final int GET_MODULE_HANDLE_FROM_ADDRESS = 0x4;
        PointerByReference pref = new PointerByReference();
        int result = f.invokeInt(new Object[] { Integer.valueOf(GET_MODULE_HANDLE_FROM_ADDRESS), fp, pref });
        assertTrue("GetModuleHandleEx(fptr) failed: " + Native.getLastError(), result != 0);

        f = kernel32.getFunction("GetModuleFileNameW");
        char[] buf = new char[1024];
        result = f.invokeInt(new Object[] { pref.getValue(), buf, buf.length });
        assertTrue("GetModuleFileName(fptr) failed: " + Native.getLastError(), result != 0);

        f = kernel32.getFunction("GetModuleHandleW");
        Pointer handle = f.invokePointer(new Object[] { Native.jnidispatchPath != null ? Native.jnidispatchPath : "jnidispatch" });
        assertNotNull("GetModuleHandle(\"jnidispatch\") failed: " + Native.getLastError(), handle);
        assertEquals("Wrong module HANDLE for DLL function pointer", handle, pref.getValue());

        // Check slot re-use
        Map<Callback, CallbackReference> refs = new WeakHashMap<>(callbackCache());
        assertTrue("Callback not cached", refs.containsKey(cb));
        CallbackReference ref = refs.get(cb);
        refs = callbackCache();
        Pointer cbstruct = ref.cbstruct;
        Pointer first_fptr = cbstruct.getPointer(0);

        cb = null;
        System.gc();
        for (int i = 0; i < 100 && (ref.get() != null || refs.containsValue(ref)); ++i) {
            Thread.sleep(10); // Give the GC a chance to run
            System.gc();
        }
        assertNull("Callback not GC'd", ref.get());
        assertFalse("Callback still in map", refs.containsValue(ref));

        ref = null;
        System.gc();
        for (int i = 0; i < 100 && (cbstruct.peer != 0 || refs.size() > 0); ++i) {
            // Flush weak hash map
            refs.size();
            Thread.sleep(10); // Give the GC a chance to run
            System.gc();
        }
        assertEquals("Callback trampoline not freed", 0, cbstruct.peer);

        // Next allocation should be at same place
        called[0] = false;
        cb = new TestCallback();

        lib.callVoidCallback(cb);
        ref = refs.get(cb);
        cbstruct = ref.cbstruct;

        assertTrue("Callback not called", called[0]);
        assertEquals("Same (in-DLL) address should be re-used for DLL callbacks after callback is GCd",
                     first_fptr, cbstruct.getPointer(0));
    }

    public void testThrowOutOfMemoryWhenDLLCallbacksExhausted() throws Exception {
        if (!Platform.HAS_DLL_CALLBACKS) {
            return;
        }

        final boolean[] called = { false };
        class TestCallback implements TestLibrary.VoidCallback, com.sun.jna.win32.DLLCallback {
            @Override
            public void callback() {
                called[0] = true;
            }
        }

        // Exceeding allocations should result in OOM error
        try {
            for (int i=0;i <= TestCallback.DLL_FPTRS;i++) {
                lib.callVoidCallback(new TestCallback());
            }
            fail("Expected out of memory error when all DLL callbacks used");
        }
        catch(OutOfMemoryError e) {
        }
    }

    public interface TaggedCallingConventionTestLibrary extends Library, AltCallingConvention {
        interface TestCallbackTagged extends Callback, AltCallingConvention {
            void invoke();
        }
    }

    public void testCallingConventionFromInterface() {
        TaggedCallingConventionTestLibrary lib = Native.load("testlib", TaggedCallingConventionTestLibrary.class);
        TaggedCallingConventionTestLibrary.TestCallbackTagged cb = new TaggedCallingConventionTestLibrary.TestCallbackTagged() {
            @Override
            public void invoke() { }
        };
        try {
            Pointer p = CallbackReference.getFunctionPointer(cb);
            CallbackReference ref = CallbackReference.callbackMap.get(cb);
            assertNotNull("CallbackReference not found", ref);
            assertEquals("Tag-based calling convention not applied", Function.ALT_CONVENTION, ref.callingConvention);
        }
        catch (IllegalArgumentException e) {
            // Alt convention not supported
        }
    }

    public interface OptionCallingConventionTestLibrary extends Library {
        interface TestCallback extends Callback {
            void invoke();
        }
    }

    public void testCallingConventionFromOptions() {
        OptionCallingConventionTestLibrary lib =
                Native.load("testlib", OptionCallingConventionTestLibrary.class, Collections.singletonMap(Library.OPTION_CALLING_CONVENTION, Function.ALT_CONVENTION));
        assertNotNull("Library not loaded", lib);
        OptionCallingConventionTestLibrary.TestCallback cb = new OptionCallingConventionTestLibrary.TestCallback() {
            @Override
            public void invoke() { }
        };
        try {
            Pointer p = CallbackReference.getFunctionPointer(cb);
            assertNotNull("No function pointer", p);
            CallbackReference ref = CallbackReference.callbackMap.get(cb);
            assertNotNull("CallbackReference not found", ref);
            assertEquals("Option-based calling convention not applied", Function.ALT_CONVENTION, ref.callingConvention);
        } catch(IllegalArgumentException e) {
            // Alt convention not supported
        }
    }

    public static void main(java.lang.String[] argList) {
        junit.textui.TestRunner.run(CallbacksTest.class);
    }
}