1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * These are all tests of JNI, but where the JNI calls themselves are
19  * represented as macro invocations. There are both C and C++ files which
20  * include this file. A #if at the top of this file (immediately below)
21  * detects which language is being used and defines macros accordingly.
22  *
23  * This file also defines a static method called runAllTests(), which
24  * does what it says.
25  */
26 
27 #ifndef INCLUDED_FROM_WRAPPER
28 #error "This file should only be compiled by including it from a wrapper file."
29 #endif
30 
31 #include "helper.h"
32 #include <stdbool.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 
37 /** reference to test class {@code InstanceFromNative} */
38 static jclass InstanceFromNative;
39 
40 /** reference to test class {@code StaticFromNative} */
41 static jclass StaticFromNative;
42 
43 /** reference to field {@code InstanceFromNative.theOne} */
44 static jfieldID InstanceFromNative_theOne;
45 
46 /** reference to string {@code "biscuits"} */
47 static jstring biscuits;
48 
49 /**
50  * how to call a method: (virtual, direct, static) x (standard, array of
51  * args, va_list) */
52 typedef enum {
53     VIRTUAL_PLAIN, VIRTUAL_ARRAY, VIRTUAL_VA,
54     DIRECT_PLAIN, DIRECT_ARRAY, DIRECT_VA,
55     STATIC_PLAIN, STATIC_ARRAY, STATIC_VA,
56 } callType;
57 
58 /*
59  * CALL() calls the JNI function with the given name, using a JNIEnv
60  * pointer named "env", and passing along any other arguments given.
61  */
62 #ifdef __cplusplus
63 #define CALL(name, args...) env->name(args)
64 #else
65 /*
66  * Note: The space before the comma below is significant with respect to
67  * the processing of the ## operator.
68  */
69 #define CALL(name, args...) (*env)->name(env , ## args)
70 #endif
71 
72 
73 /**
74  * Simple assert-like macro which returns NULL if the two values are
75  * equal, or an error message if they aren't.
76  */
77 #define FAIL_IF_UNEQUAL(printfType, expected, actual)          \
78     ((expected) == (actual)) ? NULL :                          \
79         failure("expected " printfType " but got " printfType, \
80                 expected, actual);
81 
82 
83 /**
84  * Initializes the static variables. Returns NULL on success or an
85  * error string on failure.
86  */
initializeVariables(JNIEnv * env)87 static char *initializeVariables(JNIEnv *env) {
88     jclass clazz;
89     jfieldID field;
90 
91     clazz = CALL(FindClass, "android/jni/cts/StaticFromNative");
92     if (clazz == NULL) {
93         return failure("could not find StaticFromNative");
94     }
95 
96     StaticFromNative = (jclass) CALL(NewGlobalRef, clazz);
97 
98     clazz = CALL(FindClass, "android/jni/cts/InstanceFromNative");
99     if (clazz == NULL) {
100         return failure("could not find InstanceFromNative");
101     }
102 
103     InstanceFromNative = (jclass) CALL(NewGlobalRef, clazz);
104 
105     field = CALL(GetStaticFieldID, InstanceFromNative, "theOne",
106             "Landroid/jni/cts/InstanceFromNative;");
107     if (field == NULL) {
108         return failure("could not find InstanceFromNative.theOne");
109     }
110 
111     InstanceFromNative_theOne = field;
112 
113     biscuits = CALL(NewStringUTF, "biscuits");
114     if (biscuits == NULL) {
115         return failure("could not construct string");
116     }
117 
118     biscuits = (jstring) CALL(NewGlobalRef, biscuits);
119 
120     return NULL;
121 }
122 
123 /**
124  * Gets the standard instance of InstanceFromNative.
125  */
getStandardInstance(JNIEnv * env)126 static jobject getStandardInstance(JNIEnv *env) {
127     return CALL(GetStaticObjectField, InstanceFromNative,
128             InstanceFromNative_theOne);
129 }
130 
131 /**
132  * Looks up a static method on StaticFromNative.
133  */
findStaticMethod(JNIEnv * env,char ** errorMsg,const char * name,const char * sig)134 static jmethodID findStaticMethod(JNIEnv *env, char **errorMsg,
135         const char *name, const char *sig) {
136     jmethodID result = CALL(GetStaticMethodID, StaticFromNative,
137             name, sig);
138 
139     if (result == NULL) {
140         *errorMsg = failure("could not find static test method %s:%s",
141                 name, sig);
142     }
143 
144     return result;
145 }
146 
147 /**
148  * Looks up an instance method on InstanceFromNative.
149  */
findInstanceMethod(JNIEnv * env,char ** errorMsg,const char * name,const char * sig)150 static jmethodID findInstanceMethod(JNIEnv *env, char **errorMsg,
151         const char *name, const char *sig) {
152     jmethodID result = CALL(GetMethodID, InstanceFromNative, name, sig);
153 
154     if (result == NULL) {
155         *errorMsg = failure("could not find instance test method %s:%s",
156                 name, sig);
157     }
158 
159     return result;
160 }
161 
162 /**
163  * Looks up either an instance method on InstanceFromNative or a
164  * static method on StaticFromNative, depending on the given
165  * call type.
166  */
findAppropriateMethod(JNIEnv * env,char ** errorMsg,callType ct,const char * name,const char * sig)167 static jmethodID findAppropriateMethod(JNIEnv *env, char **errorMsg,
168         callType ct, const char *name, const char *sig) {
169     if ((ct == STATIC_PLAIN) || (ct == STATIC_ARRAY) ||
170             (ct == STATIC_VA)) {
171         return findStaticMethod(env, errorMsg, name, sig);
172     } else {
173         return findInstanceMethod(env, errorMsg, name, sig);
174     }
175 }
176 
177 
178 /*
179  * The tests.
180  */
181 
182 // TODO: Missing functions:
183 //   AllocObject
184 
help_CallBooleanMethod(JNIEnv * env,callType ct,...)185 static char *help_CallBooleanMethod(JNIEnv *env, callType ct, ...) {
186     va_list args;
187     va_start(args, ct);
188 
189     char *msg;
190     jobject o = getStandardInstance(env);
191     jmethodID method = findAppropriateMethod(env, &msg, ct,
192             "returnBoolean", "()Z");
193 
194     if (method == NULL) {
195         return msg;
196     }
197 
198     jboolean result;
199 
200     switch (ct) {
201         case VIRTUAL_PLAIN: {
202             result = CALL(CallBooleanMethod, o, method);
203             break;
204         }
205         case VIRTUAL_ARRAY: {
206             result = CALL(CallBooleanMethodA, o, method, NULL);
207             break;
208         }
209         case VIRTUAL_VA: {
210             result = CALL(CallBooleanMethodV, o, method, args);
211             break;
212         }
213         case DIRECT_PLAIN: {
214             result = CALL(CallNonvirtualBooleanMethod, o, InstanceFromNative,
215                     method);
216             break;
217         }
218         case DIRECT_ARRAY: {
219             result = CALL(CallNonvirtualBooleanMethodA, o, InstanceFromNative,
220                     method, NULL);
221             break;
222         }
223         case DIRECT_VA: {
224             result = CALL(CallNonvirtualBooleanMethodV, o, InstanceFromNative,
225                     method, args);
226             break;
227         }
228         case STATIC_PLAIN: {
229             result = CALL(CallStaticBooleanMethod, StaticFromNative, method);
230             break;
231         }
232         case STATIC_ARRAY: {
233             result = CALL(CallStaticBooleanMethodA, StaticFromNative, method,
234                     NULL);
235             break;
236         }
237         case STATIC_VA: {
238             result = CALL(CallStaticBooleanMethodV, StaticFromNative, method,
239                     args);
240             break;
241         }
242         default: {
243             return failure("shouldn't happen");
244         }
245     }
246 
247     va_end(args);
248 
249     return FAIL_IF_UNEQUAL("%d", true, result);
250 }
251 
TEST_DECLARATION(CallBooleanMethod)252 TEST_DECLARATION(CallBooleanMethod) {
253     return help_CallBooleanMethod(env, VIRTUAL_PLAIN);
254 }
255 
TEST_DECLARATION(CallBooleanMethodA)256 TEST_DECLARATION(CallBooleanMethodA) {
257     return help_CallBooleanMethod(env, VIRTUAL_ARRAY);
258 }
259 
TEST_DECLARATION(CallBooleanMethodV)260 TEST_DECLARATION(CallBooleanMethodV) {
261     return help_CallBooleanMethod(env, VIRTUAL_VA);
262 }
263 
TEST_DECLARATION(CallNonvirtualBooleanMethod)264 TEST_DECLARATION(CallNonvirtualBooleanMethod) {
265     return help_CallBooleanMethod(env, DIRECT_PLAIN);
266 }
267 
TEST_DECLARATION(CallNonvirtualBooleanMethodA)268 TEST_DECLARATION(CallNonvirtualBooleanMethodA) {
269     return help_CallBooleanMethod(env, DIRECT_ARRAY);
270 }
271 
TEST_DECLARATION(CallNonvirtualBooleanMethodV)272 TEST_DECLARATION(CallNonvirtualBooleanMethodV) {
273     return help_CallBooleanMethod(env, DIRECT_VA);
274 }
275 
TEST_DECLARATION(CallStaticBooleanMethod)276 TEST_DECLARATION(CallStaticBooleanMethod) {
277     return help_CallBooleanMethod(env, STATIC_PLAIN);
278 }
279 
TEST_DECLARATION(CallStaticBooleanMethodA)280 TEST_DECLARATION(CallStaticBooleanMethodA) {
281     return help_CallBooleanMethod(env, STATIC_ARRAY);
282 }
283 
TEST_DECLARATION(CallStaticBooleanMethodV)284 TEST_DECLARATION(CallStaticBooleanMethodV) {
285     return help_CallBooleanMethod(env, STATIC_VA);
286 }
287 
help_CallByteMethod(JNIEnv * env,callType ct,...)288 static char *help_CallByteMethod(JNIEnv *env, callType ct, ...) {
289     va_list args;
290     va_start(args, ct);
291 
292     char *msg;
293     jobject o = getStandardInstance(env);
294     jmethodID method = findAppropriateMethod(env, &msg, ct,
295             "returnByte", "()B");
296 
297     if (method == NULL) {
298         return msg;
299     }
300 
301     jbyte result;
302 
303     switch (ct) {
304         case VIRTUAL_PLAIN: {
305             result = CALL(CallByteMethod, o, method);
306             break;
307         }
308         case VIRTUAL_ARRAY: {
309             result = CALL(CallByteMethodA, o, method, NULL);
310             break;
311         }
312         case VIRTUAL_VA: {
313             result = CALL(CallByteMethodV, o, method, args);
314             break;
315         }
316         case DIRECT_PLAIN: {
317             result = CALL(CallNonvirtualByteMethod, o, InstanceFromNative,
318                     method);
319             break;
320         }
321         case DIRECT_ARRAY: {
322             result = CALL(CallNonvirtualByteMethodA, o, InstanceFromNative,
323                     method, NULL);
324             break;
325         }
326         case DIRECT_VA: {
327             result = CALL(CallNonvirtualByteMethodV, o, InstanceFromNative,
328                     method, args);
329             break;
330         }
331         case STATIC_PLAIN: {
332             result = CALL(CallStaticByteMethod, StaticFromNative, method);
333             break;
334         }
335         case STATIC_ARRAY: {
336             result = CALL(CallStaticByteMethodA, StaticFromNative, method,
337                     NULL);
338             break;
339         }
340         case STATIC_VA: {
341             result = CALL(CallStaticByteMethodV, StaticFromNative, method,
342                     args);
343             break;
344         }
345         default: {
346             return failure("shouldn't happen");
347         }
348     }
349 
350     va_end(args);
351 
352     return FAIL_IF_UNEQUAL("%d", 14, result);
353 }
354 
TEST_DECLARATION(CallByteMethod)355 TEST_DECLARATION(CallByteMethod) {
356     return help_CallByteMethod(env, VIRTUAL_PLAIN);
357 }
358 
TEST_DECLARATION(CallByteMethodA)359 TEST_DECLARATION(CallByteMethodA) {
360     return help_CallByteMethod(env, VIRTUAL_ARRAY);
361 }
362 
TEST_DECLARATION(CallByteMethodV)363 TEST_DECLARATION(CallByteMethodV) {
364     return help_CallByteMethod(env, VIRTUAL_VA);
365 }
366 
TEST_DECLARATION(CallNonvirtualByteMethod)367 TEST_DECLARATION(CallNonvirtualByteMethod) {
368     return help_CallByteMethod(env, DIRECT_PLAIN);
369 }
370 
TEST_DECLARATION(CallNonvirtualByteMethodA)371 TEST_DECLARATION(CallNonvirtualByteMethodA) {
372     return help_CallByteMethod(env, DIRECT_ARRAY);
373 }
374 
TEST_DECLARATION(CallNonvirtualByteMethodV)375 TEST_DECLARATION(CallNonvirtualByteMethodV) {
376     return help_CallByteMethod(env, DIRECT_VA);
377 }
378 
TEST_DECLARATION(CallStaticByteMethod)379 TEST_DECLARATION(CallStaticByteMethod) {
380     return help_CallByteMethod(env, STATIC_PLAIN);
381 }
382 
TEST_DECLARATION(CallStaticByteMethodA)383 TEST_DECLARATION(CallStaticByteMethodA) {
384     return help_CallByteMethod(env, STATIC_ARRAY);
385 }
386 
TEST_DECLARATION(CallStaticByteMethodV)387 TEST_DECLARATION(CallStaticByteMethodV) {
388     return help_CallByteMethod(env, STATIC_VA);
389 }
390 
help_CallShortMethod(JNIEnv * env,callType ct,...)391 static char *help_CallShortMethod(JNIEnv *env, callType ct, ...) {
392     va_list args;
393     va_start(args, ct);
394 
395     char *msg;
396     jobject o = getStandardInstance(env);
397     jmethodID method = findAppropriateMethod(env, &msg, ct,
398             "returnShort", "()S");
399 
400     if (method == NULL) {
401         return msg;
402     }
403 
404     jshort result;
405 
406     switch (ct) {
407         case VIRTUAL_PLAIN: {
408             result = CALL(CallShortMethod, o, method);
409             break;
410         }
411         case VIRTUAL_ARRAY: {
412             result = CALL(CallShortMethodA, o, method, NULL);
413             break;
414         }
415         case VIRTUAL_VA: {
416             result = CALL(CallShortMethodV, o, method, args);
417             break;
418         }
419         case DIRECT_PLAIN: {
420             result = CALL(CallNonvirtualShortMethod, o, InstanceFromNative,
421                     method);
422             break;
423         }
424         case DIRECT_ARRAY: {
425             result = CALL(CallNonvirtualShortMethodA, o, InstanceFromNative,
426                     method, NULL);
427             break;
428         }
429         case DIRECT_VA: {
430             result = CALL(CallNonvirtualShortMethodV, o, InstanceFromNative,
431                     method, args);
432             break;
433         }
434         case STATIC_PLAIN: {
435             result = CALL(CallStaticShortMethod, StaticFromNative, method);
436             break;
437         }
438         case STATIC_ARRAY: {
439             result = CALL(CallStaticShortMethodA, StaticFromNative, method,
440                     NULL);
441             break;
442         }
443         case STATIC_VA: {
444             result = CALL(CallStaticShortMethodV, StaticFromNative, method,
445                     args);
446             break;
447         }
448         default: {
449             return failure("shouldn't happen");
450         }
451     }
452 
453     va_end(args);
454 
455     return FAIL_IF_UNEQUAL("%d", -608, result);
456 }
457 
TEST_DECLARATION(CallShortMethod)458 TEST_DECLARATION(CallShortMethod) {
459     return help_CallShortMethod(env, VIRTUAL_PLAIN);
460 }
461 
TEST_DECLARATION(CallShortMethodA)462 TEST_DECLARATION(CallShortMethodA) {
463     return help_CallShortMethod(env, VIRTUAL_ARRAY);
464 }
465 
TEST_DECLARATION(CallShortMethodV)466 TEST_DECLARATION(CallShortMethodV) {
467     return help_CallShortMethod(env, VIRTUAL_VA);
468 }
469 
TEST_DECLARATION(CallNonvirtualShortMethod)470 TEST_DECLARATION(CallNonvirtualShortMethod) {
471     return help_CallShortMethod(env, DIRECT_PLAIN);
472 }
473 
TEST_DECLARATION(CallNonvirtualShortMethodA)474 TEST_DECLARATION(CallNonvirtualShortMethodA) {
475     return help_CallShortMethod(env, DIRECT_ARRAY);
476 }
477 
TEST_DECLARATION(CallNonvirtualShortMethodV)478 TEST_DECLARATION(CallNonvirtualShortMethodV) {
479     return help_CallShortMethod(env, DIRECT_VA);
480 }
481 
TEST_DECLARATION(CallStaticShortMethod)482 TEST_DECLARATION(CallStaticShortMethod) {
483     return help_CallShortMethod(env, STATIC_PLAIN);
484 }
485 
TEST_DECLARATION(CallStaticShortMethodA)486 TEST_DECLARATION(CallStaticShortMethodA) {
487     return help_CallShortMethod(env, STATIC_ARRAY);
488 }
489 
TEST_DECLARATION(CallStaticShortMethodV)490 TEST_DECLARATION(CallStaticShortMethodV) {
491     return help_CallShortMethod(env, STATIC_VA);
492 }
493 
help_CallCharMethod(JNIEnv * env,callType ct,...)494 static char *help_CallCharMethod(JNIEnv *env, callType ct, ...) {
495     va_list args;
496     va_start(args, ct);
497 
498     char *msg;
499     jobject o = getStandardInstance(env);
500     jmethodID method = findAppropriateMethod(env, &msg, ct,
501             "returnChar", "()C");
502 
503     if (method == NULL) {
504         return msg;
505     }
506 
507     jchar result;
508 
509     switch (ct) {
510         case VIRTUAL_PLAIN: {
511             result = CALL(CallCharMethod, o, method);
512             break;
513         }
514         case VIRTUAL_ARRAY: {
515             result = CALL(CallCharMethodA, o, method, NULL);
516             break;
517         }
518         case VIRTUAL_VA: {
519             result = CALL(CallCharMethodV, o, method, args);
520             break;
521         }
522         case DIRECT_PLAIN: {
523             result = CALL(CallNonvirtualCharMethod, o, InstanceFromNative,
524                     method);
525             break;
526         }
527         case DIRECT_ARRAY: {
528             result = CALL(CallNonvirtualCharMethodA, o, InstanceFromNative,
529                     method, NULL);
530             break;
531         }
532         case DIRECT_VA: {
533             result = CALL(CallNonvirtualCharMethodV, o, InstanceFromNative,
534                     method, args);
535             break;
536         }
537         case STATIC_PLAIN: {
538             result = CALL(CallStaticCharMethod, StaticFromNative, method);
539             break;
540         }
541         case STATIC_ARRAY: {
542             result = CALL(CallStaticCharMethodA, StaticFromNative, method,
543                     NULL);
544             break;
545         }
546         case STATIC_VA: {
547             result = CALL(CallStaticCharMethodV, StaticFromNative, method,
548                     args);
549             break;
550         }
551         default: {
552             return failure("shouldn't happen");
553         }
554     }
555 
556     va_end(args);
557 
558     return FAIL_IF_UNEQUAL("%d", 9000, result);
559 }
560 
TEST_DECLARATION(CallCharMethod)561 TEST_DECLARATION(CallCharMethod) {
562     return help_CallCharMethod(env, VIRTUAL_PLAIN);
563 }
564 
TEST_DECLARATION(CallCharMethodA)565 TEST_DECLARATION(CallCharMethodA) {
566     return help_CallCharMethod(env, VIRTUAL_ARRAY);
567 }
568 
TEST_DECLARATION(CallCharMethodV)569 TEST_DECLARATION(CallCharMethodV) {
570     return help_CallCharMethod(env, VIRTUAL_VA);
571 }
572 
TEST_DECLARATION(CallNonvirtualCharMethod)573 TEST_DECLARATION(CallNonvirtualCharMethod) {
574     return help_CallCharMethod(env, DIRECT_PLAIN);
575 }
576 
TEST_DECLARATION(CallNonvirtualCharMethodA)577 TEST_DECLARATION(CallNonvirtualCharMethodA) {
578     return help_CallCharMethod(env, DIRECT_ARRAY);
579 }
580 
TEST_DECLARATION(CallNonvirtualCharMethodV)581 TEST_DECLARATION(CallNonvirtualCharMethodV) {
582     return help_CallCharMethod(env, DIRECT_VA);
583 }
584 
TEST_DECLARATION(CallStaticCharMethod)585 TEST_DECLARATION(CallStaticCharMethod) {
586     return help_CallCharMethod(env, STATIC_PLAIN);
587 }
588 
TEST_DECLARATION(CallStaticCharMethodA)589 TEST_DECLARATION(CallStaticCharMethodA) {
590     return help_CallCharMethod(env, STATIC_ARRAY);
591 }
592 
TEST_DECLARATION(CallStaticCharMethodV)593 TEST_DECLARATION(CallStaticCharMethodV) {
594     return help_CallCharMethod(env, STATIC_VA);
595 }
596 
help_CallIntMethod(JNIEnv * env,callType ct,...)597 static char *help_CallIntMethod(JNIEnv *env, callType ct, ...) {
598     va_list args;
599     va_start(args, ct);
600 
601     char *msg;
602     jobject o = getStandardInstance(env);
603     jmethodID method = findAppropriateMethod(env, &msg, ct,
604             "returnInt", "()I");
605 
606     if (method == NULL) {
607         return msg;
608     }
609 
610     jint result;
611 
612     switch (ct) {
613         case VIRTUAL_PLAIN: {
614             result = CALL(CallIntMethod, o, method);
615             break;
616         }
617         case VIRTUAL_ARRAY: {
618             result = CALL(CallIntMethodA, o, method, NULL);
619             break;
620         }
621         case VIRTUAL_VA: {
622             result = CALL(CallIntMethodV, o, method, args);
623             break;
624         }
625         case DIRECT_PLAIN: {
626             result = CALL(CallNonvirtualIntMethod, o, InstanceFromNative,
627                     method);
628             break;
629         }
630         case DIRECT_ARRAY: {
631             result = CALL(CallNonvirtualIntMethodA, o, InstanceFromNative,
632                     method, NULL);
633             break;
634         }
635         case DIRECT_VA: {
636             result = CALL(CallNonvirtualIntMethodV, o, InstanceFromNative,
637                     method, args);
638             break;
639         }
640         case STATIC_PLAIN: {
641             result = CALL(CallStaticIntMethod, StaticFromNative, method);
642             break;
643         }
644         case STATIC_ARRAY: {
645             result = CALL(CallStaticIntMethodA, StaticFromNative, method,
646                     NULL);
647             break;
648         }
649         case STATIC_VA: {
650             result = CALL(CallStaticIntMethodV, StaticFromNative, method,
651                     args);
652             break;
653         }
654         default: {
655             return failure("shouldn't happen");
656         }
657     }
658 
659     va_end(args);
660 
661     return FAIL_IF_UNEQUAL("%d", 4004004, result);
662 }
663 
TEST_DECLARATION(CallIntMethod)664 TEST_DECLARATION(CallIntMethod) {
665     return help_CallIntMethod(env, VIRTUAL_PLAIN);
666 }
667 
TEST_DECLARATION(CallIntMethodA)668 TEST_DECLARATION(CallIntMethodA) {
669     return help_CallIntMethod(env, VIRTUAL_ARRAY);
670 }
671 
TEST_DECLARATION(CallIntMethodV)672 TEST_DECLARATION(CallIntMethodV) {
673     return help_CallIntMethod(env, VIRTUAL_VA);
674 }
675 
TEST_DECLARATION(CallNonvirtualIntMethod)676 TEST_DECLARATION(CallNonvirtualIntMethod) {
677     return help_CallIntMethod(env, DIRECT_PLAIN);
678 }
679 
TEST_DECLARATION(CallNonvirtualIntMethodA)680 TEST_DECLARATION(CallNonvirtualIntMethodA) {
681     return help_CallIntMethod(env, DIRECT_ARRAY);
682 }
683 
TEST_DECLARATION(CallNonvirtualIntMethodV)684 TEST_DECLARATION(CallNonvirtualIntMethodV) {
685     return help_CallIntMethod(env, DIRECT_VA);
686 }
687 
TEST_DECLARATION(CallStaticIntMethod)688 TEST_DECLARATION(CallStaticIntMethod) {
689     return help_CallIntMethod(env, STATIC_PLAIN);
690 }
691 
TEST_DECLARATION(CallStaticIntMethodA)692 TEST_DECLARATION(CallStaticIntMethodA) {
693     return help_CallIntMethod(env, STATIC_ARRAY);
694 }
695 
TEST_DECLARATION(CallStaticIntMethodV)696 TEST_DECLARATION(CallStaticIntMethodV) {
697     return help_CallIntMethod(env, STATIC_VA);
698 }
699 
help_CallLongMethod(JNIEnv * env,callType ct,...)700 static char *help_CallLongMethod(JNIEnv *env, callType ct, ...) {
701     va_list args;
702     va_start(args, ct);
703 
704     char *msg;
705     jobject o = getStandardInstance(env);
706     jmethodID method = findAppropriateMethod(env, &msg, ct,
707             "returnLong", "()J");
708 
709     if (method == NULL) {
710         return msg;
711     }
712 
713     jlong result;
714 
715     switch (ct) {
716         case VIRTUAL_PLAIN: {
717             result = CALL(CallLongMethod, o, method);
718             break;
719         }
720         case VIRTUAL_ARRAY: {
721             result = CALL(CallLongMethodA, o, method, NULL);
722             break;
723         }
724         case VIRTUAL_VA: {
725             result = CALL(CallLongMethodV, o, method, args);
726             break;
727         }
728         case DIRECT_PLAIN: {
729             result = CALL(CallNonvirtualLongMethod, o, InstanceFromNative,
730                     method);
731             break;
732         }
733         case DIRECT_ARRAY: {
734             result = CALL(CallNonvirtualLongMethodA, o, InstanceFromNative,
735                     method, NULL);
736             break;
737         }
738         case DIRECT_VA: {
739             result = CALL(CallNonvirtualLongMethodV, o, InstanceFromNative,
740                     method, args);
741             break;
742         }
743         case STATIC_PLAIN: {
744             result = CALL(CallStaticLongMethod, StaticFromNative, method);
745             break;
746         }
747         case STATIC_ARRAY: {
748             result = CALL(CallStaticLongMethodA, StaticFromNative, method,
749                     NULL);
750             break;
751         }
752         case STATIC_VA: {
753             result = CALL(CallStaticLongMethodV, StaticFromNative, method,
754                     args);
755             break;
756         }
757         default: {
758             return failure("shouldn't happen");
759         }
760     }
761 
762     va_end(args);
763 
764     return FAIL_IF_UNEQUAL("%lld", -80080080087LL, result);
765 }
766 
TEST_DECLARATION(CallLongMethod)767 TEST_DECLARATION(CallLongMethod) {
768     return help_CallLongMethod(env, VIRTUAL_PLAIN);
769 }
770 
TEST_DECLARATION(CallLongMethodA)771 TEST_DECLARATION(CallLongMethodA) {
772     return help_CallLongMethod(env, VIRTUAL_ARRAY);
773 }
774 
TEST_DECLARATION(CallLongMethodV)775 TEST_DECLARATION(CallLongMethodV) {
776     return help_CallLongMethod(env, VIRTUAL_VA);
777 }
778 
TEST_DECLARATION(CallNonvirtualLongMethod)779 TEST_DECLARATION(CallNonvirtualLongMethod) {
780     return help_CallLongMethod(env, DIRECT_PLAIN);
781 }
782 
TEST_DECLARATION(CallNonvirtualLongMethodA)783 TEST_DECLARATION(CallNonvirtualLongMethodA) {
784     return help_CallLongMethod(env, DIRECT_ARRAY);
785 }
786 
TEST_DECLARATION(CallNonvirtualLongMethodV)787 TEST_DECLARATION(CallNonvirtualLongMethodV) {
788     return help_CallLongMethod(env, DIRECT_VA);
789 }
790 
TEST_DECLARATION(CallStaticLongMethod)791 TEST_DECLARATION(CallStaticLongMethod) {
792     return help_CallLongMethod(env, STATIC_PLAIN);
793 }
794 
TEST_DECLARATION(CallStaticLongMethodA)795 TEST_DECLARATION(CallStaticLongMethodA) {
796     return help_CallLongMethod(env, STATIC_ARRAY);
797 }
798 
TEST_DECLARATION(CallStaticLongMethodV)799 TEST_DECLARATION(CallStaticLongMethodV) {
800     return help_CallLongMethod(env, STATIC_VA);
801 }
802 
help_CallFloatMethod(JNIEnv * env,callType ct,...)803 static char *help_CallFloatMethod(JNIEnv *env, callType ct, ...) {
804     va_list args;
805     va_start(args, ct);
806 
807     char *msg;
808     jobject o = getStandardInstance(env);
809     jmethodID method = findAppropriateMethod(env, &msg, ct,
810             "returnFloat", "()F");
811 
812     if (method == NULL) {
813         return msg;
814     }
815 
816     jfloat result;
817 
818     switch (ct) {
819         case VIRTUAL_PLAIN: {
820             result = CALL(CallFloatMethod, o, method);
821             break;
822         }
823         case VIRTUAL_ARRAY: {
824             result = CALL(CallFloatMethodA, o, method, NULL);
825             break;
826         }
827         case VIRTUAL_VA: {
828             result = CALL(CallFloatMethodV, o, method, args);
829             break;
830         }
831         case DIRECT_PLAIN: {
832             result = CALL(CallNonvirtualFloatMethod, o, InstanceFromNative,
833                     method);
834             break;
835         }
836         case DIRECT_ARRAY: {
837             result = CALL(CallNonvirtualFloatMethodA, o, InstanceFromNative,
838                     method, NULL);
839             break;
840         }
841         case DIRECT_VA: {
842             result = CALL(CallNonvirtualFloatMethodV, o, InstanceFromNative,
843                     method, args);
844             break;
845         }
846         case STATIC_PLAIN: {
847             result = CALL(CallStaticFloatMethod, StaticFromNative, method);
848             break;
849         }
850         case STATIC_ARRAY: {
851             result = CALL(CallStaticFloatMethodA, StaticFromNative, method,
852                     NULL);
853             break;
854         }
855         case STATIC_VA: {
856             result = CALL(CallStaticFloatMethodV, StaticFromNative, method,
857                     args);
858             break;
859         }
860         default: {
861             return failure("shouldn't happen");
862         }
863     }
864 
865     va_end(args);
866 
867     return FAIL_IF_UNEQUAL("%g", 2.5e22f, result);
868 }
869 
TEST_DECLARATION(CallFloatMethod)870 TEST_DECLARATION(CallFloatMethod) {
871     return help_CallFloatMethod(env, VIRTUAL_PLAIN);
872 }
873 
TEST_DECLARATION(CallFloatMethodA)874 TEST_DECLARATION(CallFloatMethodA) {
875     return help_CallFloatMethod(env, VIRTUAL_ARRAY);
876 }
877 
TEST_DECLARATION(CallFloatMethodV)878 TEST_DECLARATION(CallFloatMethodV) {
879     return help_CallFloatMethod(env, VIRTUAL_VA);
880 }
881 
TEST_DECLARATION(CallNonvirtualFloatMethod)882 TEST_DECLARATION(CallNonvirtualFloatMethod) {
883     return help_CallFloatMethod(env, DIRECT_PLAIN);
884 }
885 
TEST_DECLARATION(CallNonvirtualFloatMethodA)886 TEST_DECLARATION(CallNonvirtualFloatMethodA) {
887     return help_CallFloatMethod(env, DIRECT_ARRAY);
888 }
889 
TEST_DECLARATION(CallNonvirtualFloatMethodV)890 TEST_DECLARATION(CallNonvirtualFloatMethodV) {
891     return help_CallFloatMethod(env, DIRECT_VA);
892 }
893 
TEST_DECLARATION(CallStaticFloatMethod)894 TEST_DECLARATION(CallStaticFloatMethod) {
895     return help_CallFloatMethod(env, STATIC_PLAIN);
896 }
897 
TEST_DECLARATION(CallStaticFloatMethodA)898 TEST_DECLARATION(CallStaticFloatMethodA) {
899     return help_CallFloatMethod(env, STATIC_ARRAY);
900 }
901 
TEST_DECLARATION(CallStaticFloatMethodV)902 TEST_DECLARATION(CallStaticFloatMethodV) {
903     return help_CallFloatMethod(env, STATIC_VA);
904 }
905 
help_CallDoubleMethod(JNIEnv * env,callType ct,...)906 static char *help_CallDoubleMethod(JNIEnv *env, callType ct, ...) {
907     va_list args;
908     va_start(args, ct);
909 
910     char *msg;
911     jobject o = getStandardInstance(env);
912     jmethodID method = findAppropriateMethod(env, &msg, ct,
913             "returnDouble", "()D");
914 
915     if (method == NULL) {
916         return msg;
917     }
918 
919     jdouble result;
920 
921     switch (ct) {
922         case VIRTUAL_PLAIN: {
923             result = CALL(CallDoubleMethod, o, method);
924             break;
925         }
926         case VIRTUAL_ARRAY: {
927             result = CALL(CallDoubleMethodA, o, method, NULL);
928             break;
929         }
930         case VIRTUAL_VA: {
931             result = CALL(CallDoubleMethodV, o, method, args);
932             break;
933         }
934         case DIRECT_PLAIN: {
935             result = CALL(CallNonvirtualDoubleMethod, o, InstanceFromNative,
936                     method);
937             break;
938         }
939         case DIRECT_ARRAY: {
940             result = CALL(CallNonvirtualDoubleMethodA, o, InstanceFromNative,
941                     method, NULL);
942             break;
943         }
944         case DIRECT_VA: {
945             result = CALL(CallNonvirtualDoubleMethodV, o, InstanceFromNative,
946                     method, args);
947             break;
948         }
949         case STATIC_PLAIN: {
950             result = CALL(CallStaticDoubleMethod, StaticFromNative, method);
951             break;
952         }
953         case STATIC_ARRAY: {
954             result = CALL(CallStaticDoubleMethodA, StaticFromNative, method,
955                     NULL);
956             break;
957         }
958         case STATIC_VA: {
959             result = CALL(CallStaticDoubleMethodV, StaticFromNative, method,
960                     args);
961             break;
962         }
963         default: {
964             return failure("shouldn't happen");
965         }
966     }
967 
968     va_end(args);
969 
970     return FAIL_IF_UNEQUAL("%g", 7.503e100, result);
971 }
972 
TEST_DECLARATION(CallDoubleMethod)973 TEST_DECLARATION(CallDoubleMethod) {
974     return help_CallDoubleMethod(env, VIRTUAL_PLAIN);
975 }
976 
TEST_DECLARATION(CallDoubleMethodA)977 TEST_DECLARATION(CallDoubleMethodA) {
978     return help_CallDoubleMethod(env, VIRTUAL_ARRAY);
979 }
980 
TEST_DECLARATION(CallDoubleMethodV)981 TEST_DECLARATION(CallDoubleMethodV) {
982     return help_CallDoubleMethod(env, VIRTUAL_VA);
983 }
984 
TEST_DECLARATION(CallNonvirtualDoubleMethod)985 TEST_DECLARATION(CallNonvirtualDoubleMethod) {
986     return help_CallDoubleMethod(env, DIRECT_PLAIN);
987 }
988 
TEST_DECLARATION(CallNonvirtualDoubleMethodA)989 TEST_DECLARATION(CallNonvirtualDoubleMethodA) {
990     return help_CallDoubleMethod(env, DIRECT_ARRAY);
991 }
992 
TEST_DECLARATION(CallNonvirtualDoubleMethodV)993 TEST_DECLARATION(CallNonvirtualDoubleMethodV) {
994     return help_CallDoubleMethod(env, DIRECT_VA);
995 }
996 
TEST_DECLARATION(CallStaticDoubleMethod)997 TEST_DECLARATION(CallStaticDoubleMethod) {
998     return help_CallDoubleMethod(env, STATIC_PLAIN);
999 }
1000 
TEST_DECLARATION(CallStaticDoubleMethodA)1001 TEST_DECLARATION(CallStaticDoubleMethodA) {
1002     return help_CallDoubleMethod(env, STATIC_ARRAY);
1003 }
1004 
TEST_DECLARATION(CallStaticDoubleMethodV)1005 TEST_DECLARATION(CallStaticDoubleMethodV) {
1006     return help_CallDoubleMethod(env, STATIC_VA);
1007 }
1008 
help_CallVoidMethod(JNIEnv * env,callType ct,...)1009 static char *help_CallVoidMethod(JNIEnv *env, callType ct, ...) {
1010     va_list args;
1011     va_start(args, ct);
1012 
1013     char *msg;
1014     jobject o = getStandardInstance(env);
1015     jmethodID method = findAppropriateMethod(env, &msg, ct,
1016             "nop", "()V");
1017 
1018     if (method == NULL) {
1019         return msg;
1020     }
1021 
1022     // For these, we simply consider "success" to be "didn't crash."
1023 
1024     switch (ct) {
1025         case VIRTUAL_PLAIN: {
1026             CALL(CallVoidMethod, o, method);
1027             break;
1028         }
1029         case VIRTUAL_ARRAY: {
1030             CALL(CallVoidMethodA, o, method, NULL);
1031             break;
1032         }
1033         case VIRTUAL_VA: {
1034             CALL(CallVoidMethodV, o, method, args);
1035             break;
1036         }
1037         case DIRECT_PLAIN: {
1038             CALL(CallNonvirtualVoidMethod, o, InstanceFromNative,
1039                     method);
1040             break;
1041         }
1042         case DIRECT_ARRAY: {
1043             CALL(CallNonvirtualVoidMethodA, o, InstanceFromNative,
1044                     method, NULL);
1045             break;
1046         }
1047         case DIRECT_VA: {
1048             CALL(CallNonvirtualVoidMethodV, o, InstanceFromNative,
1049                     method, args);
1050             break;
1051         }
1052         case STATIC_PLAIN: {
1053             CALL(CallStaticVoidMethod, StaticFromNative, method);
1054             break;
1055         }
1056         case STATIC_ARRAY: {
1057             CALL(CallStaticVoidMethodA, StaticFromNative, method,
1058                     NULL);
1059             break;
1060         }
1061         case STATIC_VA: {
1062             CALL(CallStaticVoidMethodV, StaticFromNative, method,
1063                     args);
1064             break;
1065         }
1066         default: {
1067             return failure("shouldn't happen");
1068         }
1069     }
1070 
1071     va_end(args);
1072 
1073     return NULL;
1074 }
1075 
TEST_DECLARATION(CallVoidMethod)1076 TEST_DECLARATION(CallVoidMethod) {
1077     return help_CallVoidMethod(env, VIRTUAL_PLAIN);
1078 }
1079 
TEST_DECLARATION(CallVoidMethodA)1080 TEST_DECLARATION(CallVoidMethodA) {
1081     return help_CallVoidMethod(env, VIRTUAL_ARRAY);
1082 }
1083 
TEST_DECLARATION(CallVoidMethodV)1084 TEST_DECLARATION(CallVoidMethodV) {
1085     return help_CallVoidMethod(env, VIRTUAL_VA);
1086 }
1087 
TEST_DECLARATION(CallNonvirtualVoidMethod)1088 TEST_DECLARATION(CallNonvirtualVoidMethod) {
1089     return help_CallVoidMethod(env, DIRECT_PLAIN);
1090 }
1091 
TEST_DECLARATION(CallNonvirtualVoidMethodA)1092 TEST_DECLARATION(CallNonvirtualVoidMethodA) {
1093     return help_CallVoidMethod(env, DIRECT_ARRAY);
1094 }
1095 
TEST_DECLARATION(CallNonvirtualVoidMethodV)1096 TEST_DECLARATION(CallNonvirtualVoidMethodV) {
1097     return help_CallVoidMethod(env, DIRECT_VA);
1098 }
1099 
TEST_DECLARATION(CallStaticVoidMethod)1100 TEST_DECLARATION(CallStaticVoidMethod) {
1101     return help_CallVoidMethod(env, STATIC_PLAIN);
1102 }
1103 
TEST_DECLARATION(CallStaticVoidMethodA)1104 TEST_DECLARATION(CallStaticVoidMethodA) {
1105     return help_CallVoidMethod(env, STATIC_ARRAY);
1106 }
1107 
TEST_DECLARATION(CallStaticVoidMethodV)1108 TEST_DECLARATION(CallStaticVoidMethodV) {
1109     return help_CallVoidMethod(env, STATIC_VA);
1110 }
1111 
help_CallObjectMethod(JNIEnv * env,callType ct,...)1112 static char *help_CallObjectMethod(JNIEnv *env, callType ct, ...) {
1113     va_list args;
1114     va_start(args, ct);
1115 
1116     char *msg;
1117     jobject o = getStandardInstance(env);
1118     jmethodID method = findAppropriateMethod(env, &msg, ct,
1119             "returnString", "()Ljava/lang/String;");
1120 
1121     if (method == NULL) {
1122         return msg;
1123     }
1124 
1125     jstring result;
1126 
1127     switch (ct) {
1128         case VIRTUAL_PLAIN: {
1129             result = (jstring) CALL(CallObjectMethod, o, method);
1130             break;
1131         }
1132         case VIRTUAL_ARRAY: {
1133             result = (jstring) CALL(CallObjectMethodA, o, method, NULL);
1134             break;
1135         }
1136         case VIRTUAL_VA: {
1137             result = (jstring) CALL(CallObjectMethodV, o, method, args);
1138             break;
1139         }
1140         case DIRECT_PLAIN: {
1141             result = (jstring)
1142                 CALL(CallNonvirtualObjectMethod, o, InstanceFromNative,
1143                         method);
1144             break;
1145         }
1146         case DIRECT_ARRAY: {
1147             result = (jstring)
1148                 CALL(CallNonvirtualObjectMethodA, o, InstanceFromNative,
1149                         method, NULL);
1150             break;
1151         }
1152         case DIRECT_VA: {
1153             result = (jstring)
1154                 CALL(CallNonvirtualObjectMethodV, o, InstanceFromNative,
1155                         method, args);
1156             break;
1157         }
1158         case STATIC_PLAIN: {
1159             result = (jstring)
1160                 CALL(CallStaticObjectMethod, StaticFromNative, method);
1161             break;
1162         }
1163         case STATIC_ARRAY: {
1164             result = (jstring)
1165                 CALL(CallStaticObjectMethodA, StaticFromNative, method, NULL);
1166             break;
1167         }
1168         case STATIC_VA: {
1169             result = (jstring)
1170                 CALL(CallStaticObjectMethodV, StaticFromNative, method, args);
1171             break;
1172         }
1173         default: {
1174             return failure("shouldn't happen");
1175         }
1176     }
1177 
1178     va_end(args);
1179 
1180     if (result == NULL) {
1181         return failure("got null from call");
1182     }
1183 
1184     const char *utf = CALL(GetStringUTFChars, result, NULL);
1185 
1186     if (strcmp(utf, "muffins") == 0) {
1187         msg = NULL;
1188     } else {
1189         msg = failure("unexpected string: %s", utf);
1190     }
1191 
1192     CALL(ReleaseStringUTFChars, result, utf);
1193 
1194     return msg;
1195 }
1196 
TEST_DECLARATION(CallObjectMethod)1197 TEST_DECLARATION(CallObjectMethod) {
1198     return help_CallObjectMethod(env, VIRTUAL_PLAIN);
1199 }
1200 
TEST_DECLARATION(CallObjectMethodA)1201 TEST_DECLARATION(CallObjectMethodA) {
1202     return help_CallObjectMethod(env, VIRTUAL_ARRAY);
1203 }
1204 
TEST_DECLARATION(CallObjectMethodV)1205 TEST_DECLARATION(CallObjectMethodV) {
1206     return help_CallObjectMethod(env, VIRTUAL_VA);
1207 }
1208 
TEST_DECLARATION(CallNonvirtualObjectMethod)1209 TEST_DECLARATION(CallNonvirtualObjectMethod) {
1210     return help_CallObjectMethod(env, DIRECT_PLAIN);
1211 }
1212 
TEST_DECLARATION(CallNonvirtualObjectMethodA)1213 TEST_DECLARATION(CallNonvirtualObjectMethodA) {
1214     return help_CallObjectMethod(env, DIRECT_ARRAY);
1215 }
1216 
TEST_DECLARATION(CallNonvirtualObjectMethodV)1217 TEST_DECLARATION(CallNonvirtualObjectMethodV) {
1218     return help_CallObjectMethod(env, DIRECT_VA);
1219 }
1220 
TEST_DECLARATION(CallStaticObjectMethod)1221 TEST_DECLARATION(CallStaticObjectMethod) {
1222     return help_CallObjectMethod(env, STATIC_PLAIN);
1223 }
1224 
TEST_DECLARATION(CallStaticObjectMethodA)1225 TEST_DECLARATION(CallStaticObjectMethodA) {
1226     return help_CallObjectMethod(env, STATIC_ARRAY);
1227 }
1228 
TEST_DECLARATION(CallStaticObjectMethodV)1229 TEST_DECLARATION(CallStaticObjectMethodV) {
1230     return help_CallObjectMethod(env, STATIC_VA);
1231 }
1232 
help_TakeOneOfEach(JNIEnv * env,callType ct,...)1233 static char *help_TakeOneOfEach(JNIEnv *env, callType ct, ...) {
1234     va_list args;
1235     va_start(args, ct);
1236 
1237     char *msg;
1238     jobject o = getStandardInstance(env);
1239     jmethodID method = findAppropriateMethod(env, &msg, ct,
1240             "takeOneOfEach", "(DFJICSBZLjava/lang/String;)Z");
1241 
1242     if (method == NULL) {
1243         return msg;
1244     }
1245 
1246     jvalue jargs[] = {
1247         {d: 0.0}, {f: 1.0f}, {j: 2LL}, {i: 3}, {c: 4}, {s: 5}, {b: 6},
1248         {z: true}, {l: biscuits}
1249     };
1250 
1251     jboolean result;
1252 
1253     switch (ct) {
1254         case VIRTUAL_PLAIN: {
1255             result = CALL(CallBooleanMethod, o, method,
1256                     0.0, 1.0f, 2LL, 3, (jchar) 4, (jshort) 5, (jbyte) 6,
1257                     (jboolean) true, biscuits);
1258             break;
1259         }
1260         case VIRTUAL_ARRAY: {
1261             result = CALL(CallBooleanMethodA, o, method, jargs);
1262             break;
1263         }
1264         case VIRTUAL_VA: {
1265             result = CALL(CallBooleanMethodV, o, method, args);
1266             break;
1267         }
1268         case DIRECT_PLAIN: {
1269             result = CALL(CallNonvirtualBooleanMethod, o, InstanceFromNative,
1270                     method,
1271                     0.0, 1.0f, 2LL, 3, (jchar) 4, (jshort) 5, (jbyte) 6,
1272                     (jboolean) true, biscuits);
1273             break;
1274         }
1275         case DIRECT_ARRAY: {
1276             result = CALL(CallNonvirtualBooleanMethodA, o, InstanceFromNative,
1277                     method, jargs);
1278             break;
1279         }
1280         case DIRECT_VA: {
1281             result = CALL(CallNonvirtualBooleanMethodV, o, InstanceFromNative,
1282                     method, args);
1283             break;
1284         }
1285         case STATIC_PLAIN: {
1286             result = CALL(CallStaticBooleanMethod, StaticFromNative, method,
1287                     0.0, 1.0f, 2LL, 3, (jchar) 4, (jshort) 5, (jbyte) 6,
1288                     (jboolean) true, biscuits);
1289             break;
1290         }
1291         case STATIC_ARRAY: {
1292             result = CALL(CallStaticBooleanMethodA, StaticFromNative, method,
1293                     jargs);
1294             break;
1295         }
1296         case STATIC_VA: {
1297             result = CALL(CallStaticBooleanMethodV, StaticFromNative, method,
1298                     args);
1299             break;
1300         }
1301         default: {
1302             return failure("shouldn't happen");
1303         }
1304     }
1305 
1306     va_end(args);
1307 
1308     return FAIL_IF_UNEQUAL("%d", true, result);
1309 }
1310 
TEST_DECLARATION(TakeOneOfEach)1311 TEST_DECLARATION(TakeOneOfEach) {
1312     return help_TakeOneOfEach(env, VIRTUAL_PLAIN);
1313 }
1314 
TEST_DECLARATION(TakeOneOfEachA)1315 TEST_DECLARATION(TakeOneOfEachA) {
1316     return help_TakeOneOfEach(env, VIRTUAL_ARRAY);
1317 }
1318 
TEST_DECLARATION(TakeOneOfEachV)1319 TEST_DECLARATION(TakeOneOfEachV) {
1320     return help_TakeOneOfEach(env, VIRTUAL_VA,
1321             0.0, 1.0f, 2LL, 3, (jchar) 4, (jshort) 5, (jbyte) 6,
1322             (jboolean) true, biscuits);
1323 }
1324 
TEST_DECLARATION(NonvirtualTakeOneOfEach)1325 TEST_DECLARATION(NonvirtualTakeOneOfEach) {
1326     return help_TakeOneOfEach(env, DIRECT_PLAIN);
1327 }
1328 
TEST_DECLARATION(NonvirtualTakeOneOfEachA)1329 TEST_DECLARATION(NonvirtualTakeOneOfEachA) {
1330     return help_TakeOneOfEach(env, DIRECT_ARRAY);
1331 }
1332 
TEST_DECLARATION(NonvirtualTakeOneOfEachV)1333 TEST_DECLARATION(NonvirtualTakeOneOfEachV) {
1334     return help_TakeOneOfEach(env, DIRECT_VA,
1335             0.0, 1.0f, 2LL, 3, (jchar) 4, (jshort) 5, (jbyte) 6,
1336             (jboolean) true, biscuits);
1337 }
1338 
TEST_DECLARATION(StaticTakeOneOfEach)1339 TEST_DECLARATION(StaticTakeOneOfEach) {
1340     return help_TakeOneOfEach(env, STATIC_PLAIN);
1341 }
1342 
TEST_DECLARATION(StaticTakeOneOfEachA)1343 TEST_DECLARATION(StaticTakeOneOfEachA) {
1344     return help_TakeOneOfEach(env, STATIC_ARRAY);
1345 }
1346 
TEST_DECLARATION(StaticTakeOneOfEachV)1347 TEST_DECLARATION(StaticTakeOneOfEachV) {
1348     return help_TakeOneOfEach(env, STATIC_VA,
1349             0.0, 1.0f, 2LL, 3, (jchar) 4, (jshort) 5, (jbyte) 6,
1350             (jboolean) true, biscuits);
1351 }
1352 
help_TakeCoolHandLuke(JNIEnv * env,callType ct,...)1353 static char *help_TakeCoolHandLuke(JNIEnv *env, callType ct, ...) {
1354     va_list args;
1355     va_start(args, ct);
1356 
1357     char *msg;
1358     jobject o = getStandardInstance(env);
1359     jmethodID method = findAppropriateMethod(env, &msg, ct,
1360             "takeCoolHandLuke",
1361             "(IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII)Z");
1362 
1363     if (method == NULL) {
1364         return msg;
1365     }
1366 
1367     jvalue jargs[] = {
1368         {i: 1}, {i: 2}, {i: 3}, {i: 4},
1369         {i: 5}, {i: 6}, {i: 7}, {i: 8}, {i: 9},
1370         {i: 10}, {i: 11}, {i: 12}, {i: 13}, {i: 14},
1371         {i: 15}, {i: 16}, {i: 17}, {i: 18}, {i: 19},
1372         {i: 20}, {i: 21}, {i: 22}, {i: 23}, {i: 24},
1373         {i: 25}, {i: 26}, {i: 27}, {i: 28}, {i: 29},
1374         {i: 30}, {i: 31}, {i: 32}, {i: 33}, {i: 34},
1375         {i: 35}, {i: 36}, {i: 37}, {i: 38}, {i: 39},
1376         {i: 40}, {i: 41}, {i: 42}, {i: 43}, {i: 44},
1377         {i: 45}, {i: 46}, {i: 47}, {i: 48}, {i: 49},
1378         {i: 50}
1379     };
1380 
1381     jboolean result;
1382 
1383     switch (ct) {
1384         case VIRTUAL_PLAIN: {
1385             result = CALL(CallBooleanMethod, o, method,
1386                     1, 2, 3, 4, 5, 6, 7, 8, 9,
1387                     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1388                     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1389                     30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1390                     40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1391                     50);
1392             break;
1393         }
1394         case VIRTUAL_ARRAY: {
1395             result = CALL(CallBooleanMethodA, o, method, jargs);
1396             break;
1397         }
1398         case VIRTUAL_VA: {
1399             result = CALL(CallBooleanMethodV, o, method, args);
1400             break;
1401         }
1402         case DIRECT_PLAIN: {
1403             result = CALL(CallNonvirtualBooleanMethod, o, InstanceFromNative,
1404                     method,
1405                     1, 2, 3, 4, 5, 6, 7, 8, 9,
1406                     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1407                     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1408                     30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1409                     40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1410                     50);
1411             break;
1412         }
1413         case DIRECT_ARRAY: {
1414             result = CALL(CallNonvirtualBooleanMethodA, o, InstanceFromNative,
1415                     method, jargs);
1416             break;
1417         }
1418         case DIRECT_VA: {
1419             result = CALL(CallNonvirtualBooleanMethodV, o, InstanceFromNative,
1420                     method, args);
1421             break;
1422         }
1423         case STATIC_PLAIN: {
1424             result = CALL(CallStaticBooleanMethod, StaticFromNative, method,
1425                     1, 2, 3, 4, 5, 6, 7, 8, 9,
1426                     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1427                     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1428                     30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1429                     40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1430                     50);
1431             break;
1432         }
1433         case STATIC_ARRAY: {
1434             result = CALL(CallStaticBooleanMethodA, StaticFromNative, method,
1435                     jargs);
1436             break;
1437         }
1438         case STATIC_VA: {
1439             result = CALL(CallStaticBooleanMethodV, StaticFromNative, method,
1440                     args);
1441             break;
1442         }
1443         default: {
1444             return failure("shouldn't happen");
1445         }
1446     }
1447 
1448     va_end(args);
1449 
1450     return FAIL_IF_UNEQUAL("%d", true, result);
1451 }
1452 
TEST_DECLARATION(TakeCoolHandLuke)1453 TEST_DECLARATION(TakeCoolHandLuke) {
1454     return help_TakeCoolHandLuke(env, VIRTUAL_PLAIN);
1455 }
1456 
TEST_DECLARATION(TakeCoolHandLukeA)1457 TEST_DECLARATION(TakeCoolHandLukeA) {
1458     return help_TakeCoolHandLuke(env, VIRTUAL_ARRAY);
1459 }
1460 
TEST_DECLARATION(TakeCoolHandLukeV)1461 TEST_DECLARATION(TakeCoolHandLukeV) {
1462     return help_TakeCoolHandLuke(env, VIRTUAL_VA,
1463             1, 2, 3, 4, 5, 6, 7, 8, 9,
1464             10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1465             20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1466             30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1467             40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1468             50);
1469 }
1470 
TEST_DECLARATION(NonvirtualTakeCoolHandLuke)1471 TEST_DECLARATION(NonvirtualTakeCoolHandLuke) {
1472     return help_TakeCoolHandLuke(env, DIRECT_PLAIN);
1473 }
1474 
TEST_DECLARATION(NonvirtualTakeCoolHandLukeA)1475 TEST_DECLARATION(NonvirtualTakeCoolHandLukeA) {
1476     return help_TakeCoolHandLuke(env, DIRECT_ARRAY);
1477 }
1478 
TEST_DECLARATION(NonvirtualTakeCoolHandLukeV)1479 TEST_DECLARATION(NonvirtualTakeCoolHandLukeV) {
1480     return help_TakeCoolHandLuke(env, DIRECT_VA,
1481             1, 2, 3, 4, 5, 6, 7, 8, 9,
1482             10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1483             20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1484             30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1485             40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1486             50);
1487 }
1488 
TEST_DECLARATION(StaticTakeCoolHandLuke)1489 TEST_DECLARATION(StaticTakeCoolHandLuke) {
1490     return help_TakeCoolHandLuke(env, STATIC_PLAIN);
1491 }
1492 
TEST_DECLARATION(StaticTakeCoolHandLukeA)1493 TEST_DECLARATION(StaticTakeCoolHandLukeA) {
1494     return help_TakeCoolHandLuke(env, STATIC_ARRAY);
1495 }
1496 
TEST_DECLARATION(StaticTakeCoolHandLukeV)1497 TEST_DECLARATION(StaticTakeCoolHandLukeV) {
1498     return help_TakeCoolHandLuke(env, STATIC_VA,
1499             1, 2, 3, 4, 5, 6, 7, 8, 9,
1500             10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1501             20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1502             30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1503             40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1504             50);
1505 }
1506 
TEST_DECLARATION(DefineClass)1507 TEST_DECLARATION(DefineClass) {
1508     // Android implementations should always return NULL.
1509     jclass clazz = CALL(DefineClass, "foo", NULL, NULL, 0);
1510 
1511     if (clazz != NULL) {
1512         return failure("Expected NULL but got %p", clazz);
1513     }
1514 
1515     return NULL;
1516 }
1517 
1518 // TODO: Missing functions:
1519 //   DeleteLocalRef
1520 //   DeleteWeakGlobalRef
1521 //   EnsureLocalCapacity
1522 //   ExceptionCheck
1523 //   ExceptionClear
1524 //   ExceptionDescribe
1525 //   ExceptionOccurred
1526 //   FatalError (Note: impossible to test in this framework)
1527 //   FindClass
1528 //   FromReflectedField
1529 //   FromReflectedMethod
1530 //   GetArrayLength
1531 //   GetBooleanArrayElements
1532 //   GetBooleanArrayRegion
1533 //   GetBooleanField
1534 //   GetByteArrayElements
1535 //   GetByteArrayRegion
1536 //   GetByteField
1537 //   GetCharArrayElements
1538 //   GetCharArrayRegion
1539 //   GetCharField
1540 //   GetDirectBufferAddress
1541 //   GetDirectBufferCapacity
1542 //   GetDoubleArrayElements
1543 //   GetDoubleArrayRegion
1544 //   GetDoubleField
1545 //   GetFieldID
1546 //   GetFloatArrayElements
1547 //   GetFloatArrayRegion
1548 //   GetFloatField
1549 //   GetIntArrayElements
1550 //   GetIntArrayRegion
1551 //   GetIntField
1552 //   GetJavaVM
1553 //   GetLongArrayElements
1554 //   GetLongArrayRegion
1555 //   GetLongField
1556 //   GetMethodID
1557 //   GetObjectArrayElement
1558 //   GetObjectClass
1559 //   GetObjectField
1560 //   GetObjectRefType (since 1.6)
1561 //   GetPrimitiveArrayCritical
1562 //   GetShortArrayElements
1563 //   GetShortArrayRegion
1564 //   GetShortField
1565 //   GetStaticBooleanField
1566 //   GetStaticByteField
1567 //   GetStaticCharField
1568 //   GetStaticDoubleField
1569 //   GetStaticFieldID
1570 //   GetStaticFloatField
1571 //   GetStaticIntField
1572 //   GetStaticLongField
1573 //   GetStaticMethodID
1574 //   GetStaticObjectField
1575 //   GetStaticShortField
1576 //   GetStringChars
1577 //   GetStringCritical
1578 //   GetStringLength
1579 //   GetStringRegion
1580 //   GetStringUTFChars
1581 //   GetStringUTFLength
1582 //   GetStringUTFRegion
1583 //   GetSuperclass
1584 
TEST_DECLARATION(GetVersion)1585 TEST_DECLARATION(GetVersion) {
1586     // Android implementations should all be at version 1.6.
1587     jint version = CALL(GetVersion);
1588 
1589     if (version != JNI_VERSION_1_6) {
1590         return failure("Expected JNI_VERSION_1_6 but got 0x%x", version);
1591     }
1592 
1593     return NULL;
1594 }
1595 
1596 // TODO: Missing functions:
1597 //   IsAssignableFrom
1598 //   IsInstanceOf
1599 //   IsSameObject
1600 //   MonitorEnter
1601 //   MonitorExit
1602 //   NewBooleanArray
1603 //   NewByteArray
1604 //   NewCharArray
1605 //   NewDirectByteBuffer
1606 //   NewDoubleArray
1607 //   NewFloatArray
1608 //   NewGlobalRef
1609 //   NewIntArray
1610 //   NewLocalRef
1611 //   NewLongArray
1612 //   NewObject
1613 //   NewObjectA
1614 //   NewObjectArray
1615 //   NewObjectV
1616 //   NewShortArray
1617 //   NewString
1618 //   NewStringUTF
1619 //   NewWeakGlobalRef
1620 //   PopLocalFrame
1621 //   PushLocalFrame
1622 //   RegisterNatives
1623 //   ReleaseBooleanArrayElements
1624 //   ReleaseByteArrayElements
1625 //   ReleaseCharArrayElements
1626 //   ReleaseDoubleArrayElements
1627 //   ReleaseFloatArrayElements
1628 //   ReleaseIntArrayElements
1629 //   ReleaseLongArrayElements
1630 //   ReleasePrimitiveArrayCritical
1631 //   ReleaseShortArrayElements
1632 //   ReleaseStringChars
1633 //   ReleaseStringCritical
1634 //   ReleaseStringUTFChars
1635 //   SetBooleanArrayRegion
1636 //   SetBooleanField
1637 //   SetByteArrayRegion
1638 //   SetByteField
1639 //   SetCharArrayRegion
1640 //   SetCharField
1641 //   SetDoubleArrayRegion
1642 //   SetDoubleField
1643 //   SetFloatArrayRegion
1644 //   SetFloatField
1645 //   SetIntArrayRegion
1646 //   SetIntField
1647 //   SetLongArrayRegion
1648 //   SetLongField
1649 //   SetObjectArrayElement
1650 //   SetObjectField
1651 //   SetShortArrayRegion
1652 //   SetShortField
1653 //   SetStaticBooleanField
1654 //   SetStaticByteField
1655 //   SetStaticCharField
1656 //   SetStaticDoubleField
1657 //   SetStaticFloatField
1658 //   SetStaticIntField
1659 //   SetStaticLongField
1660 //   SetStaticObjectField
1661 //   SetStaticShortField
1662 //   Throw
1663 //   ThrowNew
1664 //   ToReflectedField
1665 //   ToReflectedMethod
1666 //   UnregisterNatives
1667 
1668 
1669 
1670 /**
1671  * Runs all the tests, returning NULL if they all succeeded, or
1672  * a string listing information about all the failures.
1673  */
runAllTests(JNIEnv * env)1674 static jstring runAllTests(JNIEnv *env) {
1675     char *result = initializeVariables(env);
1676 
1677     if (CALL(ExceptionOccurred)) {
1678         CALL(ExceptionDescribe);
1679         CALL(ExceptionClear);
1680     }
1681 
1682     if (result == NULL) {
1683         result = runJniTests(env,
1684                 RUN_TEST(CallBooleanMethod),
1685                 RUN_TEST(CallBooleanMethodA),
1686                 RUN_TEST(CallBooleanMethodV),
1687                 RUN_TEST(CallNonvirtualBooleanMethod),
1688                 RUN_TEST(CallNonvirtualBooleanMethodA),
1689                 RUN_TEST(CallNonvirtualBooleanMethodV),
1690                 RUN_TEST(CallStaticBooleanMethod),
1691                 RUN_TEST(CallStaticBooleanMethodA),
1692                 RUN_TEST(CallStaticBooleanMethodV),
1693 
1694                 RUN_TEST(CallByteMethod),
1695                 RUN_TEST(CallByteMethodA),
1696                 RUN_TEST(CallByteMethodV),
1697                 RUN_TEST(CallNonvirtualByteMethod),
1698                 RUN_TEST(CallNonvirtualByteMethodA),
1699                 RUN_TEST(CallNonvirtualByteMethodV),
1700                 RUN_TEST(CallStaticByteMethod),
1701                 RUN_TEST(CallStaticByteMethodA),
1702                 RUN_TEST(CallStaticByteMethodV),
1703 
1704                 RUN_TEST(CallShortMethod),
1705                 RUN_TEST(CallShortMethodA),
1706                 RUN_TEST(CallShortMethodV),
1707                 RUN_TEST(CallNonvirtualShortMethod),
1708                 RUN_TEST(CallNonvirtualShortMethodA),
1709                 RUN_TEST(CallNonvirtualShortMethodV),
1710                 RUN_TEST(CallStaticShortMethod),
1711                 RUN_TEST(CallStaticShortMethodA),
1712                 RUN_TEST(CallStaticShortMethodV),
1713 
1714                 RUN_TEST(CallCharMethod),
1715                 RUN_TEST(CallCharMethodA),
1716                 RUN_TEST(CallCharMethodV),
1717                 RUN_TEST(CallNonvirtualCharMethod),
1718                 RUN_TEST(CallNonvirtualCharMethodA),
1719                 RUN_TEST(CallNonvirtualCharMethodV),
1720                 RUN_TEST(CallStaticCharMethod),
1721                 RUN_TEST(CallStaticCharMethodA),
1722                 RUN_TEST(CallStaticCharMethodV),
1723 
1724                 RUN_TEST(CallIntMethod),
1725                 RUN_TEST(CallIntMethodA),
1726                 RUN_TEST(CallIntMethodV),
1727                 RUN_TEST(CallNonvirtualIntMethod),
1728                 RUN_TEST(CallNonvirtualIntMethodA),
1729                 RUN_TEST(CallNonvirtualIntMethodV),
1730                 RUN_TEST(CallStaticIntMethod),
1731                 RUN_TEST(CallStaticIntMethodA),
1732                 RUN_TEST(CallStaticIntMethodV),
1733 
1734                 RUN_TEST(CallLongMethod),
1735                 RUN_TEST(CallLongMethodA),
1736                 RUN_TEST(CallLongMethodV),
1737                 RUN_TEST(CallNonvirtualLongMethod),
1738                 RUN_TEST(CallNonvirtualLongMethodA),
1739                 RUN_TEST(CallNonvirtualLongMethodV),
1740                 RUN_TEST(CallStaticLongMethod),
1741                 RUN_TEST(CallStaticLongMethodA),
1742                 RUN_TEST(CallStaticLongMethodV),
1743 
1744                 RUN_TEST(CallFloatMethod),
1745                 RUN_TEST(CallFloatMethodA),
1746                 RUN_TEST(CallFloatMethodV),
1747                 RUN_TEST(CallNonvirtualFloatMethod),
1748                 RUN_TEST(CallNonvirtualFloatMethodA),
1749                 RUN_TEST(CallNonvirtualFloatMethodV),
1750                 RUN_TEST(CallStaticFloatMethod),
1751                 RUN_TEST(CallStaticFloatMethodA),
1752                 RUN_TEST(CallStaticFloatMethodV),
1753 
1754                 RUN_TEST(CallDoubleMethod),
1755                 RUN_TEST(CallDoubleMethodA),
1756                 RUN_TEST(CallDoubleMethodV),
1757                 RUN_TEST(CallNonvirtualDoubleMethod),
1758                 RUN_TEST(CallNonvirtualDoubleMethodA),
1759                 RUN_TEST(CallNonvirtualDoubleMethodV),
1760                 RUN_TEST(CallStaticDoubleMethod),
1761                 RUN_TEST(CallStaticDoubleMethodA),
1762                 RUN_TEST(CallStaticDoubleMethodV),
1763 
1764                 RUN_TEST(CallVoidMethod),
1765                 RUN_TEST(CallVoidMethodA),
1766                 RUN_TEST(CallVoidMethodV),
1767                 RUN_TEST(CallNonvirtualVoidMethod),
1768                 RUN_TEST(CallNonvirtualVoidMethodA),
1769                 RUN_TEST(CallNonvirtualVoidMethodV),
1770                 RUN_TEST(CallStaticVoidMethod),
1771                 RUN_TEST(CallStaticVoidMethodA),
1772                 RUN_TEST(CallStaticVoidMethodV),
1773 
1774                 RUN_TEST(CallObjectMethod),
1775                 RUN_TEST(CallObjectMethodA),
1776                 RUN_TEST(CallObjectMethodV),
1777                 RUN_TEST(CallNonvirtualObjectMethod),
1778                 RUN_TEST(CallNonvirtualObjectMethodA),
1779                 RUN_TEST(CallNonvirtualObjectMethodV),
1780                 RUN_TEST(CallStaticObjectMethod),
1781                 RUN_TEST(CallStaticObjectMethodA),
1782                 RUN_TEST(CallStaticObjectMethodV),
1783 
1784                 RUN_TEST(TakeOneOfEach),
1785                 RUN_TEST(TakeOneOfEachA),
1786                 RUN_TEST(TakeOneOfEachV),
1787                 RUN_TEST(NonvirtualTakeOneOfEach),
1788                 RUN_TEST(NonvirtualTakeOneOfEachA),
1789                 RUN_TEST(NonvirtualTakeOneOfEachV),
1790                 RUN_TEST(StaticTakeOneOfEach),
1791                 RUN_TEST(StaticTakeOneOfEachA),
1792                 RUN_TEST(StaticTakeOneOfEachV),
1793 
1794                 RUN_TEST(TakeCoolHandLuke),
1795                 RUN_TEST(TakeCoolHandLukeA),
1796                 RUN_TEST(TakeCoolHandLukeV),
1797                 RUN_TEST(NonvirtualTakeCoolHandLuke),
1798                 RUN_TEST(NonvirtualTakeCoolHandLukeA),
1799                 RUN_TEST(NonvirtualTakeCoolHandLukeV),
1800                 RUN_TEST(StaticTakeCoolHandLuke),
1801                 RUN_TEST(StaticTakeCoolHandLukeA),
1802                 RUN_TEST(StaticTakeCoolHandLukeV),
1803 
1804                 RUN_TEST(DefineClass),
1805                 RUN_TEST(GetVersion),
1806                 NULL);
1807 
1808         // TODO: Add more tests, above.
1809     }
1810 
1811     if (result != NULL) {
1812         jstring s = CALL(NewStringUTF, result);
1813         free(result);
1814         return s;
1815     }
1816 
1817     return NULL;
1818 }
1819