[9000] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 25 |
|
---|
[9764] | 26 | namespace HeuristicLab.Collections.Tests {
|
---|
[9000] | 27 | [TestClass]
|
---|
| 28 | public class BidirectionalLookupTest {
|
---|
| 29 | private class ComplexType {
|
---|
| 30 | public readonly int Field;
|
---|
| 31 | public ComplexType(int field) {
|
---|
| 32 | Field = field;
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | private class ComplexTypeEqualityComparer : EqualityComparer<ComplexType> {
|
---|
| 37 | public override bool Equals(ComplexType x, ComplexType y) {
|
---|
| 38 | return x.Field == y.Field;
|
---|
| 39 | }
|
---|
| 40 | public override int GetHashCode(ComplexType obj) {
|
---|
| 41 | return obj.Field.GetHashCode();
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [TestMethod]
|
---|
[9783] | 46 | [TestCategory("General")]
|
---|
| 47 | [TestProperty("Time", "short")]
|
---|
[9000] | 48 | public void TestBidirectionalLookup() {
|
---|
| 49 | var lookup1 = new BidirectionalLookup<int, double>();
|
---|
| 50 | lookup1.Add(4, 2);
|
---|
| 51 | Assert.IsTrue(lookup1.ContainsFirst(4) && lookup1.ContainsSecond(2));
|
---|
| 52 | lookup1.Add(4, 3);
|
---|
| 53 | Assert.IsTrue(lookup1.GetByFirst(4).Count() == 2);
|
---|
| 54 | Assert.IsTrue(lookup1.GetBySecond(2).Contains(4));
|
---|
| 55 | Assert.IsTrue(lookup1.CountFirst == 1);
|
---|
| 56 | Assert.IsTrue(lookup1.CountSecond == 2);
|
---|
| 57 | lookup1.Add(3, 2);
|
---|
| 58 | lookup1.Add(2, 2);
|
---|
| 59 | lookup1.Add(1, 2);
|
---|
| 60 | Assert.IsTrue(lookup1.GetByFirst(1).Single() == 2);
|
---|
| 61 | Assert.IsTrue(lookup1.GetBySecond(2).Count() == 4);
|
---|
| 62 | lookup1.RemovePair(2, 2);
|
---|
| 63 | Assert.IsTrue(!lookup1.ContainsFirst(2));
|
---|
| 64 | Assert.IsTrue(lookup1.GetBySecond(2).Count() == 3);
|
---|
| 65 | lookup1.RemoveByFirst(4);
|
---|
| 66 | Assert.IsTrue(lookup1.CountFirst == 2);
|
---|
| 67 | Assert.IsTrue(lookup1.CountSecond == 1);
|
---|
| 68 | lookup1.RemoveBySecond(2);
|
---|
| 69 | Assert.IsTrue(lookup1.CountFirst == 0);
|
---|
| 70 | Assert.IsTrue(lookup1.CountSecond == 0);
|
---|
| 71 | lookup1.Clear();
|
---|
| 72 | Assert.IsTrue(lookup1.CountFirst == 0);
|
---|
| 73 | Assert.IsTrue(lookup1.CountSecond == 0);
|
---|
| 74 |
|
---|
| 75 | var dict2 = new BidirectionalLookup<ComplexType, int>(new ComplexTypeEqualityComparer());
|
---|
| 76 | Assert.IsTrue(!dict2.FirstEnumerable.Any());
|
---|
| 77 | dict2.Add(new ComplexType(1), 2);
|
---|
| 78 | Assert.IsTrue(dict2.SecondEnumerable.Any());
|
---|
| 79 | dict2.Add(new ComplexType(2), 1);
|
---|
| 80 | Assert.IsTrue(dict2.ContainsFirst(new ComplexType(2)));
|
---|
| 81 | Assert.IsTrue(dict2.ContainsSecond(2));
|
---|
| 82 | dict2.Add(new ComplexType(2), 3);
|
---|
| 83 | Assert.IsTrue(dict2.GetByFirst(new ComplexType(2)).Count() == 2);
|
---|
| 84 | Assert.IsTrue(dict2.GetByFirst(new ComplexType(2)).Contains(1));
|
---|
| 85 | Assert.IsTrue(dict2.GetByFirst(new ComplexType(2)).Contains(3));
|
---|
| 86 | dict2.Add(new ComplexType(3), 1);
|
---|
| 87 | Assert.IsTrue(dict2.GetBySecond(1).Count() == 2);
|
---|
| 88 | Assert.IsTrue(dict2.GetBySecond(1).Any(x => x.Field == 2));
|
---|
| 89 | Assert.IsTrue(dict2.GetBySecond(1).Any(x => x.Field == 3));
|
---|
| 90 | Assert.IsTrue(dict2.CountFirst == 3);
|
---|
| 91 | Assert.IsTrue(dict2.CountSecond == 3);
|
---|
| 92 | dict2.Add(new ComplexType(2), 2);
|
---|
| 93 | Assert.IsTrue(dict2.CountFirst == 3);
|
---|
| 94 | Assert.IsTrue(dict2.CountSecond == 3);
|
---|
| 95 | dict2.Add(new ComplexType(2), 4);
|
---|
| 96 | Assert.IsTrue(dict2.CountFirst == 3);
|
---|
| 97 | Assert.IsTrue(dict2.CountSecond == 4);
|
---|
| 98 | dict2.RemoveByFirst(new ComplexType(2));
|
---|
| 99 | Assert.IsTrue(dict2.CountFirst == 2);
|
---|
| 100 | Assert.IsTrue(dict2.CountSecond == 2);
|
---|
| 101 | dict2.RemovePair(new ComplexType(1), 1);
|
---|
| 102 | Assert.IsTrue(dict2.CountFirst == 2);
|
---|
| 103 | Assert.IsTrue(dict2.CountSecond == 2);
|
---|
| 104 | dict2.RemovePair(new ComplexType(1), 2);
|
---|
| 105 | Assert.IsTrue(dict2.CountFirst == 1);
|
---|
| 106 | Assert.IsTrue(dict2.CountSecond == 1);
|
---|
| 107 | dict2.Clear();
|
---|
| 108 | Assert.IsTrue(!dict2.SecondEnumerable.Any());
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|