c# - 如何(单元)测试弱引用列表的内存管理功能?

标签 c# unit-testing memory-management garbage-collection nunit

标题不好的借口:如果有人能更好地描述它,请做。

我有一个 WeakList<T>类,它“基本上”是一个 List<WeakReference<T>> (虽然不是字面意义上的派生自列表,但它应该对用户完全透明)。

现在的基本思想是“如果引用的变量消失,项目将被忽略”,即 WeakList<T> 的一部分来源是:

public class WeakList<T> : IReadOnlyList<T>, IWeakList<T>, IWeakCollection<T>, IReadOnlyCollection<T>,
    ICollection<T>, IEnumerable<T>, IEnumerable where T : class
{
    private readonly List<WeakReference<T>> _myList;
    public void Add(T item) {
        _myList.Add(new WeakReference<T>(item));
    }
    IEnumerator<T> IEnumerable<T>.GetEnumerator() {
        foreach (var reference in _myList) {
            T elem;
            if (reference.TryGetTarget(out elem)) {
                yield return elem;
            }
        }
    }
}

我还在考虑是否List实际上在这里是有道理的, weaklist[n] 并不总是与通过迭代到第 n 个元素得到它一样。 - 对于有顺序但没有基于索引的访问的东西是否有集合名称?

但除此之外: 此类还包括一个“清除”功能,旨在“删除不再存在的引用”。

    public void Purge() {
        for (int i = _myList.Count - 1; i >= 0; i--) {
            T elem;
            if (!_myList[i].TryGetTarget(out elem)) {
                _myList.RemoveAt(i);
            }
        }
    }

现在我的问题是:如何在单元测试(对于 Nunit)中测试上述方法?我如何设置一个列表,然后确保删除了一些元素,然后调用清除并测试它,例如:

[TestFixture]
public class WeakListTests
{
    [Test]
    public static void PurgeTest() {
        var myList = new WeakList<string>;
        string s1 = "hello world 1";
        string s2 = "hello world 2";
        string s3 = "hello world 3";
        myList.Add(s1);
        myList.Add(s2);
        myList.Add(s3);
        // "clear", force s1 & s2 away. (yet keep a reference so I can test it gets removed)
        myList.Purge();
        Assert.That(myList, Does.Not.Contain(s1));
        Assert.That(myList, Does.Not.Contain(s2));
        Assert.That(myList, Does.Contain(s3));

    }
}

评论描述了我卡在什么地方。

最佳答案

你可以这样做(见评论):

[TestFixture]
public class WeakListTests
{
    [Test]
    public static void PurgeTest()
    {
        var myList = new WeakList<string>();
        // construct strings like this to prevent interning
        string s1 = new string(new[] { 'h', 'e', 'l', 'l', 'o' });
        string s2 = new string(new[] { 'h', 'e', 'l', 'l', 'o', '2' });
        // this string can be interned, we don't want it to be collected anyway
        string s3 = "hello world 3";            
        myList.Add(s1);
        myList.Add(s2);
        myList.Add(s3);
        // set to null for that to work even in application built with "Debug"
        // in Release it will work without setting to null
        s1 = null;
        s2 = null;
        // force GC collection
        GC.Collect(2, GCCollectionMode.Forced);            
        // now s1 and s2 are away
        myList.Purge();
        // invoke your enumerator
        var left = myList.ToArray();            
        // should contain 1 item and that item should be s3
        Assert.That(left.Length == 1);
        Assert.AreEqual(s3, left[0]);            
    }
}

关于c# - 如何(单元)测试弱引用列表的内存管理功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886982/

相关文章:

c# - 如何从 C# 调用 Microsoft office outlook 宏

c# - Rhino Mocks AssertWasCalled(多次)使用 AAA 获取属性

python - 检查功能是否会通过 Nose 测试发出警告

c++ - 关于内存池的问题

c - 如果在函数内部调用 free() ,它是否只在本地释放?

c# - 生成 soap xml 消息的最佳方式?

C# Databound ComboBox 自动更新

c# - 如何在 Windows Phone 上优化带有图像的长列表选择器的性能?

oracle - 有没有办法访问私有(private) plsql 程序以进行测试?

c - scanf 不断取值(输入)