public void TestFromZeroToOver95()
        {
            var data = GetCoreDataFromAgeIds(
                AgeIds.From0To4,
                AgeIds.From5To9,
                AgeIds.From10To14,
                AgeIds.From15To19,
                AgeIds.From20To24,
                AgeIds.From25To29,
                AgeIds.From30To34,
                AgeIds.From35To39,
                AgeIds.From40To44,
                AgeIds.From45To49,
                AgeIds.From50To54,
                AgeIds.From55To59,
                AgeIds.From60To64,
                AgeIds.From65To69,
                AgeIds.From70To74,
                AgeIds.From75To79,
                AgeIds.From80To84,
                AgeIds.From85To89,
                AgeIds.From90To95,
                AgeIds.Over95
                );
            Shuffle(data);

            var sortedValues = new QuinaryPopulationSorter(data).SortedValues;

            var index = 0;
            Assert.AreEqual(AgeIds.From0To4, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From5To9, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From10To14, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From15To19, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From20To24, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From25To29, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From30To34, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From35To39, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From40To44, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From45To49, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From50To54, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From55To59, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From60To64, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From65To69, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From70To74, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From75To79, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From80To84, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From85To89, sortedValues[index++]);
            Assert.AreEqual(AgeIds.From90To95, sortedValues[index++]);
            Assert.AreEqual(AgeIds.Over95, sortedValues[index]);
        }
        public void AddCcgPopulationValues(IEnumerable<QuinaryPopulation> populations)
        {
            IRange cells = parentAreaDataWorksheet.Cells;
            int row = ccgPopulationDataRow;
            int column = parentAreaDataColumn;

            foreach (var quinaryPopulation in populations)
            {
                IList<double> vals = new QuinaryPopulationSorter(quinaryPopulation.Values).SortedValues;

                foreach (var val in vals)
                {
                    // Round to integer, populations should be atomic
                    cells[row, column++].Value = Math.Round(val, 0, MidpointRounding.AwayFromZero);
                }
            }

            ccgPopulationDataRow++;
        }
        public void Build()
        {
            var metadataRepo = IndicatorMetadataRepository.Instance;

            const int indicatorId = IndicatorIds.QuinaryPopulations;

            Grouping grouping = groupDataReader.GetGroupingsByGroupIdAndIndicatorId(GroupId, indicatorId);

            area = areasReader.GetAreaFromCode(AreaCode);

            var metadata = metadataRepo.GetIndicatorMetadata(indicatorId);
            period = new DataPointOffsetCalculator(grouping, DataPointOffset, metadata.YearType).TimePeriod;

            // Get data for each sex
            int overallTotal = 0;
            foreach (var sexId in sexIds)
            {
                IEnumerable<double> vals;

                if (area.IsCcg)
                {
                    QuinaryPopulation population = practiceReader.GetCcgQuinaryPopulation(indicatorId, period, AreaCode, sexId);
                    vals = new QuinaryPopulationSorter(population.Values).SortedValues;
                }
                else
                {
                    IList<CoreDataSet> data = groupDataReader.GetCoreDataForAllAges(indicatorId, period, AreaCode, sexId);
                    vals = new QuinaryPopulationSorter(data).SortedValues;
                }

                // Add total
                int total = Convert.ToInt32(Math.Round(vals.Sum(), MidpointRounding.AwayFromZero));
                overallTotal += total;

                Values.Add(sexId, vals);
            }

            // Convert to %
            foreach (var sexId in sexIds)
            {
                Values[sexId] = Values[sexId].Select(x => Math.Round((x / overallTotal) * 100, 2, MidpointRounding.AwayFromZero));
            }

            // List size
            var val = new QofListSizeProvider(groupDataReader, area, GroupId, DataPointOffset, metadata.YearType).Value;
            if (val.HasValue)
            {
                metadata = metadataRepo.GetIndicatorMetadata(QofListSizeProvider.IndicatorId);
                ListSize = new ValueData { Value = val.Value };

                var formatter = NumericFormatterFactory.New(metadata, groupDataReader);
                formatter.Format(ListSize);
            }

            if (area.IsGpPractice && AreOnlyPopulationsRequired == false)
            {
                SetEthnicityText();

                SetDeprivationDecile();

                Shape = practiceReader.GetShape(AreaCode);

                AdHocValues = new PracticePerformanceIndicatorValues(groupDataReader, AreaCode, DataPointOffset).IndicatorToValue;
            }
        }