|
| 1 | +using Dahomey.Cbor.ObjectModel; |
| 2 | +using Dahomey.Cbor.Serialization; |
| 3 | +using Dahomey.Cbor.Serialization.Converters; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace Dahomey.Cbor.Tests |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class CborReaderValueTest |
| 12 | + { |
| 13 | + [DataTestMethod] |
| 14 | + [DataRow("F5", true, null)] |
| 15 | + [DataRow("F4", false, null)] |
| 16 | + public void ReadBoolean(string hexBuffer, bool value, Type expectedExceptionType) |
| 17 | + { |
| 18 | + TestRead(hexBuffer, (CborBoolean)value, expectedExceptionType); |
| 19 | + } |
| 20 | + |
| 21 | + [DataTestMethod] |
| 22 | + [DataRow("3B7FFFFFFFFFFFFFFF", long.MinValue, null)] |
| 23 | + [DataRow("3903E7", -1000L, null)] |
| 24 | + [DataRow("3863", -100L, null)] |
| 25 | + [DataRow("3818", -25L, null)] |
| 26 | + [DataRow("37", -24L, null)] |
| 27 | + [DataRow("2B", -12L, null)] |
| 28 | + [DataRow("1BFFFFFFFFFFFFFFFF", -1, typeof(CborException))] |
| 29 | + [DataRow("18", -1, typeof(CborException))] |
| 30 | + [DataRow("19", -1, typeof(CborException))] |
| 31 | + [DataRow("1A", -1, typeof(CborException))] |
| 32 | + [DataRow("1B", -1, typeof(CborException))] |
| 33 | + [DataRow("1C", -1, typeof(CborException))] |
| 34 | + [DataRow("1D", -1, typeof(CborException))] |
| 35 | + [DataRow("1E", -1, typeof(CborException))] |
| 36 | + [DataRow("1F", -1, typeof(CborException))] |
| 37 | + public void ReadNegative(string hexBuffer, long expectedValue, Type expectedExceptionType) |
| 38 | + { |
| 39 | + TestRead(hexBuffer, (CborNegative)expectedValue, expectedExceptionType); |
| 40 | + } |
| 41 | + |
| 42 | + [DataTestMethod] |
| 43 | + [DataRow("00", 0ul, null)] |
| 44 | + [DataRow("0C", 12ul, null)] |
| 45 | + [DataRow("17", 23ul, null)] |
| 46 | + [DataRow("1818", 24ul, null)] |
| 47 | + [DataRow("1864", 100ul, null)] |
| 48 | + [DataRow("1903E8", 1000ul, null)] |
| 49 | + [DataRow("1B7FFFFFFFFFFFFFFF", (ulong)long.MaxValue, null)] |
| 50 | + [DataRow("1BFFFFFFFFFFFFFFFF", ulong.MaxValue, null)] |
| 51 | + [DataRow("18", 0ul, typeof(CborException))] |
| 52 | + [DataRow("19", 0ul, typeof(CborException))] |
| 53 | + [DataRow("1A", 0ul, typeof(CborException))] |
| 54 | + [DataRow("1B", 0ul, typeof(CborException))] |
| 55 | + [DataRow("1C", 0ul, typeof(CborException))] |
| 56 | + [DataRow("1D", 0ul, typeof(CborException))] |
| 57 | + [DataRow("1E", 0ul, typeof(CborException))] |
| 58 | + [DataRow("1F", 0ul, typeof(CborException))] |
| 59 | + public void ReadPositive(string hexBuffer, ulong expectedValue, Type expectedExceptionType) |
| 60 | + { |
| 61 | + TestRead(hexBuffer, (CborPositive)expectedValue, expectedExceptionType); |
| 62 | + } |
| 63 | + |
| 64 | + [DataTestMethod] |
| 65 | + [DataRow("FA4141EB85", 12.12f, null)] |
| 66 | + [DataRow("FAFFC00000", float.NaN, null)] |
| 67 | + [DataRow("FA7F800000", float.PositiveInfinity, null)] |
| 68 | + [DataRow("FAFF800000", float.NegativeInfinity, null)] |
| 69 | + public void ReadSingle(string hexBuffer, float expectedValue, Type expectedExceptionType) |
| 70 | + { |
| 71 | + TestRead(hexBuffer, (CborSingle)expectedValue, expectedExceptionType); |
| 72 | + } |
| 73 | + |
| 74 | + [DataTestMethod] |
| 75 | + [DataRow("FB40283D70A3D70A3D", 12.12, null)] |
| 76 | + [DataRow("FBFFF8000000000000", double.NaN, null)] |
| 77 | + [DataRow("FB7FF0000000000000", double.PositiveInfinity, null)] |
| 78 | + [DataRow("FBFFF0000000000000", double.NegativeInfinity, null)] |
| 79 | + public void ReadDouble(string hexBuffer, double expectedValue, Type expectedExceptionType) |
| 80 | + { |
| 81 | + TestRead(hexBuffer, (CborDouble)expectedValue, expectedExceptionType); |
| 82 | + } |
| 83 | + |
| 84 | + [DataTestMethod] |
| 85 | + [DataRow("63666F6F", "foo", null)] |
| 86 | + [DataRow("60", "", null)] |
| 87 | + public void ReadString(string hexBuffer, string expectedValue, Type expectedExceptionType) |
| 88 | + { |
| 89 | + TestRead(hexBuffer, (CborString)expectedValue, expectedExceptionType); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void ReadNull() |
| 94 | + { |
| 95 | + TestRead("F6", CborValue.Null); |
| 96 | + } |
| 97 | + |
| 98 | + [DataTestMethod] |
| 99 | + [DataRow("8401020304", "1,2,3,4", null)] |
| 100 | + public void ReadInt32List(string hexBuffer, string expectedValue, Type expectedExceptionType) |
| 101 | + { |
| 102 | + CborArray array = new CborArray(expectedValue.Split(',').Select(s => (CborValue)int.Parse(s))); |
| 103 | + TestRead(hexBuffer, array, expectedExceptionType); |
| 104 | + } |
| 105 | + |
| 106 | + [DataTestMethod] |
| 107 | + [DataRow("84626161626262626363626464", "aa,bb,cc,dd", null)] |
| 108 | + public void ReadStringList(string hexBuffer, string expectedValue, Type expectedExceptionType) |
| 109 | + { |
| 110 | + CborArray array = new CborArray(expectedValue.Split(',').Select(s => (CborValue)s)); |
| 111 | + TestRead(hexBuffer, array, expectedExceptionType); |
| 112 | + } |
| 113 | + |
| 114 | + [TestMethod] |
| 115 | + public void ReadObject() |
| 116 | + { |
| 117 | + const string hexBuffer = |
| 118 | + "A666737472696E6763666F6F666E756D626572FB40283D70A3D70A3D64626F6F6CF5646E756C6CF6656172726179820102666F626A656374A162696401"; |
| 119 | + CborValue actual = Read(hexBuffer); |
| 120 | + Assert.IsNotNull(actual); |
| 121 | + Assert.AreEqual(CborValueType.Object, actual.Type); |
| 122 | + Assert.IsInstanceOfType(actual, typeof(CborObject)); |
| 123 | + |
| 124 | + // pairs |
| 125 | + CborObject actualObject = (CborObject)actual; |
| 126 | + Assert.AreEqual(6, actualObject.Pairs.Count); |
| 127 | + |
| 128 | + // string |
| 129 | + CborPair actualStringPair = actualObject.Pairs[0]; |
| 130 | + Assert.IsNotNull(actualStringPair); |
| 131 | + Assert.AreEqual("string", actualStringPair.Name); |
| 132 | + Assert.AreEqual(CborValueType.String, actualStringPair.Value.Type); |
| 133 | + Assert.IsInstanceOfType(actualStringPair.Value, typeof(CborString)); |
| 134 | + Assert.AreEqual("foo", actualStringPair.Value.Value<string>()); |
| 135 | + |
| 136 | + // number |
| 137 | + CborPair actualNumberPair = actualObject.Pairs[1]; |
| 138 | + Assert.IsNotNull(actualNumberPair); |
| 139 | + Assert.AreEqual("number", actualNumberPair.Name); |
| 140 | + Assert.AreEqual(CborValueType.Double, actualNumberPair.Value.Type); |
| 141 | + Assert.IsInstanceOfType(actualNumberPair.Value, typeof(CborDouble)); |
| 142 | + Assert.AreEqual(12.12, actualNumberPair.Value.Value<double>(), 3); |
| 143 | + |
| 144 | + // bool |
| 145 | + CborPair actualBoolPair = actualObject.Pairs[2]; |
| 146 | + Assert.IsNotNull(actualBoolPair); |
| 147 | + Assert.AreEqual("bool", actualBoolPair.Name); |
| 148 | + Assert.AreEqual(CborValueType.Boolean, actualBoolPair.Value.Type); |
| 149 | + Assert.IsInstanceOfType(actualBoolPair.Value, typeof(CborBoolean)); |
| 150 | + Assert.IsTrue(actualBoolPair.Value.Value<bool>()); |
| 151 | + |
| 152 | + // null |
| 153 | + CborPair actualNullPair = actualObject.Pairs[3]; |
| 154 | + Assert.IsNotNull(actualNullPair); |
| 155 | + Assert.AreEqual("null", actualNullPair.Name); |
| 156 | + Assert.AreEqual(CborValueType.Null, actualNullPair.Value.Type); |
| 157 | + Assert.IsInstanceOfType(actualNullPair.Value, typeof(CborNull)); |
| 158 | + |
| 159 | + // array |
| 160 | + CborPair actualArrayPair = actualObject.Pairs[4]; |
| 161 | + Assert.IsNotNull(actualArrayPair); |
| 162 | + Assert.AreEqual("array", actualArrayPair.Name); |
| 163 | + Assert.AreEqual(CborValueType.Array, actualArrayPair.Value.Type); |
| 164 | + Assert.IsInstanceOfType(actualArrayPair.Value, typeof(CborArray)); |
| 165 | + CborArray CborArray = (CborArray)actualArrayPair.Value; |
| 166 | + Assert.AreEqual(2, CborArray.Values.Count); |
| 167 | + Assert.AreEqual(1, CborArray.Values[0].Value<double>()); |
| 168 | + Assert.AreEqual(2, CborArray.Values[1].Value<double>()); |
| 169 | + |
| 170 | + // object |
| 171 | + CborPair actualObjectPair = actualObject.Pairs[5]; |
| 172 | + Assert.IsNotNull(actualObjectPair); |
| 173 | + Assert.AreEqual("object", actualObjectPair.Name); |
| 174 | + Assert.AreEqual(CborValueType.Object, actualObjectPair.Value.Type); |
| 175 | + Assert.IsInstanceOfType(actualObjectPair.Value, typeof(CborObject)); |
| 176 | + CborObject CborObject = (CborObject)actualObjectPair.Value; |
| 177 | + Assert.AreEqual("id", CborObject.Pairs[0].Name); |
| 178 | + Assert.AreEqual(1, CborObject.Pairs[0].Value.Value<double>()); |
| 179 | + } |
| 180 | + |
| 181 | + [TestMethod] |
| 182 | + public void ReadArray() |
| 183 | + { |
| 184 | + string hexBuffer = "8663666F6FFB40283D70A3D70A3DF5F6820102A162696401"; |
| 185 | + CborValue actual = Read(hexBuffer); |
| 186 | + Assert.IsNotNull(actual); |
| 187 | + Assert.AreEqual(CborValueType.Array, actual.Type); |
| 188 | + Assert.IsInstanceOfType(actual, typeof(CborArray)); |
| 189 | + |
| 190 | + // values |
| 191 | + CborArray actualArray = (CborArray)actual; |
| 192 | + Assert.AreEqual(6, actualArray.Values.Count); |
| 193 | + |
| 194 | + // string |
| 195 | + CborValue actualString = actualArray.Values[0]; |
| 196 | + Assert.IsNotNull(actualString); |
| 197 | + Assert.AreEqual(CborValueType.String, actualString.Type); |
| 198 | + Assert.IsInstanceOfType(actualString, typeof(CborString)); |
| 199 | + Assert.AreEqual("foo", actualString.Value<string>()); |
| 200 | + |
| 201 | + // number |
| 202 | + CborValue actualNumber = actualArray.Values[1]; |
| 203 | + Assert.IsNotNull(actualNumber); |
| 204 | + Assert.AreEqual(CborValueType.Double, actualNumber.Type); |
| 205 | + Assert.IsInstanceOfType(actualNumber, typeof(CborDouble)); |
| 206 | + Assert.AreEqual(12.12, actualNumber.Value<double>(), 3); |
| 207 | + |
| 208 | + // bool |
| 209 | + CborValue actualBool = actualArray.Values[2]; |
| 210 | + Assert.IsNotNull(actualBool); |
| 211 | + Assert.AreEqual(CborValueType.Boolean, actualBool.Type); |
| 212 | + Assert.IsInstanceOfType(actualBool, typeof(CborBoolean)); |
| 213 | + Assert.IsTrue(actualBool.Value<bool>()); |
| 214 | + |
| 215 | + // null |
| 216 | + CborValue actualNull = actualArray.Values[3]; |
| 217 | + Assert.IsNotNull(actualNull); |
| 218 | + Assert.AreEqual(CborValueType.Null, actualNull.Type); |
| 219 | + |
| 220 | + // array |
| 221 | + CborValue actualArrayValue = actualArray.Values[4]; |
| 222 | + Assert.IsNotNull(actualArrayValue); |
| 223 | + Assert.AreEqual(CborValueType.Array, actualArrayValue.Type); |
| 224 | + Assert.IsInstanceOfType(actualArrayValue, typeof(CborArray)); |
| 225 | + CborArray CborArray = (CborArray)actualArrayValue; |
| 226 | + Assert.AreEqual(2, CborArray.Values.Count); |
| 227 | + Assert.AreEqual(1, CborArray.Values[0].Value<double>()); |
| 228 | + Assert.AreEqual(2, CborArray.Values[1].Value<double>()); |
| 229 | + |
| 230 | + // object |
| 231 | + CborValue actualObject = actualArray.Values[5]; |
| 232 | + Assert.IsNotNull(actualObject); |
| 233 | + Assert.AreEqual(CborValueType.Object, actualObject.Type); |
| 234 | + Assert.IsInstanceOfType(actualObject, typeof(CborObject)); |
| 235 | + CborObject CborObject = (CborObject)actualObject; |
| 236 | + Assert.AreEqual("id", CborObject.Pairs[0].Name); |
| 237 | + Assert.AreEqual(1, CborObject.Pairs[0].Value.Value<double>()); |
| 238 | + } |
| 239 | + |
| 240 | + private CborValue Read(string hexBuffer) |
| 241 | + { |
| 242 | + Span<byte> buffer = hexBuffer.HexToBytes(); |
| 243 | + CborReader reader = new CborReader(buffer); |
| 244 | + ICborConverter<CborValue> converter = CborConverter.Lookup<CborValue>(); |
| 245 | + return converter.Read(ref reader); |
| 246 | + } |
| 247 | + |
| 248 | + private void TestRead(string hexBuffer, CborValue expectedValue, Type expectedExceptionType = null) |
| 249 | + { |
| 250 | + if (expectedExceptionType != null) |
| 251 | + { |
| 252 | + try |
| 253 | + { |
| 254 | + Read(hexBuffer); |
| 255 | + } |
| 256 | + catch (Exception ex) |
| 257 | + { |
| 258 | + Assert.IsInstanceOfType(ex, expectedExceptionType); |
| 259 | + } |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + CborValue actualValue = Read(hexBuffer); |
| 264 | + Assert.AreEqual(expectedValue, actualValue); |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments