Example #1
0
            static void Validate(List <BigLifeRecord> control, ReadWriteValueListVault <BigLifeRecord> test)
            {
                if (control.Count != test.GetCurrentCount())
                {
                    throw new LogicErrorException("Collections do not have same number of items.");
                }

                using var lck = test.RoLock();
                for (int i = 0; i < lck.Count; ++i)
                {
                    BigLifeRecord ctrl = control[i];
                    if (lck[i] != ctrl)
                    {
                        throw new LogicErrorException(
                                  $"Elements at idx {i} are not equal via == - control: [{ctrl}]; test: [{lck[i]}]");
                    }
                    if (BigLifeRecord.Compare(in lck[i], in ctrl) != 0)
                    {
                        throw new LogicErrorException(
                                  $"Elements at idx {i} are not equal via compare - control: [{ctrl}]; test: [{lck[i]}]");
                    }
                    if (lck[i].GetHashCode() != ctrl.GetHashCode())
                    {
                        throw new LogicErrorException(
                                  $"Elements at idx {i} do not return same hash code - control: [{ctrl}]; test: [{lck[i]}]");
                    }
                }
            }
                ImmutableArray <BigLifeRecord> Present, ImmutableArray <BigLifeRecord> NotPresent) CreateSortAndSetTestData(
            int tstControlCount, int numPresentSamples, int numNotPresent)
        {
            if (tstControlCount < 1)
            {
                throw new ArgumentNotPositiveException <int>(nameof(tstControlCount), tstControlCount);
            }
            if (numPresentSamples >= tstControlCount || numPresentSamples < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(numPresentSamples), numPresentSamples,
                                                      @$ "Parameter must be positive and less than {nameof(tstControlCount)}.");
            }
            if (numNotPresent >= tstControlCount || numNotPresent < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(numNotPresent), numNotPresent,
                                                      @$ "Parameter must be positive and less than {nameof(tstControlCount)}.");
            }
            SortedSet <BigLifeRecord> bigLifeRecords = new SortedSet <BigLifeRecord>(GetRandomRecords(tstControlCount * 2));

            while (bigLifeRecords.Count < tstControlCount * 2)
            {
                bigLifeRecords.Add(_rgen.GetRandomBigLifeRecordData());
            }
            List <BigLifeRecord> control = new List <BigLifeRecord>(bigLifeRecords.Take(tstControlCount));

            _rgen.Shuffle <List <BigLifeRecord>, BigLifeRecord>(control);
            var notPresent = bigLifeRecords.Skip(tstControlCount).Take(numNotPresent).ToList();
            List <BigLifeRecord> present = new List <BigLifeRecord>(control.Take(numPresentSamples));

            _rgen.Shuffle <List <BigLifeRecord>, BigLifeRecord>(notPresent);

            bigLifeRecords.Clear();
            HashSet <BigLifeRecord> controlSet = new HashSet <BigLifeRecord>(control);

            bool allInPresentInControl = controlSet.IsProperSupersetOf(present);
            bool noneInControl         = !controlSet.Overlaps(notPresent) &&
                                         !(new HashSet <BigLifeRecord>(notPresent)).Overlaps(present);

            controlSet.Clear();
            if (!allInPresentInControl || !noneInControl)
            {
                throw new Exception("Logic error in setting up test data!");
            }
            var vault = new ReadWriteValueListVault <BigLifeRecord>(VsEnumerableWrapper <BigLifeRecord> .FromIEnumerable(control));

            using var lck = vault.RoLock();

            if (lck.Count != control.Count)
            {
                throw new Exception("vault count != control count.");
            }

            int ctrlCount = 0;

            foreach (ref readonly var record in lck)
            {
                if (record != control[ctrlCount++])
                {
                    throw new Exception($"itm at idx {ctrlCount - 1} not equal.");
                }
            }

            return(control, vault, present.ToImmutableArray(), notPresent.ToImmutableArray());
        }