Esempio n. 1
0
                public static TryCatch <AggregateValue> TryCreate(
                    AggregateOperator aggregateOperator,
                    string continuationToken)
                {
                    TryCatch <IAggregator> tryCreateAggregator;

                    switch (aggregateOperator)
                    {
                    case AggregateOperator.Average:
                        tryCreateAggregator = AverageAggregator.TryCreate(continuationToken);
                        break;

                    case AggregateOperator.Count:
                        tryCreateAggregator = CountAggregator.TryCreate(continuationToken);
                        break;

                    case AggregateOperator.Max:
                        tryCreateAggregator = MinMaxAggregator.TryCreateMaxAggregator(continuationToken);
                        break;

                    case AggregateOperator.Min:
                        tryCreateAggregator = MinMaxAggregator.TryCreateMinAggregator(continuationToken);
                        break;

                    case AggregateOperator.Sum:
                        tryCreateAggregator = SumAggregator.TryCreate(continuationToken);
                        break;

                    default:
                        throw new ArgumentException($"Unknown {nameof(AggregateOperator)}: {aggregateOperator}.");
                    }

                    return(tryCreateAggregator.Try <AggregateValue>((aggregator) => new AggregateAggregateValue(aggregator)));
                }
                public static AggregateAggregateValue Create(
                    AggregateOperator aggregateOperator,
                    string continuationToken)
                {
                    IAggregator aggregator;

                    switch (aggregateOperator)
                    {
                    case AggregateOperator.Average:
                        aggregator = AverageAggregator.Create(continuationToken);
                        break;

                    case AggregateOperator.Count:
                        aggregator = CountAggregator.Create(continuationToken);
                        break;

                    case AggregateOperator.Max:
                        aggregator = MinMaxAggregator.CreateMaxAggregator(continuationToken);
                        break;

                    case AggregateOperator.Min:
                        aggregator = MinMaxAggregator.CreateMinAggregator(continuationToken);
                        break;

                    case AggregateOperator.Sum:
                        aggregator = SumAggregator.Create(continuationToken);
                        break;

                    default:
                        throw new ArgumentException($"Unknown {nameof(AggregateOperator)}: {aggregateOperator}.");
                    }

                    return(new AggregateAggregateValue(aggregator));
                }
Esempio n. 3
0
        public void AverageAggregator_Merge()
        {
            var avgAggr1 = new AverageAggregator("test");
            var avgAggr2 = new AverageAggregator("test");
            var avgAggr3 = new AverageAggregator("test");
            Func <object, string, object> getVal = (r, f) => {
                return(r);
            };

            for (int i = 0; i < 10; i++)
            {
                avgAggr1.Push(i, getVal);
                avgAggr2.Push(i, getVal);
                avgAggr3.Push(i % 2 == 0 ? null : (object)i, getVal);
            }

            Assert.Equal(4.5M, Convert.ToDecimal(avgAggr1.Value));
            avgAggr1.Merge(avgAggr2);
            Assert.Equal(4.5M, Convert.ToDecimal(avgAggr1.Value));
            Assert.Equal(20, Convert.ToInt32(avgAggr1.Count));

            avgAggr1.Merge(avgAggr3);
            Assert.Equal(4.6M, Convert.ToDecimal(avgAggr1.Value));
            Assert.Equal(25, (int)avgAggr1.Count);
        }
        static void Main()
        {
            AverageAggregator avgAggregator = new AverageAggregator();
            avgAggregator.AverageChanged += AvgAggregator_AverageChanged;

            avgAggregator.AddNumber(3);
            avgAggregator.AddNumber(8);
            avgAggregator.AddNumber(8);
        }
        public void TestAverage()
        {
            var reflectiveSequence = CreateReflectiveSequence();

            var aggregator = new AverageAggregator();
            var finalValue = reflectiveSequence.Aggregate(aggregator, property1);

            Assert.That(finalValue, Is.EqualTo(2.6));
        }
Esempio n. 6
0
        public void AverageAggregator()
        {
            var avg = new AverageAggregator("test");

            Assert.Equal(0M, Convert.ToDecimal(avg.Value));

            Func <object, string, object> getVal = (r, f) => {
                Assert.Equal("test", f);
                return(r);
            };

            for (int i = 0; i <= 10; i++)
            {
                avg.Push(i, getVal);
            }

            Assert.Equal(5M, avg.Value);

            avg.Push(String.Empty, getVal);
            Assert.Equal(5M, avg.Value);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            Console.InputEncoding = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;
            AverageAggregator averageAgg = new AverageAggregator
                (delegate (object sender, decimal oldAverage, decimal newAverage)
                {
                    Console.Write("--->Handler: ");
                    Console.WriteLine("Average changed from {0} to {1}.", oldAverage, newAverage);
                });

            Console.WriteLine("---AverageAggregator---");
            Console.WriteLine();

            Console.WriteLine("Adding number to aggregator: 2");
            averageAgg.AddNumber(2);
            Console.WriteLine("Adding number to aggregator: 2");
            averageAgg.AddNumber(2);
            Console.WriteLine("Adding number to aggregator: 8");
            averageAgg.AddNumber(8);
            Console.WriteLine("Adding number to aggregator: 4");
            averageAgg.AddNumber(4);
            Console.WriteLine("Adding number to aggregator: 4");
            averageAgg.AddNumber(4);
            Console.WriteLine("Adding number to aggregator: 4");
            averageAgg.AddNumber(4);
            Console.WriteLine("Adding number to aggregator: -24");
            averageAgg.AddNumber(-24);
            Console.WriteLine("Adding number to aggregator: 0");
            averageAgg.AddNumber(0);
            Console.WriteLine("Adding number to aggregator: 0");
            averageAgg.AddNumber(0);
            Console.WriteLine("Adding number to aggregator: 10");
            averageAgg.AddNumber(10);
            Console.WriteLine("Adding number to aggregator: 1");
            averageAgg.AddNumber(1);
            Console.WriteLine("Adding number to aggregator: 1");
            averageAgg.AddNumber(1);
            Console.WriteLine("Adding number to aggregator: 2");
            averageAgg.AddNumber(2);

            Console.WriteLine();
            Console.WriteLine("---NotifyCollection---");
            Console.WriteLine();

            NotifyCollection<Book> notCol = new NotifyCollection<Book>
                ((sender, changeType, changedItemIndex, changedItemInfo) =>
                {
                    Console.Write("--->Handler: ");
                    switch (changeType)
                    {
                        case ItemChangeType.Add:
                            Console.WriteLine("Added item to collection on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.Insert:
                            Console.WriteLine("Inserted item in collection on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.Remove:
                            if (changedItemIndex == -1) Console.WriteLine("Collection items cleared!");
                            else Console.WriteLine("Removed item from collection being on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.Replace:
                            Console.WriteLine("Replaced item in collection being on {0} index!", changedItemIndex);
                            break;
                        case ItemChangeType.ChangedProperty:
                            Console.WriteLine("Item on {0} index property {1} changed!", changedItemIndex, changedItemInfo);
                            break;
                        default:
                            break;
                    }
                });

            Book book1 = new Book("author1", "book1", 2000);
            Book book2 = new Book("author2", "book2", 2001);
            Book book3 = new Book("author3", "book3", 2002);
            Console.WriteLine("Adding {0}!", book1);
            notCol.Add(book1);
            Console.WriteLine("Adding {0}!", book2);
            notCol.Add(book2);
            Console.WriteLine("Inserting {0} on index 1!", book3);
            notCol.Insert(1, book3);
            Console.WriteLine("Changing {0} year to 2011!", book1);
            book1.Year = 2011;
            Console.WriteLine("Changing {0} year to 2005!", book3);
            book3.Year = 2005;
            Console.WriteLine("Removing {0}!", book2);
            notCol.Remove(book2);
            Console.WriteLine("Changing removed book year to 1995!");
            book2.Year = 1995;
            Console.WriteLine("Clearing all items!");
            notCol.Clear();
            Console.WriteLine("Adding {0}!", book1);
            notCol.Add(book1);
            Console.WriteLine("Adding {0}!", book2);
            notCol.Add(book2);
            Console.WriteLine("Replacing book on 0 index with {0}", book3);
            notCol[0] = book3;
            Console.WriteLine("Changing old book year to 1895!");
            book1.Year = 1895;
            Console.WriteLine("Changing new book year to 2003!");
            book3.Year = 2003;

            Console.WriteLine();
            Console.WriteLine("---Network Buffer---");

            ReceiveBuffer rec = new ReceiveBuffer(MessageReceivedHandle);
            PacketGenerator pg = new PacketGenerator(rec);
            Console.WriteLine("Write messages. Write exit for exit.");
            Console.Write("Message to send: ");
            string inputMessage = Console.ReadLine();
            while(!inputMessage.Equals("exit"))
            {
                pg.SendMessage(inputMessage);
                Console.Write("Message to send: ");
                inputMessage = Console.ReadLine();
            }
        }
Esempio n. 8
0
 protected override void InitializeSystemUnderTests()
 {
     SystemUnderTests = new AverageAggregator();
 }