Ejemplo n.º 1
0
        public void ThrowOnStringConcatenation()
        {
            using (MemoryRestrictor.StartNoAlloc())
            {
                String s = String.Concat("A", "B");

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

                MemoryRestrictor.EndNoAlloc();
                Assert.IsTrue(String.Concat(boxed) == "123123");
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
 public static void AssemblyInitialize(TestContext tc)
 {
     try
     {
         MemoryRestrictor.Hijack();
     }
     catch
     {
         MemoryRestrictor.Restore();
         throw;
     }
 }
Ejemplo n.º 5
0
        public void Int32BoxingTest()
        {
            using (MemoryRestrictor.StartNoAlloc())
            {
                int val = 12345678;

                using (ClassAllocator.Box(val, out Object boxed))
                {
                    MemoryRestrictor.EndNoAlloc();
                    Assert.AreEqual(val, boxed);
                }
            }
        }
Ejemplo n.º 6
0
        public void PrimitiveOperation()
        {
            Random rd   = new Random();
            int    size = rd.Next(1000, 2000);

            int[] original = new int[size];

            for (int idx = 0; idx < original.Length; idx++)
            {
                original[idx] = rd.Next();
            }

            using (MemoryRestrictor.StartNoAlloc())
                using (ClassAllocator.AllocateSZArray(size, out int[] arr))
Ejemplo n.º 7
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);
                }
            }
        }
Ejemplo n.º 8
0
        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);
            }
        }
Ejemplo n.º 9
0
 public static void AssemblyCleanup()
 {
     MemoryRestrictor.Restore();
 }