summaryrefslogtreecommitdiff
path: root/Examples/test-suite/d/li_std_auto_ptr_runme.1.d
blob: 86fac68b5b1a1fdf625aa396d3a081b811971e2f (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
module li_std_auto_ptr_runme;

import li_std_auto_ptr.li_std_auto_ptr;
import li_std_auto_ptr.Klass;
import li_std_auto_ptr.KlassInheritance;
import std.conv;
import std.algorithm;

void checkCount(int expected_count) {
  int actual_count = Klass.getTotal_count();
  if (actual_count != expected_count)
    throw new Exception("Counts incorrect, expected:" ~ to!string(expected_count) ~ " actual:" ~ to!string(actual_count));
}

void main() {
  // Test raw pointer handling involving virtual inheritance
  {
    scope KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
    checkCount(1);
    string s = useKlassRawPtr(kini);
    if (s != "KlassInheritanceInput")
      throw new Exception("Incorrect string: " ~ s);
  }
  checkCount(0);

  // auto_ptr as input
  {
    scope Klass kin = new Klass("KlassInput");
    checkCount(1);
    string s = takeKlassAutoPtr(kin);
    checkCount(0);
    if (s != "KlassInput")
      throw new Exception("Incorrect string: " ~ s);
    if (!is_nullptr(kin))
      throw new Exception("is_nullptr failed");
  } // dispose should not fail, even though already deleted
  checkCount(0);

  {
    scope Klass kin = new Klass("KlassInput");
    checkCount(1);
    string s = takeKlassAutoPtr(kin);
    checkCount(0);
    if (s != "KlassInput")
      throw new Exception("Incorrect string: " ~ s);
    if (!is_nullptr(kin))
      throw new Exception("is_nullptr failed");
    bool exception_thrown = false;
    try {
      takeKlassAutoPtr(kin);
    } catch (Exception e) {
      if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
        throw new Exception("incorrect exception message: " ~ e.msg);
      exception_thrown = true;
    }
    if (!exception_thrown)
      throw new Exception("double usage of takeKlassAutoPtr should have been an error");
  } // dispose should not fail, even though already deleted
  checkCount(0);

  {
    scope Klass kin = new Klass("KlassInput");
    bool exception_thrown = false;
    Klass notowned = get_not_owned_ptr(kin);
    try {
      takeKlassAutoPtr(notowned);
    } catch (Exception e) {
      if (!canFind(e.msg, "Cannot release ownership as memory is not owned"))
        throw new Exception("incorrect exception message: " ~ e.msg);
      exception_thrown = true;
    }
    if (!exception_thrown)
      throw new Exception("Should have thrown 'Cannot release ownership as memory is not owned' error");
    checkCount(1);
  }
  checkCount(0);

  {
    scope KlassInheritance kini = new KlassInheritance("KlassInheritanceInput");
    checkCount(1);
    string s = takeKlassAutoPtr(kini);
    checkCount(0);
    if (s != "KlassInheritanceInput")
      throw new Exception("Incorrect string: " ~ s);
    if (!is_nullptr(kini))
      throw new Exception("is_nullptr failed");
  } // dispose should not fail, even though already deleted
  checkCount(0);

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

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


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

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

  k1.dispose();
  checkCount(1);

  if (k2.getLabel() != "second")
      throw new Exception("wrong object label");

  k2.dispose();
  checkCount(0);

  if (makeNullAutoPtr() !is null)
    throw new Exception("null failure");
}