コード例 #1
0
    public void Released()
    {
        NSObject pool  = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        long     bytes = DoGetMemory();

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations / 100; ++i)
            {
                Class klass = new Class("NSNumber");
                klass.release();
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;

        if (delta / NumIterations > 4)
        {
            Assert.Fail("Released used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations);
        }
    }
コード例 #2
0
    public void IntArg()
    {
        NSObject pool  = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        long     bytes = DoGetMemory();

        Class    nsString = new Class("NSString");
        NSObject str      = (NSObject)nsString.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello world"));

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations / 100; ++i)
            {
                str.Call("characterAtIndex:", 2);
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;

        if (delta / NumIterations > 4)
        {
            Assert.Fail("IntArg used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations);
        }
    }
コード例 #3
0
    public void Managed()
    {
        NSObject pool  = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        long     bytes = DoGetMemory();

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations / 100; ++i)
            {
                NSObject instance = (NSObject) new Class("Subclass1").Call("alloc").Call("init");
                instance.Call("TakeString", "what");
                instance.release();
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;

        if (delta / NumIterations > 4)
        {
            Assert.Fail("Managed used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations);
        }
    }
コード例 #4
0
    public void RefCount2Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        // No copy, new, or alloc so ref count is one and it's owned by the pool.
        PrettyData direct = PrettyData.makeDefault();

        Assert.AreEqual(1L, direct.retainCount());

        // Alloc so pool has no ownership stake.
        NSObject indirect = (NSObject) new Class("PrettyData").Call("alloc").Call("init");

        Assert.AreEqual(1L, indirect.retainCount());

        // If we send a message to an object its retain count doesn't change.
        int value = (int)direct.Call("get33");

        Assert.AreEqual(33, value);
        Assert.AreEqual(1L, direct.retainCount());

        pool.release();

        // Verify our counts after we empty the release pool.
        Assert.IsTrue(direct.IsDeallocated());
        Assert.AreEqual(1L, indirect.retainCount());
    }
コード例 #5
0
    public void ArrayArg()
    {
        NSObject pool   = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        Class    nsData = new Class("NSData");
        long     bytes  = DoGetMemory();

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations / 100; ++i)
            {
                byte[] data = new byte[] { 2, 5, 6, 3 };

                NSObject d = new NSObject(nsData.Call("alloc"));
                NSObject e = (NSObject)d.Call("initWithBytes:length:", data, data.Length);
                e.release();
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;

        if (delta / NumIterations > 4)
        {
            Assert.Fail("ArrayArg used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations);
        }
    }
コード例 #6
0
    public void Formatted()
    {
        NSObject pool  = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
        long     bytes = DoGetMemory();

        for (int j = 1; j < 100; ++j)
        {
            for (int i = 0; i < NumIterations / 100; ++i)
            {
                NSObject s = (NSObject)Native.Call("[[NSNumber alloc] initWithInteger:{0}]", 33);
                s.release();
            }
            GC.Collect();
        }

        pool.release();
        GC.Collect();
        GC.WaitForPendingFinalizers();

        long delta = DoGetMemory() - bytes;

        if (delta / NumIterations > 4)
        {
            Assert.Fail("Formatted used {0}K of memory ({1} bytes per iteration)!", delta / 1024, delta / NumIterations);
        }
    }
コード例 #7
0
    public void NilCallTest()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        NSObject nil = new NSObject(IntPtr.Zero);

        // Calling an NSObject method on nil does nothing and returns nil.
        NSObject result = (NSObject)nil.Call("hash");

        Assert.IsTrue(result.IsNil());

        // Calling a unknown method on nil does nothing and returns nil.
        result = (NSObject)nil.Call("foo");
        Assert.IsTrue(result.IsNil());

        // Can chain calls to nil.
        result = (NSObject)nil.Call("foo").Call("bar");
        Assert.IsTrue(result.IsNil());

        // Can use Native with null.
        result = (NSObject)nil.Call("foo");
        Assert.IsTrue(result.IsNil());

        pool.release();
    }
コード例 #8
0
ファイル: TimingTest.cs プロジェクト: divyang4481/mobjc
    public void Init()
    {
        AssertListener.Install();

        Registrar.CanInit = true;
        m_pool            = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
    }
コード例 #9
0
ファイル: DerivedTest.cs プロジェクト: divyang4481/mobjc
    public void BadSuperCall()
    {
        try
        {
            MyBase a = NSObject.AllocAndInitInstance("MyBase").To <MyBase>();
            a.badAccumulate(0);
            Assert.Fail("badAccumulate should have thrown an exception");
        }
        catch (InvalidCallException i)
        {
            if (!i.Message.Contains("NSSimpleCString"))
            {
                Assert.Fail("Expected 'NSSimpleCString' in '{0}", i.Message);
            }

            if (!i.Message.Contains("badAccumulate"))
            {
                Assert.Fail("Expected 'badAccumulate' in '{0}", i.Message);
            }
        }
        catch (Exception e)
        {
            Assert.Fail("badAccumulate should have thrown an InvalidCallException, not a {0}", e.GetType());
        }
    }
コード例 #10
0
ファイル: DerivedTest.cs プロジェクト: divyang4481/mobjc
    public void SuperCall5()
    {
        MyDerived a = NSObject.AllocAndInitInstance("MyDerived").To <MyDerived>();

        int result = a.SuperCall(new Class("MyBase"), "accumulate:", 1).To <int>();

        Assert.AreEqual(2, result);
    }
コード例 #11
0
    public void ThreadedTest()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        sbyte threaded = (sbyte)new Class("NSThread").Call("isMultiThreaded");

        Assert.AreEqual(1, threaded);

        pool.release();
    }
コード例 #12
0
    public void DeallocTest2()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        MyDerived instance = (MyDerived) new Class("MyDerived").Call("alloc").Call("init");

        Assert.AreEqual(1L, instance.retainCount());
        instance.release();
        Assert.IsTrue(instance.Dead);

        pool.release();
    }
コード例 #13
0
    public void ChainedCallTest()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        Class    nsString = new Class("NSMutableString");
        NSObject str      = (NSObject)nsString.Call("alloc").Call("initWithUTF8String:", Marshal.StringToHGlobalAuto("chained!"));

        string result = Marshal.PtrToStringAuto((IntPtr)str.Call("UTF8String"));

        Assert.AreEqual("chained!", result);

        pool.release();
    }
コード例 #14
0
    public void SuperTest()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        Media instance = (Media) new Class("Media").Call("alloc").Call("init");
        int   value    = instance.value();

        Assert.AreEqual(3, value);

        value = instance.Call("value").To <int>();
        Assert.AreEqual(3, value);

        pool.release();
    }
コード例 #15
0
ファイル: DerivedTest.cs プロジェクト: divyang4481/mobjc
    public void SuperCall4()
    {
        try
        {
            MyBase a = NSObject.AllocAndInitInstance("MyBase").To <MyBase>();

            a.SuperCall(NSObject.Class, "accumulate:", 5);
            Assert.Fail("SuperCall should have thrown an exception");
        }
        catch (InvalidCallException e)
        {
            Assert.AreEqual("Couldn't find a method for NSObject.accumulate:", e.Message);
        }
    }
コード例 #16
0
ファイル: DerivedTest.cs プロジェクト: divyang4481/mobjc
    public void SuperCall2()
    {
        try
        {
            MyDerived b = NSObject.AllocAndInitInstance("MyDerived").To <MyDerived>();

            int result = b.accumulate(0);
            Assert.AreEqual(5, result);
        }
        catch (Exception e)
        {
            Console.WriteLine("SuperCall2 failed:");
            Console.WriteLine("{0}", e);
        }
    }
コード例 #17
0
ファイル: DerivedTest.cs プロジェクト: divyang4481/mobjc
    public void SuperCall1()
    {
        try
        {
            MyBase a = NSObject.AllocAndInitInstance("MyBase").To <MyBase>();

            int result = a.accumulate(0);
            Assert.AreEqual(2, result);
        }
        catch (Exception e)
        {
            Console.WriteLine("SuperCall1 failed:");
            Console.WriteLine("{0}", e);
        }
    }
コード例 #18
0
    public void RefCount3Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        Class    klass     = new Class("NSHashTable");
        NSObject instance1 = (NSObject)klass.Call("alloc").Call("init");

        Assert.AreEqual(1L, instance1.retainCount());

        NSObject instance2 = (NSObject) new Class("NSHashTable").Call("alloc").Call("init");

        Assert.AreEqual(1L, instance2.retainCount());

        pool.release();

        Assert.AreEqual(1L, instance1.retainCount());
        Assert.AreEqual(1L, instance2.retainCount());
    }
コード例 #19
0
    internal static void Main(string[] args)
    {
        try
        {
            Registrar.CanInit = true;

            // Make our app a foreground app (this is redundant if we were started via the
            // Finder or the open command, but important if we were started by directly
            // executing the launcher script).
            var psn = new ProcessSerialNumber();
            psn.highLongOfPSN = 0;
            psn.lowLongOfPSN  = kCurrentProcess;

            int err = TransformProcessType(ref psn, kProcessTransformToForegroundApplication);
            if (err != 0)
            {
                throw new InvalidOperationException("TransformProcessType returned " + err + ".");
            }

            err = SetFrontProcess(ref psn);
            if (err != 0)
            {
                throw new InvalidOperationException("SetFrontProcess returned " + err + ".");
            }

            // Load the nib and run the main event loop.
            NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
            App      app  = new App("MainMenu.nib");
            pool.release();

            app.Run();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
コード例 #20
0
    public void RefCount1Test()
    {
        NSObject pool = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));

        // If we use alloc the object will have a ref count of one.
        NSObject instance = (NSObject) new Class("NSHashTable").Call("alloc").Call("init");

        Assert.AreEqual(1L, instance.retainCount());

        // Classes always have a very high retain count (because they
        // are not supposed to go away).
        Class nsSignature = new Class("NSMethodSignature");

        Assert.IsTrue(nsSignature.retainCount() > 1000);

        // If alloc, new, or copy aren't used then the pool owns the object.
        Class    nsString = new Class("NSString");
        NSObject str      = (NSObject)nsString.Call("stringWithUTF8String:", Marshal.StringToHGlobalAuto("hello"));

        Assert.AreEqual(1L, str.retainCount());

        // We can have two managed instances on the same native instance
        // and the ref count doesn't change.
        NSObject copy = new NSObject((IntPtr)instance);

        Assert.AreEqual(1L, copy.retainCount());

        // If we send a message to an object its retain count doesn't change.
        instance.Call("description");
        Assert.AreEqual(1L, instance.retainCount());

        pool.release();

        // Verify our counts after we empty the release pool.
        Assert.AreEqual(1L, instance.retainCount());
        Assert.AreEqual(1L, copy.retainCount());
    }
コード例 #21
0
ファイル: ExportTests.cs プロジェクト: divyang4481/mobjc
 public void Init()
 {
     Registrar.CanInit = true;
     m_pool            = new NSObject(NSObject.AllocAndInitInstance("NSAutoreleasePool"));
 }