swig/Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs
William S Fulton 299880e6a6 Add std::unique support
Simple copy of current auto_ptr support (just suppport for
functions returning std::unique_ptr).

Closes #1722
2022-07-02 16:17:18 +01:00

64 lines
2.1 KiB
C#

using System;
using cpp11_std_unique_ptrNamespace;
public class cpp11_std_unique_ptr_runme {
private static void WaitForGC()
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(10);
}
public static void Main()
{
Klass k1 = cpp11_std_unique_ptr.makeKlassUniquePtr("first");
if (k1.getLabel() != "first")
throw new Exception("wrong object label");
Klass k2 = cpp11_std_unique_ptr.makeKlassUniquePtr("second");
if (Klass.getTotal_count() != 2)
throw new Exception("number of objects should be 2");
using (Klass k3 = cpp11_std_unique_ptr.makeKlassUniquePtr("second")) {
if (Klass.getTotal_count() != 3)
throw new Exception("number of objects should be 3");
}
if (Klass.getTotal_count() != 2)
throw new Exception("number of objects should be 2");
k1 = null;
{
int countdown = 500;
int expectedCount = 1;
while (true) {
WaitForGC();
if (--countdown == 0)
break;
if (Klass.getTotal_count() == expectedCount)
break;
};
int actualCount = Klass.getTotal_count();
if (actualCount != expectedCount)
Console.Error.WriteLine("Expected count: " + expectedCount + " Actual count: " + actualCount); // Finalizers are not guaranteed to be run and sometimes they just don't
}
if (k2.getLabel() != "second")
throw new Exception("wrong object label");
k2 = null;
{
int countdown = 500;
int expectedCount = 0;
while (true) {
WaitForGC();
if (--countdown == 0)
break;
if (Klass.getTotal_count() == expectedCount)
break;
}
int actualCount = Klass.getTotal_count();
if (actualCount != expectedCount)
Console.Error.WriteLine("Expected count: " + expectedCount + " Actual count: " + actualCount); // Finalizers are not guaranteed to be run and sometimes they just don't
}
}
}