public void ParseCsvTest() { var csvStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sampleCsv)); var csvReader = new Spi.CsvReader3(new StreamReader(csvStream), ','); int sumA = 0; decimal sumB = 0; int line = 0; while (csvReader.Read()) { line++; if (line == 1) { continue; // skip header row } //sumA += Convert.ToInt32(csvReader[0], CultureInfo.InvariantCulture); //sumB += Convert.ToDecimal(csvReader[1], CultureInfo.InvariantCulture); sumA += Int32.Parse(csvReader[0], provider: CultureInfo.InvariantCulture); sumB += Decimal.Parse(csvReader[1], provider: CultureInfo.InvariantCulture); } Assert.Equal(5, line); Assert.Equal(10, sumA); Assert.Equal(12.1M, sumB); }
static void RunV3(TextReader rdr, char delim) { var csvRdr = new Spi.CsvReader3(rdr, delim); while (csvRdr.Read()) { } }
public void LongLineCsvTest() { var test = "ABCDEF,123456"; var csvRdr = new Spi.CsvReader3(new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(test))), ',', buffersize: 5); Assert.Throws <Exception>(() => { csvRdr.Read(); }); }
//[Benchmark] public void Spi_Reader_V3_dir__4k() { using (TextReader rdr = new StreamReader(@"c:\temp\allc.tsv", detectEncodingFromByteOrderMarks: true)) { var csvrdr = new Spi.CsvReader3(rdr, '\t'); while (csvrdr.Read()) { } } }
//[Benchmark] public void Spi_Reader_V3_dir_quoted_32k() { using (TextReader rdr = new StreamReader(@"c:\temp\allc_quoted.csv", detectEncodingFromByteOrderMarks: true)) { var csvrdr = new Spi.CsvReader3(rdr, ',', buffersize: 32 * 1024); while (csvrdr.Read()) { } } }
public void StrangeHeadersTest() { var csvReader = new Spi.CsvReader3(new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(strangeHdrSampleCsv))), ','); Assert.True(csvReader.Read()); Assert.Equal("A\nA", csvReader[0].ToString()); Assert.Equal("B B", csvReader[1].ToString()); Assert.Equal("C\r\n\tC", csvReader[2].ToString()); Assert.True(csvReader.Read()); Assert.Equal("1", csvReader[0].ToString()); Assert.Equal("5.5", csvReader[1].ToString()); Assert.Equal("5 Jun 2014", csvReader[2].ToString()); }
public void TestGetRawValues() { var sb = new StringBuilder(); for (int i = 0; i < 10000; i++) { //sb.AppendLine("Some test value, \"Some value with \"\"quotes\"\"\",\"Simple in quotes\",a "); sb.AppendLine("Some test value,\"Some value with \"\"quotes\"\"\",\"Simple in quotes\",a "); } var csvRdr = new Spi.CsvReader3(new StringReader(sb.ToString()), buffersize: 100); while (csvRdr.Read()) { Assert.True(csvRdr.GetRawValue(0).SequenceEqual("Some test value")); Assert.True(csvRdr.GetRawValue(1).SequenceEqual("Some value with \"\"quotes\"\"")); Assert.True(csvRdr.GetRawValue(2).SequenceEqual("Simple in quotes")); Assert.True(csvRdr.GetRawValue(3).SequenceEqual("a ")); } }
private List <string[]> RunCsvReader(TextReader r, int buffersize = 1024) { List <string[]> data = new List <string[]>(); using (r) { var csvrdr = new Spi.CsvReader3(r, ',', buffersize); while (csvrdr.Read()) { List <string> row = new List <string>(); for (int i = 0; i < csvrdr.FieldCount; ++i) { row.Add(csvrdr[i].ToString()); } data.Add(row.ToArray()); } } return(data); }
public void LargeCsvTest() { var memStream = new MemoryStream(); int UpperLimit = 50_000_000; // note: in Debug mode processing takes much more time in comparing to Release! int records_written = 0; using (var streamWr = new StreamWriter(memStream, Encoding.UTF8, 4096, leaveOpen: true)) { var bVals = new string[] { "b1", "b2", "b3" }; for (int i = 0; i < UpperLimit; i += 10) { //streamWr.WriteLine(String.Format("{0},{1}, \"Quoted\", just a value", i, bVals[i%bVals.Length] )); streamWr.WriteLine(String.Format("{0},{1},\"Quoted\", just a value", i, bVals[i % bVals.Length])); ++records_written; } } memStream.Position = 0; output.WriteLine("CSV len = " + memStream.Length.ToString()); var sw = new System.Diagnostics.Stopwatch(); sw.Start(); var csvRdr = new Spi.CsvReader3(new StreamReader(memStream), buffersize: 64); int record_read = 0; while (csvRdr.Read()) { if (csvRdr[1][0] != 'b') { throw new Exception("Wrong read!"); } //output.WriteLine($"[{csvRdr[0].ToString()}][{csvRdr[1].ToString()}][{csvRdr[2].ToString()}][{csvRdr[3].ToString()}]"); ++record_read; } sw.Stop(); output.WriteLine("Time: {0}ms", sw.ElapsedMilliseconds); Assert.Equal(records_written, record_read); }