public static CBORObject Addition(CBORObject a, CBORObject b) { if (a == null) throw new ArgumentNullException("a"); if (b == null) throw new ArgumentNullException("b"); int combo = (a.ItemType << 4) | b.ItemType; BigInteger bvalueA; BigInteger bvalueB; switch (combo) { case (CBORObject.CBORObjectType_Integer << 4) | CBORObject.CBORObjectType_Integer: { long valueA = (long)a.ThisItem; long valueB = (long)b.ThisItem; if ((valueA < 0 && valueB < Int64.MinValue - valueA) || (valueA > 0 && valueB > Int64.MaxValue - valueA)) { // would overflow, convert to BigInteger return CBORObject.FromObject(((BigInteger)valueA) + (BigInteger)valueB); } return CBORObject.FromObject(valueA + valueB); } case (CBORObject.CBORObjectType_Integer << 4) | CBORObject.CBORObjectType_BigInteger: { bvalueA = (BigInteger)(long)a.ThisItem; bvalueB = (BigInteger)b.ThisItem; return CBORObject.FromObject(bvalueA + (BigInteger)bvalueB); } case (CBORObject.CBORObjectType_BigInteger << 4) | CBORObject.CBORObjectType_Integer: { bvalueA = (BigInteger)a.ThisItem; bvalueB = (BigInteger)(long)b.ThisItem; return CBORObject.FromObject(bvalueA + (BigInteger)bvalueB); } case (CBORObject.CBORObjectType_BigInteger << 4) | CBORObject.CBORObjectType_BigInteger: { bvalueA = (BigInteger)a.ThisItem; bvalueB = (BigInteger)b.ThisItem; return CBORObject.FromObject(bvalueA + (BigInteger)bvalueB); } default: return CBORObject.FromObject(a.AsExtendedDecimal().Add(b.AsExtendedDecimal())); } }
private static string ObjectMessages(CBORObject o1, CBORObject o2, String s) { if (o1.Type == CBORType.Number && o2.Type == CBORType.Number) { return s + ":\n" + o1.ToString() + " and\n" + o2.ToString() + "\nOR\n" + o1.AsExtendedDecimal().ToString() + " and\n" + o2.AsExtendedDecimal().ToString() + "\nOR\n" + "AddSubCompare(" + ToByteArrayString(o1) + ",\n" + ToByteArrayString(o2) + ");"; } else { return s + ":\n" + o1.ToString() + " and\n" + o2.ToString() + "\nOR\n" + ToByteArrayString(o1) + " and\n" + ToByteArrayString(o2); } }
private static void CompareDecimals(CBORObject o1, CBORObject o2) { int cmpDecFrac = o1.AsExtendedDecimal().CompareTo(o2.AsExtendedDecimal()); int cmpCobj = CompareTestReciprocal(o1, o2); if (cmpDecFrac != cmpCobj) { Assert.AreEqual(cmpDecFrac, cmpCobj, ObjectMessages(o1, o2, "Compare: Results don't match")); } TestCommon.AssertRoundTrip(o1); TestCommon.AssertRoundTrip(o2); }
private static void AddSubCompare(CBORObject o1, CBORObject o2) { ExtendedDecimal cmpDecFrac = o1.AsExtendedDecimal().Add(o2.AsExtendedDecimal()); ExtendedDecimal cmpCobj = CBORObject.Addition(o1, o2).AsExtendedDecimal(); if (cmpDecFrac.CompareTo(cmpCobj) != 0) { Assert.AreEqual(0, cmpDecFrac.CompareTo(cmpCobj), ObjectMessages(o1, o2, "Add: Results don't match:\n" + cmpDecFrac + " vs\n" + cmpCobj)); } cmpDecFrac = o1.AsExtendedDecimal().Subtract(o2.AsExtendedDecimal()); cmpCobj = CBORObject.Subtract(o1, o2).AsExtendedDecimal(); if (cmpDecFrac.CompareTo(cmpCobj) != 0) { Assert.AreEqual(0, cmpDecFrac.CompareTo(cmpCobj), ObjectMessages(o1, o2, "Subtract: Results don't match:\n" + cmpDecFrac + " vs\n" + cmpCobj)); } CompareDecimals(o1, o2); }
public static CBORObject Multiply(CBORObject a, CBORObject b) { if (a == null) throw new ArgumentNullException("a"); if (b == null) throw new ArgumentNullException("b"); int combo = (a.ItemType << 4) | b.ItemType; BigInteger bvalueA; BigInteger bvalueB; switch (combo) { case (CBORObject.CBORObjectType_Integer << 4) | CBORObject.CBORObjectType_Integer: { long valueA = (long)a.ThisItem; long valueB = (long)b.ThisItem; bool apos = (valueA > 0L); bool bpos = (valueB > 0L); if ( (apos && ((!bpos && (Int64.MinValue / valueA) > valueB) || (bpos && valueA > (Int64.MaxValue / valueB)))) || (!apos && ((!bpos && valueA != 0L && (Int64.MaxValue / valueA) > valueB) || (bpos && valueA < (Int64.MinValue / valueB))))) { // would overflow, convert to BigInteger return CBORObject.FromObject(((BigInteger)valueA) * (BigInteger)valueB); } return CBORObject.FromObject(valueA * valueB); } case (CBORObject.CBORObjectType_Integer << 4) | CBORObject.CBORObjectType_BigInteger: { bvalueA = (BigInteger)(long)a.ThisItem; bvalueB = (BigInteger)b.ThisItem; return CBORObject.FromObject(bvalueA * (BigInteger)bvalueB); } case (CBORObject.CBORObjectType_BigInteger << 4) | CBORObject.CBORObjectType_Integer: { bvalueA = (BigInteger)a.ThisItem; bvalueB = (BigInteger)(long)b.ThisItem; return CBORObject.FromObject(bvalueA * (BigInteger)bvalueB); } case (CBORObject.CBORObjectType_BigInteger << 4) | CBORObject.CBORObjectType_BigInteger: { bvalueA = (BigInteger)a.ThisItem; bvalueB = (BigInteger)b.ThisItem; return CBORObject.FromObject(bvalueA * (BigInteger)bvalueB); } default: return CBORObject.FromObject(a.AsExtendedDecimal().Multiply(b.AsExtendedDecimal())); } }
public static void TestNumber(CBORObject o) { if(o.Type!= CBORType.Number){ return; } if(o.IsPositiveInfinity() || o.IsNegativeInfinity() || o.IsNaN()){ try { o.AsByte(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsInt16(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsInt32(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsInt64(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsSingle(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsDouble(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsBigInteger(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } return; } BigInteger df=o.AsExtendedDecimal().ToBigInteger(); try { o.AsBigInteger(); } catch(Exception ex){ Assert.Fail("Object: "+o+", int: "+df+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsSingle(); } catch(Exception ex){ Assert.Fail("Object: "+o+", int: "+df+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsDouble(); } catch(Exception ex){ Assert.Fail("Object: "+o+", int: "+df+", "+ex.ToString()); throw new InvalidOperationException("",ex); } }