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

            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 DbaseDecimal(sut.Field);
                    result.Read(reader);

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Equal(sut.Value, result.Value);
                }
            }
        }
        public decimal GenerateAcceptableValue(DbaseDecimal value)
        {
            decimal result = default;

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

            // positive only
            if (value.Field.Length < DbaseNumber.NegativeValueMinimumLength)
            {
                result = GeneratePositiveDecimalValue(value.Field);
                return(result);
            }

            // positive or negative
            switch (_random.Next() % 2)
            {
            case 0:
                result = GeneratePositiveDecimalValue(value.Field);
                break;

            case 1:
                result = GenerateNegativeDecimalValue(value.Field);
                break;
            }

            return(result);
        }
        public void CanReadWriteNull()
        {
            _fixture.CustomizeDbaseDecimalWithoutValue();
            var sut = _fixture.Create <DbaseDecimal>();

            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 DbaseDecimal(sut.Field);
                    result.Read(reader);

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Throws <FormatException>(() => sut.Value);
                }
            }
        }
        public void LengthOfNegativeValueBeingSetCanNotExceedFieldLength()
        {
            var length       = DbaseDecimal.MaximumLength;
            var decimalCount = _fixture.GenerateDbaseDecimalDecimalCount(length);

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

            Assert.Throws <ArgumentException>(() => sut.Value = decimal.MinValue);
        }
        public void CanReadWriteWithMaxDecimalCount()
        {
            var length       = DbaseDecimal.MaximumLength;
            var decimalCount = DbaseDecimal.MaximumDecimalCount;
            var sut          =
                new DbaseDecimal(
                    new DbaseField(
                        _fixture.Create <DbaseFieldName>(),
                        DbaseFieldType.Number,
                        _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 DbaseDecimal(sut.Field);
                        result.Read(reader);

                        Assert.Equal(sut.Field, result.Field);
                        Assert.Equal(sut.Value, result.Value);
                    }
                }
            }
        }
        public void CanNotReadPastEndOfStream()
        {
            var sut = _fixture.Create <DbaseDecimal>();

            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 DbaseDecimal(sut.Field);
                    Assert.Throws <EndOfStreamException>(() => result.Read(reader));
                }
            }
        }
        public void WritesExcessDecimalsAsZero()
        {
            var length       = _fixture.GenerateDbaseDecimalLength();
            var decimalCount = _fixture.GenerateDbaseDecimalDecimalCount(length);
            var sut          = new DbaseDecimal(
                new DbaseField(
                    _fixture.Create <DbaseFieldName>(),
                    DbaseFieldType.Number,
                    _fixture.Create <ByteOffset>(),
                    length,
                    decimalCount
                    ), 0.0m);

            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())
                        );
                }
            }
        }
Exemple #8
0
        public decimal?GenerateAcceptableValue(DbaseDecimal value)
        {
            var result = default(decimal?);

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

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

                return(result);
            }

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

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

            return(result);
        }
        public void CanReadWriteNegative()
        {
            using (var random = new PooledRandom())
            {
                var sut = new DbaseDecimal(
                    new DbaseField(
                        _fixture.Create <DbaseFieldName>(),
                        DbaseFieldType.Number,
                        _fixture.Create <ByteOffset>(),
                        DbaseDecimal.NegativeValueMinimumLength,
                        new DbaseDecimalCount(1)
                        )
                    );
                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 DbaseDecimal(sut.Field);
                        result.Read(reader);

                        Assert.Equal(sut, result, new DbaseFieldValueEqualityComparer());
                    }
                }
            }
        }
Exemple #10
0
 public static void CustomizeDbaseDecimal(this IFixture fixture)
 {
     fixture.Customize <DbaseDecimal>(
         customization =>
         customization
         .FromNumberGenerator(
             generator =>
     {
         var length       = fixture.GenerateDbaseDecimalLength();
         var decimalCount = fixture.GenerateDbaseDecimalDecimalCount(length);
         var field        = new DbaseField(
             fixture.Create <DbaseFieldName>(),
             DbaseFieldType.Number,
             fixture.Create <ByteOffset>(),
             length,
             decimalCount
             );
         var value   = new DbaseDecimal(field);
         value.Value = generator.GenerateAcceptableValue(value);
         return(value);
     }
             )
         .OmitAutoProperties());
 }
 public void Visit(DbaseDecimal value)
 {
     value.Value = _generator.GenerateAcceptableValue(value);
 }
Exemple #12
0
 public void Inspect(DbaseDecimal value)
 {
     HashCode = value.Value.HasValue
         ? value.Value.Value.GetHashCode()
         : 0;
 }
Exemple #13
0
 public void Inspect(DbaseDecimal value)
 {
     Value = value.Value;
 }
Exemple #14
0
 public void Inspect(DbaseDecimal value)
 {
     Comparer = new DelegatingDbaseFieldValueEqualityComparer <DbaseDecimal>(
         new DbaseDecimalEqualityComparer(_precision));
 }
Exemple #15
0
 public void Visit(DbaseDecimal value)
 {
     HashCode = value.Value.GetHashCode();
 }