summaryrefslogtreecommitdiff
path: root/Examples/test-suite/java/java_lib_various_runme.java
blob: 6d9e13e31ab6c525d2bcfbd4134217c8816fb7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

// Test case to check typemaps in various.i

import java_lib_various.*;

public class java_lib_various_runme {

  static {
    try {
	System.loadLibrary("java_lib_various");
    } catch (UnsatisfiedLinkError e) {
      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);
      System.exit(1);
    }
  }

  public static void main(String argv[]) {

    // STRING_ARRAY typemap parameter
    String animals[] = {"Cat","Dog","Cow","Goat"};
    if (java_lib_various.check_animals(animals) != 1)
      throw new RuntimeException("check_animals failed");

    // STRING_ARRAY typemap return value
    String expected[] = { "Dave", "Mike", "Susan", "John", "Michelle" };
    String got[] = java_lib_various.get_names();
    for (int i=0; i<got.length; i++)
      if ( !got[i].equals(expected[i]) )
        throw new RuntimeException("Name failed " + i + " " + got[i] + "|" + expected[i]);

    // STRING_ARRAY variable getter
    String langscheck[] = { "Hungarian", "Afrikaans", "Norwegian" };
    String langs[] = java_lib_various.getLanguages();
    for (int i=0; i<langs.length; i++)
      if ( !langs[i].equals(langscheck[i]) )
        throw new RuntimeException("Languages read failed " + i + " " + langs[i] + "|" + langscheck[i]);

    // STRING_ARRAY variable setter
    String newLangs[] = { "French", "Italian", "Spanish" };
    java_lib_various.setLanguages(newLangs);

    // STRING_ARRAY variable getter
    langs = java_lib_various.getLanguages();
    for (int i=0; i<langs.length; i++)
      if ( !langs[i].equals(newLangs[i]) )
        throw new RuntimeException("Languages verify failed " + i + " " + langs[i] + "|" + newLangs[i]);

    // STRING_ARRAY null
    java_lib_various.setLanguages(null);
    if (java_lib_various.getLanguages() != null)
      throw new RuntimeException("languages should be null");

    // STRING_RET test
    {
      String stringOutArray[] = { "" };
      java_lib_various.char_ptr_ptr_out(stringOutArray);
      if (!stringOutArray[0].equals("returned string"))
        throw new RuntimeException("Test failed: expected: returned string. got: " + stringOutArray[0]);
    }

    // STRING_RET null array test. Check that exception is thrown.
    try {
      String stringOutArray[] = null;
      java_lib_various.char_ptr_ptr_out(stringOutArray);
      throw new RuntimeException("Test failed: null array");
    } catch (NullPointerException e) {
    }

    // STRING_RET empty array test. Check that exception is thrown.
    try {
      String stringOutArray[] = {};
      java_lib_various.char_ptr_ptr_out(stringOutArray);
      throw new RuntimeException("Test failed: empty array");
    } catch (IndexOutOfBoundsException e) {
    }

    // BYTE typemap check
    byte b[] = new byte[20];
    java_lib_various.charout(b);
    String byjovestring = new String("by jove");
    byte byjove[] = byjovestring.getBytes();
    for (int i=0; i<byjovestring.length(); i++) {
      if (byjove[i] != b[i])
        throw new RuntimeException("By jove, it failed: [" + new String(b) + "]");
    }

    // NIOBUFFER typemap check
      java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocateDirect(10); 
      java_lib_various.niobuffer_fill_hello(buf);
      if (
	(char)buf.get(0) != 'h' ||
	(char)buf.get(1) != 'e' ||
	(char)buf.get(2) != 'l' ||
	(char)buf.get(3) != 'l' ||
	(char)buf.get(4) != 'o'
      )
        throw new RuntimeException(
          "nio test failed: " + 
          (char)buf.get(0) + 
          (char)buf.get(1) + 
          (char)buf.get(2) + 
          (char)buf.get(3) + 
          (char)buf.get(4)
        );
  }

}