示例#1
0
        public void TryReadRecord_MultipleRecordsWithHeader_RecordsAreRead()
        {
            var headerBytes  = new byte[4];
            var record1Bytes = BitConverter.GetBytes((int)100).Concat("Record 1".Select(x => (byte)x).ToArray()).Concat(new byte[] { 0, 0 });
            var record2Bytes = BitConverter.GetBytes((int)200).Concat("Record 2".Select(x => (byte)x).ToArray()).Concat(new byte[] { 0, 0 });
            var fileBytes    = headerBytes.Concat(record1Bytes).Concat(record2Bytes).ToArray();

            File.WriteAllBytes(this.FilePath, fileBytes);

            using (var fileReader = new FixedWidthBinaryFileReader(this.FilePath))
            {
                fileReader.HeaderSize = 4;
                fileReader.RecordSize = 14;
                fileReader.FooterSize = 0;

                fileReader.Open();

                var couldReadRecord1 = fileReader.TryReadRecord(out var record1, out var failures1);
                var couldReadRecord2 = fileReader.TryReadRecord(out var record2, out var failures2);
                var couldReadRecord3 = fileReader.TryReadRecord(out var record3, out var failures3);

                Assert.IsTrue(couldReadRecord1);
                Assert.IsTrue(couldReadRecord2);
                Assert.IsFalse(couldReadRecord3);
                Assert.IsNull(failures1);
                Assert.IsNull(failures2);
                FixedWidthBinaryFileReaderTests.AssertRecordMatches((byte[])record1, 100, "Record 1");
                FixedWidthBinaryFileReaderTests.AssertRecordMatches((byte[])record2, 200, "Record 2");
            }
        }
        public static void Initialize(TestContext testContext)
        {
            FixedWidthBinaryFileReaderTests.TestContext = testContext;

            if (!Directory.Exists(FixedWidthBinaryFileReaderTests.TestFilesDirectory))
            {
                Directory.CreateDirectory(FixedWidthBinaryFileReaderTests.TestFilesDirectory);
            }

            FixedWidthBinaryFileReaderTests.Cleanup();
        }