public void CanReadWriteNegative()
        {
            var value = Math.Abs(_fixture.Create <int>()) * -1;
            var sut   = new DbaseNullableInt32(
                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 DbaseNullableInt32(sut.Field);
                    result.Read(reader);

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Equal(sut.Value, result.Value);
                }
            }
        }
        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);

            var sut =
                new DbaseNullableInt32(
                    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);

            Assert.Throws <FormatException>(() => sut.Value = value);
        }
        public void CanReadWriteNull()
        {
            var sut = _fixture.Create <DbaseNullableInt32>();

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

                    Assert.Equal(sut.Field, result.Field);
                    Assert.Equal(sut.Value, result.Value);
                }
            }
        }
        public int?GenerateAcceptableValue(DbaseNullableInt32 value)
        {
            var result = default(int?);

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

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

            return(result);
        }
        public void CanNotReadPastEndOfStream()
        {
            var sut = _fixture.Create <DbaseNullableInt32>();

            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 DbaseNullableInt32(sut.Field);
                    Assert.Throws <EndOfStreamException>(() => result.Read(reader));
                }
            }
        }
 public void Visit(DbaseNullableInt32 value)
 {
     value.Value = _generator.GenerateAcceptableValue(value);
 }
Example #7
0
 public void Visit(DbaseNullableInt32 value)
 {
     HashCode = value.Value?.GetHashCode() ?? 0;
 }
Example #8
0
 public void Visit(DbaseNullableInt32 value)
 {
     Comparer = new DelegatingDbaseFieldValueEqualityComparer <DbaseNullableInt32>(
         new DbaseNullableInt32EqualityComparer());
 }