Example #1
0
 public void Test_BitFieldArray_Creation()
 {
     using (var bfa = new BitFieldArray())
     {
         Assert.Equal(0, bfa.NumBits);
         Assert.Equal(0, bfa.MemorySize());
     }
 }
Example #2
0
 public void Test_BitFieldArray_Initialise1()
 {
     using (var bfa = new BitFieldArray())
     {
         // Initialise with just a count of bits and records
         bfa.Initialise(10, 100);
         Assert.Equal(1000, bfa.NumBits);
         Assert.Equal(125, bfa.MemorySize()); // 125 bytes to store 1000 bits
     }
 }
Example #3
0
        public void Test_BitFieldArray_MultiRecordMultiBitField_WithDescriptor()
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new[]
                {
                    new BitFieldArrayRecordsDescriptor()
                    {
                        BitsPerRecord = 11, NumRecords = 1000
                    },
                };

                bfa.Initialise(fieldsArray);

                Assert.Equal(11000, bfa.NumBits);
                Assert.Equal(172, bfa.NumStorageElements());
                Assert.Equal(1375, bfa.MemorySize());

                var descriptor = new EncodedBitFieldDescriptor();
                AttributeValueRangeCalculator.CalculateAttributeValueRange(new long[] { 0, 2047 }, 0, 2, 0xffffffff, 0, false,
                                                                           ref descriptor);

                // Write a '1234' to the bfa
                bfa.StreamWriteStart();
                try
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        bfa.StreamWrite(1234, descriptor);
                    }
                }
                finally
                {
                    bfa.StreamWriteEnd();
                }

                // Read the value back again

                int bitAddress = 0;

                for (int i = 0; i < 1000; i++)
                {
                    long readValue = bfa.ReadBitField(ref bitAddress, descriptor);

                    Assert.Equal(1234, readValue);
                    Assert.Equal(bitAddress, (i + 1) * 11);
                }
            }
        }
Example #4
0
        public void Test_BitFieldArray_Initialise_FailWithTooLargeToStore()
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new BitFieldArrayRecordsDescriptor[]
                {
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = 8, NumRecords = 260_000_000
                    },
                };

                Action act = () => bfa.Initialise(fieldsArray);
                act.Should().Throw <TRexPersistencyException>().WithMessage("*AllocateBuffer limited to*in size*");
            }
        }
Example #5
0
        public void Test_BitFieldArray_Initialise_FailWithTooLargeForUintError()
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new BitFieldArrayRecordsDescriptor[]
                {
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = 100, NumRecords = 1_000_000_000
                    },
                };

                Action act = () => bfa.Initialise(fieldsArray);
                act.Should().Throw <TRexPersistencyException>().WithMessage("Attempt to create bit field array with*");
            }
        }
Example #6
0
        public void Test_BitFieldArray_StreamWriteEnd_FailWithImproperStreamEndPosition()
        {
            using (var bfa = new BitFieldArray())
            {
                bfa.Initialise(1, 1);

                // Write a '1' to the bfa
                bfa.StreamWriteStart();
                bfa.StreamWrite(1, 1);
                bfa.StreamWriteStart();                  // Set bit pos back to 0

                Action act = () => bfa.StreamWriteEnd(); // throw exception
                act.Should().Throw <TRexException>().WithMessage("*Stream bit position is not after last bit in storage*");
            }
        }
Example #7
0
        public void Test_BitFieldArray_MultiRecordMultiBitField_WithoutDescriptor()
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new[]
                {
                    new BitFieldArrayRecordsDescriptor()
                    {
                        BitsPerRecord = 11, NumRecords = 1000
                    },
                };

                bfa.Initialise(fieldsArray);

                Assert.Equal(11000, bfa.NumBits);
                Assert.Equal(172, bfa.NumStorageElements());
                Assert.Equal(1375, bfa.MemorySize());

                // Write a '1234' to the bfa
                bfa.StreamWriteStart();
                try
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        bfa.StreamWrite(1234, 11);
                    }
                }
                finally
                {
                    bfa.StreamWriteEnd();
                }


                // Read the value back again

                int bitAddress = 0;

                for (int i = 0; i < 1000; i++)
                {
                    long readValue = bfa.ReadBitField(ref bitAddress, 11);

                    Assert.Equal(1234, readValue);
                    Assert.Equal(bitAddress, (i + 1) * 11);
                }
            }
        }
Example #8
0
        public void WriteBitFieldVector_FailWithValuesArraySize()
        {
            long[] values     = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var    descriptor = new EncodedBitFieldDescriptor();

            descriptor.RequiredBits = 4;

            using (var bfa = new BitFieldArray())
            {
                bfa.Initialise(descriptor.RequiredBits, values.Length);

                int bitLocation = 0;

                Action act = () => bfa.WriteBitFieldVector(ref bitLocation, descriptor, values.Length + 1, values);
                act.Should().Throw <ArgumentException>().WithMessage("Supplied values array is null or not large enough*");
            }
        }
Example #9
0
        public void WriteBitFieldVector_FailWithStorageSize()
        {
            long[] values     = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var    descriptor = new EncodedBitFieldDescriptor();

            descriptor.RequiredBits = 4;

            using (var bfa = new BitFieldArray())
            {
                bfa.Initialise(descriptor.RequiredBits, values.Length - 1);

                int bitLocation = 0;

                Action act = () => bfa.WriteBitFieldVector(ref bitLocation, descriptor, values.Length, values);
                act.Should().Throw <ArgumentException>().WithMessage("Insufficient storage present to satisfy*");
            }
        }
Example #10
0
        public void Test_BitFieldArray_SingleRecordSingleVariableSizeField_WithDescriptor
            (int bitsPerRecord, int numRecords, int minValue, int maxValue, int mask, long valueToWrite,
            int numStorageElements, int memorySize, int nativeNullValue, bool fieldIsNullable, int expectedNumValues)
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new[]
                {
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = bitsPerRecord, NumRecords = numRecords
                    },
                };

                bfa.Initialise(fieldsArray);

                bitsPerRecord.Should().Be(bfa.NumBits);
                numStorageElements.Should().Be(bfa.NumStorageElements());
                Assert.Equal((int)memorySize, bfa.MemorySize());

                var descriptor = new EncodedBitFieldDescriptor();
                AttributeValueRangeCalculator.CalculateAttributeValueRange(new long[] { minValue, maxValue }, 0, 2, mask,
                                                                           nativeNullValue, fieldIsNullable, ref descriptor);

                descriptor.RequiredBits.Should().Be((byte)bitsPerRecord);
                descriptor.NumValues.Should().Be(expectedNumValues);

                // Write a value to the bfa
                bfa.StreamWriteStart();
                try
                {
                    bfa.StreamWrite(valueToWrite, descriptor);
                }
                finally
                {
                    bfa.StreamWriteEnd();
                }

                // Read the value back again

                int  bitAddress = 0;
                long readValue  = bfa.ReadBitField(ref bitAddress, descriptor);

                valueToWrite.Should().Be(readValue);
                bitsPerRecord.Should().Be(bitAddress);
            }
        }
Example #11
0
        public void Read(BinaryReader reader)
        {
            segmentPassCount = reader.ReadInt32();

            var passCountBuffer = GenericArrayPoolCacheHelper <long> .Caches().Rent(SubGridTreeConsts.CellsPerSubGrid);

            try
            {
                var fieldDescriptor = new EncodedBitFieldDescriptor();
                fieldDescriptor.Read(reader);

                using (var bfa = new BitFieldArray())
                {
                    bfa.Read(reader);
                    int bitLocation = 0;
                    bfa.ReadBitFieldVector(ref bitLocation, fieldDescriptor, SubGridTreeConsts.CellsPerSubGrid, passCountBuffer);
                }

                var counter = 0;

                for (var i = 0; i < SubGridTreeConsts.SubGridTreeDimension; i++)
                {
                    for (var j = 0; j < SubGridTreeConsts.SubGridTreeDimension; j++)
                    {
                        var passCount = (int)passCountBuffer[counter++];

                        if (passCount > 0)
                        {
                            AllocatePasses(i, j, passCount);

                            _passData[i, j].Passes.Count = passCount;
                            var passes = _passData[i, j].Passes;
                            for (int cpi = passes.Offset, limit = passes.OffsetPlusCount; cpi < limit; cpi++)
                            {
                                passes.Elements[cpi].Read(reader);
                            }
                        }
                    }
                }
            }
            finally
            {
                GenericArrayPoolCacheHelper <long> .Caches().Return(ref passCountBuffer);
            }
        }
Example #12
0
        public void WriteBitFieldVector_Success_PartialWrite()
        {
            long[] values     = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var    descriptor = new EncodedBitFieldDescriptor();

            descriptor.RequiredBits = 4;
            int partialWriteSize = 5;

            using (var bfa = new BitFieldArray())
            {
                bfa.Initialise(descriptor.RequiredBits, values.Length);

                int bitLocation = 0;
                bfa.WriteBitFieldVector(ref bitLocation, descriptor, partialWriteSize, values);

                bitLocation.Should().Be(descriptor.RequiredBits * partialWriteSize);
            }
        }
Example #13
0
        public void Test_BitFieldArray_SingleRecordSingleBitField_WithDescriptor()
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new[]
                {
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = 1, NumRecords = 1
                    },
                };

                bfa.Initialise(fieldsArray);

                Assert.Equal(1, bfa.NumBits);
                Assert.Equal(1, bfa.NumStorageElements());
                Assert.Equal(1, bfa.MemorySize());

                var descriptor = new EncodedBitFieldDescriptor();
                AttributeValueRangeCalculator.CalculateAttributeValueRange(new long[] { 0, 1 }, 0, 2, 0xffffffff, 0, false,
                                                                           ref descriptor);

                // Write a '1' to the bfa
                bfa.StreamWriteStart();
                try
                {
                    bfa.StreamWrite(1, descriptor);
                }
                finally
                {
                    bfa.StreamWriteEnd();
                }

                // Read the value back again

                int  bitAddress = 0;
                long readValue  = bfa.ReadBitField(ref bitAddress, descriptor);

                Assert.Equal(1, readValue);
                Assert.Equal(1, bitAddress);
            }
        }
Example #14
0
        public void Test_BitFieldArray_Initialise2()
        {
            using (var bfa = new BitFieldArray())
            {
                // Initialise with more than one field in a record

                BitFieldArrayRecordsDescriptor[] fieldsArray = new BitFieldArrayRecordsDescriptor[]
                {
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = 10, NumRecords = 100
                    },
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = 25, NumRecords = 500
                    }
                };

                bfa.Initialise(fieldsArray);

                Assert.Equal(13500, bfa.NumBits);
                Assert.Equal(1688, bfa.MemorySize()); // 1688 bytes to store 13500 bits
            }
        }
Example #15
0
        public void Test_BitFieldArray_SingleRecordSingleVariableSizeField_WithoutDescriptor
            (int bitsPerRecord, int numRecords, long valueToWrite, int numStorageElements, int memorySize)
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new[]
                {
                    new BitFieldArrayRecordsDescriptor {
                        BitsPerRecord = bitsPerRecord, NumRecords = numRecords
                    },
                };

                bfa.Initialise(fieldsArray);

                bitsPerRecord.Should().Be(bfa.NumBits);
                numStorageElements.Should().Be(bfa.NumStorageElements());
                Assert.Equal((int)memorySize, bfa.MemorySize());

                // Write a value to the bfa
                bfa.StreamWriteStart();
                try
                {
                    bfa.StreamWrite(valueToWrite, (int)bitsPerRecord);
                }
                finally
                {
                    bfa.StreamWriteEnd();
                }

                // Read the value back again

                int  bitAddress = 0;
                long readValue  = bfa.ReadBitField(ref bitAddress, (int)bitsPerRecord);

                valueToWrite.Should().Be(readValue);
                bitsPerRecord.Should().Be(bitAddress);
            }
        }
Example #16
0
        public void Test_BitFieldArray_SingleRecordSingleBitField_WithoutDescriptor()
        {
            using (var bfa = new BitFieldArray())
            {
                BitFieldArrayRecordsDescriptor[] fieldsArray = new[]
                {
                    new BitFieldArrayRecordsDescriptor()
                    {
                        BitsPerRecord = 1, NumRecords = 1
                    },
                };

                bfa.Initialise(fieldsArray);

                Assert.Equal(1, bfa.NumBits);
                Assert.Equal(1, bfa.NumStorageElements());
                Assert.Equal(1, bfa.MemorySize());

                // Write a '1' to the bfa
                bfa.StreamWriteStart();
                try
                {
                    bfa.StreamWrite(1, 1);
                }
                finally
                {
                    bfa.StreamWriteEnd();
                }

                // Read the value back again

                int  bitAddress = 0;
                long readValue  = bfa.ReadBitField(ref bitAddress, 1);

                Assert.Equal(1, readValue);
                Assert.Equal(1, bitAddress);
            }
        }
Example #17
0
        public void ReadBitFieldVector_SuccessPartial()
        {
            long[] values     = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var    descriptor = new EncodedBitFieldDescriptor();

            descriptor.RequiredBits = 4;

            using (var bfa = new BitFieldArray())
            {
                bfa.Initialise(descriptor.RequiredBits, values.Length);

                int bitLocation = 0;
                bfa.WriteBitFieldVector(ref bitLocation, descriptor, values.Length, values);

                // Read the vector back
                bitLocation = 0;
                long[] outValues = new long[5];
                bfa.ReadBitFieldVector(ref bitLocation, descriptor, outValues.Length, outValues);
                bitLocation.Should().Be(outValues.Length * descriptor.RequiredBits);

                outValues.ForEach((l, i) => l.Should().Be(values[i]));
            }
        }
Example #18
0
        public void ReadBitFieldVector_Success(byte valueBits)
        {
            long[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            var descriptor = new EncodedBitFieldDescriptor();

            descriptor.RequiredBits = valueBits;

            using (var bfa = new BitFieldArray())
            {
                bfa.Initialise(valueBits, values.Length);

                int bitLocation = 0;
                bfa.WriteBitFieldVector(ref bitLocation, descriptor, values.Length, values);

                // Read the vector back
                bitLocation = 0;
                long[] outValues = new long[values.Length];
                bfa.ReadBitFieldVector(ref bitLocation, descriptor, outValues.Length, outValues);
                bitLocation.Should().Be(outValues.Length * valueBits);

                outValues.Should().BeEquivalentTo(values);
            }
        }