Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/ParameterlessPopulationPyramid.Test/EnumerableBoolEqualityComparerTest.cs @ 11667

Last change on this file since 11667 was 11667, checked in by bgoldman, 10 years ago

#2282 Major bug fixes, now gets answers close to correct.

File size: 3.8 KB
Line 
1using System;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3using HeuristicLab.Random;
4using System.Collections.Generic;
5using HeuristicLab.Algorithms.ParameterlessPopulationPyramid;
6using System.Linq;
7
8namespace ParameterlessPopulationPyramid.Test {
9  [TestClass]
10  public class EnumerableBoolEqualityComparerTest {
11    private static EnumerableBoolEqualityComparer compare = new EnumerableBoolEqualityComparer();
12    MersenneTwister rand = new MersenneTwister();
13
14    private void randFill(IList<bool> data, MersenneTwister rand) {
15      for (int i = 0; i < data.Count; i++) {
16        data[i] = rand.Next(2) == 1;
17      }
18    }
19    [TestMethod]
20    public void EnumerableBoolEqualsTest() {
21      bool[] array = new bool[10];
22      // Referencially equal
23      randFill(array, rand);
24      Assert.IsTrue(compare.Equals(array, array));
25     
26      // Clones equal
27      var another = (bool[])array.Clone();
28      Assert.IsTrue(compare.Equals(array, another));
29     
30      // Flipping a bit makes unequal
31      int flip = rand.Next(array.Length-1);
32      another[flip] = !another[flip];
33      Assert.IsFalse(compare.Equals(array, another));
34
35      // Equality doesn't depend on container
36      List<bool> list = new List<bool>(array);
37      Assert.IsTrue(compare.Equals(list, array));
38      Assert.IsFalse(compare.Equals(list, another));
39    }
40
41    [TestMethod]
42    public void EnumerableBoolHashCollisionTest() {
43      int collisions = 0;
44
45      bool[] array = new bool[100];
46      randFill(array, rand);
47      int original = compare.GetHashCode(array);
48     
49      bool[] another = (bool[])array.Clone();
50      // Perform random walk around string checking for hash collisions
51      for (int i = 0; i < array.Length * array.Length; i++) {
52        int flip = rand.Next(array.Length - 1);
53        another[flip] = !another[flip];
54        int hash = compare.GetHashCode(another);
55        if (hash == original) {
56          // unequal sequences with same hash value
57          if (!array.SequenceEqual(another)) collisions++;
58        } else {
59          // unequal hash should mean unequal sequence
60          Assert.IsFalse(array.SequenceEqual(another));
61        }
62        // Keep the walk from getting too far afield
63        if (i % array.Length == 0) {
64          another = (bool[])array.Clone();
65        }
66      }
67      // A very lose upper bound, this only fails if you get more than sqrt(N) collisions per attempt
68      Assert.IsTrue(collisions < array.Length);
69    }
70
71    [TestMethod]
72    public void EnumerableBoolHashSetTest() {
73      HashSet<bool[]> set = new HashSet<bool[]>(compare);
74      bool[] array = new bool[40];
75      randFill(array, rand);
76      set.Add(array);
77
78      // Same object not added
79      set.Add(array);
80      Assert.AreEqual(1, set.Count);
81
82      // Copy of object not allowed
83      var another = (bool[])array.Clone();
84      set.Add(another);
85      Assert.AreEqual(1, set.Count);
86
87      // Flipping a bit makes unequal
88      int flip = rand.Next(array.Length - 1);
89      another[flip] = !another[flip];
90      set.Add(another);
91      Assert.AreEqual(2, set.Count);
92
93      // Add random solutions which are 1 bit different from array to the set
94      set.Clear();
95      List<bool[]> added = new List<bool[]>();
96      for (int i = 0; i < array.Length * 10; i++) {
97        // flip a bit at random
98        another = (bool[])array.Clone();
99        flip = rand.Next(array.Length - 1);
100        another[flip] = !another[flip];
101        bool unique = set.Add(another);
102        bool actuallyUnique = true;
103        foreach (var previous in added) {
104          if (previous.SequenceEqual(another)) {
105            actuallyUnique = false;
106          }
107        }
108        Assert.AreEqual(actuallyUnique, unique);
109        if (actuallyUnique) {
110          added.Add(another);
111        }
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.