public void CanReadWriteNull()
        {
            var sut = _fixture.Create <DbaseFloat>();

            sut.Value = null;

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    sut.Write(writer);
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var result = new DbaseFloat(sut.Field);
                    result.Read(reader);

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Equal(sut.Value, result.Value);
                }
            }
        }
        public void AcceptsInt16ValueReturnsExpectedResult(int length, short value, bool accepted)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            var result = sut.AcceptsValue(value);

            Assert.Equal(accepted, result);
        }
        public void TrySetValueAsNullableInt16ReturnsExpectedResult(int length, short?value, bool expected)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            var result = sut.TrySetValueAsNullableInt16(value);

            Assert.Equal(expected, result);
        }
        public void TryGetValueAsInt32ReturnsExpectedResult(int length, float?value, bool gotten, int gottenValueAsInt32)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ), value);

            var result = sut.TryGetValueAsInt32(out var valueAsInt32);

            Assert.Equal(gotten, result);
            Assert.Equal(gottenValueAsInt32, valueAsInt32);
        }
        public void LengthOfNegativeValueBeingSetCanNotExceedFieldLength()
        {
            var length       = DbaseFloat.MaximumLength;
            var decimalCount = _fixture.GenerateDbaseSingleDecimalCount(length);

            var sut =
                new DbaseFloat(
                    new DbaseField(
                        _fixture.Create <DbaseFieldName>(),
                        DbaseFieldType.Float,
                        _fixture.Create <ByteOffset>(),
                        length,
                        decimalCount
                        )
                    );

            Assert.Throws <ArgumentException>(() => sut.Value = float.MinValue);
        }
        public void CanReadWriteWithMaxDecimalCount()
        {
            var length       = DbaseFloat.MaximumLength;
            var decimalCount = DbaseDecimalCount.Min(DbaseFloat.MaximumDecimalCount,
                                                     new DbaseDecimalCount(length.ToInt32() - 2));
            var sut =
                new DbaseFloat(
                    new DbaseField(
                        _fixture.Create <DbaseFieldName>(),
                        DbaseFieldType.Float,
                        _fixture.Create <ByteOffset>(),
                        length,
                        decimalCount
                        )
                    );

            using (var random = new PooledRandom())
            {
                sut.Value =
                    new DbaseFieldNumberGenerator(random)
                    .GenerateAcceptableValue(sut);

                using (var stream = new MemoryStream())
                {
                    using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                    {
                        sut.Write(writer);
                        writer.Flush();
                    }

                    stream.Position = 0;

                    using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                    {
                        var result = new DbaseFloat(sut.Field);
                        result.Read(reader);

                        Assert.Equal(sut, result, new DbaseFieldValueEqualityComparer());
                    }
                }
            }
        }
        public void SetValueAsNullableInt16ReturnsExpectedResult(int length, short?value, bool expected)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ));

            if (!expected)
            {
                Assert.Throws <FormatException>(() =>
                {
                    sut.ValueAsNullableInt16 = value;
                });
            }
            else
            {
                sut.ValueAsNullableInt16 = value;
            }
        }
        public void CanNotReadPastEndOfStream()
        {
            var sut = _fixture.Create <DbaseFloat>();

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    writer.Write(_fixture.CreateMany <byte>(new Random().Next(0, sut.Field.Length.ToInt32())).ToArray());
                    writer.Flush();
                }

                stream.Position = 0;

                using (var reader = new BinaryReader(stream, Encoding.ASCII, true))
                {
                    var result = new DbaseFloat(sut.Field);
                    Assert.Throws <EndOfStreamException>(() => result.Read(reader));
                }
            }
        }
        public void WritesExcessDecimalsAsZero()
        {
            var length       = _fixture.GenerateDbaseSingleLength();
            var decimalCount = _fixture.GenerateDbaseSingleDecimalCount(length);
            var sut          = new DbaseFloat(
                new DbaseField(
                    _fixture.Create <DbaseFieldName>(),
                    DbaseFieldType.Float,
                    _fixture.Create <ByteOffset>(),
                    length,
                    decimalCount
                    ), 0.0f);

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream, Encoding.ASCII, true))
                {
                    sut.Write(writer);
                    writer.Flush();
                }

                stream.Position = 0;

                if (decimalCount.ToInt32() == 0)
                {
                    Assert.Equal(
                        "0".PadLeft(length.ToInt32()),
                        Encoding.ASCII.GetString(stream.ToArray()));
                }
                else
                {
                    Assert.Equal(
                        new string(' ', length.ToInt32() - decimalCount.ToInt32() - 2)
                        + "0."
                        + new string('0', decimalCount.ToInt32()),
                        Encoding.ASCII.GetString(stream.ToArray())
                        );
                }
            }
        }
        public void GetValueAsNullableInt16ReturnsExpectedResult(int length, float?value, bool gotten, short?gottenValueAsInt16)
        {
            var sut = new DbaseFloat(
                DbaseField.CreateFloatField(
                    _fixture.Create <DbaseFieldName>(),
                    new DbaseFieldLength(length),
                    new DbaseDecimalCount(0)
                    ), value);

            if (!gotten)
            {
                Assert.Throws <FormatException>(() =>
                {
                    var _ = sut.ValueAsNullableInt16;
                });
            }
            else
            {
                var result = sut.ValueAsNullableInt16;
                Assert.Equal(gottenValueAsInt16, result);
            }
        }
        public float?GenerateAcceptableValue(DbaseFloat value)
        {
            float?result = default;

            // null only
            if (value.Field.Length < DbaseFloat.MinimumLength)
            {
                return(result);
            }

            // null or positive only
            if (value.Field.Length < DbaseFloat.NegativeValueMinimumLength)
            {
                switch (_random.Next() % 2)
                {
                //case 0: null
                case 1:
                    result = GeneratePositiveNullableSingleValue(value.Field);
                    break;
                }

                return(result);
            }

            // null or positive or negative
            switch (_random.Next() % 3)
            {
            //case 0: null
            case 1:
                result = GeneratePositiveNullableSingleValue(value.Field);
                break;

            case 2:
                result = GenerateNegativeNullableSingleValue(value.Field);
                break;
            }

            return(result);
        }
 public void Visit(DbaseFloat value)
 {
     value.Value = _generator.GenerateAcceptableValue(value);
 }
Пример #13
0
 public void Visit(DbaseFloat value)
 {
     HashCode = value.Value?.GetHashCode() ?? 0;
 }
Пример #14
0
 public void Visit(DbaseFloat value)
 {
     Comparer = new DelegatingDbaseFieldValueEqualityComparer <DbaseFloat>(
         new DbaseFloatEqualityComparer(_precision));
 }