Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IList<T> Memory Tests #11

Open
Grauenwolf opened this issue Jun 2, 2021 · 0 comments
Open

IList<T> Memory Tests #11

Grauenwolf opened this issue Jun 2, 2021 · 0 comments

Comments

@Grauenwolf
Copy link
Owner

    <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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant