diff --git a/RDMSharp/Metadata/JSON/OneOfTypes/BooleanType.cs b/RDMSharp/Metadata/JSON/OneOfTypes/BooleanType.cs index 887a0b6..cb48fc2 100644 --- a/RDMSharp/Metadata/JSON/OneOfTypes/BooleanType.cs +++ b/RDMSharp/Metadata/JSON/OneOfTypes/BooleanType.cs @@ -95,11 +95,25 @@ public override byte[] ParsePayloadToData(DataTree dataTree) public override DataTree ParseDataToPayload(ref byte[] data) { List issueList = new List(); - 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); } } } \ No newline at end of file diff --git a/RDMSharp/Metadata/JSON/OneOfTypes/LabeledBooleanType.cs b/RDMSharp/Metadata/JSON/OneOfTypes/LabeledBooleanType.cs index 5ea6b4b..8ded8c2 100644 --- a/RDMSharp/Metadata/JSON/OneOfTypes/LabeledBooleanType.cs +++ b/RDMSharp/Metadata/JSON/OneOfTypes/LabeledBooleanType.cs @@ -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() { diff --git a/RDMSharp/Metadata/JSON/OneOfTypes/LabeledIntegerType.cs b/RDMSharp/Metadata/JSON/OneOfTypes/LabeledIntegerType.cs index 7d33280..5f88836 100644 --- a/RDMSharp/Metadata/JSON/OneOfTypes/LabeledIntegerType.cs +++ b/RDMSharp/Metadata/JSON/OneOfTypes/LabeledIntegerType.cs @@ -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}"; diff --git a/RDMSharpTests/Metadata/JSON/TestBitType.cs b/RDMSharpTests/Metadata/JSON/TestBitType.cs index 1a4730a..e0d2bd8 100644 --- a/RDMSharpTests/Metadata/JSON/TestBitType.cs +++ b/RDMSharpTests/Metadata/JSON/TestBitType.cs @@ -1,3 +1,4 @@ +using RDMSharp.Metadata; using RDMSharp.Metadata.JSON.OneOfTypes; namespace RDMSharpTests.Metadata.JSON @@ -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())); } } } \ No newline at end of file diff --git a/RDMSharpTests/Metadata/JSON/TestBooleanType.cs b/RDMSharpTests/Metadata/JSON/TestBooleanType.cs index 4ac0a76..c0fa405 100644 --- a/RDMSharpTests/Metadata/JSON/TestBooleanType.cs +++ b/RDMSharpTests/Metadata/JSON/TestBooleanType.cs @@ -1,3 +1,4 @@ +using RDMSharp.Metadata; using RDMSharp.Metadata.JSON.OneOfTypes; using RDMSharp.RDM; @@ -20,6 +21,13 @@ 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]; @@ -27,7 +35,7 @@ public void TestMany() 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)); @@ -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); + } } } \ No newline at end of file diff --git a/RDMSharpTests/Metadata/JSON/TestIntegerType.cs b/RDMSharpTests/Metadata/JSON/TestIntegerType.cs index 8974a5b..6b2751c 100644 --- a/RDMSharpTests/Metadata/JSON/TestIntegerType.cs +++ b/RDMSharpTests/Metadata/JSON/TestIntegerType.cs @@ -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[] ranges = new Range[] { new Range(0, 100) }; CommonPropertiesForNamed[] types = new CommonPropertiesForNamed[] { new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int8, null, null, null, null, 10, 2), @@ -250,7 +252,7 @@ public void TestPrefix1024() new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.UInt16, null, null, null, null, 10, 2), new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int32, null, null, null, null, 10, 2), new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.UInt32, null, null, null, null, 10, 2), - new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int64, null, null, null, null, 10, 2), + new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.Int64, labeledIntegerType, null, ranges, ERDM_SensorUnit.BYTE, 10, 2), new IntegerType("NAME", "DISPLAY_NAME", "NOTES", null, EIntegerType.UInt64, null, null, null, null, 10, 2), }; foreach (CommonPropertiesForNamed integerType in types) diff --git a/RDMSharpTests/Metadata/JSON/TestLabeledBooleanType.cs b/RDMSharpTests/Metadata/JSON/TestLabeledBooleanType.cs index b7c0b68..f509997 100644 --- a/RDMSharpTests/Metadata/JSON/TestLabeledBooleanType.cs +++ b/RDMSharpTests/Metadata/JSON/TestLabeledBooleanType.cs @@ -1,3 +1,4 @@ +using RDMSharp.Metadata; using RDMSharp.Metadata.JSON.OneOfTypes; namespace RDMSharpTests.Metadata.JSON @@ -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())); } } } \ No newline at end of file diff --git a/RDMSharpTests/Metadata/JSON/TestLabeledIntegerType.cs b/RDMSharpTests/Metadata/JSON/TestLabeledIntegerType.cs index 9008fba..6545873 100644 --- a/RDMSharpTests/Metadata/JSON/TestLabeledIntegerType.cs +++ b/RDMSharpTests/Metadata/JSON/TestLabeledIntegerType.cs @@ -1,3 +1,4 @@ +using RDMSharp.Metadata; using RDMSharp.Metadata.JSON.OneOfTypes; namespace RDMSharpTests.Metadata.JSON @@ -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())); } } } \ No newline at end of file diff --git a/RDMSharpTests/Metadata/JSON/TestPD_EnvelopeType.cs b/RDMSharpTests/Metadata/JSON/TestPD_EnvelopeType.cs index eb2cd33..aee4f82 100644 --- a/RDMSharpTests/Metadata/JSON/TestPD_EnvelopeType.cs +++ b/RDMSharpTests/Metadata/JSON/TestPD_EnvelopeType.cs @@ -18,6 +18,7 @@ public void TestMany() }); Assert.Throws(typeof(ArgumentException), () => pdEnvelopeType = new PD_EnvelopeType("NAME", "DISPLAY_NAME", "NOTES", null, "pdEnvelop", 2)); + } } } \ No newline at end of file