Guest User

C# DotMemoryUnit

a guest
Apr 15th, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using JetBrains.dotMemoryUnit;
  3. using JetBrains.dotMemoryUnit.Kernel;
  4. using NUnit.Framework;
  5.  
  6. namespace DotMemoryUnitTests
  7. {
  8.   public class CustomTestCaseAttribute : TestCaseAttribute
  9.   {
  10.   }
  11.  
  12.   public class DotMemoryUnit1 : IDisposable
  13.   {
  14.     public static IDisposable CollectAllocations { get { return new DotMemoryUnit1(true); } }
  15.     public static IDisposable DoNotCollectAllocations { get { return new DotMemoryUnit1(false); } }
  16.  
  17.     private DotMemoryUnit1(bool collectAllocations)
  18.     {
  19.       dotMemoryApi.CollectAllocations = collectAllocations;
  20.       DotMemoryUnitController.TestStart();
  21.     }
  22.  
  23.     public void Dispose()
  24.     {
  25.       DotMemoryUnitController.TestEnd();
  26.       dotMemoryApi.CollectAllocations = false;
  27.     }
  28.   }
  29.  
  30.   public class DotMemoryUnit2 : IDisposable
  31.   {
  32.     public static IDisposable CollectAllocations { get { return new DotMemoryUnit2(true); } }
  33.     public static IDisposable DoNotCollectAllocations { get { return new DotMemoryUnit2(false); } }
  34.  
  35.     private DotMemoryUnit2(bool collectAllocations)
  36.     {
  37.       Console.WriteLine("11111");
  38.       dotMemoryApi.CollectAllocations = collectAllocations;
  39.       DotMemoryUnitController.TestStart();
  40.       Console.WriteLine("22222");
  41.     }
  42.  
  43.     public void Dispose()
  44.     {
  45.       Console.WriteLine("33333");
  46.       DotMemoryUnitController.TestEnd();
  47.       dotMemoryApi.CollectAllocations = false;
  48.       Console.WriteLine("44444");
  49.     }
  50.   }
  51.  
  52.   public class TestProgram
  53.   {
  54.     [CustomTestCaseAttribute]
  55.     [AssertTraffic(AllocatedSizeInBytes = 100)]
  56.     public void UsingDotMemoryUnit1()
  57.     {
  58.       using (DotMemoryUnit1.CollectAllocations)
  59.       {
  60.         var memoryCheckPoint1 = dotMemory.Check();
  61.  
  62.         Count();
  63.  
  64.         var memoryCheckPoint2 = dotMemory.Check();
  65.       }
  66.     }
  67.  
  68.     [CustomTestCaseAttribute]
  69.     [AssertTraffic(AllocatedSizeInBytes = 100)]
  70.     public void UsingDotMemoryUnit2()
  71.     {
  72.       using (DotMemoryUnit2.CollectAllocations)
  73.       {
  74.         var memoryCheckPoint1 = dotMemory.Check();
  75.  
  76.         Count();
  77.  
  78.         var memoryCheckPoint2 = dotMemory.Check();
  79.       }
  80.     }
  81.  
  82.     private void Count()
  83.     {
  84.       string[] numbers = { "One", "Two", "Three", "Four", "Five"};
  85.  
  86.       Console.WriteLine("Let's count: " + String.Join(",", numbers));
  87.     }
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment