[9000] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9000] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 26 |
|
---|
[9764] | 27 | namespace HeuristicLab.Collections.Tests {
|
---|
[9000] | 28 | [TestClass]
|
---|
| 29 | public class BidirectionalDictionaryTest {
|
---|
| 30 | private class ComplexType {
|
---|
| 31 | public readonly int Field;
|
---|
| 32 | public ComplexType(int field) {
|
---|
| 33 | Field = field;
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | private class ComplexTypeEqualityComparer : EqualityComparer<ComplexType> {
|
---|
| 38 | public override bool Equals(ComplexType x, ComplexType y) {
|
---|
| 39 | return x.Field == y.Field;
|
---|
| 40 | }
|
---|
| 41 | public override int GetHashCode(ComplexType obj) {
|
---|
| 42 | return obj.Field.GetHashCode();
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | [TestMethod]
|
---|
[9783] | 47 | [TestCategory("General")]
|
---|
| 48 | [TestProperty("Time", "short")]
|
---|
[9000] | 49 | public void TestBidirectionalDictionary() {
|
---|
| 50 | var dict1 = new BidirectionalDictionary<int, double>();
|
---|
| 51 | dict1.Add(4, 2.0);
|
---|
| 52 | Assert.IsTrue(dict1.ContainsFirst(4) && dict1.ContainsSecond(2));
|
---|
| 53 | bool exceptionOnDuplicate = false;
|
---|
| 54 | try {
|
---|
| 55 | dict1.Add(4, 3.0);
|
---|
[9012] | 56 | }
|
---|
| 57 | catch (ArgumentException) { exceptionOnDuplicate = true; }
|
---|
[9000] | 58 | Assert.IsTrue(exceptionOnDuplicate);
|
---|
| 59 | Assert.IsTrue(dict1.GetByFirst(4) == 2);
|
---|
| 60 | Assert.IsTrue(dict1.GetBySecond(2) == 4);
|
---|
| 61 | Assert.IsTrue(dict1.Count == 1);
|
---|
| 62 | dict1.Clear();
|
---|
| 63 | Assert.IsTrue(dict1.Count == 0);
|
---|
| 64 |
|
---|
| 65 | var dict2 = new BidirectionalDictionary<ComplexType, int>(new ComplexTypeEqualityComparer());
|
---|
[9012] | 66 | Assert.IsTrue(!dict2.Any());
|
---|
[9000] | 67 | dict2.Add(new ComplexType(1), 2);
|
---|
[9012] | 68 | Assert.IsTrue(dict2.Any());
|
---|
[9000] | 69 | dict2.Add(new ComplexType(2), 1);
|
---|
| 70 | Assert.IsTrue(dict2.ContainsFirst(new ComplexType(2)));
|
---|
| 71 | Assert.IsTrue(dict2.ContainsSecond(2));
|
---|
| 72 | exceptionOnDuplicate = false;
|
---|
| 73 | try {
|
---|
| 74 | dict2.Add(new ComplexType(2), 3);
|
---|
[9012] | 75 | }
|
---|
| 76 | catch (ArgumentException) { exceptionOnDuplicate = true; }
|
---|
[9000] | 77 | Assert.IsTrue(exceptionOnDuplicate);
|
---|
| 78 | exceptionOnDuplicate = false;
|
---|
| 79 | try {
|
---|
| 80 | dict2.Add(new ComplexType(3), 1);
|
---|
[9012] | 81 | }
|
---|
| 82 | catch (ArgumentException) { exceptionOnDuplicate = true; }
|
---|
[9000] | 83 | Assert.IsTrue(exceptionOnDuplicate);
|
---|
| 84 | Assert.IsTrue(dict2.Count == 2);
|
---|
| 85 | Assert.IsTrue(dict2.GetBySecond(1).Field == 2);
|
---|
| 86 | Assert.IsTrue(dict2.GetByFirst(new ComplexType(1)) == 2);
|
---|
| 87 | dict2.Clear();
|
---|
[9012] | 88 | Assert.IsTrue(!dict2.Any());
|
---|
[9000] | 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|