PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 83 lines | 58 code | 15 blank | 10 comment | 12 complexity | cc10e87740784862293e2a60c5a48899 MD5 | raw file
 1
 2// Test case to check typemaps in various.i
 3
 4import java_lib_various.*;
 5
 6public class java_lib_various_runme {
 7
 8  static {
 9    try {
10	System.loadLibrary("java_lib_various");
11    } catch (UnsatisfiedLinkError e) {
12      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);
13      System.exit(1);
14    }
15  }
16
17  public static void main(String argv[]) {
18
19    // STRING_ARRAY typemap parameter
20    String animals[] = {"Cat","Dog","Cow","Goat"};
21    if (java_lib_various.check_animals(animals) != 1)
22      throw new RuntimeException("check_animals failed");
23
24    // STRING_ARRAY typemap return value
25    String expected[] = { "Dave", "Mike", "Susan", "John", "Michelle" };
26    String got[] = java_lib_various.get_names();
27    for (int i=0; i<got.length; i++)
28      if ( !got[i].equals(expected[i]) )
29        throw new RuntimeException("Name failed " + i + " " + got[i] + "|" + expected[i]);
30
31    // STRING_ARRAY variable getter
32    String langscheck[] = { "Hungarian", "Afrikaans", "Norwegian" };
33    String langs[] = java_lib_various.getLanguages();
34    for (int i=0; i<langs.length; i++)
35      if ( !langs[i].equals(langscheck[i]) )
36        throw new RuntimeException("Languages read failed " + i + " " + langs[i] + "|" + langscheck[i]);
37
38    // STRING_ARRAY variable setter
39    String newLangs[] = { "French", "Italian", "Spanish" };
40    java_lib_various.setLanguages(newLangs);
41
42    // STRING_ARRAY variable getter
43    langs = java_lib_various.getLanguages();
44    for (int i=0; i<langs.length; i++)
45      if ( !langs[i].equals(newLangs[i]) )
46        throw new RuntimeException("Languages verify failed " + i + " " + langs[i] + "|" + newLangs[i]);
47
48    // STRING_RET test
49    {
50      String stringOutArray[] = { "" };
51      java_lib_various.char_ptr_ptr_out(stringOutArray);
52      if (!stringOutArray[0].equals("returned string"))
53        throw new RuntimeException("Test failed: expected: returned string. got: " + stringOutArray[0]);
54    }
55
56    // STRING_RET null array test. Check that exception is thrown.
57    try {
58      String stringOutArray[] = null;
59      java_lib_various.char_ptr_ptr_out(stringOutArray);
60      throw new RuntimeException("Test failed: null array");
61    } catch (NullPointerException e) {
62    }
63
64    // STRING_RET empty array test. Check that exception is thrown.
65    try {
66      String stringOutArray[] = {};
67      java_lib_various.char_ptr_ptr_out(stringOutArray);
68      throw new RuntimeException("Test failed: empty array");
69    } catch (IndexOutOfBoundsException e) {
70    }
71
72    // BYTE typemap check
73    byte b[] = new byte[20];
74    java_lib_various.charout(b);
75    String byjovestring = new String("by jove");
76    byte byjove[] = byjovestring.getBytes();
77    for (int i=0; i<byjovestring.length(); i++) {
78      if (byjove[i] != b[i])
79        throw new RuntimeException("By jove, it failed: [" + new String(b) + "]");
80    }
81  }
82
83}