PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Examples/test-suite/java/java_lib_arrays_runme.java

#
Java | 149 lines | 117 code | 25 blank | 7 comment | 26 complexity | 1b29e5ddb95c2fb5be0052738e6d206b MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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_sc = {10, 20};
 27    short[] array_uc = {101, 201};
 28    short[] array_s = {1002, 2002};
 29    int[] array_us = {1003, 2003};
 30    int[] array_i = {1004, 2004};
 31    long[] array_ui = {1005, 2005};
 32    int[] array_l = {1006, 2006};
 33    long[] array_ul = {1007, 2007};
 34    long[] array_ll = {1008, 2008};
 35    float[] array_f = {1009.1f, 2009.1f};
 36    double[] array_d = {1010.2f, 2010.2f};
 37    int[] array_enum = {finger.Three, finger.Four};
 38
 39    SimpleStruct[] array_struct={new SimpleStruct(), new SimpleStruct()};
 40    array_struct[0].setDouble_field(222.333);
 41    array_struct[1].setDouble_field(444.555);
 42
 43    AnotherStruct[] another_struct={new AnotherStruct(), new AnotherStruct()};
 44    another_struct[0].setSimple(array_struct[0]);
 45    another_struct[1].setSimple(array_struct[1]);
 46
 47    if (another_struct[0].getSimple().getDouble_field() != 222.333) throw new RuntimeException("AnotherStruct[0] failed");
 48    if (another_struct[1].getSimple().getDouble_field() != 444.555) throw new RuntimeException("AnotherStruct[1] failed");
 49
 50    // Now set the array members and check that they have been set correctly
 51    as.setArray_c(array_c);
 52    check_string(array_c, as.getArray_c());
 53
 54    as.setArray_sc(array_sc);
 55    check_byte_array(array_sc, as.getArray_sc());
 56
 57    as.setArray_uc(array_uc);
 58    check_short_array(array_uc, as.getArray_uc());
 59
 60    as.setArray_s(array_s);
 61    check_short_array(array_s, as.getArray_s());
 62
 63    as.setArray_us(array_us);
 64    check_int_array(array_us, as.getArray_us());
 65
 66    as.setArray_i(array_i);
 67    check_int_array(array_i, as.getArray_i());
 68
 69    as.setArray_ui(array_ui);
 70    check_long_array(array_ui, as.getArray_ui());
 71
 72    as.setArray_l(array_l);
 73    check_int_array(array_l, as.getArray_l());
 74
 75    as.setArray_ul(array_ul);
 76    check_long_array(array_ul, as.getArray_ul());
 77
 78    as.setArray_ll(array_ll);
 79    check_long_array(array_ll, as.getArray_ll());
 80
 81    as.setArray_f(array_f);
 82    check_float_array(array_f, as.getArray_f());
 83
 84    as.setArray_d(array_d);
 85    check_double_array(array_d, as.getArray_d());
 86
 87    as.setArray_enum(array_enum);
 88    check_int_array(array_enum, as.getArray_enum());
 89
 90    as.setArray_struct(array_struct);
 91    check_struct_array(array_struct, as.getArray_struct());
 92 }
 93
 94  // Functions to check that the array values were set correctly
 95  public static void check_string(String original, String checking) {
 96    if (!checking.equals(original)) {
 97      throw new RuntimeException("Runtime test failed. checking = [" + checking + "]");
 98    }
 99  }
100  public static void check_byte_array(byte[] original, byte[] checking) {
101    for (int i=0; i<original.length; i++) {
102      if (checking[i] != original[i]) {
103        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
104      }
105    }
106  }
107  public static void check_short_array(short[] original, short[] checking) {
108    for (int i=0; i<original.length; i++) {
109      if (checking[i] != original[i]) {
110        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
111      }
112    }
113  }
114  public static void check_int_array(int[] original, int[] checking) {
115    for (int i=0; i<original.length; i++) {
116      if (checking[i] != original[i]) {
117        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
118      }
119    }
120  }
121  public static void check_long_array(long[] original, long[] checking) {
122    for (int i=0; i<original.length; i++) {
123      if (checking[i] != original[i]) {
124        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
125      }
126    }
127  }
128  public static void check_float_array(float[] original, float[] checking) {
129    for (int i=0; i<original.length; i++) {
130      if (checking[i] != original[i]) {
131        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
132      }
133    }
134  }
135  public static void check_double_array(double[] original, double[] checking) {
136    for (int i=0; i<original.length; i++) {
137      if (checking[i] != original[i]) {
138        throw new RuntimeException("Runtime test failed. checking[" + i + "]=" + checking[i]);
139      }
140    }
141  }
142  public static void check_struct_array(SimpleStruct[] original, SimpleStruct[] checking) {
143    for (int i=0; i<original.length; i++) {
144      if (checking[i].getDouble_field() != original[i].getDouble_field()) {
145        throw new RuntimeException("Runtime test failed. checking[" + i + "].double_field=" + checking[i].getDouble_field());
146      }
147    }
148  }
149}