Skip to content

Commit

Permalink
Version 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Jun 22, 2014
1 parent 5509f30 commit 90172c8
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 147 deletions.
1 change: 0 additions & 1 deletion CBORDocs2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Any copyright is dedicated to the Public Domain.
at: http://upokecenter.com/d/
*/
using System;
using PeterO.DocGen;

namespace CBORDocs {
internal class Program {
Expand Down
5 changes: 1 addition & 4 deletions CBORDocs2/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CBORDocs2")]
Expand Down
1 change: 0 additions & 1 deletion CBORTest/BEncodingTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using PeterO;
using PeterO.Cbor;
Expand Down
1 change: 0 additions & 1 deletion CBORTest/CBORExtraTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Any copyright is dedicated to the Public Domain.
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using PeterO;
using PeterO.Cbor;
Expand Down
82 changes: 78 additions & 4 deletions CBORTest/CBORObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void TestAsByte() {
CBORObject numberinfo = numbers[i];
CBORObject cbornumber = CBORObject.FromObject(ExtendedDecimal.FromString(numberinfo["number"].AsString()));
if (numberinfo["byte"].AsBoolean()) {
Assert.AreEqual(BigInteger.fromString(numberinfo["integer"].AsString()).intValue(), ((int)cbornumber.AsByte()) &0xff);
Assert.AreEqual(BigInteger.fromString(numberinfo["integer"].AsString()).intValue(), ((int)cbornumber.AsByte()) & 0xff);
} else {
try {
cbornumber.AsByte();
Expand Down Expand Up @@ -775,9 +775,61 @@ public void TestCount() {
Assert.AreEqual(0, CBORObject.NewArray().Count);
Assert.AreEqual(0, CBORObject.NewMap().Count);
}

// TODO: For 2.0
public static void TestDecodeFromBytesVersion2Dot0() {
try {
CBORObject.DecodeFromBytes(new byte[] { });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}

[TestMethod]
public void TestDecodeFromBytes() {
// not implemented yet
try {
CBORObject.DecodeFromBytes(new byte[] { 0x1c });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(null);
Assert.Fail("Should have failed");
} catch (ArgumentNullException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0x1e });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0xfe });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0xff });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
[TestMethod]
public void TestDivide() {
Expand Down Expand Up @@ -1193,7 +1245,9 @@ public void TestFromJSONString() {
}
[TestMethod]
public void TestFromObject() {
CBORObject[] cborarray = { CBORObject.False, CBORObject.True};
var cborarray = new CBORObject[2];
cborarray[0] = CBORObject.False;
cborarray[1] = CBORObject.True;
CBORObject cbor = CBORObject.FromObject(cborarray);
Assert.AreEqual(2, cbor.Count);
Assert.AreEqual(CBORObject.False, cbor[0]);
Expand Down Expand Up @@ -1555,7 +1609,6 @@ public void TestSet() {
}
[TestMethod]
public void TestSign() {
// TODO: Use number database
try {
int sign = CBORObject.True.Sign;
Assert.Fail("Should have failed");
Expand Down Expand Up @@ -1588,6 +1641,27 @@ public void TestSign() {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
CBORObject numbers = GetNumberData();
for (int i = 0; i < numbers.Count; ++i) {
CBORObject numberinfo = numbers[i];
CBORObject cbornumber = CBORObject.FromObject(ExtendedDecimal.FromString(numberinfo["number"].AsString()));
if (cbornumber.IsNaN()) {
try {
Assert.Fail(String.Empty + cbornumber.Sign);
Assert.Fail("Should have failed");
} catch (InvalidOperationException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
} else if (numberinfo["number"].AsString().IndexOf('-') == 0) {
Assert.AreEqual(-1, cbornumber.Sign);
} else if (numberinfo["number"].AsString().Equals("0")) {
Assert.AreEqual(0, cbornumber.Sign);
} else {
Assert.AreEqual(1, cbornumber.Sign);
}
}
}
[TestMethod]
public void TestSimpleValue() {
Expand Down
40 changes: 0 additions & 40 deletions CBORTest/CBORSupplementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,46 +226,6 @@ public void TestCBORObjectArgumentValidation() {
Assert.AreEqual(CBORObject.True, CBORObject.FromObject(true));
Assert.AreEqual(CBORObject.False, CBORObject.FromObject(false));
Assert.AreEqual(CBORObject.FromObject(8), CBORObject.FromObject((byte)8));
try {
CBORObject.DecodeFromBytes(null);
Assert.Fail("Should have failed");
} catch (ArgumentNullException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0x1e });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0xfe });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0xff });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.AddConverter(null, new FakeConverter());
Assert.Fail("Should have failed");
Expand Down
16 changes: 0 additions & 16 deletions CBORTest/CBORTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -948,22 +948,6 @@ public void TestCBORExceptions() {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.DecodeFromBytes(new byte[] { 0x1c });
Assert.Fail("Should have failed");
} catch (CBORException) {
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
CBORObject.NewArray().Remove(null);
Assert.Fail("Should have failed");
Expand Down
2 changes: 1 addition & 1 deletion CBORTest/CBORTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<StartArguments>Test.</StartArguments>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
Expand Down
1 change: 0 additions & 1 deletion CBORTest/TestCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Any copyright is dedicated to the Public Domain.
*/
using System;
using System.Globalization;
// using System.Numerics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PeterO;
using PeterO.Cbor;
Expand Down
4 changes: 2 additions & 2 deletions CBORTest/mstest2nunit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def utf8edit(file,createIfNotFound=false)
end

##################

Dir.chdir(File.dirname(__FILE__))
Dir.glob("*.cs"){|f|
utf8edit(f){|data|
data=data.gsub(/\[TestMethod\]/,"[Test]")
Expand All @@ -75,4 +75,4 @@ def utf8edit(file,createIfNotFound=false)
" <Reference Include=\"nunit.framework\" />")
next data
}
}
}
1 change: 1 addition & 0 deletions CBORTest/nunit2mstest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def utf8edit(file,createIfNotFound=false)

##################

Dir.chdir(File.dirname(__FILE__))
Dir.glob("*.cs"){|f|
utf8edit(f){|data|
data=data.gsub(/\[Test\]/,"[TestMethod]")
Expand Down
9 changes: 5 additions & 4 deletions Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,19 +1278,20 @@ internal static CBORObject GetFixedLengthObject(int firstbyte, byte[] data) {
/// <summary>Generates a CBOR object from an array of CBOR-encoded bytes.</summary>
/// <param name='data'>A byte array.</param>
/// <returns>A CBOR object corresponding to the data.</returns>
/// <exception cref='System.ArgumentException'>Data is null or empty.</exception>
/// <exception cref='System.ArgumentException'>Data is empty. Note:
/// In version 2.0, this method may change to throw CBORException instead
/// if the data is empty.</exception>
/// <exception cref='CBORException'>There was an error in reading
/// or parsing the data. This includes cases where not all of the byte array
/// represents a CBOR object, and cases where <paramref name='data'/>
/// is empty.</exception>
/// represents a CBOR object.</exception>
/// <exception cref='System.ArgumentNullException'>The parameter
/// <paramref name='data'/> is null.</exception>
public static CBORObject DecodeFromBytes(byte[] data) {
if (data == null) {
throw new ArgumentNullException("data");
}
if (data.Length == 0) {
throw new CBORException("data is empty.");
throw new ArgumentException("data is empty.");
}
var firstbyte = (int)(data[0] & (int)0xff);
int expectedLength = valueExpectedLengths[firstbyte];
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ project, add the following to the `dependencies` section in your `pom.xml` file:
<dependency>
<groupId>com.upokecenter</groupId>
<artifactId>cbor</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</dependency>
```

Expand Down Expand Up @@ -260,6 +260,14 @@ The following are some clarifications to RFC 7049.
Release Notes
-----------

In version 1.3:

- Added a CompareToIgnoreTags method to CBORObject
- The BigInteger constructor in the C# version is deprecated
- Fixed bugs in converting from CBOR float and double to integers in some corner cases
- Fixed a bug where CBORObject's OutermostTag returns 0 instead of the correct -1 for untagged objects
- Fixed a bug where BigInteger's bitLength return value can be wrong in some corner cases

In version 1.2:

- The WriteJSON and WriteToJSON methods were added to CBORObject
Expand Down
7 changes: 3 additions & 4 deletions docs/PeterO.Cbor.CBORObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,10 @@ A CBOR object corresponding to the data.
<b>Exceptions:</b>

* System.ArgumentException:
Data is null or empty.
Data is empty. Note: In version 2.0, this method may change to throw CBORException instead if the data is empty.

* PeterO.Cbor.CBORException:
There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object, and cases where <i>data</i>
is empty.
There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object.

* System.ArgumentNullException:
The parameter <i>data</i>
Expand Down Expand Up @@ -1869,7 +1868,7 @@ A CBOR object where each key and value of the given map is converted to a CBOR o
public static PeterO.Cbor.CBORObject FromObject(
object obj);

Generates a CBORObject from an arbitrary object. The following types are specially handled by this method: `null` , primitive types, strings, `CBORObject` , `ExtendedDecimal` , `ExtendedFloat` , the custom `BigInteger` , lists, arrays, enumerations ( `Enum` objects), and maps.
Generates a CBORObject from an arbitrary object. The following types are specially handled by this method: `null` , primitive types, strings, `CBORObject` , `ExtendedDecimal` , `ExtendedFloat` , the custom `BigInteger` , lists, arrays, enumerations ( `Enum` objects), and maps.

In the .NET version, if the object is a type not specially handled by this method, returns a CBOR map with the values of each of its read/write properties (or all properties in the case of an anonymous type). Properties are converted to their camel-case names (meaning if a name starts with A to Z, that letter is lower-cased). If the property name begins with the word "Is", that word is deleted from the name. Also, .NET `Enum` objects will be converted to their integer values, and a multidimensional array is converted to an array of arrays. The .NET value DBNull.Value is converted to CBORObject.Undefined.

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/upokecenter/cbor/CBORObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1229,10 +1229,12 @@ static CBORObject GetFixedLengthObject(int firstbyte, byte[] data) {
* Generates a CBOR object from an array of CBOR-encoded bytes.
* @param data A byte array.
* @return A CBOR object corresponding to the data.
* @throws java.lang.IllegalArgumentException Data is null or empty.
* @throws java.lang.IllegalArgumentException Data is empty. Note: In version
* 2.0, this method may change to throw CBORException instead if the
* data is empty.
* @throws CBORException There was an error in reading or parsing the
* data. This includes cases where not all of the byte array represents
* a CBOR object, and cases where {@code data} is empty.
* a CBOR object.
* @throws java.lang.NullPointerException The parameter {@code data}
* is null.
*/
Expand All @@ -1241,7 +1243,7 @@ public static CBORObject DecodeFromBytes(byte[] data) {
throw new NullPointerException("data");
}
if (data.length == 0) {
throw new CBORException("data is empty.");
throw new IllegalArgumentException("data is empty.");
}
int firstbyte = (int)(data[0] & (int)0xff);
int expectedLength = valueExpectedLengths[firstbyte];
Expand Down Expand Up @@ -1491,7 +1493,6 @@ public CBORObject get(int index) {
/**
* Sets the value of a CBOR object by integer index in this array.
* @param index Zero-based index of the element.
* @return A CBORObject object.
* @throws java.lang.IllegalStateException This object is not an
* array.
* @throws java.lang.NullPointerException The parameter "value" is
Expand Down Expand Up @@ -1567,7 +1568,6 @@ public CBORObject get(CBORObject key) {
* Sets the value of a CBOR object in this map, using a CBOR object as the
* key.
* @param key A CBORObject object. (2).
* @return A CBORObject object.
* @throws java.lang.NullPointerException The key is null (as opposed
* to CBORObject.Null); or the set method is called and the value is null.
* @throws java.lang.IllegalStateException This object is not a map.
Expand Down Expand Up @@ -1605,7 +1605,6 @@ public CBORObject get(String key) {
/**
* Sets the value of a CBOR object in this map, using a string as the key.
* @param key A key that points to the desired value.
* @return A CBORObject object.
* @throws java.lang.NullPointerException The key is null.
* @throws java.lang.IllegalStateException This object is not a map.
*/
Expand Down
Loading

0 comments on commit 90172c8

Please sign in to comment.