Пример #1
0
        public SecretSantaPairer(List <Person> people)
        {
            int             noMatchCounter = 0;
            SecretSantaPair pair2          = null;
            Person          temp;
            Func <SecretSantaPair, bool> ValidFunction = null;

            List = people.CreatePairs();
            do
            {
                foreach (var pair in List.Where(p => !p.IsValid))
                {
                    if (ValidFunction == null)
                    {
                        ValidFunction = p => SecretSantaPair.WouldBeValid(pair.Giver, p.Receiver) &&
                                        SecretSantaPair.WouldBeValid(p.Giver, pair.Receiver);
                    }

                    pair2 = List.Where(p => !p.IsValid).FirstOrDefault(ValidFunction)
                            ?? List.FirstOrDefault(ValidFunction);

                    if (pair2 != null)
                    {
                        temp           = pair.Receiver;
                        pair.Receiver  = pair2.Receiver;
                        pair2.Receiver = temp;
                    }
                    else
                    {
                        noMatchCounter++;
                    }
                }
            } while ((List.Where(p => !p.IsValid).Count() > 0) && (noMatchCounter < 3));
        }
        public void ValidPair()
        {
            Person person1 = new Person("Curtis Thibault", "Email");
            Person person2 = new Person("Jake Gamble", "Email");
            SecretSantaPair pair = new SecretSantaPair(person1, person2);

            Assert.True(pair.IsValid);
        }
        public void Receiver()
        {
            Person person1 = new Person("One Last", "Email");
            Person person2 = new Person("Two Last", "Email");
            SecretSantaPair pair = new SecretSantaPair(person1, person2);

            Assert.AreEqual(person2, pair.Receiver);
        }
        public void InvalidPair_SamePerson()
        {
            Person person1 = new Person("First Person", "Email");
            Person person2 = new Person("First Person", "Email");
            SecretSantaPair pair = new SecretSantaPair(person1, person2);

            Assert.False(pair.IsValid);
        }