File: cpp11_std_unique_ptr_runme.m

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 (138 lines) | stat: -rw-r--r-- 3,227 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
# do not dump Octave core
if exist("crash_dumps_octave_core", "builtin")
  crash_dumps_octave_core(0);
endif

cpp11_std_unique_ptr

function checkCount(expected_count)
  actual_count = Klass_getTotal_count();
  if (actual_count != expected_count)
    error("Counts incorrect, expected:%d actual:%d", expected_count, actual_count);
  endif
end

# Test raw pointer handling involving virtual inheritance
kini = KlassInheritance("KlassInheritanceInput");
checkCount(1);
s = useKlassRawPtr(kini);
if (!strcmp(s, "KlassInheritanceInput"))
  error("Incorrect string: %s", s);
endif
clear kini;
checkCount(0);


# unique_ptr as input
kin = Klass("KlassInput");
checkCount(1);
s = takeKlassUniquePtr(kin);
checkCount(0);
if (!strcmp(s, "KlassInput"))
  error("Incorrect string: %s", s);
endif
if (!is_nullptr(kin))
  error("is_nullptr failed");
endif
clear kin; # Should not fail, even though already deleted
checkCount(0);

kin = Klass("KlassInput");
checkCount(1);
s = takeKlassUniquePtr(kin);
checkCount(0);
if (!strcmp(s, "KlassInput"))
  error("Incorrect string: %s", s);
endif
if (!is_nullptr(kin))
  error("is_nullptr failed");
endif
exception_thrown = false;
try
  takeKlassUniquePtr(kin);
catch e
  if (isempty(strfind(e.message, "cannot release ownership as memory is not owned")))
    error("incorrect exception message %s", e.message);
  endif
  exception_thrown = true;
end_try_catch
if (!exception_thrown)
    error("double usage of takeKlassUniquePtr should have been an error");
endif
clear kin; # Should not fail, even though already deleted
checkCount(0);

kin = Klass("KlassInput");
exception_thrown = false;
notowned = get_not_owned_ptr(kin);
try
  takeKlassUniquePtr(notowned);
catch e
  if (isempty(strfind(e.message, "cannot release ownership as memory is not owned")))
    error("incorrect exception message %s", e.message);
  endif
  exception_thrown = true;
end_try_catch
if (!exception_thrown)
  error("Should have thrown 'Cannot release ownership as memory is not owned' error");
endif
checkCount(1);
clear kin;
checkCount(0);

kini = KlassInheritance("KlassInheritanceInput");
checkCount(1);
s = takeKlassUniquePtr(kini);
checkCount(0);
if (!strcmp(s, "KlassInheritanceInput"))
  error("Incorrect string: %s", s);
endif
if (!is_nullptr(kini))
  error("is_nullptr failed");
endif
clear kini; # Should not fail, even though already deleted
checkCount(0);

null = []; # NULL pointer
null_ptr = make_null();
takeKlassUniquePtr([]);
takeKlassUniquePtr(null);
takeKlassUniquePtr(null_ptr);
checkCount(0);

# overloaded parameters
if (overloadTest() != 0)
  error("overloadTest failed");
endif
if (overloadTest(null) != 1)
  error("overloadTest failed");
endif
if (overloadTest(Klass("over")) != 1)
  error("overloadTest failed");
endif
checkCount(0);


# unique_ptr as output
k1 = makeKlassUniquePtr("first");
if (!strcmp(k1.getLabel(), "first"))
  error("wrong object label");
endif

k2 = makeKlassUniquePtr("second");
checkCount(2);

clear k1;
checkCount(1);

if (!strcmp(k2.getLabel(), "second"))
  error("wrong object label");
endif

clear k2;
checkCount(0);

null_smart_prt = makeNullUniquePtr();
assert(ismatrix(null_smart_prt))
assert(size(null_smart_prt) == size([]))
assert(isequal([], null_smart_prt))