Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » Utilities » Downloads
 
Add your own
alternative version

A Fast CSV Reader

, 10 Nov 2011
A reader that provides fast, non-cached, forward-only access to CSV data.
csvreader11_demo.zip
CsvReader11_demo
CsvReader11_demo
CsvReader11_demo
CsvReaderBenchmarks
App.ico
data.rar
CsvReaderDemo
App.ico
LumenWorks.Framework.IO
Csv
Events
Exceptions
Resources
LumenWorks.Framework.Tests.Unit
IO
Csv
CsvReaderBenchmarks
App.ico
data.rar
CsvReaderDemo
App.ico
LumenWorks.Framework.IO
Csv
Events
Exceptions
Resources
LumenWorks.Framework.Tests.Unit
IO
Csv
CsvReaderBenchmarks
App.ico
data.rar
CsvReaderDemo
App.ico
LumenWorks.Framework.IO
Csv
Events
Exceptions
Resources
LumenWorks.Framework.Tests.Unit
IO
Csv
csvreader11_src.zip
CsvReader11_src
CsvReader11_src
CsvReader11_src
LumenWorks.Framework.IO
Csv
Events
Exceptions
Resources
LumenWorks.Framework.Tests.Unit
IO
Csv
LumenWorks.Framework.IO
Csv
Events
Exceptions
Resources
LumenWorks.Framework.Tests.Unit
IO
Csv
LumenWorks.Framework.IO
Csv
Events
Exceptions
Resources
LumenWorks.Framework.Tests.Unit
IO
Csv
csvreader20_demo.zip
CsvReader20_demo
CsvReader20_demo
CsvReader20_demo
CsvReaderBenchmarks
bin
Release
CsvReaderBenchmarks.exe
CsvReaderBenchmarks.vshost.exe
LumenWorks.Framework.IO.dll
data.rar
Properties
CsvReaderDemo
bin
Release
CsvReaderDemo.exe
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
Properties
Settings.settings
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
CsvReaderBenchmarks
bin
Release
CsvReaderBenchmarks.exe
LumenWorks.Framework.IO.dll
data.rar
Properties
CsvReaderDemo
bin
Release
CsvReaderDemo.exe
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
Properties
Settings.settings
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
CsvReaderBenchmarks
bin
Release
CsvReaderBenchmarks.exe
LumenWorks.Framework.IO.dll
data.rar
Properties
CsvReaderDemo
bin
Release
CsvReaderDemo.exe
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
Properties
Settings.settings
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
csvreader20_src.zip
CsvReader20_src
CsvReader20_src
CsvReader20_src
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
CsvReader_bin.zip
csvreader_demo.zip
CsvReaderDemo
CsvReaderDemo
bin
Release
CsvReaderDemo.exe
CsvReaderDemo.vshost.exe
LumenWorks.Framework.IO.dll
data.rar
Properties
CsvReaderTests
bin
Release
CsvReaderTests.dll
LumenWorks.Framework.IO.dll
Properties
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Exceptions
Resources
Properties
CsvReader_Profiler.zip
CsvReader_src.zip
bin
Release
CsvReaderDemo.exe
CsvReaderDemo.vshost.exe
CsvReaderDemo.vshost.exe.manifest
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
Properties
Settings.settings
CsvReaderDemoWeb
bin
CsvReaderDemoWeb.dll
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
Properties
LumenWorks.Framework.IO
bin
Release
LumenWorks.Framework.IO.dll
Csv
Events
Exceptions
Resources
LumenWorks.Framework.snk
Properties
LumenWorks.Framework.Tests.Unit
bin
Release
LumenWorks.Framework.IO.dll
LumenWorks.Framework.Tests.Unit.dll
IO
Csv
Properties
CsvReaderBenchmarks
bin
Release
CsvReaderBenchmarks.exe
CsvReaderBenchmarks.vshost.exe
CsvReaderBenchmarks.vshost.exe.manifest
LumenWorks.Framework.IO.dll
data.rar
Properties
//	LumenWorks.Framework.Tests.Unit.IO.CSV.CsvReaderTest
//	Copyright (c) 2005 S�bastien Lorion
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights 
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//	of the Software, and to permit persons to whom the Software is furnished to do so, 
//	subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in all 
//	copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//	INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
//	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//	FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


// A special thanks goes to "shriop" at CodeProject for providing many of the standard and Unicode parsing tests.


#region Using directives

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

using NUnit.Framework;

using LumenWorks.Framework.IO.Csv;

#endregion

namespace LumenWorks.Framework.Tests.Unit.IO.Csv
{
	[TestFixture()]
	public class CsvReaderTest
	{
		#region Sample data

		const int SampleData1RecordCount = 6;

		// <blank>
		// # This is a comment
		// "First Name", "Last Name", Address, City, State, "Zip Code"<tab>
		// John,Doe,120 jefferson st.,Riverside, NJ, 08075
		// Jack,McGinnis,220 hobo Av.,Phila<tab>, PA,09119
		// "John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
		// <blank>
		// # This is a comment
		// Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
		// ,Blankman,,SomeTown, SD, 00298
		// "Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

		const string SampleData1 = @"
# This is a comment
""First Name"", ""Last Name"", Address, City, State, ""Zip Code""	
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila	, PA,09119
""John """"Da Man"""""",Repici,120 Jefferson St.,Riverside, NJ,08075

# This is a comment
Stephen,Tyler,""7452 Terrace """"At the Plaza"""" road"",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
""Joan """"the bone"""", Anne"",Jet,""9th, at Terrace plc"",Desert City,CO,00123";

		#endregion

		#region Sample data utility methods

		public void CheckSampleData1(CsvReader csv)
		{
			CheckSampleData1(csv.CurrentRecordIndex, csv);
		}

		public void CheckSampleData1(int recordIndex, CsvReader csv)
		{
			string[] fields = new string[6];
			csv.CopyCurrentRecordTo(fields);

			CheckSampleData1(recordIndex, fields, 0);
		}

		public void CheckSampleData1(int recordIndex, string[] fields)
		{
			CheckSampleData1(recordIndex, fields, 0);
		}

		public void CheckSampleData1(int recordIndex, string[] fields, int startIndex)
		{
			Assert.IsTrue(fields.Length - startIndex >= 6);

			switch (recordIndex)
			{
				case 0:
					Assert.AreEqual("John", fields[startIndex]);
					Assert.AreEqual("Doe", fields[startIndex + 1]);
					Assert.AreEqual("120 jefferson st.", fields[startIndex + 2]);
					Assert.AreEqual("Riverside", fields[startIndex + 3]);
					Assert.AreEqual("NJ", fields[startIndex + 4]);
					Assert.AreEqual("08075", fields[startIndex + 5]);
					break;

				case 1:
					Assert.AreEqual("Jack", fields[startIndex]);
					Assert.AreEqual("McGinnis", fields[startIndex + 1]);
					Assert.AreEqual("220 hobo Av.", fields[startIndex + 2]);
					Assert.AreEqual("Phila", fields[startIndex + 3]);
					Assert.AreEqual("PA", fields[startIndex + 4]);
					Assert.AreEqual("09119", fields[startIndex + 5]);
					break;

				case 2:
					Assert.AreEqual(@"John ""Da Man""", fields[startIndex]);
					Assert.AreEqual("Repici", fields[startIndex + 1]);
					Assert.AreEqual("120 Jefferson St.", fields[startIndex + 2]);
					Assert.AreEqual("Riverside", fields[startIndex + 3]);
					Assert.AreEqual("NJ", fields[startIndex + 4]);
					Assert.AreEqual("08075", fields[startIndex + 5]);
					break;

				case 3:
					Assert.AreEqual("Stephen", fields[startIndex]);
					Assert.AreEqual("Tyler", fields[startIndex + 1]);
					Assert.AreEqual(@"7452 Terrace ""At the Plaza"" road", fields[startIndex + 2]);
					Assert.AreEqual("SomeTown", fields[startIndex + 3]);
					Assert.AreEqual("SD", fields[startIndex + 4]);
					Assert.AreEqual("91234", fields[startIndex + 5]);
					break;

				case 4:
					Assert.AreEqual("", fields[startIndex]);
					Assert.AreEqual("Blankman", fields[startIndex + 1]);
					Assert.AreEqual("", fields[startIndex + 2]);
					Assert.AreEqual("SomeTown", fields[startIndex + 3]);
					Assert.AreEqual("SD", fields[startIndex + 4]);
					Assert.AreEqual("00298", fields[startIndex + 5]);
					break;

				case 5:
					Assert.AreEqual(@"Joan ""the bone"", Anne", fields[startIndex]);
					Assert.AreEqual("Jet", fields[startIndex + 1]);
					Assert.AreEqual("9th, at Terrace plc", fields[startIndex + 2]);
					Assert.AreEqual("Desert City", fields[startIndex + 3]);
					Assert.AreEqual("CO", fields[startIndex + 4]);
					Assert.AreEqual("00123", fields[startIndex + 5]);
					break;

				default:
					throw new IndexOutOfRangeException(string.Format("Specified recordIndex is '{0}'. Possible range is [0, 5].", recordIndex));
			}
		}

		#endregion
		
		#region Parsing tests

		[Test()]
		public void ParsingTest1()
		{
			const string data = "1\r\n\r\n1";

			using (CsvReader csv = new CsvReader(new StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);

				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);

				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest2()
		{
			// ["Bob said, ""Hey!""",2, 3 ]
			const string data = "\"Bob said, \"\"Hey!\"\"\",2, 3 ";

			using (CsvReader csv = new CsvReader(new StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(@"Bob said, ""Hey!""", csv[0]);
				Assert.AreEqual("2", csv[1]);
				Assert.AreEqual("3", csv[2]);

				Assert.IsFalse(csv.ReadNextRecord());
			}

			using (CsvReader csv = new CsvReader(new StringReader(data), false, ',', '"', '"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(@"Bob said, ""Hey!""", csv[0]);
				Assert.AreEqual("2", csv[1]);
				Assert.AreEqual(" 3 ", csv[2]);

				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest3()
		{
			const string data = "1\r2\n";

			using (CsvReader csv = new CsvReader(new StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);

				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("2", csv[0]);

				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest4()
		{
			const string data = "\"\n\r\n\n\r\r\",,\t,\n";

			using (CsvReader csv = new CsvReader(new StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());

				Assert.AreEqual(4, csv.FieldCount);

				Assert.AreEqual("\n\r\n\n\r\r", csv[0]);
				Assert.AreEqual("", csv[1]);
				Assert.AreEqual("", csv[2]);
				Assert.AreEqual("", csv[3]);

				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest5()
		{
			Checkdata5(1024);

			// some tricky ones ...

			Checkdata5(1);
			Checkdata5(9);
			Checkdata5(14);
			Checkdata5(39);
			Checkdata5(166);
			Checkdata5(194);
		}

		[Test()]
		public void ParsingTest5_RandomBufferSizes()
		{
			Random random = new Random();

			for (int i = 0; i < 1000; i++)
				Checkdata5(random.Next(1, 512));
		}

		public void Checkdata5(int bufferSize)
		{
			const string data = SampleData1;

			try
			{
				using (CsvReader csv = new CsvReader(new StringReader(data), true, bufferSize))
				{
					Assert.IsTrue(csv.ReadNextRecord());

					Assert.AreEqual(6, csv.FieldCount);

					Assert.AreEqual(0, csv.FieldHeaders["First Name"]);
					Assert.AreEqual(1, csv.FieldHeaders["Last Name"]);
					Assert.AreEqual(2, csv.FieldHeaders["Address"]);
					Assert.AreEqual(3, csv.FieldHeaders["City"]);
					Assert.AreEqual(4, csv.FieldHeaders["State"]);
					Assert.AreEqual(5, csv.FieldHeaders["Zip Code"]);

					CheckSampleData1(csv);

					Assert.IsTrue(csv.ReadNextRecord());
					CheckSampleData1(csv);

					Assert.IsTrue(csv.ReadNextRecord());
					CheckSampleData1(csv);

					Assert.IsTrue(csv.ReadNextRecord());
					CheckSampleData1(csv);

					Assert.IsTrue(csv.ReadNextRecord());
					CheckSampleData1(csv);

					Assert.IsTrue(csv.ReadNextRecord());
					CheckSampleData1(csv);

					Assert.IsFalse(csv.ReadNextRecord());
				}
			}
			catch (Exception ex)
			{
				throw new Exception(string.Format("BufferSize={0}", bufferSize), ex);
			}
		}

		[Test()]
		public void ParsingTest6()
		{
			using (CsvReader csv = new CsvReader(new System.IO.StringReader("1,2"), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual("2", csv[1]);
				Assert.AreEqual(',', csv.Delimiter);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest7()
		{
			using (CsvReader csv = new CsvReader(new System.IO.StringReader("\r\n1\r\n"), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(',', csv.Delimiter);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.AreEqual("1", csv[0]);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest8()
		{
			const string data = "\"bob said, \"\"Hey!\"\"\",2, 3 ";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', true))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("bob said, \"Hey!\"", csv[0]);
				Assert.AreEqual("2", csv[1]);
				Assert.AreEqual("3", csv[2]);
				Assert.AreEqual(',', csv.Delimiter);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(3, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest9()
		{
			const string data = ",";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(String.Empty, csv[0]);
				Assert.AreEqual(String.Empty, csv[1]);
				Assert.AreEqual(',', csv.Delimiter);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest10()
		{
			const string data = "1\r2";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("2", csv[0]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest11()
		{
			const string data = "1\n2";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("2", csv[0]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest12()
		{
			const string data = "1\r\n2";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("2", csv[0]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest13()
		{
			const string data = "1\r";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest14()
		{
			const string data = "1\n";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest15()
		{
			const string data = "1\r\n";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest16()
		{
			const string data = "1\r2\n";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, '\r', '"', '\"', '#', true))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual("2", csv[1]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest17()
		{
			const string data = "\"July 4th, 2005\"";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("July 4th, 2005", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest18()
		{
			const string data = " 1";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(" 1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest19()
		{
			string data = String.Empty;

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest20()
		{
			const string data = "user_id,name\r\n1,Bruce";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), true))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual("Bruce", csv[1]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.AreEqual("1", csv["user_id"]);
				Assert.AreEqual("Bruce", csv["name"]);
				Assert.IsFalse(csv.ReadNextRecord());
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest21()
		{
			const string data = "\"data \r\n here\"";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("data \r\n here", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest22()
		{
			const string data = "\r\r\n1\r";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, '\r', '\"', '\"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(3, csv.FieldCount);

				Assert.AreEqual(String.Empty, csv[0]);
				Assert.AreEqual(String.Empty, csv[1]);
				Assert.AreEqual(String.Empty, csv[2]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(String.Empty, csv[1]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest23()
		{
			const string data = "\"double\"\"\"\"double quotes\"";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("double\"\"double quotes", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest24()
		{
			const string data = "1\r";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest25()
		{
			const string data = "1\r\n";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest26()
		{
			const string data = "1\n";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest27()
		{
			const string data = "'bob said, ''Hey!''',2, 3 ";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\'', '\'', '#', true))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("bob said, 'Hey!'", csv[0]);
				Assert.AreEqual("2", csv[1]);
				Assert.AreEqual("3", csv[2]);
				Assert.AreEqual(',', csv.Delimiter);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(3, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest28()
		{
			const string data = "\"data \"\" here\"";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\0', '\\', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("\"data \"\" here\"", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest29()
		{
			string data = new String('a', 75) + "," + new String('b', 75);

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(new String('a', 75), csv[0]);
				Assert.AreEqual(new String('b', 75), csv[1]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest30()
		{
			const string data = "1\r\n\r\n1";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest31()
		{
			const string data = "1\r\n# bunch of crazy stuff here\r\n1";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest32()
		{
			const string data = "\"1\",Bruce\r\n\"2\n\",Toni\r\n\"3\",Brian\r\n";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("1", csv[0]);
				Assert.AreEqual("Bruce", csv[1]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("2\n", csv[0]);
				Assert.AreEqual("Toni", csv[1]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("3", csv[0]);
				Assert.AreEqual("Brian", csv[1]);
				Assert.AreEqual(2, csv.CurrentRecordIndex);
				Assert.AreEqual(2, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest33()
		{
			const string data = "\"double\\\\\\\\double backslash\"";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\\', '#', false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("double\\\\double backslash", csv[0]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(1, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void ParsingTest34()
		{
			const string data = "\"Chicane\", \"Love on the Run\", \"Knight Rider\", \"This field contains a comma, but it doesn't matter as the field is quoted\"\r\n" +
				"\"Samuel Barber\", \"Adagio for Strings\", \"Classical\", \"This field contains a double quote character, \"\", but it doesn't matter as it is escaped\"";

			using (CsvReader csv = new CsvReader(new System.IO.StringReader(data), false, ',', '\"', '\"', '#', true))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("Chicane", csv[0]);
				Assert.AreEqual("Love on the Run", csv[1]);
				Assert.AreEqual("Knight Rider", csv[2]);
				Assert.AreEqual("This field contains a comma, but it doesn't matter as the field is quoted", csv[3]);
				Assert.AreEqual(0, csv.CurrentRecordIndex);
				Assert.AreEqual(4, csv.FieldCount);
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual("Samuel Barber", csv[0]);
				Assert.AreEqual("Adagio for Strings", csv[1]);
				Assert.AreEqual("Classical", csv[2]);
				Assert.AreEqual("This field contains a double quote character, \", but it doesn't matter as it is escaped", csv[3]);
				Assert.AreEqual(1, csv.CurrentRecordIndex);
				Assert.AreEqual(4, csv.FieldCount);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		#endregion

		#region UnicodeParsing tests

		[Test()]
		public void UnicodeParsingTest1()
		{
			// control characters and comma are skipped

			char[] raw = new char[65536 - 13];

			for (int i = 0; i < raw.Length; i++)
				raw[i] = (char) (i + 14);

			raw[44 - 14] = ' '; // skip comma

			string data = new string(raw);

			using (CsvReader csv = new CsvReader(new StringReader(data), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(data, csv[0]);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void UnicodeParsingTest2()
		{
			byte[] buffer;

			string test = "M�nchen";

			using (MemoryStream stream = new MemoryStream())
			{
				using (TextWriter writer = new StreamWriter(stream, Encoding.Unicode))
				{
					writer.WriteLine(test);
				}

				buffer = stream.ToArray();
			}

			using (CsvReader csv = new CsvReader(new StreamReader(new MemoryStream(buffer), Encoding.Unicode, false), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(test, csv[0]);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		[Test()]
		public void UnicodeParsingTest3()
		{
			byte[] buffer;

			string test = "M�nchen";

			using (MemoryStream stream = new MemoryStream())
			{
				using (TextWriter writer = new StreamWriter(stream, Encoding.Unicode))
				{
					writer.Write(test);
				}

				buffer = stream.ToArray();
			}

			using (CsvReader csv = new CsvReader(new StreamReader(new MemoryStream(buffer), Encoding.Unicode, false), false))
			{
				Assert.IsTrue(csv.ReadNextRecord());
				Assert.AreEqual(test, csv[0]);
				Assert.IsFalse(csv.ReadNextRecord());
			}
		}

		#endregion

		#region MoveTo tests

		[Test()]
		public void MoveToTest1()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				for (int i = 0; i < SampleData1RecordCount; i++)
				{
					csv.MoveTo(i);
					CheckSampleData1(i, csv);
				}
			}
		}

		[Test()]
		[ExpectedException(typeof(InvalidOperationException))]
		public void MoveToTest2()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				csv.MoveTo(1);
				csv.MoveTo(0);
			}
		}

		[Test()]
		[ExpectedException(typeof(EndOfStreamException))]
		public void MoveToTest3()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				csv.MoveTo(SampleData1RecordCount);
			}
		}

		[Test()]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void MoveToTest4()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				csv.MoveTo(-1);
			}
		}

		#endregion

		#region Iteration tests

		[Test()]
		public void IterationTest1()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				int index = 0;

				foreach (string[] record in csv)
				{
					CheckSampleData1(index, record);
					index++;
				}
			}
		}

		#endregion

		#region Indexer tests

		[Test()]
		public void IndexerTest1()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				for (int i = 0; i < SampleData1RecordCount; i++)
				{
					string s = csv[i, 0];
					CheckSampleData1(i, csv);
				}
			}
		}

		[Test()]
		[ExpectedException(typeof(InvalidOperationException))]
		public void IndexerTest2()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				string s = csv[1, 0];
				s = csv[0, 0];
			}
		}

		[Test()]
		[ExpectedException(typeof(EndOfStreamException))]
		public void IndexerTest3()
		{
			using (CsvReader csv = new CsvReader(new StringReader(SampleData1), true))
			{
				string s = csv[SampleData1RecordCount, 0];
			}
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Sebastien Lorion
Architect
Canada Canada
Sébastien Lorion is software architect as day job.
 
He is also a musician, actually singing outside the shower Smile | :)
 
He needs constant mental and emotional stimulation, so all of this might change someday ...

| Advertise | Privacy | Mobile
Web04 | 2.8.140826.1 | Last Updated 10 Nov 2011
Article Copyright 2005 by Sebastien Lorion
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid