Skip to content

Commit 6fb2581

Browse files
committed
Extra numeric and reader functionality and tests
1 parent d785acb commit 6fb2581

File tree

8 files changed

+63
-4
lines changed

8 files changed

+63
-4
lines changed

convex-core/src/main/java/convex/core/data/AString.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public int getBytes(byte[] dest, int destOffset) {
125125
return toBlob().getBytes(dest, destOffset);
126126
}
127127

128+
/**
129+
* Returns the Java String representing the UTF-8 content of this AString
130+
*
131+
* NOTE: this is not round-trippable through the CVX Reader, intended for output purposes only.
132+
*/
128133
@Override
129134
public String toString() {
130135
int n=Utils.checkedInt(count());

convex-core/src/main/java/convex/core/data/prim/ANumeric.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ public abstract class ANumeric extends APrimitive implements Comparable<ANumeric
3535
* @return Signum of the numeric value
3636
*/
3737
public abstract APrimitive signum();
38+
39+
/**
40+
* Returns true if this numeric value is negative. Note zero and NaN are not negative
41+
* @return true if negative
42+
*/
43+
public abstract boolean isNegative();
44+
45+
/**
46+
* Returns true if this numeric value is positive. Note zero and NaN are not positive
47+
* @return true if negative
48+
*/
49+
public abstract boolean isPositive();
3850

3951

4052
/**

convex-core/src/main/java/convex/core/data/prim/CVMBigInteger.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ public CVMLong signum() {
146146
if (data!=null) return CVMLong.forSignum(data.signum());
147147
return CVMLong.forSignum(blob.byteAt(0));
148148
}
149+
150+
@Override
151+
public boolean isNegative() {
152+
return signum().equals(CVMLong.MINUS_ONE);
153+
}
154+
155+
@Override
156+
public boolean isPositive() {
157+
return signum().equals(CVMLong.ONE);
158+
}
149159

150160
@Override
151161
public int estimatedEncodingSize() {
@@ -432,4 +442,6 @@ public boolean isLong() {
432442

433443

434444

445+
446+
435447
}

convex-core/src/main/java/convex/core/data/prim/CVMDouble.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,14 @@ public boolean isZero() {
269269
return value==0.0;
270270
}
271271

272+
@Override
273+
public boolean isNegative() {
274+
return value<0;
275+
}
272276

273-
277+
@Override
278+
public boolean isPositive() {
279+
return value>0;
280+
}
274281

275282
}

convex-core/src/main/java/convex/core/data/prim/CVMLong.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ public boolean isLong() {
453453
return true;
454454
}
455455

456+
@Override
457+
public boolean isNegative() {
458+
return value<0;
459+
}
460+
461+
@Override
462+
public boolean isPositive() {
463+
return value>0;
464+
}
465+
456466

457467

458468
}

convex-core/src/main/java/convex/core/json/JSONReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void exitNumber(NumberContext ctx) {
121121

122122
@Override
123123
public void exitString(StringContext ctx) {
124-
String text=ctx.getText();
124+
String text=ctx.getStart().getText();
125125
String content=text.substring(1, text.length()-1);
126126
push(JSONUtils.unescape(content));
127127
}

convex-core/src/test/java/convex/core/lang/NumericsTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,24 @@ public static void doNumberTests(ANumeric a) {
459459
}
460460

461461
public static void doIntegerTests(AInteger a) {
462-
assertEquals(a.abs(),RT.multiply(a.signum(),a));
462+
CVMLong signum=a.signum();
463+
assertEquals(a.abs(),RT.multiply(signum,a));
464+
if (signum.equals(CVMLong.ONE)) {
465+
assertTrue(a.isPositive());
466+
} else if (signum.equals(CVMLong.MINUS_ONE)) {
467+
assertTrue(a.isNegative());
468+
} else {
469+
assertTrue(a.isZero());
470+
}
463471

464472
doGenericNumberTests(a);
465473
}
466474

467475
public static void doDoubleTests(CVMDouble a) {
468-
assertEquals(a.abs(),RT.multiply(a.signum(),a));
476+
CVMDouble signum = a.signum();
477+
assertEquals(a.abs(),RT.multiply(signum,a));
478+
479+
assertEquals(Math.signum(a.doubleValue()),signum.doubleValue());
469480

470481
doGenericNumberTests(a);
471482
}

convex-core/src/test/java/convex/core/utils/JSONUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public void testJSONObjects() {
155155
assertEquals(Strings.COLON,JSONReader.read("\":\""));
156156
assertEquals(Vectors.of(1,2),JSONReader.read("[1,2]"));
157157

158+
JSONReader.read("{\"a\":[{\"b\":\"c\"},2]}");
159+
158160
assertThrows(ParseException.class,()->JSONReader.readObject("[]")); // not an object
159161

160162
}

0 commit comments

Comments
 (0)