public void LengthOfValueBeingSetCanNotExceedFieldLength()
        {
            var maxLength = new DbaseFieldLength(
                short.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.GenerateDbaseInt16LengthLessThan(maxLength);

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

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

            Assert.Throws <FormatException>(() => sut.Value = value);
        }
        public void CanReadWriteNegative()
        {
            var value = Convert.ToInt16(Math.Abs(_fixture.Create <short>()) * -1);
            var sut   = new DbaseNullableInt16(
                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 DbaseNullableInt16(sut.Field);
                    result.Read(reader);

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

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

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

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

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

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

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