冠名不当的借口:如果有人可以更好地描述它,请这样做。

我有一个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在这里是否真的有意义,弱列表[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]);
    }
}

10-07 19:22
查看更多