Example #1
0
        public void SameEdges <TVertex, TEdge>([PexAssumeNotNull] IVertexAndEdgeListGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var ag = GraphExtensions.ToArrayAdjacencyGraph(g);

            PexAssertEx.AreElementsEqual(g.Edges, ag.Edges, (l, r) => l.Equals(r));
        }
        public void Unions(int elementCount, [PexAssumeNotNull] KeyValuePair <int, int>[] unions)
        {
            PexAssume.IsTrue(0 < elementCount);
            PexSymbolicValue.Minimize(elementCount);
            PexAssertEx.TrueForAll(
                unions,
                u => 0 <= u.Key && u.Key < elementCount &&
                0 <= u.Value && u.Value < elementCount
                );

            var target = new ForestDisjointSet <int>();

            // fill up with 0..elementCount - 1
            for (int i = 0; i < elementCount; i++)
            {
                target.MakeSet(i);
                Assert.IsTrue(target.Contains(i));
                Assert.AreEqual(i + 1, target.ElementCount);
                Assert.AreEqual(i + 1, target.SetCount);
            }

            // apply Union for pairs unions[i], unions[i+1]
            for (int i = 0; i < unions.Length; i++)
            {
                var left  = unions[i].Key;
                var right = unions[i].Value;

                var  setCount = target.SetCount;
                bool unioned  = target.Union(left, right);
                // should be in the same set now
                Assert.IsTrue(target.AreInSameSet(left, right));
                // if unioned, the count decreased by 1
                PexAssertEx.ImpliesIsTrue(unioned, () => setCount - 1 == target.SetCount);
            }
        }
Example #3
0
        public void Add([PexAssumeNotNull] int[] keys)
        {
            PexAssertEx.TrueForAll(keys, k => k < int.MaxValue);
            PexAssume.IsTrue(keys.Length > 0);

            var target = new SoftHeap <int, int>(1 / 4.0, int.MaxValue);

            TestConsole.WriteLine("expected error rate: {0}", target.ErrorRate);
            foreach (var key in keys)
            {
                var count = target.Count;
                target.Add(key, key + 1);
                Assert.AreEqual(count + 1, target.Count);
            }

            int lastMin = int.MaxValue;
            int error   = 0;

            while (target.Count > 0)
            {
                var kv = target.DeleteMin();
                if (lastMin < kv.Key)
                {
                    error++;
                }
                lastMin = kv.Key;
                Assert.AreEqual(kv.Key + 1, kv.Value);
            }

            TestConsole.WriteLine("error rate: {0}", error / (double)keys.Length);
            Assert.IsTrue(error / (double)keys.Length <= target.ErrorRate);
        }
Example #4
0
        public IEnumerable <IEnumerable <TEdge> > HoffmanPavleyRankedShortestPath <TVertex, TEdge>(
            [PexAssumeNotNull] IBidirectionalGraph <TVertex, TEdge> g,
            [PexAssumeNotNull] Dictionary <TEdge, double> edgeWeights,
            TVertex rootVertex,
            TVertex goalVertex,
            int pathCount
            )
            where TEdge : IEdge <TVertex>
        {
            //GraphConsoleSerializer.DisplayGraph((IEdgeListGraph<TVertex, TEdge>)g);

            PexAssertEx.TrueForAll(g.Edges, edgeWeights.ContainsKey);

            var target = new HoffmanPavleyRankedShortestPathAlgorithm <TVertex, TEdge>(g, e => edgeWeights[e]);

            target.ShortestPathCount = pathCount;
            target.Compute(rootVertex, goalVertex);

            double lastWeight = double.MinValue;

            foreach (var path in target.ComputedShortestPaths)
            {
                TestConsole.WriteLine("path: {0}", Enumerable.Sum(path, e => edgeWeights[e]));
                double weight = Enumerable.Sum(path, e => edgeWeights[e]);
                Assert.IsTrue(lastWeight <= weight, "{0} <= {1}", lastWeight, weight);
                Assert.AreEqual(rootVertex, Enumerable.First(path).Source);
                Assert.AreEqual(goalVertex, Enumerable.Last(path).Target);
                Assert.IsTrue(EdgeExtensions.IsPathWithoutCycles <TVertex, TEdge>(path));

                lastWeight = weight;
            }

            return(target.ComputedShortestPaths);
        }
Example #5
0
        public void SameOutEdges <TVertex, TEdge>([PexAssumeNotNull] IVertexAndEdgeListGraph <TVertex, TEdge> g)
            where TEdge : IEdge <TVertex>
        {
            var ag = GraphExtensions.ToArrayAdjacencyGraph(g);

            foreach (var v in g.Vertices)
            {
                PexAssertEx.AreElementsEqual(g.OutEdges(v), ag.OutEdges(v), (l, r) => l.Equals(r));
            }
        }
Example #6
0
 public void CurrentBeforeMoveNext <TPriority, TValue>(
     [PexAssumeUnderTest] BinaryHeap <TPriority, TValue> target,
     KeyValuePair <TPriority, TValue> kv)
 {
     target.Add(kv.Key, kv.Value);
     PexAssertEx.Throws <InvalidOperationException>(delegate
     {
         var enumerator = target.GetEnumerator();
         var current    = enumerator.Current;
     });
 }
Example #7
0
 public void InsertAndResetAndModify <TPriority, TValue>(
     [PexAssumeUnderTest] BinaryHeap <TPriority, TValue> target,
     KeyValuePair <TPriority, TValue> kv)
 {
     target.Add(kv.Key, kv.Value);
     PexAssertEx.Throws <InvalidOperationException>(delegate
     {
         var enumerator = target.GetEnumerator();
         target.Add(kv.Key, kv.Value);
         enumerator.Reset();
     });
 }
Example #8
0
        public void InsertAndEnumerate <TPriority, TValue>(
            [PexAssumeUnderTest] BinaryHeap <TPriority, TValue> target,
            [PexAssumeNotNull] KeyValuePair <TPriority, TValue>[] kvs)
        {
            var dic = new Dictionary <TPriority, TValue>();

            foreach (var kv in kvs)
            {
                target.Add(kv.Key, kv.Value);
                dic[kv.Key] = kv.Value;
            }

            PexAssertEx.TrueForAll(target, kv => dic.ContainsKey(kv.Key));
        }
        public void CompareBinary <TPriority, TValue>(
            [PexAssumeNotNull] KeyValuePair <bool, TPriority>[] values)
        {
            var fib = new FibonacciHeap <TPriority, TValue>();
            var bin = new BinaryHeap <TPriority, TValue>();

            foreach (var value in values)
            {
                if (value.Key)
                {
                    PexAssertEx.AreBehaviorsEqual(
                        () => fib.Enqueue(value.Value, default(TValue)),
                        () => bin.Add(value.Value, default(TValue))
                        );
                }
                else
                {
                    PexAssertEx.AreBehaviorsEqual(
                        () => fib.Dequeue().Key,
                        () => bin.RemoveMinimum().Key
                        );
                }
            }
        }