Skip to content

Commit

Permalink
Minor code edits
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Dec 14, 2023
1 parent bf99aa2 commit ac112f8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
19 changes: 12 additions & 7 deletions CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static CBORObject ParseJSONNumber(
return null;
}

options = options ?? CBORDataUtilities.DefaultOptions;
_ = options ?? CBORDataUtilities.DefaultOptions;
bool preserveNegativeZero = options.PreserveNegativeZero;
JSONOptions.ConversionMode kind = options.NumberConversion;
int endPos = offset + count;
Expand Down Expand Up @@ -260,14 +260,19 @@ internal static CBORObject ParseJSONNumber(
chars,
initialOffset,
endPos - initialOffset);
return ed.IsZero && negative ? ed.Exponent.IsZero ?
preserveNegativeZero ?
if (ed.IsZero && negative) {
if (ed.Exponent.IsZero) {
return preserveNegativeZero ?
CBORObject.FromEDecimal(EDecimal.NegativeZero) :
CBORObject.FromInt32(0) :
!preserveNegativeZero ? CBORObject.FromEDecimal(ed.Negate()) :
CBORObject.FromEDecimal(ed) :
ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) :
CBORObject.FromInt32(0);
} else {
return !preserveNegativeZero ?
CBORObject.FromEDecimal(ed.Negate()) : CBORObject.FromEDecimal(ed);
}
} else {
return ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) :
CBORObject.FromEDecimal(ed);
}
} else if (kind == JSONOptions.ConversionMode.Double) {
var ef = EFloat.FromString(
chars,
Expand Down
2 changes: 1 addition & 1 deletion CBOR/PeterO/Cbor/CBORNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public CBORObject ToCBORObject() {
if (obj is ERational erf) {
return CBORObject.FromERational(erf);
}
throw new Exception("Unexpected type: " + obj.GetType());
throw new InvalidOperationException("Unexpected type: " + obj.GetType());
}

/// <summary>Gets this value's sign: -1 if nonzero and negative; 1 if
Expand Down
20 changes: 11 additions & 9 deletions CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@ public CBORObject GetOrDefault(CBORObject cborkey, CBORObject
}
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
var ckey = cborkey;
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
return PropertyMap.GetOrDefault(map, cborkey, defaultValue);
}
return defaultValue;
}
Expand Down Expand Up @@ -621,8 +620,10 @@ public CBORObject GetOrDefault(int key, CBORObject defaultValue) {
}
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
var ckey = FromInt32(key);
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
return PropertyMap.GetOrDefault(
map,
FromInt32(key),
defaultValue);
}
return defaultValue;
}
Expand All @@ -645,8 +646,10 @@ public CBORObject GetOrDefault(string key, CBORObject defaultValue) {
}
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
var ckey = FromString(key);
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
return PropertyMap.GetOrDefault(
map,
FromString(key),
defaultValue);
}
return defaultValue;
}
Expand Down Expand Up @@ -755,8 +758,7 @@ public CBORObject this[string key]
if (key == null) {
throw new ArgumentNullException(nameof(key));
}
var objkey = FromString(key);
return this[objkey];
return this[FromString(key)];
}

set {
Expand All @@ -766,9 +768,9 @@ public CBORObject this[string key]
if (value == null) {
throw new ArgumentNullException(nameof(value));
}
var objkey = FromString(key);
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
CBORObject objkey = FromString(key);
map[objkey] = value;
} else {
throw new InvalidOperationException("Not a map");
Expand Down
40 changes: 24 additions & 16 deletions CBORTest/CBORObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,10 @@ public void TestAsInt16() {
ToObjectTest.TestToFromObjectRoundTrip(
EDecimal.FromString(numberinfo["number"].AsString()));
if (numberinfo["int16"].AsBoolean()) {
int intForShort = TestCommon.StringToInt(
numberinfo["integer"].AsString());
Assert.AreEqual(
TestCommon.StringToInt(numberinfo["integer"].AsString()),
(short)intForShort,
cbornumber.ToObject(typeof(short)));
} else {
try {
Expand Down Expand Up @@ -9827,39 +9829,45 @@ public void TestFromJsonStringSmallDoubleSpec() {
Int32.MinValue);
}

private static void AreEqualDouble(double a, double b) {
if (a != b) {
Assert.Fail(a+ ", " + b);

Check warning on line 9834 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Check warning on line 9834 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Check warning on line 9834 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (windows-latest)

Check warning on line 9834 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (ubuntu-latest)

Check warning on line 9834 in CBORTest/CBORObjectTest.cs

View workflow job for this annotation

GitHub Actions / Core (macos-latest)

}
}

[Test]
[Timeout(10000)]
public void TestFromJsonStringSmallDouble() {
CBORObject cbor;
AssertJSONDouble("0", "double", 0.0);
cbor = FromJSON("[0, 1, 2, 3]", "double");
Assert.AreEqual(4, cbor.Count);
Assert.AreEqual(0.0, cbor[0].AsDouble());
Assert.AreEqual(1.0, cbor[1].AsDouble());
Assert.AreEqual(2.0, cbor[2].AsDouble());
Assert.AreEqual(3.0, cbor[3].AsDouble());
AreEqualDouble(0.0, cbor[0].AsDouble());
AreEqualDouble(1.0, cbor[1].AsDouble());
AreEqualDouble(2.0, cbor[2].AsDouble());
AreEqualDouble(3.0, cbor[3].AsDouble());
cbor = FromJSON("[0]", "double");
Assert.AreEqual(1, cbor.Count);
Assert.AreEqual(0.0, cbor[0].AsDouble());
AreEqualDouble(0.0, cbor[0].AsDouble());
cbor = FromJSON("[-0]", "double");
Assert.AreEqual(1, cbor.Count);
cbor = FromJSON("[1]", "double");
Assert.AreEqual(1, cbor.Count);
Assert.AreEqual(1.0, cbor[0].AsDouble());
AreEqualDouble(1.0, cbor[0].AsDouble());
cbor = FromJSON("[-1]", "double");
Assert.AreEqual(1, cbor.Count);
Assert.AreEqual(-1.0, cbor[0].AsDouble());
AreEqualDouble(-1.0, cbor[0].AsDouble());
cbor = FromJSON("[-1022,-1023,-1024,-1025,1022,1023,1024,1025]",
"double");
Assert.AreEqual(8, cbor.Count);
Assert.AreEqual(-1022.0, cbor[0].AsDouble());
Assert.AreEqual(-1023.0, cbor[1].AsDouble());
Assert.AreEqual(-1024.0, cbor[2].AsDouble());
Assert.AreEqual(-1025.0, cbor[3].AsDouble());
Assert.AreEqual(1022.0, cbor[4].AsDouble());
Assert.AreEqual(1023.0, cbor[5].AsDouble());
Assert.AreEqual(1024.0, cbor[6].AsDouble());
Assert.AreEqual(1025.0, cbor[7].AsDouble());
AreEqualDouble(-1022.0, cbor[0].AsDouble());
AreEqualDouble(-1023.0, cbor[1].AsDouble());
AreEqualDouble(-1024.0, cbor[2].AsDouble());
AreEqualDouble(-1025.0, cbor[3].AsDouble());
AreEqualDouble(1022.0, cbor[4].AsDouble());
AreEqualDouble(1023.0, cbor[5].AsDouble());
AreEqualDouble(1024.0, cbor[6].AsDouble());
AreEqualDouble(1025.0, cbor[7].AsDouble());
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion CBORTest/CBORTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,7 @@ public void TestRandomSlightlyModified() {
for (int i = 0; i < 2000; ++i) {
CBORObject originalObject = CBORTestCommon.RandomCBORObject(rand);
byte[] array = originalObject.EncodeToBytes();
Console.WriteLine("i=" + i + " obj=" + array.Length);
// Console.WriteLine("i=" + i + " obj=" + array.Length);
TestRandomOne(SlightlyModify(array, rand));
}
}
Expand Down

0 comments on commit ac112f8

Please sign in to comment.