public void MoveToTest_SeekableReader() { using (var ms = new MemoryStream()) { var writer = new StreamWriter(ms, Encoding.Unicode); writer.Write(FixedWidthRecordReaderTestData.SampleData1); writer.Flush(); ms.Position = 0; using (var fix = new FixedWidthRecordReader(new StreamReader(ms, Encoding.Unicode))) { fix.StartCachingRecordPositions(); FixedWidthRecordReaderTestData.SetupReaderForSampleData1(fix); while (fix.Read() == ReadResult.Success) { } for (int i = FixedWidthRecordReaderTestData.SampleData1RecordCount - 1; i >= 0; i--) { Assert.AreEqual(ReadResult.Success, fix.MoveTo(i)); FixedWidthRecordReaderTestData.CheckSampleData1(fix, i); } } } }
public static void SetupReaderForSampleData1(FixedWidthRecordReader fix) { for (int i = 0; i < SampleData1ColumnCount; i++) { fix.Columns.Add(new FixedWidthRecordColumn("c" + i.ToString(), i * 4, 4)); } }
public static void ReadAll(FixedRecordReaderBenchmarkArguments args) { using (var reader = new FixedWidthRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize)) { reader.SkipEmptyLines = args.SkipEmptyLines; for (int i = 0; i < ColumnCount; i++) reader.Columns.Add(new FixedWidthRecordColumn(reader.GetDefaultColumnName(i), typeof(string), string.Empty, i * ColumnWidth, ColumnWidth)); string s; if (args.FieldIndex < 0) { while (reader.Read() != ReadResult.EndOfFile) { for (int i = 0; i < reader.Columns.Count - 1; i++) s = reader[i]; } } else { while (reader.Read() != ReadResult.EndOfFile) { for (int i = 0; i < args.FieldIndex + 1; i++) s = reader[i]; } } } }
public static void ReadAll(FixedRecordReaderBenchmarkArguments args) { using (var reader = new FixedWidthRecordReader(new StreamReader(args.Path, args.Encoding, true, args.BufferSize), args.BufferSize)) { reader.SkipEmptyLines = args.SkipEmptyLines; for (int i = 0; i < ColumnCount; i++) { reader.Columns.Add(new FixedWidthRecordColumn(reader.GetDefaultColumnName(i), typeof(string), string.Empty, i * ColumnWidth, ColumnWidth)); } string s; if (args.FieldIndex < 0) { while (reader.Read() != ReadResult.EndOfFile) { for (int i = 0; i < reader.Columns.Count - 1; i++) { s = reader[i]; } } } else { while (reader.Read() != ReadResult.EndOfFile) { for (int i = 0; i < args.FieldIndex + 1; i++) { s = reader[i]; } } } } }
public static void CheckSampleData1(FixedWidthRecordReader fix, long recordIndex) { Assert.AreEqual(recordIndex, fix.CurrentRecordIndex); Assert.AreEqual(SampleData1ColumnCount, fix.Columns.Count); for (int i = 0; i < _data1[recordIndex].Length; i++) Assert.AreEqual(_data1[recordIndex][i], fix[i]); }
public void MoveToTest3() { using (var fix = new FixedWidthRecordReader(new StringReader(FixedWidthRecordReaderTestData.SampleData1))) { FixedWidthRecordReaderTestData.SetupReaderForSampleData1(fix); Assert.AreEqual(ReadResult.EndOfFile, fix.MoveTo(FixedWidthRecordReaderTestData.SampleData1RecordCount)); } }
public void FirstColumnStartPositionIsNonZeroTest() { using (var reader = new FixedWidthRecordReader(new StringReader("abcdefThisghijklmnop"))) { reader.Columns.Add(new FixedWidthRecordColumn("field1", typeof(string), 6, 4)); Assert.AreEqual(ReadResult.Success, reader.Read()); Assert.AreEqual("This", reader[0]); } }
public static void CheckSampleData1(FixedWidthRecordReader fix, long recordIndex) { Assert.AreEqual(recordIndex, fix.CurrentRecordIndex); Assert.AreEqual(SampleData1ColumnCount, fix.Columns.Count); for (int i = 0; i < _data1[recordIndex].Length; i++) { Assert.AreEqual(_data1[recordIndex][i], fix[i]); } }
public void TrimPaddingAlignRightTest() { using (var reader = new FixedWidthRecordReader(new StringReader(" 123"))) { reader.Columns.Add(new FixedWidthRecordColumn("a", 0, 6)); reader.Columns[0].TrimPadding = true; reader.Columns[0].ValueAlignment = RecordColumnAlignment.Right; Assert.IsTrue(reader.Read() == ReadResult.Success); Assert.AreEqual("123", reader[0]); } }
public void NonContiguousColumnsTest() { using (var reader = new FixedWidthRecordReader(new StringReader("123123412345"))) { reader.Columns.Add(new FixedWidthRecordColumn("a", 0, 3)); reader.Columns.Add(new FixedWidthRecordColumn("c", 7, 5)); Assert.IsTrue(reader.Read() == ReadResult.Success); Assert.AreEqual("123", reader[0]); Assert.AreEqual("12345", reader[1]); } }
public void MoveToTest1() { using (var fix = new FixedWidthRecordReader(new StringReader(FixedWidthRecordReaderTestData.SampleData1))) { FixedWidthRecordReaderTestData.SetupReaderForSampleData1(fix); for (int i = 0; i < FixedWidthRecordReaderTestData.SampleData1RecordCount; i++) { fix.MoveTo(i); FixedWidthRecordReaderTestData.CheckSampleData1(fix, i); } } }
public static void CheckSampleData1(FixedWidthRecordReader fix, bool readToEnd) { if (!readToEnd) CheckSampleData1(fix, fix.CurrentRecordIndex); else { long recordIndex = 0; while (fix.Read() == ReadResult.Success) { CheckSampleData1(fix, recordIndex); recordIndex++; } Assert.AreEqual(SampleData1RecordCount - 1, fix.CurrentRecordIndex); } }
public void SkipEmptyLinesTest2() { using (var fix = new FixedWidthRecordReader(new StringReader("00\n\n10"))) { fix.Columns.Add(new FixedWidthRecordColumn("c1", 0, 2)); fix.SkipEmptyLines = true; Assert.AreEqual(ReadResult.Success, fix.Read()); Assert.AreEqual(1, fix.Columns.Count); Assert.AreEqual("00", fix[0]); Assert.AreEqual(ReadResult.Success, fix.Read()); Assert.AreEqual(1, fix.Columns.Count); Assert.AreEqual("10", fix[0]); Assert.AreEqual(ReadResult.EndOfFile, fix.Read()); } }
public static void CheckSampleData1(FixedWidthRecordReader fix, bool readToEnd) { if (!readToEnd) { CheckSampleData1(fix, fix.CurrentRecordIndex); } else { long recordIndex = 0; while (fix.Read() == ReadResult.Success) { CheckSampleData1(fix, recordIndex); recordIndex++; } Assert.AreEqual(SampleData1RecordCount - 1, fix.CurrentRecordIndex); } }
protected override IDataReader CreateDataReaderInstance() { var reader = new FixedWidthRecordReader(new StringReader(FixedWidthRecordReaderTestData.SampleTypedData1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Boolean", typeof(System.Boolean), 0, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.DateTime", typeof(System.DateTime), 1, 10)); reader.Columns.Add(new FixedWidthRecordColumn("System.Single", typeof(System.Single), 11, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Double", typeof(System.Double), 12, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Decimal", typeof(System.Decimal), 13, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.SByte", typeof(System.SByte), 14, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Int16", typeof(System.Int16), 15, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Int32", typeof(System.Int32), 16, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Int64", typeof(System.Int64), 17, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Byte", typeof(System.Byte), 18, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.UInt16", typeof(System.UInt16), 19, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.UInt32", typeof(System.UInt32), 20, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.UInt64", typeof(System.UInt64), 21, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Char", typeof(System.Char), 22, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.String", typeof(System.String), 23, 3)); reader.Columns.Add(new FixedWidthRecordColumn("System.Guid", typeof(System.Guid), 26, 38)); return reader; }
public void SkipEmptyLinesTest1() { using (var fix = new FixedWidthRecordReader(new StringReader("00\n\n10"))) { fix.Columns.Add(new FixedWidthRecordColumn("c1", 0, 2)); fix.SkipEmptyLines = false; fix.MissingFieldAction = MissingRecordFieldAction.ReturnEmptyValue; Assert.AreEqual(ReadResult.Success, fix.Read()); Assert.AreEqual(1, fix.Columns.Count); Assert.AreEqual("00", fix[0]); Assert.AreEqual(ReadResult.Success, fix.Read()); Assert.AreEqual(1, fix.Columns.Count); Assert.AreEqual(string.Empty, fix[0]); Assert.AreEqual(ReadResult.Success, fix.Read()); Assert.AreEqual(1, fix.Columns.Count); Assert.AreEqual("10", fix[0]); Assert.AreEqual(ReadResult.EndOfFile, fix.Read()); } }
protected override IDataReader CreateDataReaderInstance() { var reader = new FixedWidthRecordReader(new StringReader(FixedWidthRecordReaderTestData.SampleTypedData1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Boolean", typeof(System.Boolean), 0, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.DateTime", typeof(System.DateTime), 1, 10)); reader.Columns.Add(new FixedWidthRecordColumn("System.Single", typeof(System.Single), 11, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Double", typeof(System.Double), 12, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Decimal", typeof(System.Decimal), 13, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.SByte", typeof(System.SByte), 14, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Int16", typeof(System.Int16), 15, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Int32", typeof(System.Int32), 16, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Int64", typeof(System.Int64), 17, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Byte", typeof(System.Byte), 18, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.UInt16", typeof(System.UInt16), 19, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.UInt32", typeof(System.UInt32), 20, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.UInt64", typeof(System.UInt64), 21, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.Char", typeof(System.Char), 22, 1)); reader.Columns.Add(new FixedWidthRecordColumn("System.String", typeof(System.String), 23, 3)); reader.Columns.Add(new FixedWidthRecordColumn("System.Guid", typeof(System.Guid), 26, 38)); return(reader); }
public void ParsingTest(int bufferSize, int recordCount, int columnCount, int columnWidth) { if (bufferSize < 0) { throw new ArgumentOutOfRangeException(nameof(bufferSize), bufferSize, "Must be 1 or more."); } if (recordCount < 0 || recordCount > 99) { throw new ArgumentOutOfRangeException(nameof(recordCount), recordCount, "Must be between 1 and 99 inclusively."); } if (columnCount < 0 || columnCount > 99) { throw new ArgumentOutOfRangeException(nameof(columnCount), columnCount, "Must be between 1 and 99 inclusively."); } var expectedValues = new string[recordCount][]; var data = new StringBuilder(recordCount * columnCount * columnWidth + recordCount); for (int recordIndex = 0; recordIndex < recordCount; recordIndex++) { expectedValues[recordIndex] = new string[columnCount]; for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { string value = recordIndex.ToString().PadRight(2) + columnIndex.ToString().PadRight(2) + new string(' ', columnWidth - 4); expectedValues[recordIndex][columnIndex] = value; data.Append(value); } data.Append('\n'); } using (var reader = new FixedWidthRecordReader(new StringReader(data.ToString()), bufferSize)) { for (int i = columnCount - 1; i >= 0; i--) { var column = new FixedWidthRecordColumn("col" + (columnCount - i - 1).ToString(), typeof(string), string.Empty, i * columnWidth, columnWidth); column.TrimPadding = false; reader.Columns.Add(column); } int recordIndex = -1; Assert.AreEqual(recordIndex, reader.CurrentRecordIndex); while (reader.Read() == ReadResult.Success) { recordIndex++; Assert.AreEqual(recordIndex, reader.CurrentRecordIndex); for (int i = 0; i < reader.Columns.Count; i++) { Assert.AreEqual(expectedValues[recordIndex][i], reader[columnCount - i - 1]); Assert.AreEqual(expectedValues[recordIndex][i], reader["col" + (columnCount - i - 1).ToString()]); } } Assert.AreEqual(recordCount, recordIndex + 1); Assert.AreEqual(recordCount, reader.CurrentRecordIndex + 1); } }
public void ParsingTest(int bufferSize, int recordCount, int columnCount, int columnWidth) { if (bufferSize < 0) throw new ArgumentOutOfRangeException(nameof(bufferSize), bufferSize, "Must be 1 or more."); if (recordCount < 0 || recordCount > 99) throw new ArgumentOutOfRangeException(nameof(recordCount), recordCount, "Must be between 1 and 99 inclusively."); if (columnCount < 0 || columnCount > 99) throw new ArgumentOutOfRangeException(nameof(columnCount), columnCount, "Must be between 1 and 99 inclusively."); var expectedValues = new string[recordCount][]; var data = new StringBuilder(recordCount * columnCount * columnWidth + recordCount); for (int recordIndex = 0; recordIndex < recordCount; recordIndex++) { expectedValues[recordIndex] = new string[columnCount]; for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) { string value = recordIndex.ToString().PadRight(2) + columnIndex.ToString().PadRight(2) + new string(' ', columnWidth - 4); expectedValues[recordIndex][columnIndex] = value; data.Append(value); } data.Append('\n'); } using (var reader = new FixedWidthRecordReader(new StringReader(data.ToString()), bufferSize)) { for (int i = columnCount - 1; i >= 0; i--) { var column = new FixedWidthRecordColumn("col" + (columnCount - i - 1).ToString(), typeof(string), string.Empty, i * columnWidth, columnWidth); column.TrimPadding = false; reader.Columns.Add(column); } int recordIndex = -1; Assert.AreEqual(recordIndex, reader.CurrentRecordIndex); while (reader.Read() == ReadResult.Success) { recordIndex++; Assert.AreEqual(recordIndex, reader.CurrentRecordIndex); for (int i = 0; i < reader.Columns.Count; i++) { Assert.AreEqual(expectedValues[recordIndex][i], reader[columnCount - i - 1]); Assert.AreEqual(expectedValues[recordIndex][i], reader["col" + (columnCount - i - 1).ToString()]); } } Assert.AreEqual(recordCount, recordIndex + 1); Assert.AreEqual(recordCount, reader.CurrentRecordIndex + 1); } }
public static void SetupReaderForSampleData1(FixedWidthRecordReader fix) { for (int i = 0; i < SampleData1ColumnCount; i++) fix.Columns.Add(new FixedWidthRecordColumn("c" + i.ToString(), i * 4, 4)); }