PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/java/java_lib_arrays_runme.java

#
Java | 172 lines | 133 code | 31 blank | 8 comment | 33 complexity | efdf64ca658dac41e49fe02350ad33a9 MD5 | raw file
  1
  2// This is the java_lib_arrays runtime testcase. It ensures that a getter and a setter has 
  3// been produced for array members and that they function as expected. It is a
  4// pretty comprehensive test for all the Java array library typemaps.
  5
  6import java_lib_arrays.*;
  7
  8public class java_lib_arrays_runme {
  9
 10  static {
 11    try {
 12	System.loadLibrary("java_lib_arrays");
 13    } catch (UnsatisfiedLinkError e) {
 14      System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
 15      System.exit(1);
 16    }
 17  }
 18
 19  public static void main(String argv[]) {
 20
 21    // Check array member variables
 22    ArrayStruct as = new ArrayStruct();
 23
 24    // Create arrays for all the array types that ArrayStruct can handle
 25    String array_c = "X";
 26    byte[] array_c_extra = {11, 22};
 27    byte[] array_sc = {10, 20};
 28    short[] array_uc = {101, 201};
 29    short[] array_s = {1002, 2002};
 30    int[] array_us = {1003, 2003};
 31    int[] array_i = {1004, 2004};
 32    long[] array_ui = {1005, 2005};
 33    int[] array_l = {1006, 2006};
 34    long[] array_ul = {1007, 2007};
 35    long[] array_ll = {1008, 2008};
 36    float[] array_f = {1009.1f, 2009.1f};
 37    double[] array_d = {1010.2f, 2010.2f};
 38    int[] array_finger = {finger.Three, finger.Four};
 39    int[] array_toe = {toe.Big, toe.Little};
 40
 41    SimpleStruct[] array_struct={new SimpleStruct(), new SimpleStruct()};
 42    array_struct[0].setDouble_field(222.333);
 43    array_struct[1].setDouble_field(444.555);
 44
 45    AnotherStruct[] array_another_struct={new AnotherStruct(), new AnotherStruct()};
 46    array_another_struct[0].setSimple(array_struct[0]);
 47    array_another_struct[1].setSimple(array_struct[1]);
 48
 49    YetAnotherStruct[] array_yet_another_struct={new YetAnotherStruct(), new YetAnotherStruct()};
 50    array_yet_another_struct[0].setSimple(array_struct[0]);
 51    array_yet_another_struct[1].setSimple(array_struct[1]);
 52
 53    if (array_another_struct[0].getSimple().getDouble_field() != 222.333) throw new RuntimeException("AnotherStruct[0] failed");
 54    if (array_another_struct[1].getSimple().getDouble_field() != 444.555) throw new RuntimeException("AnotherStruct[1] failed");
 55
 56    if (java_lib_arrays.extract_ptr(array_yet_another_struct, 0) != 222.333) throw new RuntimeException("extract_ptr 0 failed");
 57    if (java_lib_arrays.extract_ptr(array_yet_another_struct, 1) != 444.555) throw new RuntimeException("extract_ptr 1 failed");
 58
 59    java_lib_arrays.modifyYAS(array_yet_another_struct, array_yet_another_struct.length);
 60    for (int i=0; i<2; ++i) {
 61      if (array_yet_another_struct[i].getSimple().getDouble_field() != array_struct[i].getDouble_field() * 10.0)
 62        throw new RuntimeException("modifyYAS failed ");
 63    }
 64
 65    java_lib_arrays.toestest(array_toe, array_toe, array_toe);
 66
 67    // Now set the array members and check that they have been set correctly
 68    as.setArray_c(array_c);
 69    check_string(array_c, as.getArray_c());
 70
 71    as.setArray_sc(array_sc);
 72    check_byte_array(array_sc, as.getArray_sc());
 73
 74    as.setArray_uc(array_uc);
 75    check_short_array(array_uc, as.getArray_uc());
 76
 77    as.setArray_s(array_s);
 78    check_short_array(array_s, as.getArray_s());
 79
 80    as.setArray_us(array_us);
 81    check_int_array(array_us, as.getArray_us());
 82
 83    as.setArray_i(array_i);
 84    check_int_array(array_i, as.getArray_i());
 85
 86    as.setArray_ui(array_ui);
 87    check_long_array(array_ui, as.getArray_ui());
 88
 89    as.setArray_l(array_l);
 90    check_int_array(array_l, as.getArray_l());
 91
 92    as.setArray_ul(array_ul);
 93    check_long_array(array_ul, as.getArray_ul());
 94
 95    as.setArray_ll(array_ll);
 96    check_long_array(array_ll, as.getArray_ll());
 97
 98    as.setArray_f(array_f);
 99    check_float_array(array_f, as.getArray_f());
100
101    as.setArray_d(array_d);
102    check_double_array(array_d, as.getArray_d());
103
104    as.setArray_enum(array_finger);
105    check_int_array(array_finger, as.getArray_enum());
106
107    as.setArray_struct(array_struct);
108    check_struct_array(array_struct, as.getArray_struct());
109
110    // Extended element (for char[])
111    ArrayStructExtra ase = new ArrayStructExtra();
112    ase.setArray_c2(array_c_extra);
113    check_byte_array(array_c_extra, ase.getArray_c2());
114
115 }
116
117  // Functions to check that the array values were set correctly
118  public static void check_string(String original, String checking) {
119    if (!checking.equals(original)) {
120      throw new RuntimeException("Runtime test failed. checking = [" + checking + "]");
121    }
122  }
123  public static void check_byte_array(byte[] original, byte[] checking) {
124    for (int i=0; i<original.length; i++) {
125      if (checking[i] != original[i]) {
126        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
127      }
128    }
129  }
130  public static void check_short_array(short[] original, short[] checking) {
131    for (int i=0; i<original.length; i++) {
132      if (checking[i] != original[i]) {
133        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
134      }
135    }
136  }
137  public static void check_int_array(int[] original, int[] checking) {
138    for (int i=0; i<original.length; i++) {
139      if (checking[i] != original[i]) {
140        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
141      }
142    }
143  }
144  public static void check_long_array(long[] original, long[] checking) {
145    for (int i=0; i<original.length; i++) {
146      if (checking[i] != original[i]) {
147        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
148      }
149    }
150  }
151  public static void check_float_array(float[] original, float[] checking) {
152    for (int i=0; i<original.length; i++) {
153      if (checking[i] != original[i]) {
154        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
155      }
156    }
157  }
158  public static void check_double_array(double[] original, double[] checking) {
159    for (int i=0; i<original.length; i++) {
160      if (checking[i] != original[i]) {
161        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
162      }
163    }
164  }
165  public static void check_struct_array(SimpleStruct[] original, SimpleStruct[] checking) {
166    for (int i=0; i<original.length; i++) {
167      if (checking[i].getDouble_field() != original[i].getDouble_field()) {
168        throw new RuntimeException("Runtime test failed. checking[" + i + "].double_field=" + checking[i].getDouble_field());
169      }
170    }
171  }
172}