Home | History | Annotate | Download | only in system
      1 /*
      2  * Copyright (C) 2011 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 package dalvik.system;
     18 
     19 import junit.framework.TestCase;
     20 
     21 /**
     22  * Test JNI behavior
     23  */
     24 public final class JniTest extends TestCase {
     25 
     26     static {
     27         System.loadLibrary("javacoretests");
     28     }
     29 
     30     /** @return this argument of method */
     31     private native JniTest returnThis();
     32     /** @return class argument of method */
     33     private static native Class<JniTest> returnClass();
     34 
     35     private native Object returnObjectArgFrom16(int arg_no,
     36                                                 Object o1,  Object o2,  Object o3,  Object o4,  Object o5,
     37                                                 Object o6,  Object o7,  Object o8,  Object o9,  Object o10,
     38                                                 Object o11, Object o12, Object o13, Object o14, Object o15,
     39                                                 Object o16);
     40     private native boolean returnBooleanArgFrom16(int arg_no,
     41                                                   boolean o1,  boolean o2,  boolean o3,  boolean o4,  boolean o5,
     42                                                   boolean o6,  boolean o7,  boolean o8,  boolean o9,  boolean o10,
     43                                                   boolean o11, boolean o12, boolean o13, boolean o14, boolean o15,
     44                                                   boolean o16);
     45     private native char returnCharArgFrom16(int arg_no,
     46                                             char o1,  char o2,  char o3,  char o4,  char o5,
     47                                             char o6,  char o7,  char o8,  char o9,  char o10,
     48                                             char o11, char o12, char o13, char o14, char o15,
     49                                             char o16);
     50     private native byte returnByteArgFrom16(int arg_no,
     51                                             byte o1,  byte o2,  byte o3,  byte o4,  byte o5,
     52                                             byte o6,  byte o7,  byte o8,  byte o9,  byte o10,
     53                                             byte o11, byte o12, byte o13, byte o14, byte o15,
     54                                             byte o16);
     55     private native short returnShortArgFrom16(int arg_no,
     56                                               short o1,  short o2,  short o3,  short o4,  short o5,
     57                                               short o6,  short o7,  short o8,  short o9,  short o10,
     58                                               short o11, short o12, short o13, short o14, short o15,
     59                                               short o16);
     60     private native int returnIntArgFrom16(int arg_no,
     61                                           int o1,  int o2,  int o3,  int o4,  int o5,
     62                                           int o6,  int o7,  int o8,  int o9,  int o10,
     63                                           int o11, int o12, int o13, int o14, int o15,
     64                                           int o16);
     65     private native long returnLongArgFrom16(int arg_no,
     66                                             long o1,  long o2,  long o3,  long o4,  long o5,
     67                                             long o6,  long o7,  long o8,  long o9,  long o10,
     68                                             long o11, long o12, long o13, long o14, long o15,
     69                                             long o16);
     70     private native float returnFloatArgFrom16(int arg_no,
     71                                               float o1,  float o2,  float o3,  float o4,  float o5,
     72                                               float o6,  float o7,  float o8,  float o9,  float o10,
     73                                               float o11, float o12, float o13, float o14, float o15,
     74                                               float o16);
     75     private native double returnDoubleArgFrom16(int arg_no,
     76                                                 double o1,  double o2,  double o3,  double o4,  double o5,
     77                                                 double o6,  double o7,  double o8,  double o9,  double o10,
     78                                                 double o11, double o12, double o13, double o14, double o15,
     79                                                 double o16);
     80 
     81     /** Test cases for implicit this argument */
     82     public void testPassingThis() {
     83         assertEquals(this, returnThis());
     84     }
     85 
     86     /** Test cases for implicit class argument */
     87     public void testPassingClass() {
     88         assertEquals(JniTest.class, returnClass());
     89     }
     90 
     91     /** Test passing object references as arguments to a native method */
     92     public void testPassingObjectReferences() {
     93         final Object[] literals = {"Bradshaw", "Isherwood", "Oldknow", "Mallet",
     94                                    JniTest.class, null, Integer.valueOf(0)};
     95         final Object[] a  = new Object[16];
     96         // test selection from a list of object literals where the literals are all the same
     97         for(Object literal : literals) {
     98             for(int i = 0; i < 16; i++) {
     99                 a[i] = literal;
    100             }
    101             for(int i = 0; i < 16; i++) {
    102                 assertEquals(a[i], returnObjectArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    103                                                          a[5], a[6], a[7], a[8], a[9], a[10],
    104                                                          a[11], a[12], a[13], a[14], a[15]));
    105             }
    106         }
    107         // test selection from a list of object literals where the literals are shuffled
    108         for(int j = 0; j < literals.length; j++) {
    109             for(int i = 0; i < 16; i++) {
    110                 a[i] = literals[(i + j) % literals.length];
    111             }
    112             for(int i = 0; i < 16; i++) {
    113                 assertEquals(a[i], returnObjectArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    114                                                          a[5], a[6], a[7], a[8], a[9], a[10],
    115                                                          a[11], a[12], a[13], a[14], a[15]));
    116             }
    117         }
    118     }
    119 
    120     /** Test passing booleans as arguments to a native method */
    121     public void testPassingBooleans() {
    122         final boolean[] literals = {true, false, false, true};
    123         final boolean[] a  = new boolean[16];
    124         // test selection from a list of object literals where the literals are all the same
    125         for(boolean literal : literals) {
    126             for(int i = 0; i < 16; i++) {
    127                 a[i] = literal;
    128             }
    129             for(int i = 0; i < 16; i++) {
    130                 assertEquals(a[i], returnBooleanArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    131                                                           a[5], a[6], a[7], a[8], a[9], a[10],
    132                                                           a[11], a[12], a[13], a[14], a[15]));
    133             }
    134         }
    135         // test selection from a list of object literals where the literals are shuffled
    136         for(int j = 0; j < literals.length; j++) {
    137             for(int i = 0; i < 16; i++) {
    138                 a[i] = literals[(i + j) % literals.length];
    139             }
    140             for(int i = 0; i < 16; i++) {
    141                 assertEquals(a[i], returnBooleanArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    142                                                           a[5], a[6], a[7], a[8], a[9], a[10],
    143                                                           a[11], a[12], a[13], a[14], a[15]));
    144             }
    145         }
    146     }
    147 
    148     /** Test passing characters as arguments to a native method */
    149     public void testPassingChars() {
    150         final char[] literals = {Character.MAX_VALUE, Character.MIN_VALUE,
    151                                  Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE,
    152                                  Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE,
    153                                  'a', 'z', 'A', 'Z', '0', '9'};
    154         final char[] a  = new char[16];
    155         // test selection from a list of object literals where the literals are all the same
    156         for(char literal : literals) {
    157             for(int i = 0; i < 16; i++) {
    158                 a[i] = literal;
    159             }
    160             for(int i = 0; i < 16; i++) {
    161                 assertEquals(a[i], returnCharArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    162                                                        a[5], a[6], a[7], a[8], a[9], a[10],
    163                                                        a[11], a[12], a[13], a[14], a[15]));
    164             }
    165         }
    166         // test selection from a list of object literals where the literals are shuffled
    167         for(int j = 0; j < literals.length; j++) {
    168             for(int i = 0; i < 16; i++) {
    169                 a[i] = literals[(i + j) % literals.length];
    170             }
    171             for(int i = 0; i < 16; i++) {
    172                 assertEquals(a[i], returnCharArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    173                                                        a[5], a[6], a[7], a[8], a[9], a[10],
    174                                                        a[11], a[12], a[13], a[14], a[15]));
    175             }
    176         }
    177     }
    178 
    179     /** Test passing bytes as arguments to a native method */
    180     public void testPassingBytes() {
    181         final byte[] literals = {Byte.MAX_VALUE, Byte.MIN_VALUE, 0, -1};
    182         final byte[] a  = new byte[16];
    183         // test selection from a list of object literals where the literals are all the same
    184         for(byte literal : literals) {
    185             for(int i = 0; i < 16; i++) {
    186                 a[i] = literal;
    187             }
    188             for(int i = 0; i < 16; i++) {
    189                 assertEquals(a[i], returnByteArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    190                                                        a[5], a[6], a[7], a[8], a[9], a[10],
    191                                                        a[11], a[12], a[13], a[14], a[15]));
    192             }
    193         }
    194         // test selection from a list of object literals where the literals are shuffled
    195         for(int j = 0; j < literals.length; j++) {
    196             for(int i = 0; i < 16; i++) {
    197                 a[i] = literals[(i + j) % literals.length];
    198             }
    199             for(int i = 0; i < 16; i++) {
    200                 assertEquals(a[i], returnByteArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    201                                                        a[5], a[6], a[7], a[8], a[9], a[10],
    202                                                        a[11], a[12], a[13], a[14], a[15]));
    203             }
    204         }
    205     }
    206 
    207     /** Test passing shorts as arguments to a native method */
    208     public void testPassingShorts() {
    209         final short[] literals = {Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE, 0, -1};
    210         final short[] a  = new short[16];
    211         // test selection from a list of object literals where the literals are all the same
    212         for(short literal : literals) {
    213             for(int i = 0; i < 16; i++) {
    214                 a[i] = literal;
    215             }
    216             for(int i = 0; i < 16; i++) {
    217                 assertEquals(a[i], returnShortArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    218                                                         a[5], a[6], a[7], a[8], a[9], a[10],
    219                                                         a[11], a[12], a[13], a[14], a[15]));
    220             }
    221         }
    222         // test selection from a list of object literals where the literals are shuffled
    223         for(int j = 0; j < literals.length; j++) {
    224             for(int i = 0; i < 16; i++) {
    225                 a[i] = literals[(i + j) % literals.length];
    226             }
    227             for(int i = 0; i < 16; i++) {
    228                 assertEquals(a[i], returnShortArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    229                                                         a[5], a[6], a[7], a[8], a[9], a[10],
    230                                                         a[11], a[12], a[13], a[14], a[15]));
    231             }
    232         }
    233     }
    234 
    235     /** Test passing ints as arguments to a native method */
    236     public void testPassingInts() {
    237         final int[] literals = {Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE,
    238                                 Integer.MAX_VALUE, Integer.MIN_VALUE, 0, -1};
    239         final int[] a  = new int[16];
    240         // test selection from a list of object literals where the literals are all the same
    241         for(int literal : literals) {
    242             for(int i = 0; i < 16; i++) {
    243                 a[i] = literal;
    244             }
    245             for(int i = 0; i < 16; i++) {
    246                 assertEquals(a[i], returnIntArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    247                                                       a[5], a[6], a[7], a[8], a[9], a[10],
    248                                                       a[11], a[12], a[13], a[14], a[15]));
    249             }
    250         }
    251         // test selection from a list of object literals where the literals are shuffled
    252         for(int j = 0; j < literals.length; j++) {
    253             for(int i = 0; i < 16; i++) {
    254                 a[i] = literals[(i + j) % literals.length];
    255             }
    256             for(int i = 0; i < 16; i++) {
    257                 assertEquals(a[i], returnIntArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    258                                                       a[5], a[6], a[7], a[8], a[9], a[10],
    259                                                       a[11], a[12], a[13], a[14], a[15]));
    260             }
    261         }
    262     }
    263 
    264     /** Test passing longs as arguments to a native method */
    265     public void testPassingLongs() {
    266         final long[] literals = {Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE,
    267                                  Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE, 0, -1};
    268         final long[] a  = new long[16];
    269         // test selection from a list of object literals where the literals are all the same
    270         for(long literal : literals) {
    271             for(int i = 0; i < 16; i++) {
    272                 a[i] = literal;
    273             }
    274             for(int i = 0; i < 16; i++) {
    275                 assertEquals(a[i], returnLongArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    276                                                        a[5], a[6], a[7], a[8], a[9], a[10],
    277                                                        a[11], a[12], a[13], a[14], a[15]));
    278             }
    279         }
    280         // test selection from a list of object literals where the literals are shuffled
    281         for(int j = 0; j < literals.length; j++) {
    282             for(int i = 0; i < 16; i++) {
    283                 a[i] = literals[(i + j) % literals.length];
    284             }
    285             for(int i = 0; i < 16; i++) {
    286                 assertEquals(a[i], returnLongArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    287                                                        a[5], a[6], a[7], a[8], a[9], a[10],
    288                                                        a[11], a[12], a[13], a[14], a[15]));
    289             }
    290         }
    291     }
    292 
    293     /** Test passing floats as arguments to a native method */
    294     public void testPassingFloats() {
    295         final float[] literals = {Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE,
    296                                   Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE,
    297                                   Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_NORMAL, Float.NaN,
    298                                   Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, (float)Math.E, (float)Math.PI, 0, -1};
    299         final float[] a  = new float[16];
    300         // test selection from a list of object literals where the literals are all the same
    301         for(float literal : literals) {
    302             for(int i = 0; i < 16; i++) {
    303                 a[i] = literal;
    304             }
    305             for(int i = 0; i < 16; i++) {
    306                 assertEquals(a[i], returnFloatArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    307                                                         a[5], a[6], a[7], a[8], a[9], a[10],
    308                                                         a[11], a[12], a[13], a[14], a[15]));
    309             }
    310         }
    311         // test selection from a list of object literals where the literals are shuffled
    312         for(int j = 0; j < literals.length; j++) {
    313             for(int i = 0; i < 16; i++) {
    314                 a[i] = literals[(i + j) % literals.length];
    315             }
    316             for(int i = 0; i < 16; i++) {
    317                 assertEquals(a[i], returnFloatArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    318                                                         a[5], a[6], a[7], a[8], a[9], a[10],
    319                                                         a[11], a[12], a[13], a[14], a[15]));
    320             }
    321         }
    322     }
    323 
    324     /** Test passing doubles as arguments to a native method */
    325     public void testPassingDoubles() {
    326         final double[] literals = {Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE,
    327                                    Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE,
    328                                    Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_NORMAL, Float.NaN,
    329                                    Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY,
    330                                    Double.MAX_VALUE, Double.MIN_VALUE, Double.MIN_NORMAL, Double.NaN,
    331                                    Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
    332                                    Math.E, Math.PI, 0, -1};
    333         final double[] a  = new double[16];
    334         // test selection from a list of object literals where the literals are all the same
    335         for(double literal : literals) {
    336             for(int i = 0; i < 16; i++) {
    337                 a[i] = literal;
    338             }
    339             for(int i = 0; i < 16; i++) {
    340                 assertEquals(a[i], returnDoubleArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    341                                                          a[5], a[6], a[7], a[8], a[9], a[10],
    342                                                          a[11], a[12], a[13], a[14], a[15]));
    343             }
    344         }
    345         // test selection from a list of object literals where the literals are shuffled
    346         for(int j = 0; j < literals.length; j++) {
    347             for(int i = 0; i < 16; i++) {
    348                 a[i] = literals[(i + j) % literals.length];
    349             }
    350             for(int i = 0; i < 16; i++) {
    351                 assertEquals(a[i], returnDoubleArgFrom16(i, a[0], a[1], a[2], a[3], a[4],
    352                                                          a[5], a[6], a[7], a[8], a[9], a[10],
    353                                                          a[11], a[12], a[13], a[14], a[15]));
    354             }
    355         }
    356     }
    357 
    358     private static native Class<?> envGetSuperclass(Class<?> clazz);
    359 
    360     public void testGetSuperclass() {
    361         assertEquals(Object.class, envGetSuperclass(String.class));
    362         assertEquals(null, envGetSuperclass(Object.class));
    363         assertEquals(null, envGetSuperclass(int.class));
    364         assertEquals(null, envGetSuperclass(Runnable.class));
    365     }
    366 }
    367