|
| 1 | +#region License and Terms |
| 2 | +// MoreLINQ - Extensions to LINQ to Objects |
| 3 | +// Copyright (c) 2010 Leopold Bushkin. All rights reserved. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +#endregion |
| 17 | + |
| 18 | +namespace MoreLinq.Test |
| 19 | +{ |
| 20 | + using System; |
| 21 | + using NUnit.Framework; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Verify the behavior of the Interleave operator |
| 25 | + /// </summary> |
| 26 | + [TestFixture] |
| 27 | + public class EquiInterleaveTests |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// Verify that EquiInterleave behaves in a lazy manner |
| 31 | + /// </summary> |
| 32 | + [Test] |
| 33 | + public void TestEquiInterleaveIsLazy() |
| 34 | + { |
| 35 | + new BreakingSequence<int>().EquiInterleave(new BreakingSequence<int>()); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Verify that EquiInterleave disposes those enumerators that it managed |
| 40 | + /// to open successfully |
| 41 | + /// </summary> |
| 42 | + [Test] |
| 43 | + public void TestEquiInterleaveDisposesOnError() |
| 44 | + { |
| 45 | + using (var sequenceA = TestingSequence.Of<int>()) |
| 46 | + { |
| 47 | + Assert.Throws<InvalidOperationException>(() => // Expected and thrown by BreakingSequence |
| 48 | + sequenceA.EquiInterleave(new BreakingSequence<int>()).Consume()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Verify that two balanced sequences will EquiInterleave all of their elements |
| 54 | + /// </summary> |
| 55 | + [Test] |
| 56 | + public void TestEquiInterleaveTwoBalancedSequences() |
| 57 | + { |
| 58 | + const int count = 10; |
| 59 | + var sequenceA = Enumerable.Range(1, count); |
| 60 | + var sequenceB = Enumerable.Range(1, count); |
| 61 | + var result = sequenceA.EquiInterleave(sequenceB); |
| 62 | + |
| 63 | + Assert.That(result, Is.EqualTo(Enumerable.Range(1, count).Select(x => new[] { x, x }).SelectMany(z => z))); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Verify that EquiInterleave with two empty sequences results in an empty sequence |
| 68 | + /// </summary> |
| 69 | + [Test] |
| 70 | + public void TestEquiInterleaveTwoEmptySequences() |
| 71 | + { |
| 72 | + var sequenceA = Enumerable.Empty<int>(); |
| 73 | + var sequenceB = Enumerable.Empty<int>(); |
| 74 | + var result = sequenceA.EquiInterleave(sequenceB); |
| 75 | + |
| 76 | + Assert.That(result, Is.EqualTo(Enumerable.Empty<int>())); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Verify that EquiInterleave throw on two unbalanced sequences |
| 81 | + /// </summary> |
| 82 | + [Test] |
| 83 | + public void TestEquiInterleaveThrowOnUnbalanced() |
| 84 | + { |
| 85 | + void Code() |
| 86 | + { |
| 87 | + var sequenceA = new[] { 0, 0, 0, 0, 0, 0 }; |
| 88 | + var sequenceB = new[] { 1, 1, 1, 1 }; |
| 89 | + sequenceA.EquiInterleave(sequenceB).Consume(); |
| 90 | + } |
| 91 | + |
| 92 | + Assert.Throws<InvalidOperationException>(Code); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Verify that EquiInterleave multiple empty sequences results in an empty sequence |
| 97 | + /// </summary> |
| 98 | + [Test] |
| 99 | + public void TestEquiInterleaveManyEmptySequences() |
| 100 | + { |
| 101 | + var sequenceA = Enumerable.Empty<int>(); |
| 102 | + var sequenceB = Enumerable.Empty<int>(); |
| 103 | + var sequenceC = Enumerable.Empty<int>(); |
| 104 | + var sequenceD = Enumerable.Empty<int>(); |
| 105 | + var sequenceE = Enumerable.Empty<int>(); |
| 106 | + var result = sequenceA.EquiInterleave(sequenceB, sequenceC, sequenceD, sequenceE); |
| 107 | + |
| 108 | + Assert.That(result, Is.Empty); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Verify that EquiInterleave throw on multiple unbalanced sequences |
| 113 | + /// </summary> |
| 114 | + [Test] |
| 115 | + public void TestEquiInterleaveManyImbalanceStrategySkip() |
| 116 | + { |
| 117 | + void Code() |
| 118 | + { |
| 119 | + var sequenceA = new[] {1, 5, 8, 11, 14, 16,}; |
| 120 | + var sequenceB = new[] {2, 6, 9, 12,}; |
| 121 | + var sequenceC = new int[] { }; |
| 122 | + var sequenceD = new[] {3}; |
| 123 | + var sequenceE = new[] {4, 7, 10, 13, 15, 17,}; |
| 124 | + sequenceA.EquiInterleave(sequenceB, sequenceC, sequenceD, sequenceE).Consume(); |
| 125 | + } |
| 126 | + |
| 127 | + Assert.Throws<InvalidOperationException>(Code); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Verify that Interleave disposes of all iterators it creates. |
| 132 | + /// </summary> |
| 133 | + [Test] |
| 134 | + public void TestEquiInterleaveDisposesAllIterators() |
| 135 | + { |
| 136 | + const int count = 10; |
| 137 | + |
| 138 | + using (var sequenceA = Enumerable.Range(1, count).AsTestingSequence()) |
| 139 | + using (var sequenceB = Enumerable.Range(1, count).AsTestingSequence()) |
| 140 | + using (var sequenceC = Enumerable.Range(1, count).AsTestingSequence()) |
| 141 | + using (var sequenceD = Enumerable.Range(1, count).AsTestingSequence()) |
| 142 | + { |
| 143 | + sequenceA.EquiInterleave(sequenceB, sequenceC, sequenceD).Consume(); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments