Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        static void RunV3(TextReader rdr, char delim)
        {
            var csvRdr = new Spi.CsvReader3(rdr, delim);

            while (csvRdr.Read())
            {
            }
        }
Ejemplo n.º 3
0
        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();
            });
        }
Ejemplo n.º 4
0
 //[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())
         {
         }
     }
 }
Ejemplo n.º 5
0
 //[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())
         {
         }
     }
 }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
0
        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 "));
            }
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }