Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Sep 25, 2024
1 parent f95c1ec commit 67240aa
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 6 deletions.
22 changes: 18 additions & 4 deletions RDMSharp/Metadata/JSON/OneOfTypes/BooleanType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,25 @@ public override byte[] ParsePayloadToData(DataTree dataTree)
public override DataTree ParseDataToPayload(ref byte[] data)
{
List<DataTreeIssue> issueList = new List<DataTreeIssue>();
if (data.Length != 1)
issueList.Add(new DataTreeIssue($"Data length is not 1"));

uint pdl = GetDataLength().Value.Value;
if (data.Length < pdl)
{
issueList.Add(new DataTreeIssue("Given Data not fits PDL"));
byte[] cloneData = new byte[pdl];
Array.Copy(data, cloneData, data.Length);
data = cloneData;
}


DataTreeValueLabel[] labels = null;
if ((Labels?.Length ?? 0) != 0)
labels = Labels.Select(lb => new DataTreeValueLabel(lb.Value, (lb.DisplayName ?? lb.Name))).ToArray();

bool value = false;
value = Tools.DataToBool(ref data);

data = data.Skip(1).ToArray();
return new DataTree(this.Name, 0, Tools.DataToBool(ref data), issueList.Count != 0 ? issueList.ToArray() : null);
return new DataTree(this.Name, 0, value, issueList.Count != 0 ? issueList.ToArray() : null, labels: labels);
}
}
}
3 changes: 3 additions & 0 deletions RDMSharp/Metadata/JSON/OneOfTypes/LabeledBooleanType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public LabeledBooleanType(string name,
Resources = resources;
Value = value;
}
public LabeledBooleanType(string name, bool value) : this(name, null, null, null, value)
{
}

public override string ToString()
{
Expand Down
5 changes: 5 additions & 0 deletions RDMSharp/Metadata/JSON/OneOfTypes/LabeledIntegerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public LabeledIntegerType(string name,
Value = value;
}

public LabeledIntegerType(string name, long value): this(name, null, null, null, value)
{

}

public override string ToString()
{
return $"{Value} -> {Name}";
Expand Down
4 changes: 4 additions & 0 deletions RDMSharpTests/Metadata/JSON/TestBitType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using RDMSharp.Metadata;
using RDMSharp.Metadata.JSON.OneOfTypes;

namespace RDMSharpTests.Metadata.JSON
Expand All @@ -10,6 +11,9 @@ public void TestMany()
var bitType = new BitType("NAME", "DISPLAY_NAME", "NOTES", null, "bit", 1, true, false);
Assert.That(bitType.Index, Is.EqualTo(1));
Assert.Throws(typeof(NotSupportedException), () => bitType.GetDataLength());
byte[] bytes = new byte[0];
Assert.Throws(typeof(NotSupportedException), () => bitType.ParseDataToPayload(ref bytes));
Assert.Throws(typeof(NotSupportedException), () => bitType.ParsePayloadToData(new DataTree()));
}
}
}
33 changes: 32 additions & 1 deletion RDMSharpTests/Metadata/JSON/TestBooleanType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using RDMSharp.Metadata;
using RDMSharp.Metadata.JSON.OneOfTypes;
using RDMSharp.RDM;

Expand All @@ -20,14 +21,21 @@ public void TestMany()
Assert.That(pdl.Value, Is.EqualTo(1));
});

DoParseDataTest(booleanType, false, new byte[] { 0x00 });
DoParseDataTest(booleanType, true, new byte[] { 0x01 });

booleanType = new BooleanType("NAME", "DISPLAY_NAME", "NOTES", null, "boolean", null);
DoParseDataTest(booleanType, false, new byte[] { 0x00 });
DoParseDataTest(booleanType, true, new byte[] { 0x01 });

Assert.Throws(typeof(ArgumentException), () => booleanType = new BooleanType("NAME", "DISPLAY_NAME", "NOTES", null, "bolean", labeledBooleanType));

labeledBooleanType = new LabeledBooleanType[1];
labeledBooleanType[0] = new LabeledBooleanType("NAME11", "DISPLAY_NAME11", "NOTES11", null, false);
Assert.Throws(typeof(ArgumentException), () => booleanType = new BooleanType("NAME", "DISPLAY_NAME", "NOTES", null, "boolean", labeledBooleanType));

labeledBooleanType = new LabeledBooleanType[3];
labeledBooleanType[0] = new LabeledBooleanType("NAME11", "DISPLAY_NAME11", "NOTES11", null, false);
labeledBooleanType[0] = new LabeledBooleanType("NAME11", false);
labeledBooleanType[1] = new LabeledBooleanType("NAME22", "DISPLAY_NAME22", "NOTES22", null, true);
labeledBooleanType[2] = new LabeledBooleanType("NAME33", "DISPLAY_NAME33", "NOTES33", null, false);
Assert.Throws(typeof(ArgumentException), () => booleanType = new BooleanType("NAME", "DISPLAY_NAME", "NOTES", null, "boolean", labeledBooleanType));
Expand All @@ -37,5 +45,28 @@ public void TestMany()
labeledBooleanType[1] = new LabeledBooleanType("NAME22", "DISPLAY_NAME22", "NOTES22", null, false);
Assert.Throws(typeof(ArgumentException), () => booleanType = new BooleanType("NAME", "DISPLAY_NAME", "NOTES", null, "boolean", labeledBooleanType));
}
private void DoParseDataTest(BooleanType booleanType, bool value, byte[] expectedData, string message = null)
{
var dataTree = new DataTree(booleanType.Name, 0, value);
var data = new byte[0];
Assert.DoesNotThrow(() => data = booleanType.ParsePayloadToData(dataTree), message);
Assert.That(data, Is.EqualTo(expectedData), message);

byte[] clonaData = new byte[data.Length];
Array.Copy(data, clonaData, clonaData.Length);
var parsedDataTree = booleanType.ParseDataToPayload(ref clonaData);
Assert.That(clonaData, Has.Length.EqualTo(0), message);

Assert.That(parsedDataTree, Is.EqualTo(dataTree), message);

//Test for short Data & PDL Issue
clonaData = new byte[data.Length - 1];
Array.Copy(data, clonaData, clonaData.Length);
Assert.DoesNotThrow(() => parsedDataTree = booleanType.ParseDataToPayload(ref clonaData));
Assert.That(parsedDataTree.Issues, Is.Not.Null);
Assert.That(parsedDataTree.Value, Is.Not.Null);

Assert.Throws(typeof(ArithmeticException), () => data = booleanType.ParsePayloadToData(new DataTree("Different Name", dataTree.Index, dataTree.Value)), message);
}
}
}
4 changes: 3 additions & 1 deletion RDMSharpTests/Metadata/JSON/TestIntegerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ public void TestManyUInt128()
[Test]
public void TestPrefix1024()
{
LabeledIntegerType[] labeledIntegerType = new LabeledIntegerType[] { new LabeledIntegerType("Test 0", 0), new LabeledIntegerType("Test 1", 1), new LabeledIntegerType("Test 100", 100), };
Range<long>[] ranges = new Range<long>[] { new Range<long>(0, 100) };
CommonPropertiesForNamed[] types = new CommonPropertiesForNamed[]
{
new IntegerType<sbyte>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int8, null, null, null, null, 10, 2),
Expand All @@ -250,7 +252,7 @@ public void TestPrefix1024()
new IntegerType<ushort>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.UInt16, null, null, null, null, 10, 2),
new IntegerType<int>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int32, null, null, null, null, 10, 2),
new IntegerType<uint>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.UInt32, null, null, null, null, 10, 2),
new IntegerType<long>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int64, null, null, null, null, 10, 2),
new IntegerType<long>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int64, labeledIntegerType, null, ranges, ERDM_SensorUnit.BYTE, 10, 2),
new IntegerType<ulong>("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.UInt64, null, null, null, null, 10, 2),
};
foreach (CommonPropertiesForNamed integerType in types)
Expand Down
4 changes: 4 additions & 0 deletions RDMSharpTests/Metadata/JSON/TestLabeledBooleanType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using RDMSharp.Metadata;
using RDMSharp.Metadata.JSON.OneOfTypes;

namespace RDMSharpTests.Metadata.JSON
Expand All @@ -10,6 +11,9 @@ public void TestMany()
var labeledBooleanType = new LabeledBooleanType("NAME", "DISPLAY_NAME", "NOTES", null, true);
Assert.That(labeledBooleanType.Value, Is.True);
Assert.Throws(typeof(NotSupportedException), () => labeledBooleanType.GetDataLength());
byte[] bytes = new byte[0];
Assert.Throws(typeof(NotSupportedException), () => labeledBooleanType.ParseDataToPayload(ref bytes));
Assert.Throws(typeof(NotSupportedException), () => labeledBooleanType.ParsePayloadToData(new DataTree()));
}
}
}
4 changes: 4 additions & 0 deletions RDMSharpTests/Metadata/JSON/TestLabeledIntegerType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using RDMSharp.Metadata;
using RDMSharp.Metadata.JSON.OneOfTypes;

namespace RDMSharpTests.Metadata.JSON
Expand All @@ -10,6 +11,9 @@ public void TestMany()
var labeledIntegerType = new LabeledIntegerType("NAME", "DISPLAY_NAME", "NOTES", null, 3);
Assert.That(labeledIntegerType.Value, Is.EqualTo(3));
Assert.Throws(typeof(NotSupportedException), () => labeledIntegerType.GetDataLength());
byte[] bytes = new byte[0];
Assert.Throws(typeof(NotSupportedException), () => labeledIntegerType.ParseDataToPayload(ref bytes));
Assert.Throws(typeof(NotSupportedException), () => labeledIntegerType.ParsePayloadToData(new DataTree()));
}
}
}
1 change: 1 addition & 0 deletions RDMSharpTests/Metadata/JSON/TestPD_EnvelopeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void TestMany()
});

Assert.Throws(typeof(ArgumentException), () => pdEnvelopeType = new PD_EnvelopeType("NAME", "DISPLAY_NAME", "NOTES", null, "pdEnvelop", 2));

}
}
}

0 comments on commit 67240aa

Please sign in to comment.