Tortuga-TestMonkey icon indicating copy to clipboard operation
Tortuga-TestMonkey copied to clipboard

IList<T> Memory Tests

Open Grauenwolf opened this issue 3 years ago • 0 comments

    <text>

    @Tag("IList")
    @Tag("Memory")
	@TestAttribute
    public void @(Test.ClassName + "_ListT_" + itemType.SafeName + "_AddClear_Memory") ()
	{
		IList<@itemType.FullName> objectUnderTest = CreateObject();

		if (objectUnderTest.IsReadOnly)
            return;

		Func<WeakReference> run = () =>
		{
			@itemType.FullName newItem;

			try
			{
				newItem = @itemType.GetConstructorCode();
				objectUnderTest.Add(newItem);
				return new WeakReference(newItem);
			}
			catch (ArgumentException)
			{
				return null;
			}            
		};

		var wr = run();
		if (wr == null)
			return; //test inconclusive

		//release strong references
		objectUnderTest.Clear();
		
        GC.Collect();
		GC.WaitForPendingFinalizers();
		GC.Collect();

		//verify the object was collected
		Assert.IsFalse(wr.IsAlive);

	}


    @Tag("IList")
    @Tag("Memory")
    @TestAttribute
    public void @(Test.ClassName + "_ListT_" + itemType.SafeName + "_AddRemove_Memory") ()
	{
		IList<@itemType.FullName> objectUnderTest = CreateObject();

		if (objectUnderTest.IsReadOnly)
            return;

        var oldCount = objectUnderTest.Count;
        
		Func<WeakReference> run = () =>
		{
			@itemType.FullName newItem;

			try
			{
				newItem = @itemType.GetConstructorCode();
				objectUnderTest.Add(newItem);
				return new WeakReference(newItem);
			}
			catch (ArgumentException)
			{
				return null;
			}            
		};

		var wr = run();
		if (wr == null)
			return; //test inconclusive

		//release strong references
		Action run2 = () => {
			objectUnderTest.Remove((@itemType.FullName)wr.Target);
		};
		
		run2();

        GC.Collect();
		GC.WaitForPendingFinalizers();
		GC.Collect();

		//verify the object was collected
		Assert.IsFalse(wr.IsAlive);		
    }

    @Tag("IList")
    @Tag("Memory")
    @TestAttribute
    public void @(Test.ClassName + "_ListT_" + itemType.SafeName + "_AddRemoveAt_Memory") ()
	{
		IList<@itemType.FullName> objectUnderTest = CreateObject();

		if (objectUnderTest.IsReadOnly)
            return;

		Func<WeakReference> run = () =>
		{
			@itemType.FullName newItem;

			try
			{
				newItem = @itemType.GetConstructorCode();
				objectUnderTest.Add(newItem);
				return new WeakReference(newItem);
			}
			catch (ArgumentException)
			{
				return null;
			}            
		};

		var wr = run();
		if (wr == null)
			return; //test inconclusive

		//release strong references
		//release strong references
		Action run2 = () => {
			var newIndex = objectUnderTest.IndexOf((@itemType.FullName)wr.Target);
			objectUnderTest.RemoveAt(newIndex);
		};
		
		run2();
		
        GC.Collect();
		GC.WaitForPendingFinalizers();
		GC.Collect();

		//verify the object was collected
		Assert.IsFalse(wr.IsAlive);		
	}


    </text>

Grauenwolf avatar Jun 02 '21 01:06 Grauenwolf