⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multidictionarybasetests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 3 页
字号:
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement 
// contained in the file "License.txt" accompanying this file.
//******************************

// Still need additional tests on ReadOnlyMultiDictionaryBase.

using System;
using System.Collections.Generic;
using System.Collections;
using NUnit.Framework;
using Wintellect.PowerCollections;

namespace Wintellect.PowerCollections.Tests
{
    class ReadWriteTestMultiDictionary<TKey, TValue> : MultiDictionaryBase<TKey, TValue>
    {
        List<TKey> keys;
        List<List<TValue>> values;

        public ReadWriteTestMultiDictionary(List<TKey> keys, List<List<TValue>> values)
        {
            this.keys = keys;
            this.values = values;
        }

        public override void Clear()
        {
            keys = new List<TKey>();
            values = new List<List<TValue>>();
        }

        public override int Count
        {
            get
            {
                return keys.Count;
            }
        }

        public override void Add(TKey key, TValue value)
        {
            int index = keys.IndexOf(key);
            if (index >= 0) {
                values[index].Add(value);
            }
            else {
                keys.Add(key);
                values.Add(new List<TValue>(new TValue[] { value }));
            }
        }

        public override bool Remove(TKey key)
        {
            int index = keys.IndexOf(key);

            if (index >= 0) {
                keys.RemoveAt(index);
                values.RemoveAt(index);
                return true;
            }
            else {
                return false;
            }
        }

        public override bool Remove(TKey key, TValue value)
        {
            int index = keys.IndexOf(key);

            if (index >= 0) {
                int valIndex = values[index].IndexOf(value);
                if (valIndex >= 0) {
                    values[index].RemoveAt(valIndex);
                    if (values[index].Count == 0)
                        Remove(key);
                    return true;
                }
            }

            return false;
        }

        public override bool Contains(TKey key, TValue value)
        {
            int index = keys.IndexOf(key);

            if (index >= 0) {
                int valIndex = values[index].IndexOf(value);
                if (valIndex >= 0) {
                    return true;
                }
            }

            return false;
        }

        protected override bool TryEnumerateValuesForKey(TKey key, out IEnumerator<TValue> values)
        {
            int index = keys.IndexOf(key);

            if (index >= 0) {
                values = this.values[index].GetEnumerator();
                return true;
            }
            else {
                values = null;
                return false;
            }
        }

        protected override IEnumerator<TKey> EnumerateKeys()
        {
            for (int i = 0; i < keys.Count; ++i) 
                yield return keys[i];
        }
    }

    class ReadOnlyTestMultiDictionary<TKey, TValue> : ReadOnlyMultiDictionaryBase<TKey, TValue>
    {
        List<TKey> keys;
        List<List<TValue>> values;

        public ReadOnlyTestMultiDictionary(List<TKey> keys, List<List<TValue>> values)
        {
            this.keys = keys;
            this.values = values;
        }

        public override int Count
        {
            get
            {
                return keys.Count;
            }
        }

        public override bool Contains(TKey key, TValue value)
        {
            int index = keys.IndexOf(key);

            if (index >= 0) {
                int valIndex = values[index].IndexOf(value);
                if (valIndex >= 0) {
                    return true;
                }
            }

            return false;
        }

        protected override bool TryEnumerateValuesForKey(TKey key, out IEnumerator<TValue> values)
        {
            int index = keys.IndexOf(key);

            if (index >= 0) {
                values = this.values[index].GetEnumerator();
                return true;
            }
            else {
                values = null;
                return false;
            }
        }

        protected override IEnumerator<TKey> EnumerateKeys()
        {
            for (int i = 0; i < keys.Count; ++i)
                yield return keys[i];
        }
    }

    [TestFixture]
    public class MultiDictionaryBaseTests
    {
        // Check the contents of a Multi-Dictionary non-destructively. Keys and Values must be in order.
        internal static void CheckOrderedMultiDictionaryContents<TKey, TValue>(MultiDictionaryBase<TKey, TValue> dict, TKey[] keys, TValue[][] values, TKey nonKey, TValue nonValue, BinaryPredicate<TKey> keyEquals, BinaryPredicate<TValue> valueEquals)
        {
            int iKey, iValue;
            ICollection<TValue> getValues;

            if (keyEquals == null)
                keyEquals = delegate(TKey x, TKey y) { return object.Equals(x, y); };
            if (valueEquals == null)
                valueEquals = delegate(TValue x, TValue y) { return object.Equals(x, y); };

            // Check Count.
            Assert.AreEqual(keys.Length, dict.Count);

            // Check indexer, ContainsKey, Contains, TryGetValue for each key.
            for (iKey = 0; iKey < keys.Length; ++iKey) {
                Assert.IsTrue(dict.ContainsKey(keys[iKey]));
                Assert.IsTrue(dict.Contains(new KeyValuePair<TKey, ICollection<TValue>>(keys[iKey], values[iKey])));

                bool b = ((IDictionary<TKey, ICollection<TValue>>)dict).TryGetValue(keys[iKey], out getValues);
                Assert.IsTrue(b);
                iValue = 0;
                foreach(TValue val in getValues) {
                    Assert.IsTrue(valueEquals(values[iKey][iValue], val));
                    ++iValue;
                }

                iValue = 0;
                foreach (TValue val in values[iKey]) {
                    Assert.IsTrue(dict.Contains(keys[iKey], val));
                    ++iValue;
                }

                iValue = 0;
                foreach (TValue val in dict[keys[iKey]]) {
                    Assert.IsTrue(valueEquals(values[iKey][iValue], val));
                    ++iValue;
                }
                Assert.IsTrue(iValue == values[iKey].Length);
            }

            // Check Keys collection.
            iKey = 0;
            foreach (TKey key in dict.Keys) {
                Assert.IsTrue(keyEquals(keys[iKey], key));
                ++iKey;
            }
            Assert.IsTrue(iKey == keys.Length);
            InterfaceTests.TestReadonlyCollectionGeneric<TKey>(dict.Keys, keys, true, null);

            // Check Values collection
            iKey = 0; iValue = 0;
            int valueCount = 0;
            foreach (TValue val in dict.Values) {
                Assert.IsTrue(valueEquals(values[iKey][iValue], val));
                ++iValue;
                if (iValue == values[iKey].Length) {
                    iValue = 0;
                    ++iKey;
                }
                ++valueCount;
            }
            Assert.IsTrue(iKey == keys.Length);

            int a = 0;
            TValue[] vals = new TValue[valueCount];
            for (iKey = 0; iKey < keys.Length; ++iKey) {
                for (iValue = 0; iValue < values[iKey].Length; ++iValue) {
                    vals[a++] = values[iKey][iValue];
                }
            }
            InterfaceTests.TestReadonlyCollectionGeneric<TValue>(dict.Values, vals, true, null);

            // Check KeyValuePairs collection.
            iKey = 0; iValue = 0;
            valueCount = 0;
            foreach (KeyValuePair<TKey,TValue> pair in dict.KeyValuePairs) {
                Assert.IsTrue(keyEquals(keys[iKey], pair.Key));
                Assert.IsTrue(valueEquals(values[iKey][iValue], pair.Value));
                ++iValue;
                if (iValue == values[iKey].Length) {
                    iValue = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -