using NathanMcRae; using System.Linq; using System.Text; { string testName = "Bool test"; string testString1 = "column1:ty\\#pe:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" + "\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" + "\nFALSE\tnother\tno\\ther"; SaneTsv parsed = SaneTsv.ParseTypedTsv(Encoding.UTF8.GetBytes(testString1)); if (parsed.Records[0]["column1:ty#pe"] is bool result && result) { Console.WriteLine($"Passed {testName}"); } else { Console.WriteLine($"Failed {testName}"); } } { string testName = "Bad bool test"; try { string testString1 = "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" + "\nTUE\tvalue\\\\t\0woo\tvaluetrhee" + "\nFALSE\tnother\tno\\ther"; SaneTsv parsed = SaneTsv.ParseTypedTsv(Encoding.UTF8.GetBytes(testString1)); Console.WriteLine($"Failed {testName}"); } catch (Exception) { Console.WriteLine($"Passed {testName}"); } } { string testName = "Comment test"; string testString1 = "#This is a file comment\n" + "#One more file comment line\n" + "column1:type:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" + "\n#This is a comment" + "\n#Another comment line" + "\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" + "\nFALSE\tnother\tno\\ther"; SaneTsv parsed = SaneTsv.ParseCommentedTsv(Encoding.UTF8.GetBytes(testString1)); } { string testName = "Serde test"; string testString1 = "column1\tcolumn2\tcolumnthree\\nyep" + "\nTRUE\tvalue\\\\twoo\tvaluetrhee" + "\nFALSE\tnother\tno\\ther"; SaneTsv parsed = SaneTsv.ParseSimpleTsv(Encoding.UTF8.GetBytes(testString1)); string serialized = Encoding.UTF8.GetString(SaneTsv.SerializeSimpleTsv(parsed.ColumnNames, parsed.Records.Select(r => r.Fields.Select(f => f.ToString()).ToArray()).ToArray())); if (testString1 == serialized) { Console.WriteLine($"Passed {testName}"); } else { Console.WriteLine($"Failed {testName}"); } } { string testName = "Float binary test"; var bytes = new List(); bytes.AddRange(Encoding.UTF8.GetBytes("somefloat:float64\tbinfloat:float64-le" + "\n1.5\t")); bytes.AddRange(BitConverter.GetBytes(1.5)); bytes.AddRange(Encoding.UTF8.GetBytes("\n-8.0000005E-14\t")); bytes.AddRange(BitConverter.GetBytes(-8.0000005E-14)); SaneTsv parsed = SaneTsv.ParseTypedTsv(bytes.ToArray()); if ((double)parsed.Records[0]["binfloat"] == (double)parsed.Records[0]["somefloat"]) { Console.WriteLine($"Passed {testName}"); } else { Console.WriteLine($"Failed {testName}"); } } Console.WriteLine("Done with tests"); // TODO: Check qNaN, sNaN, +inf, -inf values for float types