File: li_std_auto_ptr_runme.java

package info (click to toggle)
swig 4.1.0-0.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 47,992 kB
  • sloc: cpp: 50,555; ansic: 27,840; java: 15,366; python: 11,221; cs: 8,852; ruby: 6,307; yacc: 6,290; makefile: 5,702; sh: 5,492; perl: 3,818; php: 3,046; ml: 2,094; lisp: 1,756; javascript: 1,751; tcl: 1,499; xml: 115
file content (146 lines) | stat: -rw-r--r-- 4,848 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import li_std_auto_ptr.*;

public class li_std_auto_ptr_runme {
  static {
    try {
        System.loadLibrary("li_std_auto_ptr");
    } 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 void WaitForGC()
  {
    System.gc();
    System.runFinalization();
    try {
      java.lang.Thread.sleep(10);
    } catch (java.lang.InterruptedException e) {
    }
  }

  private static void checkCount(int expected_count) {
    int actual_count = Klass.getTotal_count();
    if (actual_count != expected_count)
      throw new RuntimeException("Counts incorrect, expected:" + expected_count + " actual:" + actual_count);
  }

  public static void main(String argv[]) throws Throwable
  {
    // Test raw pointer handling involving virtual inheritance
    {
      KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
      checkCount(1);
      String s = li_std_auto_ptr.useKlassRawPtr(kini);
      if (!s.equals("KlassInheritanceInput"))
        throw new RuntimeException("Incorrect string: " + s);
      kini.delete();
      checkCount(0);
    }

    // auto_ptr as input
    {
      Klass kin = new Klass("KlassInput");
      checkCount(1);
      String s = li_std_auto_ptr.takeKlassAutoPtr(kin);
      checkCount(0);
      if (!s.equals("KlassInput"))
        throw new RuntimeException("Incorrect string: " + s);
      if (!li_std_auto_ptr.is_nullptr(kin))
        throw new RuntimeException("is_nullptr failed");
      kin.delete(); // Should not fail, even though already deleted
      checkCount(0);
    }

    {
      Klass kin = new Klass("KlassInput");
      checkCount(1);
      String s = li_std_auto_ptr.takeKlassAutoPtr(kin);
      checkCount(0);
      if (!s.equals("KlassInput"))
        throw new RuntimeException("Incorrect string: " + s);
      if (!li_std_auto_ptr.is_nullptr(kin))
        throw new RuntimeException("is_nullptr failed");
      boolean exception_thrown = false;
      try {
        li_std_auto_ptr.takeKlassAutoPtr(kin);
      } catch (RuntimeException e) {
        if (!e.getMessage().contains("Cannot release ownership as memory is not owned"))
          throw new RuntimeException("incorrect exception message");
        exception_thrown = true;
      }
      if (!exception_thrown)
          throw new RuntimeException("double usage of takeKlassAutoPtr should have been an error");
      kin.delete(); // Should not fail, even though already deleted
      checkCount(0);
    }

    {
      Klass kin = new Klass("KlassInput");
      boolean exception_thrown = false;
      Klass notowned = li_std_auto_ptr.get_not_owned_ptr(kin);
      try {
        li_std_auto_ptr.takeKlassAutoPtr(notowned);
      } catch (RuntimeException e) {
        if (!e.getMessage().contains("Cannot release ownership as memory is not owned"))
          throw new RuntimeException("incorrect exception message");
        exception_thrown = true;
      }
      if (!exception_thrown)
        throw new RuntimeException("Should have thrown 'Cannot release ownership as memory is not owned' error");
      checkCount(1);
      kin.delete();
      checkCount(0);
    }

    {
      KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
      checkCount(1);
      String s = li_std_auto_ptr.takeKlassAutoPtr(kini);
      checkCount(0);
      if (!s.equals("KlassInheritanceInput"))
        throw new RuntimeException("Incorrect string: " + s);
      if (!li_std_auto_ptr.is_nullptr(kini))
        throw new RuntimeException("is_nullptr failed");
      kini.delete(); // Should not fail, even though already deleted
      checkCount(0);
    }

    li_std_auto_ptr.takeKlassAutoPtr(null);
    li_std_auto_ptr.takeKlassAutoPtr(li_std_auto_ptr.make_null());
    checkCount(0);

    // overloaded parameters
    if (li_std_auto_ptr.overloadTest() != 0)
      throw new RuntimeException("overloadTest failed");
    if (li_std_auto_ptr.overloadTest(null) != 1)
      throw new RuntimeException("overloadTest failed");
    if (li_std_auto_ptr.overloadTest(new Klass("over")) != 1)
      throw new RuntimeException("overloadTest failed");
    checkCount(0);


    // auto_ptr as output
    Klass k1 = li_std_auto_ptr.makeKlassAutoPtr("first");
    if (!k1.getLabel().equals("first"))
      throw new RuntimeException("wrong object label");

    Klass k2 = li_std_auto_ptr.makeKlassAutoPtr("second");
    checkCount(2);

    k1.delete();
    k1 = null;
    checkCount(1);

    if (!k2.getLabel().equals("second"))
      throw new RuntimeException("wrong object label");

    k2.delete();
    k2 = null;
    checkCount(0);

    if (li_std_auto_ptr.makeNullAutoPtr() != null)
      throw new RuntimeException("null failure");
  }
}