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); } }
public static void AssemblyInitialize(TestContext tc) { try { MemoryRestrictor.Hijack(); } catch { MemoryRestrictor.Restore(); throw; } }
public void Int32BoxingTest() { using (MemoryRestrictor.StartNoAlloc()) { int val = 12345678; using (ClassAllocator.Box(val, out Object boxed)) { MemoryRestrictor.EndNoAlloc(); Assert.AreEqual(val, boxed); } } }
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))
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); } }
public static void AssemblyCleanup() { MemoryRestrictor.Restore(); }