Add support for std::auto_ptr inputs

Ported from std::unique, behaviour is identical with regard to memory ownership/handling
This commit is contained in:
William S Fulton 2022-07-18 08:32:26 +01:00
commit db5e37a1d7
11 changed files with 477 additions and 47 deletions

View file

@ -9,8 +9,71 @@ public class li_std_auto_ptr_runme {
System.Threading.Thread.Sleep(10);
}
private static void checkCount(int expected_count)
{
int actual_count = Klass.getTotal_count();
if (actual_count != expected_count)
throw new ApplicationException("Counts incorrect, expected:" + expected_count + " actual:" + actual_count);
}
public static void Main()
{
// auto_ptr as input
using (Klass kin = new Klass("KlassInput")) {
checkCount(1);
string s = li_std_auto_ptr.takeKlassAutoPtr(kin);
checkCount(0);
if (s != "KlassInput")
throw new ApplicationException("Incorrect string: " + s);
if (!li_std_auto_ptr.is_nullptr(kin))
throw new ApplicationException("is_nullptr failed");
} // Dispose should not fail, even though already deleted
checkCount(0);
using (Klass kin = new Klass("KlassInput")) {
checkCount(1);
string s = li_std_auto_ptr.takeKlassAutoPtr(kin);
checkCount(0);
if (s != "KlassInput")
throw new ApplicationException("Incorrect string: " + s);
if (!li_std_auto_ptr.is_nullptr(kin))
throw new ApplicationException("is_nullptr failed");
bool exception_thrown = false;
try {
li_std_auto_ptr.takeKlassAutoPtr(kin);
} catch (ApplicationException) {
exception_thrown = true;
}
if (!exception_thrown)
throw new ApplicationException("double usage of takeKlassAutoPtr should have been an error");
} // Dispose should not fail, even though already deleted
checkCount(0);
using (Klass kin = new Klass("KlassInput")) {
bool exception_thrown = false;
try {
Klass notowned = li_std_auto_ptr.get_not_owned_ptr(kin);
li_std_auto_ptr.takeKlassAutoPtr(notowned);
} catch (ApplicationException) {
exception_thrown = true;
}
if (!exception_thrown)
throw new ApplicationException("Should have thrown 'Cannot release ownership as memory is not owned' error");
}
checkCount(0);
using (KlassInheritance kini = new KlassInheritance("KlassInheritanceInput")) {
checkCount(1);
string s = li_std_auto_ptr.takeKlassAutoPtr(kini);
checkCount(0);
if (s != "KlassInheritanceInput")
throw new ApplicationException("Incorrect string: " + s);
if (!li_std_auto_ptr.is_nullptr(kini))
throw new ApplicationException("is_nullptr failed");
} // Dispose should not fail, even though already deleted
checkCount(0);
// auto_ptr as output
Klass k1 = li_std_auto_ptr.makeKlassAutoPtr("first");
if (k1.getLabel() != "first")
throw new Exception("wrong object label");