Beispiel #1
0
        public async Task GivenMultipleCandidates_EachOneIsReturnedInStatisticallyEvenNumber()
        {
            // Arrange
            const int         warId                = 987;
            const int         contestantCount      = 10;
            const int         iterations           = 100000;
            const float       lowerBoundPercentage = 0.9f;
            const float       upperBoundPercentage = 1.1f;
            List <Contestant> contestants          = CreateContestantsCollection(contestantCount);

            SetupContestantRepository(warId, contestants);
            var factory = new RandomMatchStrategy(_stubMatchRepository.Object, _stubContestantRepository.Object);
            var matches = new List <MatchWithContestants>();

            // Act
            for (var i = 0; i < iterations; i++)
            {
                var match = await factory.Create(warId, _userId);

                matches.Add(match);
            }

            // Assert
            var groupedByContestant1 = matches.GroupBy(x => x.Contestant1.Id);

            foreach (var g in groupedByContestant1)
            {
                const float expectedCount = (iterations / contestantCount);
                var         actualCount   = (float)g.Count();
                actualCount.Should().BeInRange(expectedCount * lowerBoundPercentage, expectedCount * upperBoundPercentage);
                float expectedRivalCounts  = actualCount / (contestantCount - 1);
                var   groupedByContestant2 = g.GroupBy(x => x.Contestant2.Id);
                groupedByContestant2.Select(x => (float)x.Count()).Should().OnlyContain(x => x >= expectedRivalCounts * lowerBoundPercentage && x <= expectedRivalCounts * upperBoundPercentage);
            }
        }
Beispiel #2
0
 public void InitializeTests()
 {
     _userId      = new UserIdentifier();
     _contestant1 = new Contestant {
         Id = Guid.NewGuid()
     };
     _contestant2 = new Contestant {
         Id = Guid.NewGuid()
     };
     _matchId = Guid.NewGuid();
     _stubContestantRepository = new Mock <IContestantRepository>();
     _stubContestantRepository.Setup(x => x.GetCount(WarId)).Returns(Task.FromResult(ContestantCount));
     _stubMatchRepository = new Mock <IMatchRepository>();
     _stubMatchRepository.Setup(x => x.Create(WarId,
                                              It.Is <MatchRequest>(m => m.Contestant1 == _contestant1.Id &&
                                                                   m.Contestant2 == _contestant2.Id &&
                                                                   m.UserIdentifier == _userId)))
     .Returns(Task.FromResult(_matchId));
     randomNumbers = new Queue <int>();
     _factory      = new RandomMatchStrategy(_stubMatchRepository.Object, _stubContestantRepository.Object, GenerateRandomNumber);
 }