public void ThrowOnStringConcatenation()
        {
            using (MemoryRestrictor.StartNoAlloc())
            {
                String s = String.Concat("A", "B");

                MemoryRestrictor.EndNoAlloc();
                Assert.AreEqual(s.Length, 2);
            }
        }
        public void ThrowOnBoxing()
        {
            using (MemoryRestrictor.StartNoAlloc())
            {
                int    intValue = 0x123123;
                Object boxed    = (Object)intValue;

                MemoryRestrictor.EndNoAlloc();
                Assert.IsTrue(String.Concat(boxed) == "123123");
            }
        }
        public void ThrowOnAllocation()
        {
            using (MemoryRestrictor.StartNoAlloc())
            {
                Object obj = new Object();

                MemoryRestrictor.EndNoAlloc();
                // Need to use 'obj' variable to prevent being optimized out.
                Assert.IsNotNull(obj);
            }
        }
Beispiel #4
0
        public void Int32BoxingTest()
        {
            using (MemoryRestrictor.StartNoAlloc())
            {
                int val = 12345678;

                using (ClassAllocator.Box(val, out Object boxed))
                {
                    MemoryRestrictor.EndNoAlloc();
                    Assert.AreEqual(val, boxed);
                }
            }
        }
Beispiel #5
0
        public void UninitializedAllocationTest()
        {
            Object original = FormatterServices.GetUninitializedObject(typeof(DummyClass));

            using (MemoryRestrictor.StartNoAlloc())
            {
                using (ClassAllocator.UninitializedAllocation(out DummyClass uheapObj))
                {
                    MemoryRestrictor.EndNoAlloc();

                    Assert.AreEqual(DummyClass.UninitializedIntField, uheapObj.IntField);
                    Assert.AreEqual(original, uheapObj);
                }
            }
        }
        public void ThrowOnLinq()
        {
            int[] array = new int[] { 1, 2, 3, 4 };

            using (MemoryRestrictor.StartNoAlloc())
            {
                // Enumerable.Sum(this IEnumerable`1) extension method internally uses
                // IEnumerable`1.GetEnumerator, and invoking IEnumerable`1.GetEnumerator
                // on array makes allocation. (See SZArrayHelper.GetEnumerator method)
                int sum = array.Sum();

                MemoryRestrictor.EndNoAlloc();
                Assert.AreEqual(sum, 10);
            }
        }