DictionaryBaseTests.cs :  » Development » TULP2G » Wintellect » PowerCollections » Tests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » TULP2G 
TULP2G » Wintellect » PowerCollections » Tests » DictionaryBaseTests.cs
//******************************
// 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.
//******************************

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

namespace Wintellect.PowerCollections.Tests{
    // A simple read-only dictionary.
    class ReadOnlyTestDictionary<TKey, TValue> : ReadOnlyDictionaryBase<TKey, TValue>
    {
        private TKey[] keys;
        private TValue[] values;

        public ReadOnlyTestDictionary(TKey[] keys, TValue[] values) 
        {
            this.keys = keys;
            this.values = values;
        }

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

        public override IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            for (int i = 0; i < keys.Length; ++i)
                yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
        }

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

            if (index >= 0) {
                value = values[index];
                return true;
            }
            else {
                value = default(TValue);
                return false;
            }
        }
    }

    // A simple read-write dictionary.
    class ReadWriteTestDictionary<TKey, TValue> : DictionaryBase<TKey, TValue>
    {
        private List<TKey> keys;
        private List<TValue> values;

        public ReadWriteTestDictionary(TKey[] keys, TValue[] values) 
        {
            this.keys = new List<TKey>(keys);
            this.values = new List<TValue>(values);
        }

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

        public override IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            for (int i = 0; i < keys.Count; ++i)
                yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
        }

        public override bool TryGetValue(TKey key, out TValue value)
        {
            int index = keys.IndexOf(key);
            if (index < 0) {
                value = default(TValue);
                return false;
            }
            else {
                value = values[index];
                return true;
            }
        }

        public override TValue this[TKey key]
        {
            set
            {
                int index = keys.IndexOf(key);
                if (index < 0) {
                    keys.Add(key);
                    values.Add(value);
                }
                else
                    values[index] = value;
            }
        }

        public override bool Remove(TKey key)
        {
            int index = keys.IndexOf(key);
            if (index < 0) {
                return false;
            }
            else {
                keys.RemoveAt(index);
                values.RemoveAt(index);
                return true;
            }
        }

        public override void Clear()
        {
            keys.Clear();
            values.Clear();
        }
    }

    [TestFixture]
    public class DictionaryBaseTests
    {
        [Test]
        public void ReadOnlyDictionary()
        {
            string[] s_array = { "Eric", "Clapton", "Rules", "The", "World" };
            int[] i_array = { 1, 5, 6, 5, 19 };

            ReadOnlyTestDictionary<string, int> dict = new ReadOnlyTestDictionary<string, int>(s_array, i_array);

            InterfaceTests.TestReadOnlyDictionary<string, int>(dict, s_array, i_array, "foo", true, "ReadOnlyTestDictionary");
            InterfaceTests.TestReadOnlyDictionary<string, int>(dict, s_array, i_array, "foo", false, "ReadOnlyTestDictionary");
            InterfaceTests.TestReadOnlyDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", true, "ReadOnlyTestDictionary", null, null);
            InterfaceTests.TestReadOnlyDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", false, "ReadOnlyTestDictionary", null, null);
        }

        [Test]
        public void ReadWriteDictionary()
        {
            string[] s_array = { "Eric", "Clapton", "Rules", "The", "World" };
            int[] i_array = { 1, 5, 6, 5, 19 };

            ReadWriteTestDictionary<string, int> dict = new ReadWriteTestDictionary<string, int>(s_array, i_array);

            InterfaceTests.TestReadWriteDictionary<string, int>(dict, s_array, i_array, "foo", true, "ReadOnlyTestDictionary");
            InterfaceTests.TestReadWriteDictionary<string, int>(dict, s_array, i_array, "foo", false, "ReadOnlyTestDictionary");
            InterfaceTests.TestReadWriteDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", true, "ReadOnlyTestDictionary", null, null);
            InterfaceTests.TestReadWriteDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", false, "ReadOnlyTestDictionary", null, null);
        }

        [Test]
        public void ConvertToString()
        {
            string[] s_array = { "Eric", "Clapton", null, "The", "World" };
            int[] i_array = { 1, 5, 6, 5, 19 };
            string s;

            ReadWriteTestDictionary<string, int> dict1 = new ReadWriteTestDictionary<string, int>(s_array, i_array);
            s = dict1.ToString();
            Assert.AreEqual("{Eric->1, Clapton->5, null->6, The->5, World->19}", s);

            ReadOnlyTestDictionary<int, string> dict2 = new ReadOnlyTestDictionary<int, string>(i_array, s_array);
            s = dict2.ToString();
            Assert.AreEqual("{1->Eric, 5->Clapton, 6->null, 5->The, 19->World}", s);
        }

        [Test]
        public void DebuggerDisplayString()
        {
            string[] s_array = { "Eric", "Clapton", null, "The", "World" };
            int[] i_array = { 1, 5, 6, 5, 19 };
            string s;

            ReadWriteTestDictionary<string, int> dict1 = new ReadWriteTestDictionary<string, int>(s_array, i_array);
            s = dict1.ToString();
            Assert.AreEqual("{Eric->1, Clapton->5, null->6, The->5, World->19}", s);

            ReadOnlyTestDictionary<int, string> dict2 = new ReadOnlyTestDictionary<int, string>(i_array, s_array);
            s = dict2.ToString();
            Assert.AreEqual("{1->Eric, 5->Clapton, 6->null, 5->The, 19->World}", s);

            string[] s_big = new string[1000];
            int[] i_big = new int[1000];

            for (int i = 0; i < i_big.Length; ++i) {
                i_big[i] = i * 2 + 1;
                s_big[i] = "foo" + i.ToString() + "bar";
            }

            string expected = "{1->foo0bar, 3->foo1bar, 5->foo2bar, 7->foo3bar, 9->foo4bar, 11->foo5bar, 13->foo6bar, 15->foo7bar, 17->foo8bar, 19->foo9bar, 21->foo10bar, 23->foo11bar, 25->foo12bar, 27->foo13bar, 29->foo14bar, 31->foo15bar, 33->foo16bar, 35->foo17bar, 37->foo18bar, ...}";
 
            ReadWriteTestDictionary<int,string> dict3 = new ReadWriteTestDictionary<int, string>(i_big,s_big);
            s = dict3.DebuggerDisplayString();
            Assert.AreEqual(expected, s);

            ReadOnlyTestDictionary<int, string> dict4 = new ReadOnlyTestDictionary<int, string>(i_big,s_big);
            s = dict4.DebuggerDisplayString();
            Assert.AreEqual(expected, s);
        }

        [Test]
        public void TestDictionary()
        {
            string[] s_array = { "Eric", "Clapton", "Rules", "The", "World" };
            int[] i_array = { 1, 5, 6, 5, 19 };

            Dictionary<string, int> dict = new Dictionary<string, int>();
            for (int i = 0; i < s_array.Length; ++i)
                dict[s_array[i]] = i_array[i];

            InterfaceTests.TestDictionary<string, int>(dict, s_array, i_array, "foo", false);
            InterfaceTests.TestDictionaryGeneric<string, int>(dict, s_array, i_array, "foo", false, null, null);
        }

        [Test]
        public void AsReadOnly()
        {
            string[] s_array = { "Eric", "Clapton", null, "The", "World" };
            int[] i_array = { 1, 5, 6, 5, 19 };

            ReadWriteTestDictionary<string, int> dict1 = new ReadWriteTestDictionary<string, int>(s_array, i_array);
            IDictionary<string, int> dict2 = dict1.AsReadOnly();

            InterfaceTests.TestReadOnlyDictionaryGeneric<string, int>(dict2, s_array, i_array, "foo", true, null, null, null);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.