public void Constructor_byteArray_MapperArg()
        {
            string data  = $"c0,c1,c2,c3,c4{Environment.NewLine}1,1/1/2017,hello,true,02:10:01";
            var    bytes = System.Text.Encoding.UTF8.GetBytes(data);

            var parser = new DsvParser <Record, RecordMapping>(bytes, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions, new RecordMapping());
        }
Example #2
0
 public void Beffyman_DsvParserGeneric()
 {
     if (Columns == 10)
     {
         var parser = new DsvParser <Record10, DsvRecordMapping10>(MemoryFile, Encoding.UTF8, DsvOptions.DefaultCsvOptions);
     }
     else
     {
         var parser = new DsvParser <Record50, DsvRecordMapping50>(MemoryFile, Encoding.UTF8, DsvOptions.DefaultCsvOptions);
     }
 }
        public void TestNoMappings()
        {
            string data = $"c0,c1,c2,c3,c4{Environment.NewLine}1,1/1/2017,hello,true,02:10:01";

            var parser = new DsvParser <Record, NoRecordMapping>(data, Encoding.UTF8, DsvOptions.DefaultCsvOptions);

            Assert.Equal(1, parser.Rows.Count);

            var record = parser.Rows[0];

            Assert.Equal(default, record.c0);
        public void ValidateParser()
        {
            string data = $"c0,c1,c2,c3,c4{Environment.NewLine}1,1/1/2017,hello,true,02:10:01";

            var parser = new DsvParser <Record, RecordMapping>(data, Encoding.UTF8, DsvOptions.DefaultCsvOptions);

            Assert.Equal(1, parser.Rows.Count);

            var record = parser.Rows[0];

            Assert.Equal(1, record.c0);
            Assert.Equal(DateTime.Parse("1/1/2017"), record.c1);
            Assert.Equal("hello", record.c2);
            Assert.True(record.c3);
            Assert.Equal(TimeSpan.Parse("02:10:01"), record.c4);
        }
Example #5
0
        public void NoHeader()
        {
            string file = FileGenerator("Column", "Data", 1, 3);

            var data = new DsvParser(file, Encoding.UTF8, new DsvOptions(',', '"', false));


            Assert.Equal(0, data.Columns.Length);
            Assert.Equal(2, data.Rows.Count);

            Assert.Equal("Column1", data.Rows[0].Span[0].ToString());
            Assert.Equal("Column2", data.Rows[0].Span[1].ToString());
            Assert.Equal("Column3", data.Rows[0].Span[2].ToString());

            Assert.Equal("Data1", data.Rows[1].Span[0].ToString());
            Assert.Equal("Data2", data.Rows[1].Span[1].ToString());
            Assert.Equal("Data3", data.Rows[1].Span[2].ToString());
        }
Example #6
0
        public void SimpleData_OneDataRow()
        {
            string file = FileGenerator("Column", "Data", 1, 3);

            var data = new DsvParser(file, Encoding.UTF8, DsvOptions.DefaultCsvOptions);


            Assert.Equal(3, data.Columns.Length);
            Assert.Equal(1, data.Rows.Count);

            Assert.Equal("Column1", data.Columns.Span[0].ToString());
            Assert.Equal("Column2", data.Columns.Span[1].ToString());
            Assert.Equal("Column3", data.Columns.Span[2].ToString());

            Assert.Equal("Data1", data.Rows[0].Span[0].ToString());
            Assert.Equal("Data2", data.Rows[0].Span[1].ToString());
            Assert.Equal("Data3", data.Rows[0].Span[2].ToString());
        }
Example #7
0
        public void Constructor_Memory()
        {
            string file = FileGenerator("Column", "Data", 1, 3);
            var    mem  = file.AsMemory();


            var data = new DsvParser(mem, Encoding.UTF8, DsvOptions.DefaultCsvOptions);


            Assert.Equal(3, data.Columns.Length);
            Assert.Equal(1, data.Rows.Count);

            Assert.Equal("Column1", data.Columns.Span[0].ToString());
            Assert.Equal("Column2", data.Columns.Span[1].ToString());
            Assert.Equal("Column3", data.Columns.Span[2].ToString());

            Assert.Equal("Data1", data.Rows[0].Span[0].ToString());
            Assert.Equal("Data2", data.Rows[0].Span[1].ToString());
            Assert.Equal("Data3", data.Rows[0].Span[2].ToString());
        }
Example #8
0
        public void Constructor_ByteArray()
        {
            string file  = FileGenerator("Column", "Data", 1, 3);
            var    bytes = System.Text.Encoding.UTF8.GetBytes(file);


            var data = new DsvParser(bytes, System.Text.Encoding.UTF8, DsvOptions.DefaultCsvOptions);


            Assert.Equal(3, data.Columns.Length);
            Assert.Equal(1, data.Rows.Count);

            Assert.Equal("Column1", data.Columns.Span[0].ToString());
            Assert.Equal("Column2", data.Columns.Span[1].ToString());
            Assert.Equal("Column3", data.Columns.Span[2].ToString());

            Assert.Equal("Data1", data.Rows[0].Span[0].ToString());
            Assert.Equal("Data2", data.Rows[0].Span[1].ToString());
            Assert.Equal("Data3", data.Rows[0].Span[2].ToString());
        }
Example #9
0
        public void JaggedTable()
        {
            const string ValueDelimeter   = "|";
            const string RecordDelimeter  = "\r\n";
            const int    NumRecords       = 4;
            const int    NumValuesOddRow  = 3;
            const int    NumValuesEvenRow = 5;

            var builder = new StringBuilder(128);

            for (var recordNum = 0; recordNum < NumRecords; recordNum++)
            {
                if (recordNum > 0)
                {
                    builder.Append(RecordDelimeter);
                }

                var numValues = recordNum % 2 == 0 ? NumValuesEvenRow : NumValuesOddRow;
                AddRecord(builder, ValueDelimeter, recordNum, numValues);
            }

            var parser = new DsvParser(builder.ToString(), ValueDelimeter, RecordDelimeter);

            var numRecords = parser.RecordsLength;

            Assert.AreEqual(NumRecords, numRecords);
            for (var recordNum = 0; recordNum < numRecords; recordNum++)
            {
                var numValues = parser.GetRecordLength(recordNum);
                Assert.AreEqual(recordNum % 2 == 0 ? NumValuesEvenRow : NumValuesOddRow, numValues);
                for (var valueNum = 0; valueNum < numValues; valueNum++)
                {
                    var value = parser.GetValue(recordNum, valueNum);
                    Assert.AreEqual($"Value {recordNum}-{valueNum}", value.ToString());
                }
            }
        }
Example #10
0
        public void LongDelimeters()
        {
            const string ValueDelimeter  = "This is a very long and terrible value delimeter";
            const string RecordDelimeter = "This is a very long and terrible record delimeter";
            const int    NumRecords      = 3;
            const int    NumValues       = 4;

            var builder = CreateTable(ValueDelimeter, RecordDelimeter, NumRecords, NumValues);
            var parser  = new DsvParser(builder.ToString(), ValueDelimeter, RecordDelimeter);

            var numRecords = parser.RecordsLength;

            Assert.AreEqual(NumRecords, numRecords);
            for (var recordNum = 0; recordNum < numRecords; recordNum++)
            {
                var numValues = parser.GetRecordLength(recordNum);
                Assert.AreEqual(NumValues, numValues);
                for (var valueNum = 0; valueNum < numValues; valueNum++)
                {
                    var value = parser.GetValue(recordNum, valueNum);
                    Assert.AreEqual($"Value {recordNum}-{valueNum}", value.ToString());
                }
            }
        }
Example #11
0
        public void LargeTable()
        {
            const string ValueDelimeter  = "|";
            const string RecordDelimeter = "\r\n";
            const int    NumRecords      = 10_000;
            const int    NumValues       = 100;

            var builder = CreateTable(ValueDelimeter, RecordDelimeter, NumRecords, NumValues);
            var parser  = new DsvParser(builder.ToString(), ValueDelimeter, RecordDelimeter);

            var numRecords = parser.RecordsLength;

            Assert.AreEqual(NumRecords, numRecords);
            for (var recordNum = 0; recordNum < numRecords; recordNum++)
            {
                var numValues = parser.GetRecordLength(recordNum);
                Assert.AreEqual(NumValues, numValues);
                for (var valueNum = 0; valueNum < numValues; valueNum++)
                {
                    var value = parser.GetValue(recordNum, valueNum);
                    Assert.AreEqual($"Value {recordNum}-{valueNum}", value.ToString());
                }
            }
        }
Example #12
0
        static void Main(string[] args)
        {
            //var file = FileGenerator("Column", "Data", 10000, 100);
            var file = FileGenerator("Column", "Data", 1_000_000, 100);

            //var file = FileGenerator("Column", "Data", 100_000_00, 10);
            //var file = FileGenerator("Column", "Data", 100, 1_00_000);

            Console.WriteLine("Done generating csv");
            Thread.Sleep(5000);
            Stopwatch timer = new Stopwatch();

            timer.Start();

            var parser = new DsvParser(file.AsSpan(), Encoding.UTF8, DsvOptions.DefaultCsvOptions);

            timer.Stop();
            Console.WriteLine(nameof(DsvParser));
            Console.WriteLine("Elapsed Time");
            Console.WriteLine(timer.Elapsed.TotalSeconds);
            Console.WriteLine($"# of Columns = {parser.Columns.Length.ToString()}");
            Console.WriteLine($"# of Rows = {parser.Rows.Count.ToString()}");

            Thread.Sleep(5000);
            timer.Restart();


            var genericParser = new DsvParser <Record100, DsvRecordMapping100>(file.AsSpan(), Encoding.UTF8, DsvOptions.DefaultCsvOptions);

            timer.Stop();
            Console.WriteLine($"{nameof(DsvParser)}<{nameof(Record100)}, {nameof(DsvRecordMapping100)}> ");
            Console.WriteLine("Elapsed Time");
            Console.WriteLine(timer.Elapsed.TotalSeconds);
            Console.WriteLine($"# of Columns = {genericParser.Columns.Length.ToString()}");
            Console.WriteLine($"# of Rows = {genericParser.Rows.Count.ToString()}");
        }
        public void Constructor_string_MapperArg()
        {
            string data = $"c0,c1,c2,c3,c4{Environment.NewLine}1,1/1/2017,hello,true,02:10:01";

            var parser = new DsvParser <Record, RecordMapping>(data, Encoding.UTF8, DsvOptions.DefaultCsvOptions, new RecordMapping());
        }
        public void Constructor_MemoryChar()
        {
            string data = $"c0,c1,c2,c3,c4{Environment.NewLine}1,1/1/2017,hello,true,02:10:01";

            var parser = new DsvParser <Record, RecordMapping>(data.AsMemory(), Encoding.UTF8, DsvOptions.DefaultCsvOptions);
        }