Exemplo n.º 1
0
        public void ForEachContinue()
        {
            var enumerable_counter = new EnumerableCounter(0, 10);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item             = Expression.Variable(typeof(int), "i");
            var foreach_break    = Expression.Label("foreach_break");
            var foreach_continue = Expression.Label("foreach_continue");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Block(
                        Expression.Condition(
                            Expression.Equal(Expression.Modulo(item, Expression.Constant(2)), Expression.Constant(0)),
                            Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                            Expression.Goto(foreach_continue))),
                    foreach_break,
                    foreach_continue),
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(5, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
Exemplo n.º 2
0
        public void ForEachException()
        {
            var enumerable_counter = new EnumerableCounter(0, 100);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item = Expression.Variable(typeof(int), "i");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Block(
                        Expression.Condition(
                            Expression.LessThanOrEqual(item, Expression.Constant(10)),
                            Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                            Expression.Throw(Expression.New(typeof(TestException)))))),
                ec).Compile();

            try {
                hitcounter(enumerable_counter);
                Assert.Fail();
            } catch (TestException) {}

            Assert.AreEqual(10, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
Exemplo n.º 3
0
        public void ForEachBreakAfterUpdate()
        {
            var enumerable_counter = new EnumerableCounter(0, 100);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item             = Expression.Variable(typeof(int), "i");
            var foreach_break    = Expression.Label("foreach_break");
            var foreach_continue = Expression.Label("foreach_continue");
            var foreach_body     = Expression.Block(
                Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                Expression.IfThen(
                    Expression.GreaterThanOrEqual(item, Expression.Constant(10)),
                    Expression.Goto(foreach_break)
                    ));

            var foreach_expr = CustomExpression.ForEach(
                item,
                ec,
                foreach_body,
                foreach_break,
                foreach_continue);

            foreach_expr = foreach_expr.Update(item, ec, foreach_body, foreach_break, null);
            var hitcounter = Expression.Lambda <Action <EnumerableCounter> >(
                foreach_expr,
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(10, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
        public void ForEachBreak()
        {
            var enumerable_counter = new EnumerableCounter(0, 100);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item         = Expression.Variable(typeof(int), "i");
            var foreachBreak = Expression.Label("foreachBreak");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> >(
                ExpressionEx.ForEach(
                    item,
                    ec,
                    Expression.Block(
                        Expression.Condition(
                            Expression.LessThanOrEqual(item, Expression.Constant(10)),
                            Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes)),
                            Expression.Goto(foreachBreak))),
                    foreachBreak),
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.Equal(10, enumerable_counter.Count);
            Assert.True(enumerable_counter.Disposed);
        }
    static void Main()
    {
        var data = new List <int> {
            23, 15, 16, 24, -9, 11, 32, 14
        };

        var item = -9;
        var es   = new EnumerableSearcher <int>(data);
        var i    = es.IndexOf(item);

        var ec    = new EnumerableCounter <int>(data);
        var count = ec.Count();

        var em  = new EnumerableFindMax <int>(data);
        var max = em.FindMax();

        var ep = new EnumerablePrinter <int>(data);

        ep.Print();

        Console.WriteLine("Number of Items: " + count);
        Console.WriteLine("Index of {0}: {1}", item, i);
        Console.WriteLine("Maximum: {0}", max);
        Console.ReadKey();
    }
Exemplo n.º 6
0
        public void ForEach()
        {
            var enumerable_counter = new EnumerableCounter(0, 10);

            var ec = Expression.Parameter(typeof(EnumerableCounter), "ec");

            var item = Expression.Variable(typeof(int), "i");

            var hitcounter = Expression.Lambda <Action <EnumerableCounter> > (
                CustomExpression.ForEach(
                    item,
                    ec,
                    Expression.Call(ec, typeof(EnumerableCounter).GetMethod("Hit", Type.EmptyTypes))),
                ec).Compile();

            hitcounter(enumerable_counter);

            Assert.AreEqual(10, enumerable_counter.Count);
            Assert.IsTrue(enumerable_counter.Disposed);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Verifies that the specified sequence, collection, or array contains the expected number of elements.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The assertion counts the elements according to the underlying type of the sequence.
        /// <list type="bullet">
        /// <item>Uses <see cref="Array.Length"/> if the sequence is an array.</item>
        /// <item>Uses <see cref="ICollection.Count"/> or <see cref="ICollection{T}.Count"/> if the sequence is a collection such as <see cref="List{T}"/> or <see cref="Dictionary{K,V}"/>. It enumerates and counts the elements as well.</item>
        /// <item>Enumerates and counts the elements if the sequence is a simple <see cref="IEnumerable"/>.</item>
        /// </list>
        /// </para>
        /// </remarks>
        /// <param name="expectedCount">The expected number of elements.</param>
        /// <param name="values">The enumeration of elements to count.</param>
        /// <param name="messageFormat">The custom assertion message format, or null if none.</param>
        /// <param name="messageArgs">The custom assertion message arguments, or null if none.</param>
        /// <exception cref="AssertionException">Thrown if the verification failed unless the current <see cref="AssertionContext.AssertionFailureBehavior" /> indicates otherwise.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="expectedCount"/> is negative.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="values"/> is null.</exception>
        public static void Count(int expectedCount, IEnumerable values, string messageFormat, params object[] messageArgs)
        {
            if (expectedCount < 0)
            {
                throw new ArgumentOutOfRangeException("expectedCount", "The expected count value must be greater than or equal to 0.");
            }

            AssertionHelper.Verify(() =>
            {
                var counter  = new EnumerableCounter(values);
                var failures = new List <ICountingStrategy>();

                foreach (ICountingStrategy strategy in counter.Count())
                {
                    if (strategy.Count != expectedCount)
                    {
                        failures.Add(strategy);
                    }
                }

                if (failures.Count == 0)
                {
                    return(null);
                }

                var builder = new AssertionFailureBuilder(String.Format(
                                                              "Expected the sequence to contain a certain number of elements but {0} counting strateg{1} failed.", failures.Count, failures.Count > 1 ? "ies have" : "y has"))
                              .AddRawExpectedValue(expectedCount);

                foreach (var failure in failures)
                {
                    builder.AddRawLabeledValue(String.Format("Actual Value ({0})", failure.Description), failure.Count);
                }

                return(builder
                       .SetMessage(messageFormat, messageArgs)
                       .ToAssertionFailure());
            });
        }
        protected override void Context()
        {
            _snapshot = new Snapshot(streamId, HeadStreamRevision, "snapshot");
            _committed = new EnumerableCounter<ICommit>(
                new[] { BuildCommitStub(HeadStreamRevision, HeadCommitSequence)});

            A.CallTo(() => Persistence.GetFrom(Bucket.Default, streamId, HeadStreamRevision, int.MaxValue))
                .Returns(_committed);
        }
		protected override void Context()
	    {
            snapshot = new Snapshot(streamId, HeadStreamRevision, "snapshot");
            Committed = new EnumerableCounter(
                new[] { BuildCommitStub(HeadStreamRevision, HeadCommitSequence) });

	        Persistence.Setup(x => x.GetFrom(streamId, HeadStreamRevision, int.MaxValue)).Returns(Committed);
	    }
Exemplo n.º 10
0
        public void Counts_pure_generic_collection() // Implemented ICollection<T> only
        {
            var counter = new EnumerableCounter(GetHashSet());

            AssertStrategies(counter.Count(), CountingStrategyName.ByCountGetter, CountingStrategyName.ByEnumeratingElements);
        }
Exemplo n.º 11
0
        public void Counts_mixed_generic_collection() // Implemented both ICollection and ICollection<T>
        {
            var counter = new EnumerableCounter(GetGenericList());

            AssertStrategies(counter.Count(), CountingStrategyName.ByCountGetter, CountingStrategyName.ByEnumeratingElements);
        }
Exemplo n.º 12
0
        public void Counts_array()
        {
            var counter = new EnumerableCounter(GetArray());

            AssertStrategies(counter.Count(), CountingStrategyName.ByLengthGetter);
        }
Exemplo n.º 13
0
        public void Counts_custom_enumerable_with_count()
        {
            var counter = new EnumerableCounter(new CustomEnumerable());

            AssertStrategies(counter.Count(), CountingStrategyName.ByEnumeratingElements, CountingStrategyName.ByReflectedCountGetter);
        }
Exemplo n.º 14
0
        public void Counts_simple_enumerable()
        {
            var counter = new EnumerableCounter(GetSimpleEnumerable());

            AssertStrategies(counter.Count(), CountingStrategyName.ByEnumeratingElements);
        }
        protected override Task Context()
        {
            _snapshot = new Snapshot(streamId, HeadStreamRevision, "snapshot");
            _committed = new EnumerableCounter<ICommit>(
                new[] { BuildCommitStub(HeadStreamRevision, HeadCommitSequence)});

            Persistence
                .GetFrom(Bucket.Default, streamId, HeadStreamRevision, int.MaxValue)
                .Returns(_committed.ToAsync());
            //A.CallTo(() =>  Persistence.GetFrom(Bucket.Default, streamId, HeadStreamRevision, int.MaxValue))
            //    .Returns(_committed.ToAsync());
            return Task.FromResult(true);
        }