Example #1
0
        public void Test_SubgridSegmentCleaving_AdoptCellPassesFrom()
        {
            // Create the sub grid with lots of cell passes
            var subGrid  = MakeSubgridWith10240CellPassesAtOneSecondIntervals();
            var segment1 = subGrid.Cells.PassesData[0];

            // Get the time range
            segment1.PassesData.CalculateTimeRange(out DateTime startSegmentTime, out DateTime endSegmentTime);

            // Create a second segment specially and use the cell pass adopter to move cell passes
            var segment2 = new SubGridCellPassesDataSegment
            {
                Owner       = subGrid,
                SegmentInfo = new SubGridCellPassesDataSegmentInfo()
            };

            segment2.AllocateFullPassStacks();

            SegmentCellPassAdopter.AdoptCellPassesFrom(segment2.PassesData, segment1.PassesData, startSegmentTime + new TimeSpan((endSegmentTime.Ticks - startSegmentTime.Ticks) / 2));

            // Check the times of the adopted cells are correct
            Assert.True(segment1.VerifyComputedAndRecordedSegmentTimeRangeBounds(), "Segment1 has inappropriate cell pass time range compared to segment time range");
            Assert.True(segment2.VerifyComputedAndRecordedSegmentTimeRangeBounds(), "Segment2 has inappropriate cell pass time range compared to segment time range");

            segment1.PassesData.CalculateTotalPasses(out int totalPassCount1, out _, out int maximumPassCount1);
            segment2.PassesData.CalculateTotalPasses(out int totalPassCount2, out _, out int maximumPassCount2);

            Assert.True(10240 == (totalPassCount1 + totalPassCount2), $"Totals ({totalPassCount1} and {totalPassCount2} don't add up to 10240 after cleaving");
            Assert.True(5 == maximumPassCount1, $"Maximum pass count 1 {maximumPassCount1}, is not 5");
            Assert.True(5 == maximumPassCount2, $"Maximum pass count 2 {maximumPassCount2}, is not 5");

            // Check the segment pass count in the segment is correct
            Assert.True(totalPassCount1 == segment1.PassesData.SegmentPassCount, $"Total passes for segment 1 {totalPassCount1} is not equal to segmentPassCount in that segment {segment1.PassesData.SegmentPassCount}");
            Assert.True(totalPassCount2 == segment2.PassesData.SegmentPassCount, $"Total passes for segment 2 {totalPassCount2} is not equal to segmentPassCount in that segment {segment2.PassesData.SegmentPassCount}");
        }
Example #2
0
        public void Test_MutabilityConverterTests_ConvertSubgridSegmentTest()
        {
            // Create a segment with some cell passes. Create a stream fron it then use the
            // mutability converter to convert it to the immutable form. Read this back into an immutable representation
            // and compare the mutable and immutable versions for consistency.

            // Create a mutable segment to contain the passes
            var mutableSegment = new SubGridCellPassesDataSegment
                                     (DIContext.Obtain <ISubGridCellLatestPassesDataWrapperFactory>().NewMutableWrapper_Global(),
                                     DIContext.Obtain <ISubGridCellSegmentPassesDataWrapperFactory>().NewMutableWrapper());

            // Load the mutable stream of information
            SubGridUtilities.SubGridDimensionalIterator((x, y) =>
            {
                var cellPass    = (mutableSegment.LatestPasses as SubGridCellLatestPassDataWrapper_NonStatic)[x, y];
                cellPass.Height = 1234.5678F;
                (mutableSegment.LatestPasses as SubGridCellLatestPassDataWrapper_NonStatic)[x, y] = cellPass;

                // Add 5 passes to each cell
                for (int i = 0; i < 5; i++)
                {
                    cellPass = TestCellPass();

                    // Adjust the height/time so there is a range of values
                    cellPass.Time    = cellPass.Time.AddMinutes(i);
                    cellPass.Height += i;

                    mutableSegment.PassesData.AddPass(x, y, cellPass);
                }
            });

            mutableSegment.SegmentInfo = new SubGridCellPassesDataSegmentInfo(Consts.MIN_DATETIME_AS_UTC, Consts.MAX_DATETIME_AS_UTC, null);

            // Take a copy of the mutable cells and cell passes for later reference
            var mutableLatest = mutableSegment.LatestPasses as SubGridCellLatestPassDataWrapper_NonStatic;

            Cell_NonStatic[,] mutablePasses = mutableSegment.PassesData.GetState();

            MemoryStream mutableStream = new MemoryStream(Consts.TREX_DEFAULT_MEMORY_STREAM_CAPACITY_ON_CREATION);

            using (var writer = new BinaryWriter(mutableStream, Encoding.UTF8, true))
            {
                mutableSegment.Write(writer);
            }

            // Convert the mutable data, using the sourceSegment, into the immutable form and reload it into an immutable segment
            /* todo also test using the mutableStream */
            var mutabilityConverter = new MutabilityConverter();

            mutabilityConverter.ConvertToImmutable(FileSystemStreamType.SubGridSegment, null, mutableSegment, out MemoryStream immutableStream);

            var immutableSegment = new SubGridCellPassesDataSegment
                                       (DIContext.Obtain <ISubGridCellLatestPassesDataWrapperFactory>().NewImmutableWrapper_Segment(),
                                       DIContext.Obtain <ISubGridCellSegmentPassesDataWrapperFactory>().NewImmutableWrapper());

            immutableStream.Position = 0;

            using (var reader = new BinaryReader(immutableStream, Encoding.UTF32, true))
            {
                immutableSegment.Read(reader, true, true);
            }

            var immutableLatest = immutableSegment.LatestPasses as SubGridCellLatestPassDataWrapper_StaticCompressed;

            if (immutableLatest != null)
            {
                // Check height of the latest cells match to tolerance given the compressed lossiness.
                SubGridUtilities.SubGridDimensionalIterator((x, y) =>
                {
                    double mutableValue   = mutableLatest[x, y].Height;
                    double immutableValue = immutableLatest.ReadHeight(x, y);

                    double diff = immutableValue - mutableValue;

                    Assert.True(Math.Abs(diff) <= 0.001,
                                $"Cell height at ({x}, {y}) has unexpected value: {immutableValue} vs {mutableValue}, diff = {diff}");
                });
            }

            // Check the heights specially to account for tolerance differences
            SubGridUtilities.SubGridDimensionalIterator((x, y) =>
            {
                for (int i = 0; i < immutableSegment.PassesData.PassCount(x, y); i++)
                {
                    double mutableValue   = mutableSegment.PassesData.PassHeight(x, y, i);
                    double immutableValue = immutableSegment.PassesData.PassHeight(x, y, i);

                    double diff = immutableValue - mutableValue;

                    Assert.True(Math.Abs(diff) <= 0.001, $"Cell height at ({x}, {y}) has unexpected value: {immutableValue} vs {mutableValue}, diff = {diff}");
                }
            });

            // Check the times specially to account for tolerance differences
            SubGridUtilities.SubGridDimensionalIterator((x, y) =>
            {
                for (int i = 0; i < immutableSegment.PassesData.PassCount(x, y); i++)
                {
                    DateTime mutableValue   = mutableSegment.PassesData.PassTime(x, y, i);
                    DateTime immutableValue = immutableSegment.PassesData.PassTime(x, y, i);

                    TimeSpan diff = mutableValue - immutableValue;

                    Assert.True(diff.Duration() <= TimeSpan.FromSeconds(1), $"Cell time at ({x}, {y}) has unexpected value: {immutableValue} vs {mutableValue}, diff = {diff}");
                }
            });

            // Check that the cell passes in the cell pass stacks for the segment match to tolerance given the compressed lossiness
            SubGridUtilities.SubGridDimensionalIterator((x, y) =>
            {
                for (int i = 0; i < immutableSegment.PassesData.PassCount(x, y); i++)
                {
                    CellPass cellPass = immutableSegment.PassesData.ExtractCellPass(x, y, i);

                    // Force the height and time in the immutable record to be the same as the immutable record
                    // as they have been independently checked above. Also set the machine ID to be the same as the mutable
                    // machine ID as the immutable representation does not include it in the Ignite POC

                    var mutablePass = mutablePasses[x, y].Passes.GetElement(i);
                    cellPass.Time   = mutablePass.Time;
                    cellPass.Height = mutablePass.Height;
                    cellPass.InternalSiteModelMachineIndex = mutablePass.InternalSiteModelMachineIndex;

                    CellPass mutableCellPass = mutablePasses[x, y].Passes.GetElement(i);
                    Assert.True(mutableCellPass.Equals(cellPass), $"Cell passes not equal at Cell[{x}, {y}], cell pass index {i}");
                }
            });
        }