Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Collections-3.3/BidirectionalDictionaryTest.cs @ 9000

Last change on this file since 9000 was 9000, checked in by abeham, 11 years ago

#1991:

  • Added BidirectionalDictionary for storing 1:1 mapping and BidirectionalLookup for storing M:N mappings
    • I used HashSets in the BidirectionalLookup instead of Lists, because of the difficulty of removing items when custom equality comparers are used.
  • Added unit tests
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Collections_33.Tests {
29  [TestClass]
30  public class BidirectionalDictionaryTest {
31    private class ComplexType {
32      public readonly int Field;
33      public ComplexType(int field) {
34        Field = field;
35      }
36    }
37
38    private class ComplexTypeEqualityComparer : EqualityComparer<ComplexType> {
39      public override bool Equals(ComplexType x, ComplexType y) {
40        return x.Field == y.Field;
41      }
42      public override int GetHashCode(ComplexType obj) {
43        return obj.Field.GetHashCode();
44      }
45    }
46
47    [TestMethod]
48    public void TestBidirectionalDictionary() {
49      var dict1 = new BidirectionalDictionary<int, double>();
50      dict1.Add(4, 2.0);
51      Assert.IsTrue(dict1.ContainsFirst(4) && dict1.ContainsSecond(2));
52      bool exceptionOnDuplicate = false;
53      try {
54        dict1.Add(4, 3.0);
55      } catch (ArgumentException) { exceptionOnDuplicate = true; }
56      Assert.IsTrue(exceptionOnDuplicate);
57      Assert.IsTrue(dict1.GetByFirst(4) == 2);
58      Assert.IsTrue(dict1.GetBySecond(2) == 4);
59      Assert.IsTrue(dict1.Count == 1);
60      dict1.Clear();
61      Assert.IsTrue(dict1.Count == 0);
62
63      var dict2 = new BidirectionalDictionary<ComplexType, int>(new ComplexTypeEqualityComparer());
64      Assert.IsTrue(!dict2.FirstEnumerable.Any());
65      dict2.Add(new ComplexType(1), 2);
66      Assert.IsTrue(dict2.SecondEnumerable.Any());
67      dict2.Add(new ComplexType(2), 1);
68      Assert.IsTrue(dict2.ContainsFirst(new ComplexType(2)));
69      Assert.IsTrue(dict2.ContainsSecond(2));
70      exceptionOnDuplicate = false;
71      try {
72        dict2.Add(new ComplexType(2), 3);
73      } catch (ArgumentException) { exceptionOnDuplicate = true; }
74      Assert.IsTrue(exceptionOnDuplicate);
75      exceptionOnDuplicate = false;
76      try {
77        dict2.Add(new ComplexType(3), 1);
78      } catch (ArgumentException) { exceptionOnDuplicate = true; }
79      Assert.IsTrue(exceptionOnDuplicate);
80      Assert.IsTrue(dict2.Count == 2);
81      Assert.IsTrue(dict2.GetBySecond(1).Field == 2);
82      Assert.IsTrue(dict2.GetByFirst(new ComplexType(1)) == 2);
83      dict2.Clear();
84      Assert.IsTrue(!dict2.SecondEnumerable.Any());
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.