Library/Library.Test/TestCsvReader.cs
C# | Copy Code |
---|---|
string content = @"True, 129, f0a12b, a, string, ""a,b,c"", 1993-06-26 2:00pm, 0.25, {74A51B0C-21B1-42b4-BAE4-AE6480B8B8DB}, 20000, -2000000000, 2000000000000"; using (CsvReader r = new CsvReader(new StringReader(content), CsvOptions.None)) { int field = 0; byte[] buf = new byte[10]; char[] ch = new char[10]; Assert.IsTrue(r.Read()); Assert.AreEqual(true, r.GetBoolean(field++)); Assert.AreEqual((byte)129, r.GetByte(field++)); Assert.AreEqual(3L, r.GetBytes(field++, 0, buf, 0, buf.Length)); Assert.AreEqual((byte)0xf0, buf[0]); Assert.AreEqual((byte)0xa1, buf[1]); Assert.AreEqual((byte)0x2b, buf[2]); Assert.AreEqual((byte)0, buf[3]); Assert.AreEqual('a', r.GetChar(field++)); Assert.AreEqual(6, r.GetChars(field++, 0, ch, 0, ch.Length)); Assert.AreEqual("string", new String(ch, 0, 6)); using (IDataReader c = r.GetData(field++)) { Assert.AreEqual(1, c.Depth); Assert.AreEqual(0, c.FieldCount); Assert.IsTrue(c.Read()); Assert.AreEqual(3, c.FieldCount); Assert.AreEqual("a", c.GetString(0)); Assert.AreEqual("b", c.GetString(1)); Assert.AreEqual("c", c.GetString(2)); } Assert.AreEqual(DateTime.Parse("1993-06-26 2:00pm"), r.GetDateTime(field++)); Assert.AreEqual((decimal)0.25, r.GetDecimal(field)); Assert.AreEqual(0.25f, r.GetFloat(field)); Assert.AreEqual(0.25, r.GetDouble(field++)); Assert.AreEqual(new Guid("74A51B0C-21B1-42b4-BAE4-AE6480B8B8DB"), r.GetGuid(field++)); Assert.AreEqual((short)20000, r.GetInt16(field++)); Assert.AreEqual(-2000000000, r.GetInt32(field++)); Assert.AreEqual(2000000000000L, r.GetInt64(field++)); } |
VB.NET | Copy Code |
---|---|
Dim content As String = "True, 129, f0a12b, a, string, ""a,b,c"", 1993-06-26 2:00pm, 0.25, {74A51B0C-21B1-42b4-BAE4-AE6480B8B8DB}, 20000, -2000000000, 2000000000000" Using r As New CsvReader(New StringReader(content), CsvOptions.None) Dim field As Integer = 0 Dim buf As Byte() = New Byte(10) {} Dim ch As Char() = New Char(10) {} Assert.IsTrue(r.Read()) Assert.AreEqual(True, r.GetBoolean(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(DirectCast(129, Byte), r.GetByte(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(3L, r.GetBytes(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1), 0, buf, 0, buf.Length)) Assert.AreEqual(DirectCast(&Hf0, Byte), buf(0)) Assert.AreEqual(DirectCast(&Ha1, Byte), buf(1)) Assert.AreEqual(DirectCast(&H2b, Byte), buf(2)) Assert.AreEqual(DirectCast(0, Byte), buf(3)) Assert.AreEqual("a"C, r.GetChar(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(6, r.GetChars(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1), 0, ch, 0, ch.Length)) Assert.AreEqual("string", New [String](ch, 0, 6)) Using c As IDataReader = r.GetData(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1)) Assert.AreEqual(1, c.Depth) Assert.AreEqual(0, c.FieldCount) Assert.IsTrue(c.Read()) Assert.AreEqual(3, c.FieldCount) Assert.AreEqual("a", c.GetString(0)) Assert.AreEqual("b", c.GetString(1)) Assert.AreEqual("c", c.GetString(2)) End Using Assert.AreEqual(DateTime.Parse("1993-06-26 2:00pm"), r.GetDateTime(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(DirectCast(0.25, Decimal), r.GetDecimal(field)) Assert.AreEqual(0.25F, r.GetFloat(field)) Assert.AreEqual(0.25, r.GetDouble(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(New Guid("74A51B0C-21B1-42b4-BAE4-AE6480B8B8DB"), r.GetGuid(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(DirectCast(20000, Short), r.GetInt16(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(-2000000000, r.GetInt32(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) Assert.AreEqual(2000000000000L, r.GetInt64(System.Math.Max(System.Threading.Interlocked.Increment(field),field - 1))) End Using |
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7