示例#1
0
 public void ManyMatch()
 {
     var(t, x) =
         Source.Concat(MoreEnumerable.From(BreakingFunc.Of <int>()))
         .SingleOrNone(x => x > 200);
     Assert.That(t, Is.False);
     Assert.That(x, Is.Zero);
 }
示例#2
0
                public void Many()
                {
                    var source = MoreEnumerable.From(() => 123, BreakingFunc.Of <int>());

                    var(t, x) = source.FirstOrNone();
                    Assert.That(t, Is.True);
                    Assert.That(x, Is.EqualTo(123));
                }
示例#3
0
        public void AtLeastDoesNotIterateUnnecessaryElements()
        {
            var source = MoreEnumerable.From(() => 1,
                                             () => 2,
                                             () => throw new TestException());

            Assert.IsTrue(source.AtLeast(2));
        }
示例#4
0
                public void Many()
                {
                    var source = MoreEnumerable.From(() => 123, () => 456, () => 789);

                    var(t, x) = source.LastOrNone();
                    Assert.That(t, Is.True);
                    Assert.That(x, Is.EqualTo(789));
                }
示例#5
0
                public void One()
                {
                    var source = MoreEnumerable.From(() => 123);

                    var(t, x) = source.LastOrNone();
                    Assert.That(t, Is.True);
                    Assert.That(x, Is.EqualTo(123));
                }
示例#6
0
                public void Many()
                {
                    var source = MoreEnumerable.From(() => 123, () => 456, BreakingFunc.Of <object>());

                    var(t, x) = source.SingleOrNone();
                    Assert.That(t, Is.False);
                    Assert.That(x, Is.Null);
                }
示例#7
0
        public void TestFromIsLazy()
        {
            var breakingFunc = BreakingFunc.Of <int>();

            MoreEnumerable.From(breakingFunc);
            MoreEnumerable.From(breakingFunc, breakingFunc);
            MoreEnumerable.From(breakingFunc, breakingFunc, breakingFunc);
            MoreEnumerable.From(breakingFunc, breakingFunc, breakingFunc, breakingFunc);
        }
示例#8
0
        public void AtMostDoesNotIterateUnnecessaryElements()
        {
            var source = MoreEnumerable.From(() => 1,
                                             () => 2,
                                             () => 3,
                                             () => throw new InvalidOperationException());

            Assert.IsFalse(source.AtMost(2));
        }
示例#9
0
        public void ExactlyDoesNotIterateUnnecessaryElements()
        {
            var source = MoreEnumerable.From(() => 1,
                                             () => 2,
                                             () => 3,
                                             () => throw new TestException());

            Assert.IsFalse(source.Exactly(2));
        }
示例#10
0
        public void CountBetweenDoesNotIterateUnnecessaryElements()
        {
            var source = MoreEnumerable.From(() => 1,
                                             () => 2,
                                             () => 3,
                                             () => 4,
                                             () => throw new TestException());

            Assert.False(source.CountBetween(2, 3));
        }
示例#11
0
        public void MoveNextIsNotCalledUnnecessarily()
        {
            using var s1 = TestingSequence.Of(1, 2);
            using var s2 = TestingSequence.Of(1, 2, 3);
            using var s3 = MoreEnumerable.From(() => 1,
                                               () => 2,
                                               () => throw new TestException())
                           .AsTestingSequence();

            Assert.Throws <InvalidOperationException>(() =>
                                                      s1.EquiZip(s2, s3, (x, y, z) => x + y + z).Consume());
        }
示例#12
0
        public void CompareCountDoesNotIterateUnnecessaryElements()
        {
            var seq1 = MoreEnumerable.From(() => 1,
                                           () => 2,
                                           () => 3,
                                           () => 4,
                                           () => throw new TestException());

            var seq2 = Enumerable.Range(1, 3);

            Assert.AreEqual(1, seq1.CompareCount(seq2));
            Assert.AreEqual(-1, seq2.CompareCount(seq1));
        }
示例#13
0
        public void FlattenInterruptedIterationDisposesInnerSequences()
        {
            using var inner1 = TestingSequence.Of(4, 5);
            using var inner2 = MoreEnumerable.From(() => true,
                                                   () => false,
                                                   () => throw new TestException())
                               .AsTestingSequence();
            using var inner3 = TestingSequence.Of <object>(6, inner2, 7);
            using var source = TestingSequence.Of <object>(inner1, inner3);

            Assert.Throws <TestException>(() =>
                                          source.Flatten().Consume());
        }
示例#14
0
 public void MoveNextIsNotCalledUnnecessarilyWhenFirstIsShorter()
 {
     using (var s1 = TestingSequence.Of(1, 2))
         using (var s2 = MoreEnumerable.From(() => 4,
                                             () => 5,
                                             () => throw new TestException())
                         .AsTestingSequence())
         {
             var zipped = s1.ZipShortest(s2, Tuple.Create);
             Assert.That(zipped, Is.Not.Null);
             zipped.AssertSequenceEqual((1, 4), (2, 5));
         }
 }
示例#15
0
 public void ZipShortestNotIterateUnnecessaryElements()
 {
     using (var s1 = MoreEnumerable.From(() => 4,
                                         () => 5,
                                         () => 6,
                                         () => throw new TestException())
                     .AsTestingSequence())
         using (var s2 = TestingSequence.Of(1, 2))
         {
             var zipped = s1.ZipShortest(s2, Tuple.Create);
             Assert.That(zipped, Is.Not.Null);
             zipped.AssertSequenceEqual((4, 1), (5, 2));
         }
 }
示例#16
0
        public void TestFromInvokesMethodsMultipleTimes(int numArgs)
        {
            var evals = new [] { 0, 0, 0, 0 };

            int F1()
            {
                evals[0]++; return(-2);
            }

            int F2()
            {
                evals[1]++; return(-2);
            }

            int F3()
            {
                evals[2]++; return(-2);
            }

            int F4()
            {
                evals[3]++; return(-2);
            }

            IEnumerable <int> results;

            switch (numArgs)
            {
            case 1: results = MoreEnumerable.From(F1); break;

            case 2: results = MoreEnumerable.From(F1, F2); break;

            case 3: results = MoreEnumerable.From(F1, F2, F3); break;

            case 4: results = MoreEnumerable.From(F1, F2, F3, F4); break;

            default: throw new ArgumentOutOfRangeException(nameof(numArgs));
            }

            results.Consume();
            results.Consume();
            results.Consume();

            // numArgs functions were evaluated...
            evals.Take(numArgs).AssertSequenceEqual(Enumerable.Repeat(3, numArgs));
            // safety check: we didn't evaluate functions past numArgs
            evals.Skip(numArgs).AssertSequenceEqual(Enumerable.Repeat(0, evals.Length - numArgs));
        }
示例#17
0
 public void FlattenEvaluatesInnerSequencesLazily()
 {
     var source = new object[]
     {
         1, 2, 3, 4, 5,
         new object[]
         {
             6, 7,
             new object[]
             {
                 8, 9,
                 MoreEnumerable.From
                 (
                     () => 10,
                     () => throw new TestException(),
                     () => 12
                 ),
                 13, 14, 15,
             },
示例#18
0
        public void AcquireSome()
        {
            Disposable a = null;
            Disposable b = null;
            Disposable c = null;

            var allocators = MoreEnumerable.From(() => a = new Disposable(),
                                                 () => b = new Disposable(),
                                                 () => throw new TestException(),
                                                 () => c = new Disposable());

            Assert.Throws <TestException>(() => allocators.Acquire());

            Assert.That(a, Is.Not.Null);
            Assert.That(a.Disposed, Is.True);
            Assert.That(b, Is.Not.Null);
            Assert.That(b.Disposed, Is.True);
            Assert.That(c, Is.Null);
        }
示例#19
0
        public void AcquireAll()
        {
            Disposable a = null;
            Disposable b = null;
            Disposable c = null;

            var allocators = MoreEnumerable.From(() => a = new Disposable(),
                                                 () => b = new Disposable(),
                                                 () => c = new Disposable());

            var disposables = allocators.Acquire();

            Assert.That(disposables.Length, Is.EqualTo(3));

            foreach (var disposable in disposables.ZipShortest(new[] { a, b, c }, (act, exp) => new { Actual = act, Expected = exp }))
            {
                Assert.That(disposable.Actual, Is.SameAs(disposable.Expected));
                Assert.That(disposable.Actual.Disposed, Is.False);
            }
        }
示例#20
0
        public void TestFromInvokesMethods(int numArgs)
        {
            int F1() => - 2;
            int F2() => 4;
            int F3() => int.MaxValue;
            int F4() => int.MinValue;

            switch (numArgs)
            {
            case 1: MoreEnumerable.From(F1).AssertSequenceEqual(F1()); break;

            case 2: MoreEnumerable.From(F1, F2).AssertSequenceEqual(F1(), F2()); break;

            case 3: MoreEnumerable.From(F1, F2, F3).AssertSequenceEqual(F1(), F2(), F3()); break;

            case 4: MoreEnumerable.From(F1, F2, F3, F4).AssertSequenceEqual(F1(), F2(), F3(), F4()); break;

            default: throw new ArgumentOutOfRangeException(nameof(numArgs));
            }
        }
示例#21
0
        public void FlattenInterruptedIterationDisposesInnerSequences()
        {
            using (var inner1 = TestingSequence.Of(4, 5))
                using (var inner2 = MoreEnumerable.From(() => true,
                                                        () => false,
                                                        () => throw new InvalidOperationException())
                                    .AsTestingSequence())
                    using (var inner3 = TestingSequence.Of <object>(6, inner2, 7))
                    {
                        var source = new object[]
                        {
                            inner1,
                            inner3,
                        };

                        using (var test = source.AsTestingSequence())
                        {
                            Assert.Throws <InvalidOperationException>(() =>
                                                                      test.Flatten().Consume());
                        }
                    }
        }
示例#22
0
        public void IndexBytDoesNotIterateUnnecessaryElements()
        {
            var source = MoreEnumerable.From(() => "ana",
                                             () => "beatriz",
                                             () => "carla",
                                             () => "bob",
                                             () => "davi",
                                             () => throw new TestException(),
                                             () => "angelo",
                                             () => "carlos");

            var result = source.IndexBy(x => x.First());

            result.Take(5).AssertSequenceEqual(
                KeyValuePair.Create(0, "ana"),
                KeyValuePair.Create(0, "beatriz"),
                KeyValuePair.Create(0, "carla"),
                KeyValuePair.Create(1, "bob"),
                KeyValuePair.Create(0, "davi"));

            Assert.Throws <TestException>(() =>
                                          result.ElementAt(5));
        }
示例#23
0
        public void ScanByDoesNotIterateUnnecessaryElements()
        {
            var source = MoreEnumerable.From(() => "ana",
                                             () => "beatriz",
                                             () => "carla",
                                             () => "bob",
                                             () => "davi",
                                             () => throw new TestException(),
                                             () => "angelo",
                                             () => "carlos");

            var result = source.ScanBy(c => c.First(), k => - 1, (i, k, e) => i + 1);

            result.Take(5).AssertSequenceEqual(
                KeyValuePair.Create('a', 0),
                KeyValuePair.Create('b', 0),
                KeyValuePair.Create('c', 0),
                KeyValuePair.Create('b', 1),
                KeyValuePair.Create('d', 0));

            Assert.Throws <TestException>(() =>
                                          result.ElementAt(5));
        }