Skip to content

Commit

Permalink
further prepare for version 4.0.0-beta2
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Aug 24, 2019
1 parent a8bddef commit 1d8a6b4
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 122 deletions.
18 changes: 4 additions & 14 deletions CBOR/PeterO/Cbor/CBORTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,8 @@ internal object ConvertBackWithConverter(
if (convinfo == null) {
return null;
}
if (convinfo.FromObject == null) {
return null;
}
return PropertyMap.InvokeOneArgumentMethod(
convinfo.FromObject,
convinfo.Converter,
cbor);
return (convinfo.FromObject == null) ? null :
PropertyMap.CallFromObject(convinfo, cbor);
}

internal CBORObject ConvertWithConverter(object obj) {
Expand All @@ -89,13 +84,8 @@ internal CBORObject ConvertWithConverter(object obj) {
} else {
return null;
}
if (convinfo == null) {
return null;
}
return (CBORObject)PropertyMap.InvokeOneArgumentMethod(
convinfo.ToObject,
convinfo.Converter,
obj);
return (convinfo == null) ? null :
PropertyMap.CallToObject(convinfo, obj);
}

/// <summary>Returns whether the given Java or.NET type name fits the
Expand Down
23 changes: 18 additions & 5 deletions CBOR/PeterO/Cbor/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ private static MethodInfo GetTypeMethod(
private static bool HasCustomAttribute(
Type t,
string name) {
#if NET40 || NET20
foreach (var attr in t.GetCustomAttributes(false)) {
#else
foreach (var attr in t.CustomAttributes) {
#endif
if (attr.GetType().FullName.Equals(name,
StringComparison.Ordinal)) {
return true;
Expand Down Expand Up @@ -141,7 +137,6 @@ private static bool HasCustomAttribute(
}
return false;
}

#endif

private static readonly IDictionary<Type, IList<PropertyData>>
Expand Down Expand Up @@ -826,6 +821,24 @@ public static object ObjectWithProperties(
return o;
}

public static CBORObject CallToObject(
CBORTypeMapper.ConverterInfo convinfo,
object obj) {
return (CBORObject)PropertyMap.InvokeOneArgumentMethod(
convinfo.ToObject,
convinfo.Converter,
obj);
}

public static object CallFromObject(
CBORTypeMapper.ConverterInfo convinfo,
CBORObject obj) {
return (CBORObject)PropertyMap.InvokeOneArgumentMethod(
convinfo.FromObject,
convinfo.Converter,
obj);
}

public static IEnumerable<KeyValuePair<string, object>>
GetProperties(Object o) {
return GetProperties(o, true);
Expand Down
231 changes: 130 additions & 101 deletions CBORTest/CBORObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2341,13 +2341,25 @@ public void TestReadSequence() {
byte[] bytes;
bytes = new byte[] { 0 };
using (var ms = new MemoryStream(bytes)) {
objs = CBORObject.ReadSequence(ms);
objs = null;
try {
objs = CBORObject.ReadSequence(ms);
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
Assert.AreEqual(1, objs.Length);
Assert.AreEqual(CBORObject.FromObject(0), objs[0]);
bytes = new byte[] { 0, 1, 2 };
using (var ms = new MemoryStream(bytes)) {
objs = CBORObject.ReadSequence(ms);
objs = null;
try {
objs = CBORObject.ReadSequence(ms);
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
Assert.AreEqual(3, objs.Length);
Assert.AreEqual(CBORObject.FromObject(0), objs[0]);
Expand Down Expand Up @@ -2379,15 +2391,27 @@ public void TestReadSequence() {
}
bytes = new byte[] { 0, 1, 0x61, 0x41 };
using (var ms = new MemoryStream(bytes)) {
objs = CBORObject.ReadSequence(ms);
objs = null;
try {
objs = CBORObject.ReadSequence(ms);
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
Assert.AreEqual(3, objs.Length);
Assert.AreEqual(CBORObject.FromObject(0), objs[0]);
Assert.AreEqual(CBORObject.FromObject(1), objs[1]);
Assert.AreEqual(CBORObject.FromObject("A"), objs[2]);
bytes = new byte[] { };
using (var ms = new MemoryStream(bytes)) {
objs = CBORObject.ReadSequence(ms);
objs = null;
try {
objs = CBORObject.ReadSequence(ms);
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
Assert.AreEqual(0, objs.Length);
try {
Expand Down Expand Up @@ -3093,13 +3117,13 @@ private void CheckKeyValue(CBORObject o, string key, object value) {
}

public enum EnumClass {
/// <summary>Internal API.</summary>
/// <summary>Internal API.</summary>
Value1,

/// <summary>Internal API.</summary>
/// <summary>Internal API.</summary>
Value2,

/// <summary>Internal API.</summary>
/// <summary>Internal API.</summary>
Value3,
}

Expand Down Expand Up @@ -6798,111 +6822,116 @@ public void TestWriteValue() {
public void TestWriteFloatingPointValue() {
var r = new RandomGenerator();
var bytes = new byte[] { 0, 0, 0 };
for (var i = 0; i < 0x10000; ++i) {
bytes[0] = (byte)0xf9;
bytes[1] = (byte)((i >> 8) & 0xff);
bytes[2] = (byte)(i & 0xff);
CBORObject cbor = CBORObject.DecodeFromBytes(bytes);
if (!cbor.IsNaN()) {
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsDouble(),
2);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsSingle(),
2);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
try {
for (var i = 0; i < 0x10000; ++i) {
bytes[0] = (byte)0xf9;
bytes[1] = (byte)((i >> 8) & 0xff);
bytes[2] = (byte)(i & 0xff);
CBORObject cbor = CBORObject.DecodeFromBytes(bytes);
if (!cbor.IsNaN()) {
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsDouble(),
2);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsSingle(),
2);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
}
}
}
// 32-bit values
bytes = new byte[5];
for (var i = 0; i < 100000; ++i) {
bytes[0] = (byte)0xfa;
for (var j = 1; j <= 4; ++j) {
bytes[j] = (byte)r.UniformInt(256);
}

CBORObject cbor = CBORObject.DecodeFromBytes(bytes);
if (!cbor.IsNaN()) {
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsDouble(),
4);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
// 32-bit values
bytes = new byte[5];
for (var i = 0; i < 100000; ++i) {
bytes[0] = (byte)0xfa;
for (var j = 1; j <= 4; ++j) {
bytes[j] = (byte)r.UniformInt(256);
}
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsSingle(),
4);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());

CBORObject cbor = CBORObject.DecodeFromBytes(bytes);
if (!cbor.IsNaN()) {
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsDouble(),
4);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsSingle(),
4);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
}
}
}
// 64-bit values
bytes = new byte[9];
for (var i = 0; i < 100000; ++i) {
bytes[0] = (byte)0xfb;
for (var j = 1; j <= 8; ++j) {
bytes[j] = (byte)r.UniformInt(256);
}
CBORObject cbor = CBORObject.DecodeFromBytes(bytes);
if (!cbor.IsNaN()) {
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsDouble(),
8);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
CBORObject c2 = null;
byte[] c2bytes = null;
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsSingle(),
8);
c2bytes = ms.ToArray();
c2 = CBORObject.DecodeFromBytes(
c2bytes);
// 64-bit values
bytes = new byte[9];
for (var i = 0; i < 100000; ++i) {
bytes[0] = (byte)0xfb;
for (var j = 1; j <= 8; ++j) {
bytes[j] = (byte)r.UniformInt(256);
}
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
c2.AsSingle(),
8);
TestCommon.AssertByteArraysEqual(c2bytes, ms.ToArray());
}
if (i == 0) {
CBORObject cbor = CBORObject.DecodeFromBytes(bytes);
if (!cbor.IsNaN()) {
using (var ms = new MemoryStream()) {
try {
CBORObject.WriteFloatingPointValue(ms, cbor.AsSingle(), 5);
Assert.Fail("Should have failed");
} catch (ArgumentException) {
// NOTE: Intentionally empty
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.WriteFloatingPointValue(null, cbor.AsSingle(), 4);
Assert.Fail("Should have failed");
} catch (ArgumentNullException) {
// NOTE: Intentionally empty
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsDouble(),
8);
TestCommon.AssertByteArraysEqual(bytes, ms.ToArray());
}
CBORObject c2 = null;
byte[] c2bytes = null;
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
cbor.AsSingle(),
8);
c2bytes = ms.ToArray();
c2 = CBORObject.DecodeFromBytes(
c2bytes);
}
using (var ms = new MemoryStream()) {
CBORObject.WriteFloatingPointValue(
ms,
c2.AsSingle(),
8);
TestCommon.AssertByteArraysEqual(c2bytes, ms.ToArray());
}
if (i == 0) {
using (var ms = new MemoryStream()) {
try {
CBORObject.WriteFloatingPointValue(ms, cbor.AsSingle(), 5);
Assert.Fail("Should have failed");
} catch (ArgumentException) {
// NOTE: Intentionally empty
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.WriteFloatingPointValue(null, cbor.AsSingle(), 4);
Assert.Fail("Should have failed");
} catch (ArgumentNullException) {
// NOTE: Intentionally empty
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
}
}
}
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}

Expand Down
2 changes: 1 addition & 1 deletion CBORTest/CBORTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,7 +2181,7 @@ public void TestDecodeCtap2Canonical() {
}
}
for (var i = 2; i <= 5; ++i) {
int eb = (byte)(0x20 * i);
int eb = 0x20 * i;
bytes = new byte[] { (byte)eb };
try {
CBORObject.DecodeFromBytes(bytes, options);
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Reflection;
[assembly: System.CLSCompliant(true)]
[assembly: AssemblyInformationalVersion("4.0.0-beta1")]
[assembly: AssemblyVersion("4.0.0")]
[assembly: AssemblyVersion("4.0.0-beta2")]
[assembly: AssemblyFileVersion("4.0.0.0")]
[assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" +
"on)")]
Expand Down

0 comments on commit 1d8a6b4

Please sign in to comment.