extra-tsv/SaneTsvTest/Program.cs

220 lines
7.1 KiB
C#
Raw Normal View History

2024-02-14 03:15:07 +00:00
using NathanMcRae;
using System.Text;
internal class Program
2024-02-14 22:30:36 +00:00
{
public class TestRecord : SaneTsv.SaneTsvRecord
2024-02-14 22:30:36 +00:00
{
[SaneTsv.SaneTsvColumn("string-test")]
public string StringTest { get; set; }
2024-02-14 22:30:36 +00:00
[SaneTsv.SaneTsvColumn("bool-test")]
public bool BoolTest { get; set; }
2024-02-14 22:30:36 +00:00
[SaneTsv.SaneTsvColumn("float32-test")]
public float Float32Test { get; set; }
2024-02-14 22:30:36 +00:00
[SaneTsv.SaneTsvColumn("float32-le-test", typeof(SaneTsv.Float32LEType))]
public float Float32LETest { get; set; }
2024-02-15 02:32:31 +00:00
[SaneTsv.SaneTsvColumn("float64-test")]
public double Float64Test { get; set; }
2024-02-15 02:32:31 +00:00
[SaneTsv.SaneTsvColumn("float64-le-test", typeof(SaneTsv.Float64LEType))]
public double Float64LETest { get; set; }
[SaneTsv.SaneTsvColumn("uint32-test")]
public UInt32 UInt32Test { get; set; }
[SaneTsv.SaneTsvColumn("uint64-test")]
public UInt64 UInt64Test { get; set; }
[SaneTsv.SaneTsvColumn("int32-test")]
public Int32 Int32Test { get; set; }
[SaneTsv.SaneTsvColumn("int64-test")]
public Int64 Int64Test { get; set; }
[SaneTsv.SaneTsvColumn("binary-test")]
public byte[] BinaryTest { get; set; }
public TestRecord(string stringTest, bool boolTest, float float32Test, float float32LETest, double float64Test, double float64LETest, UInt32 uInt32Test, UInt64 uInt64Test, Int32 int32Test, Int64 int64Test, byte[] binaryTest)
{
StringTest = stringTest;
BoolTest = boolTest;
Float32Test = float32Test;
Float32LETest = float32LETest;
Float64Test = float64Test;
Float64LETest = float64LETest;
UInt32Test = uInt32Test;
UInt64Test = uInt64Test;
Int32Test = int32Test;
Int64Test = int64Test;
BinaryTest = binaryTest;
}
public TestRecord() { }
}
public class BoolTestRecord : SaneTsv.SaneTsvRecord
2024-02-17 05:26:35 +00:00
{
[SaneTsv.SaneTsvColumn("column1:ty#pe")]
public bool Column1 { get; set; }
[SaneTsv.SaneTsvColumn]
public byte[] column2 { get; set; }
[SaneTsv.SaneTsvColumn("columnthree\nyep")]
public string Column3 { get; set; }
2024-02-17 05:26:35 +00:00
}
public class BoolTestRecord2 : SaneTsv.SaneTsvRecord
2024-02-17 05:26:35 +00:00
{
[SaneTsv.SaneTsvColumn("column1:type")]
public bool Column1 { get; set; }
2024-02-17 05:26:35 +00:00
[SaneTsv.SaneTsvColumn]
public byte[] column2 { get; set; }
2024-02-16 04:26:56 +00:00
[SaneTsv.SaneTsvColumn("columnthree\nyep")]
public string Column3 { get; set; }
}
public class SerdeTestRecord : SaneTsv.SaneTsvRecord
2024-02-18 04:44:33 +00:00
{
[SaneTsv.SaneTsvColumn("column1")]
public bool Column1 { get; set; }
2024-02-18 04:44:33 +00:00
[SaneTsv.SaneTsvColumn]
public byte[] column2 { get; set; }
2024-02-18 04:44:33 +00:00
[SaneTsv.SaneTsvColumn("columnthree\nyep")]
public string Column3 { get; set; }
}
2024-02-16 04:26:56 +00:00
public class FloatTestRecord : SaneTsv.SaneTsvRecord
2024-02-18 04:44:33 +00:00
{
[SaneTsv.SaneTsvColumn("somefloat")]
public double SomeFloat { get; set; }
[SaneTsv.SaneTsvColumn("binfloat", typeof(SaneTsv.Float64LEType))]
public double BinFloat { get; set; }
2024-02-18 04:44:33 +00:00
}
private static void Main(string[] args)
2024-02-18 04:44:33 +00:00
{
{
string testName = "Bool test";
string testString1 = "column1:ty\\#pe:boolean\tcolumn2:binary\tcolumnthree\\nyep:string" +
"\nTRUE\tvalue\\\\t\0woo\tvaluetrhee" +
"\nFALSE\tnother\tno\\ther";
Tsv<BoolTestRecord> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord>(Encoding.UTF8.GetBytes(testString1));
if (parsed.Records[0].Column1)
{
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";
Tsv<BoolTestRecord> parsed = SaneTsv.ParseTypedTsv<BoolTestRecord>(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";
Tsv<BoolTestRecord2> parsed = SaneTsv.ParseCommentedTsv<BoolTestRecord2>(Encoding.UTF8.GetBytes(testString1));
}
//{
// string testName = "Serde test";
// string testString1 = "column1\tcolumn2\tcolumnthree\\nyep" +
// "\nTRUE\tvalue\\\\twoo\tvaluetrhee" +
// "\nFALSE\tnother\tno\\ther";
// Tsv<SerdeTestRecord> parsed = SaneTsv.ParseSimpleTsv<SerdeTestRecord>(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<byte>();
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));
Tsv<FloatTestRecord> parsed = SaneTsv.ParseTypedTsv<FloatTestRecord>(bytes.ToArray());
if (parsed.Records[0].BinFloat == parsed.Records[0].SomeFloat)
{
Console.WriteLine($"Passed {testName}");
}
else
{
Console.WriteLine($"Failed {testName}");
}
}
Console.WriteLine("Done with tests");
{
string testName = "Serde test";
TestRecord[] data =
{
new TestRecord("test", true, 44.5f, 44.5f, -88e-3, -88e-3, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3 }),
new TestRecord("test2", false, 44.5000005f, 44.5000005f, -88e-30, -88e-30, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
new TestRecord("test2", false, float.NaN, float.NaN, double.NaN, double.NaN, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
new TestRecord("test2", false, float.NegativeInfinity, float.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
new TestRecord("test2", false, float.PositiveInfinity, float.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, 7773, 88888888, -7773, -88888888, new byte[] { 0, 1, 2, 3, 4 }),
};
byte[] serialized = SaneTsv.SerializeTypedTsv(data);
Tsv<TestRecord> parsed = SaneTsv.ParseTypedTsv<TestRecord>(serialized);
if ((float)parsed.Records[1].Float32Test == 44.5000005f)
{
Console.WriteLine($"Passed {testName}");
}
else
{
Console.WriteLine($"Failed {testName}");
}
}
2024-02-18 04:44:33 +00:00
}
}