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