Skip to content

Commit f1e6643

Browse files
committed
update Java version
1 parent b95a37f commit f1e6643

File tree

7 files changed

+309
-180
lines changed

7 files changed

+309
-180
lines changed

src/test/java/com/upokecenter/test/BigIntegerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ public void TestCanFitInInt() {
4545
}
4646
@Test
4747
public void TestCompareTo() {
48-
// not implemented yet
48+
FastRandom r = new FastRandom();
49+
for (int i = 0; i < 500; ++i) {
50+
BigInteger bigintA = RandomObjects.RandomBigInteger(r);
51+
BigInteger bigintB = RandomObjects.RandomBigInteger(r);
52+
BigInteger bigintC = RandomObjects.RandomBigInteger(r);
53+
TestCommon.CompareTestRelations(bigintA, bigintB, bigintC);
54+
TestCommon.CompareTestConsistency(bigintA, bigintB, bigintC);
55+
}
4956
}
5057
@Test
5158
public void TestDivide() {

src/test/java/com/upokecenter/test/CBORObjectTest.java

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,30 +1049,137 @@ public void TestCanTruncatedIntFitInInt64() {
10491049
}
10501050
}
10511051
}
1052+
1053+
static void CompareDecimals(CBORObject o1, CBORObject o2) {
1054+
int cmpDecFrac = o1.AsExtendedDecimal().compareTo(o2.AsExtendedDecimal());
1055+
int cmpCobj = TestCommon.CompareTestReciprocal(o1, o2);
1056+
if (cmpDecFrac != cmpCobj) {
1057+
Assert.assertEquals(TestCommon.ObjectMessages(o1, o2, "Compare: Results don't match"),cmpDecFrac,cmpCobj);
1058+
}
1059+
TestCommon.AssertRoundTrip(o1);
1060+
TestCommon.AssertRoundTrip(o2);
1061+
}
1062+
10521063
@Test
10531064
public void TestCompareTo() {
1065+
FastRandom r = new FastRandom();
1066+
int CompareCount = 500;
1067+
for (int i = 0; i < CompareCount; ++i) {
1068+
CBORObject o1 = RandomObjects.RandomCBORObject(r);
1069+
CBORObject o2 = RandomObjects.RandomCBORObject(r);
1070+
CBORObject o3 = RandomObjects.RandomCBORObject(r);
1071+
TestCommon.CompareTestRelations(o1, o2, o3);
1072+
}
1073+
for (int i = 0; i < 5000; ++i) {
1074+
CBORObject o1 = RandomObjects.RandomNumber(r);
1075+
CBORObject o2 = RandomObjects.RandomNumber(r);
1076+
CompareDecimals(o1, o2);
1077+
}
1078+
for (int i = 0; i < 50; ++i) {
1079+
CBORObject o1 = CBORObject.FromObject(Float.NEGATIVE_INFINITY);
1080+
CBORObject o2 = RandomObjects.RandomNumberOrRational(r);
1081+
if (o2.IsInfinity() || o2.IsNaN()) {
1082+
continue;
1083+
}
1084+
TestCommon.CompareTestLess(o1, o2);
1085+
o1 = CBORObject.FromObject(Double.NEGATIVE_INFINITY);
1086+
TestCommon.CompareTestLess(o1, o2);
1087+
o1 = CBORObject.FromObject(Float.POSITIVE_INFINITY);
1088+
TestCommon.CompareTestLess(o2, o1);
1089+
o1 = CBORObject.FromObject(Double.POSITIVE_INFINITY);
1090+
TestCommon.CompareTestLess(o2, o1);
1091+
o1 = CBORObject.FromObject(Float.NaN);
1092+
TestCommon.CompareTestLess(o2, o1);
1093+
o1 = CBORObject.FromObject(Double.NaN);
1094+
TestCommon.CompareTestLess(o2, o1);
1095+
}
1096+
byte[] bytes1 = { 0, 1 };
1097+
byte[] bytes2 = { 0, 2 };
1098+
byte[] bytes3 = { 0, 2, 0 };
1099+
byte[] bytes4 = { 1, 1 };
1100+
byte[] bytes5 = { 1, 1, 4 };
1101+
byte[] bytes6 = { 1, 2 };
1102+
byte[] bytes7 = { 1, 2, 6 };
1103+
CBORObject[] sortedObjects = {
1104+
CBORObject.Undefined, CBORObject.Null,
1105+
CBORObject.False, CBORObject.True,
1106+
CBORObject.FromObject(Double.NEGATIVE_INFINITY),
1107+
CBORObject.FromObject(ExtendedDecimal.FromString("-1E+5000")),
1108+
CBORObject.FromObject(Long.MIN_VALUE),
1109+
CBORObject.FromObject(Integer.MIN_VALUE),
1110+
CBORObject.FromObject(-2), CBORObject.FromObject(-1),
1111+
CBORObject.FromObject(0), CBORObject.FromObject(1),
1112+
CBORObject.FromObject(2), CBORObject.FromObject(Long.MAX_VALUE),
1113+
CBORObject.FromObject(ExtendedDecimal.FromString("1E+5000")),
1114+
CBORObject.FromObject(Double.POSITIVE_INFINITY),
1115+
CBORObject.FromObject(Double.NaN), CBORObject.FromSimpleValue(0),
1116+
CBORObject.FromSimpleValue(19), CBORObject.FromSimpleValue(32),
1117+
CBORObject.FromSimpleValue(255), CBORObject.FromObject(bytes1),
1118+
CBORObject.FromObject(bytes2), CBORObject.FromObject(bytes3),
1119+
CBORObject.FromObject(bytes4), CBORObject.FromObject(bytes5),
1120+
CBORObject.FromObject(bytes6), CBORObject.FromObject(bytes7),
1121+
CBORObject.FromObject("aa"), CBORObject.FromObject("ab"),
1122+
CBORObject.FromObject("abc"), CBORObject.FromObject("ba"),
1123+
CBORObject.FromObject(CBORObject.NewArray()),
1124+
CBORObject.FromObject(CBORObject.NewMap()),
1125+
};
1126+
for (int i = 0; i < sortedObjects.length; ++i) {
1127+
for (int j = i; j < sortedObjects.length; ++j) {
1128+
if (i == j) {
1129+
TestCommon.CompareTestEqual(sortedObjects[i], sortedObjects[j]);
1130+
} else {
1131+
TestCommon.CompareTestLess(sortedObjects[i], sortedObjects[j]);
1132+
}
1133+
}
1134+
Assert.assertEquals(1, sortedObjects[i].compareTo(null));
1135+
}
1136+
CBORObject sp = CBORObject.FromObject(Float.POSITIVE_INFINITY);
1137+
CBORObject sn = CBORObject.FromObject(Float.NEGATIVE_INFINITY);
1138+
CBORObject snan = CBORObject.FromObject(Float.NaN);
1139+
CBORObject dp = CBORObject.FromObject(Double.POSITIVE_INFINITY);
1140+
CBORObject dn = CBORObject.FromObject(Double.NEGATIVE_INFINITY);
1141+
CBORObject dnan = CBORObject.FromObject(Double.NaN);
1142+
TestCommon.CompareTestEqual(sp, sp);
1143+
TestCommon.CompareTestEqual(sp, dp);
1144+
TestCommon.CompareTestEqual(dp, dp);
1145+
TestCommon.CompareTestEqual(sn, sn);
1146+
TestCommon.CompareTestEqual(sn, dn);
1147+
TestCommon.CompareTestEqual(dn, dn);
1148+
TestCommon.CompareTestEqual(snan, snan);
1149+
TestCommon.CompareTestEqual(snan, dnan);
1150+
TestCommon.CompareTestEqual(dnan, dnan);
1151+
TestCommon.CompareTestLess(sn, sp);
1152+
TestCommon.CompareTestLess(sn, dp);
1153+
TestCommon.CompareTestLess(sn, snan);
1154+
TestCommon.CompareTestLess(sn, dnan);
1155+
TestCommon.CompareTestLess(sp, snan);
1156+
TestCommon.CompareTestLess(sp, dnan);
1157+
TestCommon.CompareTestLess(dn, dp);
1158+
TestCommon.CompareTestLess(dp, dnan);
10541159
Assert.assertEquals(1, CBORObject.True.compareTo(null));
10551160
Assert.assertEquals(1, CBORObject.False.compareTo(null));
10561161
Assert.assertEquals(1, CBORObject.Null.compareTo(null));
10571162
Assert.assertEquals(1, CBORObject.NewArray().compareTo(null));
10581163
Assert.assertEquals(1, CBORObject.NewMap().compareTo(null));
10591164
Assert.assertEquals(1, CBORObject.FromObject(100).compareTo(null));
10601165
Assert.assertEquals(1, CBORObject.FromObject(Double.NaN).compareTo(null));
1061-
CBORTest.CompareTestLess(CBORObject.Undefined, CBORObject.Null);
1062-
CBORTest.CompareTestLess(CBORObject.Null, CBORObject.False);
1063-
CBORTest.CompareTestLess(CBORObject.False, CBORObject.True);
1064-
CBORTest.CompareTestLess(CBORObject.False, CBORObject.FromObject(0));
1065-
CBORTest.CompareTestLess(CBORObject.False, CBORObject.FromSimpleValue(0));
1066-
CBORTest.CompareTestLess(
1166+
TestCommon.CompareTestLess(CBORObject.Undefined, CBORObject.Null);
1167+
TestCommon.CompareTestLess(CBORObject.Null, CBORObject.False);
1168+
TestCommon.CompareTestLess(CBORObject.False, CBORObject.True);
1169+
TestCommon.CompareTestLess(CBORObject.False, CBORObject.FromObject(0));
1170+
TestCommon.CompareTestLess(
1171+
CBORObject.False,
1172+
CBORObject.FromSimpleValue(0));
1173+
TestCommon.CompareTestLess(
10671174
CBORObject.FromSimpleValue(0),
10681175
CBORObject.FromSimpleValue(1));
1069-
CBORTest.CompareTestLess(
1176+
TestCommon.CompareTestLess(
10701177
CBORObject.FromObject(0),
10711178
CBORObject.FromObject(1));
1072-
CBORTest.CompareTestLess(
1179+
TestCommon.CompareTestLess(
10731180
CBORObject.FromObject(0.0f),
10741181
CBORObject.FromObject(1.0f));
1075-
CBORTest.CompareTestLess(
1182+
TestCommon.CompareTestLess(
10761183
CBORObject.FromObject(0.0),
10771184
CBORObject.FromObject(1.0));
10781185
}

0 commit comments

Comments
 (0)