public void AddIfNotExistsSingleItemTest()
        {
            var          people     = RandomData.GeneratePersonCollection <PersonProper>(10);
            PersonProper nullPerson = null;

            // Test Parameters
            _ = Assert.ThrowsException <ArgumentReadOnlyException>(() => people.AsReadOnly().AddIfNotExists(RandomData.GeneratePerson <PersonProper>()));

            // TEST
            Assert.IsFalse(people.AddIfNotExists(nullPerson));

            var testPerson = RandomData.GeneratePerson <PersonProper>();

            Assert.IsTrue(people.AddIfNotExists(testPerson));

            Assert.IsFalse(people.AddIfNotExists(testPerson));
        }
예제 #2
0
        public void AddRangeListTest()
        {
            var people    = RandomData.GeneratePersonCollection <PersonProper>(10);
            var newPeople = RandomData.GeneratePersonCollection <PersonProper>(2);

            _ = people.AddRange(newPeople, Tristate.True);

            Assert.IsTrue(people.Count() == 12);

            _ = people.AddRange(newPeople, Tristate.UseDefault);

            Assert.IsTrue(people.Count() == 12);

            var nullCollection = new List <PersonProper>();

            Assert.IsFalse(people.AddRange <PersonProper>(nullCollection));
        }
        public void WriteListenTest01()
        {
            const int Capacity = 100;
            var       channel  = new ChannelQueue <PersonProper>();
            var       people   = RandomData.GeneratePersonCollection <PersonProper>(Capacity);
            var       token    = CancellationToken.None;

            var tasks = new List <Task>();

            tasks.Add(AddToQueueAsync(channel, people, token));

            tasks.Add(ListenToQueue(channel, token));

            Task.WaitAll(tasks.ToArray());

            Assert.IsTrue(channel.Count == 0);
        }
        public void AddIfNotExistDictionaryTest()
        {
            var people    = RandomData.GeneratePersonCollection <PersonProper>(10).ToDictionary(p => p.Id);
            var newPerson = RandomData.GeneratePerson <PersonProper>();

            // Test parameters
            _ = Assert.ThrowsException <ArgumentNullException>(() => people.AddIfNotExists(null, newPerson));

            // Test
            Assert.IsTrue(people.AddIfNotExists(newPerson.Id, newPerson));

            Assert.IsFalse(people.AddIfNotExists(newPerson.Id, newPerson));

            var readOnlyPeople = new ReadOnlyDictionary <string, PersonProper>(people);

            _ = Assert.ThrowsException <ArgumentReadOnlyException>(() => readOnlyPeople.AddIfNotExists(newPerson.Id, newPerson));
        }
        public void PagingTest()
        {
            var       people      = RandomData.GeneratePersonCollection <PersonProper>(1000);
            const int pageCount   = 10;
            var       peopleCount = 0;
            var       loopedCount = 0;

            foreach (var peoplePage in people.Page(pageCount))
            {
                loopedCount++;
                peopleCount += peoplePage.Count();
            }

            Assert.IsTrue(peopleCount == 1000);

            Assert.IsTrue(loopedCount == 100);
        }
예제 #6
0
        public void TryValidateParamCollectionTest()
        {
            try
            {
                var people = RandomData.GeneratePersonCollection <PersonProper>(10);
                Validate.TryValidateParam(people, nameof(people));
            }
            catch
            {
                Assert.Fail();
            }

            // Test null collection
            List <PersonProper> nullPeople = null;

            _ = Assert.ThrowsException <NullReferenceException>(() => Validate.TryValidateParam(nullPeople, "none"));
        }
        public void GetOrAddTest()
        {
            var          people     = RandomData.GeneratePersonCollection <PersonProper>(10).ToDictionary(p => p.Id);
            var          newPerson  = RandomData.GeneratePerson <PersonProper>();
            PersonProper nullPerson = null;

            // Test Parameters
            _ = Assert.ThrowsException <ArgumentNullException>(() => people.GetOrAdd(null, newPerson));
            _ = Assert.ThrowsException <ArgumentNullException>(() => people.GetOrAdd(newPerson.Id, nullPerson));

            // TEST
            _ = people.GetOrAdd(newPerson.Id, newPerson);
            Assert.IsTrue(people.Count == 11);

            _ = people.GetOrAdd(newPerson.Id, newPerson);
            Assert.IsTrue(people.Count == 11);
        }
        public void UpsertTest01()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(10).ToArray();
            var personFromCollection = people.Shuffle().First();
            var person = RandomData.GeneratePerson <PersonProper>();

            var result = people.Upsert(personFromCollection);

            Assert.IsTrue(result.Count() == 10);

            result = people.Upsert(person);

            Assert.IsTrue(result.Count() == 11);

            result = people.Upsert(personFromCollection);

            Assert.IsTrue(result.Count() == 10);
        }
        public void UpdatePersonRecordTest()
        {
            var person1 = RandomData.GeneratePersonCollection(count: 1, addressCount: 2).First();

            Assert.IsNotNull(person1);

            // Update Postal code
            var person2 = person1 with {
                CellPhone = "(858) 123-1234"
            };

            Assert.IsNotNull(person2);

            Debug.WriteLine(person2.ToString());

            Debug.WriteLine(person2.PropertiesToString());
        }
    }
예제 #10
0
        /// <summary>
        /// Setups this instance.
        /// </summary>
        /// TODO Edit XML Comment Template for Setup
        public override void Setup()
        {
            base.Setup();

            BenchmarkDotNet.Loggers.ConsoleLogger.Default.WriteLine(BenchmarkDotNet.Loggers.LogKind.Info, $"Collection Count={this.CollectionCount}.");

            this.personFixedCollection = new PersonCollection <PersonFixed>();
            this.personFixedCollection.AddRange(RandomData.GeneratePersonCollection <PersonFixed>(this.CollectionCount));

            this.personProperCollection = new PersonCollection <PersonProper>();
            this.personProperCollection.AddRange(RandomData.GeneratePersonCollection <PersonProper>(this.CollectionCount));

            this.personProperDictionary = this.personProperCollection.ToDictionary(p => p.Id);

            this.testEmail = this.personProperCollection[RandomData.GenerateInteger(0, this.CollectionCount - 1)].Email;

            this.sortablePersonProperCollection = new PersonCollection <PersonProper>(this.personProperCollection);
        }
예제 #11
0
        public void SerializeDeserializeTestPersonRecord()
        {
            var person = RandomData.GeneratePersonCollection(1).First();

            //Serialize
            var xml = XmlSerialization.Serialize(person);

            var fileName = Path.Combine(Environment.GetEnvironmentVariable(EnvironmentKey.APPDATA.ToString()), "PersonRecord.xml");

            //For debugging
            File.WriteAllText(fileName, xml);

            Assert.IsTrue(string.IsNullOrEmpty(xml) == false);

            //Deserialize
            var serializedPerson = XmlSerialization.Deserialize <PersonRecord>(xml);

            Assert.IsNotNull(serializedPerson);
        }
        public void UpsertDictionaryTest()
        {
            var people               = RandomData.GeneratePersonCollection <PersonProper>(10).ToDictionary(p => p.Id);
            var newPerson            = RandomData.GeneratePerson <PersonProper>();
            var personFromCollection = people.Shuffle().First();

            // Test Parameters
            _ = Assert.ThrowsException <ArgumentNullException>(() => people.Upsert(null, newPerson));

            // Test
            people.Upsert(newPerson.Id, newPerson);
            Assert.IsTrue(people.Count == 11);

            people.Upsert(newPerson);
            Assert.IsTrue(people.Count == 11);

            people.Upsert(personFromCollection.Value.Id, personFromCollection.Value);
            Assert.IsTrue(people.Count == 11);
        }
예제 #13
0
        public void GetNextRepeatTest()
        {
            const int count                = 10;
            var       collection           = RandomData.GeneratePersonCollection <PersonProper>(count);
            var       collectionRandomizer = new CollectionRandomizer <PersonProper>(collection, true);

            var counter = 0;

            do
            {
                var item = collectionRandomizer.GetNext();

                counter++;

                Debug.WriteLine(item.ToString());
            } while (counter < 55);

            Assert.IsTrue(55 == counter);
        }
예제 #14
0
        public void AppendKeyValueTest()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(10).ToDictionary(p => p.Id);

            var sb = new StringBuilder();

            foreach (var person in people)
            {
                sb.AppendKeyValue(person.Key, person.Value.Email);
                sb.AppendKeyValue(person.Key, person.Value.Email, includeQuotes: true);
                sb.AppendKeyValue(person.Key, person.Value.Email, includeComma: true);
                sb.AppendKeyValue(person.Key, person.Value.Email, includeQuotes: true, includeComma: true);

                Assert.ThrowsException <ArgumentException>(() => sb.AppendKeyValue(person.Key, null));
                Assert.ThrowsException <ArgumentException>(() => sb.AppendKeyValue(null, person.Value.Email));
            }

            Assert.IsTrue(sb.ToString().Length > 50 * 4);
        }
예제 #15
0
        public void AddIfNotExistsComparerTest()
        {
            var                  people       = RandomData.GeneratePersonCollection <PersonProper>(10);
            var                  person       = RandomData.GeneratePerson <PersonProper>();
            PersonProper         nullPerson   = null;
            var                  comparer     = new PersonProperComparer();
            PersonProperComparer nullComparer = null;

            // Test Parameters
            _ = Assert.ThrowsException <ArgumentException>(() => people.AsReadOnly().AddIfNotExists(person, nullComparer));
            _ = Assert.ThrowsException <ArgumentNullException>(() => people.AddIfNotExists(nullPerson, comparer));
            _ = Assert.ThrowsException <ArgumentException>(() => people.ToReadOnlyCollection().AddIfNotExists(person, comparer));

            // TEST
            people.AddIfNotExists(person, comparer);
            Assert.IsTrue(people.Count == 11);

            people.AddIfNotExists(person, comparer);
            Assert.IsTrue(people.Count == 11);
        }
예제 #16
0
        public void TryValidateParamCollectionWithCountTest()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(10);

            try
            {
                Validate.TryValidateParam(people, 10, nameof(people));
            }
            catch
            {
                Assert.Fail();
            }

            // Test null collection
            List <PersonProper> nullPeople = null;

            _ = Assert.ThrowsException <NullReferenceException>(() => Validate.TryValidateParam(nullPeople, nameof(nullPeople)));

            // Test invalid Count
            _ = Assert.ThrowsException <ArgumentOutOfRangeException>(() => Validate.TryValidateParam(people, 5, nameof(people)));
        }
예제 #17
0
        public void AddFirstTest()
        {
            var peopleList  = RandomData.GeneratePersonCollection <PersonProper>(10);
            var peopleArray = peopleList.ToArray();
            var person      = RandomData.GeneratePerson <PersonProper>();


            // Collection test
            var result1 = peopleList.AddFirst(person);

            Assert.IsTrue(result1.First().Equals(person));
            _ = Assert.ThrowsException <ArgumentNullException>(() => peopleList.AddFirst(null));
            _ = Assert.ThrowsException <ArgumentException>(() => peopleList.ToReadOnlyCollection().AddFirst(person));

            // Array Test
            var result2 = peopleArray.AddFirst(person);

            Assert.IsTrue(result2.First().Equals(person));
            _ = Assert.ThrowsException <ArgumentNullException>(() => peopleArray.AddFirst(null));
            _ = Assert.ThrowsException <ArgumentException>(() => Array.AsReadOnly(peopleArray).AddFirst(person));
        }
예제 #18
0
        public void UpsertTest()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(10);
            var personFromCollection = people.Shuffle().First();
            var person        = RandomData.GeneratePerson <PersonProper>();
            var personRecords = RandomData.GeneratePersonCollection(10);
            var personRecord  = RandomData.GeneratePersonCollection(1).First();

            // TEST
            people.Upsert(person);

            Assert.IsTrue(people.Count() == 11);

            people.Upsert <PersonProper, string>(personFromCollection);

            Assert.IsTrue(people.Count() == 11);

            personRecords.Upsert(personRecord);

            Assert.IsTrue(personRecords.Count() == 11);
        }
        public async Task WriteReadAsyncTest04()
        {
            const int Capacity = 100;
            var       channel  = new ChannelQueue <PersonProper>(Capacity);
            var       people   = RandomData.GeneratePersonCollection <PersonProper>(Capacity);
            var       token    = CancellationToken.None;

            foreach (var person in people)
            {
                await channel.WriteAsync(person, cancellationToken : token).ConfigureAwait(false);
            }

            Assert.IsTrue(channel.Count == Capacity);

            do
            {
                _ = await channel.ReadAsync(token).ConfigureAwait(false);
            } while (channel.Count != 0);

            Assert.IsTrue(channel.Count == 0);
        }
        public void AddToPersonCollectionTest()
        {
            try
            {
                var people = RandomData.GeneratePersonCollection <PersonProper>(Count);

                var newPeople = new List <PersonProper>();

                for (var personCount = 0; personCount < people.Count; personCount++)
                {
                    _ = newPeople.AddIfNotExists(people[personCount]);
                }

                Assert.IsTrue(newPeople.Count == Count);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Assert.Fail();
            }
        }
        public void PropertiesToDictionaryTest()
        {
            var personProper = RandomData.GeneratePersonCollection(1).First();

            var propertiesTest = new PropertiesTest
            {
                Id           = RandomData.GenerateKey(),
                PersonProper = RandomData.GeneratePerson <PersonProper>(),
                PersonRecord = RandomData.GeneratePersonCollection(1).First(),
                Today        = DateTime.Now
            };

            var result = personProper.PropertiesToDictionary(memberName: $"Person-{personProper.Id}", ignoreNulls: true);

            Assert.IsTrue(result.Count() > 1);

            result = propertiesTest.PropertiesToDictionary(
                memberName: $"TestPerson-{personProper.Id}",
                ignoreNulls: true);

            Assert.IsTrue(result.Count() > 1);
        }
예제 #22
0
        public void AddToPersonCollectionTest()
        {
            const int count = 100;

            try
            {
                var people = RandomData.GeneratePersonCollection <PersonProper>(count);

                var newPeople = new PersonCollection <PersonProper>();

                foreach (var person in people)
                {
                    newPeople.AddIfNotExists(person);
                }

                Assert.IsTrue(newPeople.Count == count);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Assert.Fail();
            }
        }
        public async Task WriteReadAsyncTest03()
        {
            var       channel = new ChannelQueue <PersonProper>();
            const int Count   = 5;

            var people = RandomData.GeneratePersonCollection <PersonProper>(Count);
            var token  = CancellationToken.None;

            foreach (var person in people)
            {
                await channel.WriteAsync(person, cancellationToken : token).ConfigureAwait(false);
            }

            Assert.IsTrue(channel.Count == Count);

            _ = channel.Lock();

            await foreach (var item in channel.ListenAsync(token))
            {
                Trace.WriteLine(item.Email);
            }

            Assert.IsTrue(channel.Count == 0);
        }
        public void HasItemsTest02()
        {
            var collection = RandomData.GeneratePersonCollection <PersonProper>(10).AsEnumerable();

            Assert.IsFalse(collection.HasItems(5));
        }
예제 #25
0
        public override void Setup()
        {
            base.Setup();

            this._distinctConcurrentBag = new DistinctConcurrentBag <PersonProper>(RandomData.GeneratePersonCollection <PersonProper>(this.CollectionCount));
        }
예제 #26
0
        public void PropertiesToString02()
        {
            var person = RandomData.GeneratePersonCollection(count: 1, addressCount: 1).First();

            base.Consumer.Consume(person.PropertiesToString());
        }
예제 #27
0
        public void CreatePerson05()
        {
            var person = RandomData.GeneratePersonCollection(count: 1, addressCount: 1).First();

            base.Consumer.Consume(person);
        }
예제 #28
0
        public void ToConcurrentHashSetTest()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(10).ToHashSet().ToConcurrentHashSet();

            Assert.IsTrue(people.Count() == 10);
        }
        public void RemoveLastTest()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(10).ToArray();

            Assert.IsTrue(people.RemoveLast().Count() == 9);
        }
        public void ToLinkedListTest()
        {
            var people = RandomData.GeneratePersonCollection <PersonProper>(50);

            Assert.IsTrue(people.ToLinkedList().HasItems());
        }