Пример #1
0
        public void CanReadWriteNull()
        {
            var sut = _fixture.Create <DbaseSingle>();

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

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

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

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

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

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

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

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

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Throws <FormatException>(() => sut.Value);
                }
            }
        }
Пример #4
0
        public void LengthOfNegativeValueBeingSetCanNotExceedFieldLength()
        {
            var length       = DbaseSingle.MaximumLength;
            var decimalCount = _fixture.GenerateDbaseSingleDecimalCount(length);

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

            Assert.Throws <ArgumentException>(() => sut.Value = float.MinValue);
        }
Пример #5
0
        public void CanReadWriteWithMaxDecimalCount()
        {
            var length       = DbaseSingle.MaximumLength;
            var decimalCount = DbaseDecimalCount.Min(DbaseSingle.MaximumDecimalCount,
                                                     new DbaseDecimalCount(length.ToInt32() - 2));
            var sut =
                new DbaseSingle(
                    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 DbaseSingle(sut.Field);
                        result.Read(reader);

                        Assert.Equal(sut, result, new DbaseFieldValueEqualityComparer());
                    }
                }
            }
        }
        public void CanNotReadPastEndOfStream()
        {
            var sut = _fixture.Create <DbaseSingle>();

            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 DbaseSingle(sut.Field);
                    Assert.Throws <EndOfStreamException>(() => result.Read(reader));
                }
            }
        }
Пример #7
0
        public void WritesExcessDecimalsAsZero()
        {
            var length       = _fixture.GenerateDbaseSingleLength();
            var decimalCount = _fixture.GenerateDbaseSingleDecimalCount(length);
            var sut          = new DbaseSingle(
                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())
                        );
                }
            }
        }
Пример #8
0
        public float?GenerateAcceptableValue(DbaseSingle value)
        {
            var result = default(float?);

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

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

                return(result);
            }

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

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

            return(result);
        }
Пример #9
0
 public static void CustomizeDbaseSingle(this IFixture fixture)
 {
     fixture.Customize <DbaseSingle>(
         customization =>
         customization
         .FromNumberGenerator(
             generator =>
     {
         var length       = fixture.GenerateDbaseSingleLength();
         var decimalCount = fixture.GenerateDbaseSingleDecimalCount(length);
         var field        = new DbaseField(
             fixture.Create <DbaseFieldName>(),
             DbaseFieldType.Float,
             fixture.Create <ByteOffset>(),
             length,
             decimalCount
             );
         var value   = new DbaseSingle(field);
         value.Value = generator.GenerateAcceptableValue(value);
         return(value);
     }
             )
         .OmitAutoProperties());
 }
 public void Visit(DbaseSingle value)
 {
     value.Value = _generator.GenerateAcceptableValue(value);
 }
Пример #11
0
 public void Inspect(DbaseSingle value)
 {
     HashCode = value.Value.HasValue
         ? value.Value.Value.GetHashCode()
         : 0;
 }
Пример #12
0
 public void Inspect(DbaseSingle value)
 {
     Value = value.Value;
 }
Пример #13
0
 public void Inspect(DbaseSingle value)
 {
     Comparer = new DelegatingDbaseFieldValueEqualityComparer <DbaseSingle>(
         new DbaseSingleEqualityComparer(_precision));
 }
Пример #14
0
 public void Visit(DbaseSingle value)
 {
     HashCode = value.Value.GetHashCode();
 }