/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

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