예제 #1
0
        public void CanReadWriteNegative()
        {
            var value = Math.Abs(_fixture.Create <int>()) * -1;
            var sut   = new DbaseInt32(
                new DbaseField(
                    _fixture.Create <DbaseFieldName>(),
                    _fixture.GenerateDbaseInt32FieldType(),
                    ByteOffset.Initial,
                    new DbaseFieldLength(value.ToString(CultureInfo.InvariantCulture).Length),
                    new DbaseDecimalCount(0)
                    ),
                value
                );

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

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Equal(sut.Value, result.Value);
                }
            }
        }
예제 #2
0
        public void LengthOfValueBeingSetCanNotExceedFieldLength()
        {
            var maxLength = new DbaseFieldLength(
                int.MaxValue.ToString(CultureInfo.InvariantCulture).Length - 1
                // because it's impossible to create a value longer than this (we need the test to generate a longer value)
                );
            var length = _fixture.GenerateDbaseInt32LengthLessThan(maxLength);

            _out.WriteLine("Length used is: {0}", length.ToString());

            var sut =
                new DbaseInt32(
                    new DbaseField(
                        _fixture.Create <DbaseFieldName>(),
                        _fixture.GenerateDbaseInt32FieldType(),
                        _fixture.Create <ByteOffset>(),
                        length,
                        new DbaseDecimalCount(0)
                        )
                    );

            var value = Enumerable
                        .Range(0, sut.Field.Length.ToInt32())
                        .Aggregate(1, (current, _) => current * 10);

            _out.WriteLine("Value used is: {0}", value.ToString());

            Assert.Throws <ArgumentException>(() => sut.Value = value);
        }
예제 #3
0
        public void CanReadWriteNull()
        {
            var sut = _fixture.Create <DbaseInt32>();

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

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Equal(sut.Value, result.Value);
                }
            }
        }
예제 #4
0
        public void CanReadWriteNull()
        {
            _fixture.CustomizeDbaseInt32WithoutValue();
            var sut = _fixture.Create <DbaseInt32>();

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

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Throws <FormatException>(() => result.Value);
                }
            }
        }
        public bool AcceptsValue(int?value)
        {
            if (value.HasValue)
            {
                return(DbaseInt32.FormatAsString(value.Value).Length <= Field.Length.ToInt32());
            }

            return(true);
        }
예제 #6
0
 public static void WriteAsNullableInt32(this BinaryWriter writer, DbaseField field, int?value)
 {
     if (value.HasValue)
     {
         var unpadded = DbaseInt32.FormatAsString(value.Value);
         writer.WriteLeftPaddedString(unpadded, field.Length.ToInt32(), ' ');
     }
     else
     {
         writer.Write(new string(' ', field.Length.ToInt32()).ToCharArray());
         // or writer.Write(new byte[Field.Length]); // to determine
     }
 }
        public int GenerateAcceptableValue(DbaseInt32 value)
        {
            var result = default(int);

            switch (_random.Next() % 2)
            {
            case 0:
                result = GeneratePositiveInt32Value(value.Field);
                break;

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

            return(result);
        }
예제 #8
0
        public void CanNotReadPastEndOfStream()
        {
            var sut = _fixture.Create <DbaseInt32>();

            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 DbaseInt32(sut.Field);
                    Assert.Throws <EndOfStreamException>(() => result.Read(reader));
                }
            }
        }
예제 #9
0
        public int?GenerateAcceptableValue(DbaseInt32 value)
        {
            if (value.Field.Length == new DbaseFieldLength(0))
            {
                return(null);
            }

            var result = default(int?);

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

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

            return(result);
        }
예제 #10
0
 public static void CustomizeDbaseInt32(this IFixture fixture)
 {
     fixture.Customize <DbaseInt32>(
         customization =>
         customization
         .FromNumberGenerator(
             generator =>
     {
         var fieldType = fixture.GenerateDbaseInt32FieldType();
         var length    = fixture.GenerateDbaseInt32Length(fieldType);
         var field     = new DbaseField(
             fixture.Create <DbaseFieldName>(),
             fieldType,
             fixture.Create <ByteOffset>(),
             length,
             new DbaseDecimalCount(0)
             );
         var value   = new DbaseInt32(field);
         value.Value = generator.GenerateAcceptableValue(value);
         return(value);
     }
             )
         .OmitAutoProperties());
 }
 public void Visit(DbaseInt32 value)
 {
     value.Value = _generator.GenerateAcceptableValue(value);
 }
예제 #12
0
            public FakeDbaseRecord()
            {
                Id = new DbaseInt32(Schema.Id);

                Values = new DbaseFieldValue[] { Id };
            }
예제 #13
0
 public void Inspect(DbaseInt32 value)
 {
     HashCode = value.Value.HasValue
         ? value.Value.Value.GetHashCode()
         : 0;
 }
예제 #14
0
 public void Inspect(DbaseInt32 value)
 {
     Value = value.Value;
 }
예제 #15
0
 public void Inspect(DbaseInt32 value)
 {
     Comparer = new DelegatingDbaseFieldValueEqualityComparer <DbaseInt32>(
         new DbaseInt32EqualityComparer());
 }
예제 #16
0
 public void Visit(DbaseInt32 value)
 {
     HashCode = value.Value.GetHashCode();
 }