File: cpp11_li_std_array_runme.java

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (82 lines) | stat: -rw-r--r-- 2,970 bytes parent folder | download | duplicates (7)
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
import cpp11_li_std_array.*;

public class cpp11_li_std_array_runme {

  static {
    try {
        System.loadLibrary("cpp11_li_std_array");
    } 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);
    }
  }

  private static ArrayInt6 ToArray6(int [] a) {
    ArrayInt6 ai = new ArrayInt6();
    if (a.length != 6)
      throw new RuntimeException("a is incorrect size");
    for (int i=0; i<6; ++i)
      ai.set(i, a[i]);
    return ai;
  }

  private static void compareContainers(ArrayInt6 actual, int[] expected) {
    if (actual.size() != expected.length)
      throw new RuntimeException("Sizes are different: " + actual.size() + " " + expected.length);
    for (int i=0; i<actual.size(); ++i) {
      int actualValue = actual.get(i);
      int expectedValue = expected[i];
      if (actualValue != expectedValue)
        throw new RuntimeException("Value is wrong for element " + i + ". Expected " + expectedValue + " got: " + actualValue);
    }
    if (actual.isEmpty())
      throw new RuntimeException("ai should not be empty");
  }

  public static void main(String argv[]) {
    ArrayInt6 ai = new ArrayInt6();
    compareContainers(ai, new int[] {0, 0, 0, 0, 0, 0});

    int[] vals = {10, 20, 30, 40, 50, 60};
    for (int i=0; i<ai.size(); ++i)
      ai.set(i, vals[i]);
    compareContainers(ai, vals);

    // Check return
    compareContainers(cpp11_li_std_array.arrayOutVal(), new int[] {-2, -1, 0, 0, 1, 2});
    compareContainers(cpp11_li_std_array.arrayOutConstRef(), new int[] {-2, -1, 0, 0, 1, 2});
    compareContainers(cpp11_li_std_array.arrayOutRef(), new int[] {-2, -1, 0, 0, 1, 2});
    compareContainers(cpp11_li_std_array.arrayOutPtr(), new int[] {-2, -1, 0, 0, 1, 2});

    // Check passing arguments
    ai = cpp11_li_std_array.arrayInVal(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
    compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});

    ai = cpp11_li_std_array.arrayInConstRef(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
    compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});

    ai = new ArrayInt6(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
    cpp11_li_std_array.arrayInRef(ai);
    compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});

    ai = new ArrayInt6(ToArray6(new int[] {9, 8, 7, 6, 5, 4}));
    cpp11_li_std_array.arrayInPtr(ai);
    compareContainers(ai, new int[] {90, 80, 70, 60, 50, 40});

    // fill
    ai.fill(111);
    compareContainers(ai, new int[] {111, 111, 111, 111, 111, 111});

    // out of range errors
    try {
      ai.set((int)ai.size(), 0);
      throw new RuntimeException("Out of range exception not caught");
    } catch(IndexOutOfBoundsException e) {
    }
    try {
      ai.set(-1, 0);
      throw new RuntimeException("Out of range exception not caught");
    } catch(IndexOutOfBoundsException e) {
    }
  }
}