コード例 #1
0
        public void GetCurrentCalculation_WhenNoItemInWindow_ShouldReturnEmpty()
        {
            // Arrange
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(100), 10, Trade.CreateCalculation);

            // Act
            var calculation = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((0, 0), (calculation.Volume, calculation.CurrentCount));
        }
コード例 #2
0
        public void SlidingWindow_WhenWindowLengthIsNotAFactorOfBucketInterval_AdjustsCorrectly(
            Trade[] items, int windowLengthInMs, int bucketInterval, decimal expectedVolume, decimal expectedCount)
        {
            // Arrange
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(windowLengthInMs), bucketInterval, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            var calculation = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((expectedVolume, expectedCount), (calculation.Volume, calculation.CurrentCount));
        }
コード例 #3
0
        public void Add_WhenFirstBucketShiftedOutsideOfTheWindow_ShouldRemoveTheBucketFromWindow()
        {
            // Arrange
            var items  = CreateTradeItemsList(new[] { -90, 0 }, new[] { 10, 20 });
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(100), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            testDateTimeProvider.Now = items[0].TimeStamp;
            window.Add(items[0]);
            testDateTimeProvider.Now = items[1].TimeStamp;
            window.Add(items[1]);
            testDateTimeProvider.Now = testDateTimeProvider.Now.AddMilliseconds(20);

            var calculationAfterSecondItem = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((items[1].Volume, 1), (calculationAfterSecondItem.Volume, calculationAfterSecondItem.CurrentCount));
        }
コード例 #4
0
        public void GetCurrentCalculation_WhenNoItemInWindow_ShouldUpdateWindowAndReturnEmpty(
            Trade[] items, int windowLengthInMs, decimal expectedVolume, decimal expectedCount)
        {
            // Arrange
            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(windowLengthInMs), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            testDateTimeProvider.Now = DateTime.Now;
            var calculation = window.GetCurrentWindowCalculation() as Calculation;

            // Assert
            Assert.Equal((expectedVolume, expectedCount), (calculation.Volume, calculation.CurrentCount));
        }
コード例 #5
0
        public void SlidingWindow_IfShiftToRight_AdjustsCorrectly(int shift, int expectedVolume, int expectedCount)
        {
            // Arrange
            var items = CreateTradeItemsList(new[] { -1000, -900, -800, -700, -600, -500, -400, -300, -200, -100, 0 },
                                             new[] { 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0 });

            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(500), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var item in items)
            {
                testDateTimeProvider.Now = item.TimeStamp;
                window.Add(item);
            }

            var currentCalculation = window.GetCurrentWindowCalculation() as Calculation;
            var shiftRight         = window.GetCalculationFor(testDateTimeProvider.Now.AddMilliseconds(shift)) as Calculation;

            // Assert
            Assert.Equal((1500m / 6, 6), (currentCalculation.Volume, currentCalculation.CurrentCount));
            Assert.Equal((expectedVolume, expectedCount), (shiftRight.Volume, shiftRight.CurrentCount));
        }
コード例 #6
0
        public void SlidingWindow_IfShiftToLeft_AdjustsCorrectly()
        {
            // Arrange
            var trades = CreateTradeItemsList(new[] { -1000, -900, -800, -700, -600, -500, -400, -300, -200, -100, 0 },
                                              new[] { 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 0 });

            var window = new SlidingWindow <Trade>(TimeSpan.FromMilliseconds(500), 10, Trade.CreateCalculation, testDateTimeProvider);

            // Act
            foreach (var trade in trades)
            {
                testDateTimeProvider.Now = trade.TimeStamp;
                window.Add(trade);
            }

            var currentCalculation = window.GetCurrentWindowCalculation() as Calculation;
            var shiftLeft          = window.GetCalculationFor(testDateTimeProvider.Now.AddMilliseconds(-250)) as Calculation;

            // Assert
            Assert.Equal((250, 6), (currentCalculation.Volume, currentCalculation.CurrentCount));
            Assert.Equal((2500m / 5, 5), (shiftLeft.Volume, shiftLeft.CurrentCount));
        }