Skip to content

Commit c605460

Browse files
PropertyQuery: test unsigned properties.
1 parent cdfc6e6 commit c605460

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/query/PropertyQueryTest.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ private void putTestEntityInteger(byte vByte, short vShort, int vInt, long vLong
4545
box.put(entity);
4646
}
4747

48+
private void putTestEntityUnsignedInteger(short vShort, int vInt, long vLong) {
49+
TestEntity entity = new TestEntity();
50+
entity.setSimpleShortU(vShort);
51+
entity.setSimpleIntU(vInt);
52+
entity.setSimpleLongU(vLong);
53+
box.put(entity);
54+
}
55+
4856
private void putTestEntityFloat(float vFloat, double vDouble) {
4957
TestEntity entity = new TestEntity();
5058
entity.setSimpleFloat(vFloat);
@@ -496,6 +504,9 @@ public void minDouble_notSupported() {
496504
assertUnsupported(() -> query.property(simpleShort).minDouble(), exceptionMessage);
497505
assertUnsupported(() -> query.property(simpleInt).minDouble(), exceptionMessage);
498506
assertUnsupported(() -> query.property(simpleLong).minDouble(), exceptionMessage);
507+
assertUnsupported(() -> query.property(simpleShortU).minDouble(), exceptionMessage);
508+
assertUnsupported(() -> query.property(simpleIntU).minDouble(), exceptionMessage);
509+
assertUnsupported(() -> query.property(simpleLongU).minDouble(), exceptionMessage);
499510
}
500511

501512
@Test
@@ -523,6 +534,9 @@ public void maxDouble_notSupported() {
523534
assertUnsupported(() -> query.property(simpleShort).maxDouble(), exceptionMessage);
524535
assertUnsupported(() -> query.property(simpleInt).maxDouble(), exceptionMessage);
525536
assertUnsupported(() -> query.property(simpleLong).maxDouble(), exceptionMessage);
537+
assertUnsupported(() -> query.property(simpleShortU).maxDouble(), exceptionMessage);
538+
assertUnsupported(() -> query.property(simpleIntU).maxDouble(), exceptionMessage);
539+
assertUnsupported(() -> query.property(simpleLongU).maxDouble(), exceptionMessage);
526540
}
527541

528542
@Test
@@ -545,6 +559,10 @@ public void avg_noData() {
545559
assertEquals(Double.NaN, baseQuery.property(simpleShort).avg(), 0.0);
546560
assertEquals(Double.NaN, baseQuery.property(simpleInt).avg(), 0.0);
547561
assertEquals(Double.NaN, baseQuery.property(simpleLong).avg(), 0.0);
562+
// Integer treated as unsigned.
563+
assertEquals(Double.NaN, baseQuery.property(simpleShortU).avg(), 0.0);
564+
assertEquals(Double.NaN, baseQuery.property(simpleIntU).avg(), 0.0);
565+
assertEquals(Double.NaN, baseQuery.property(simpleLongU).avg(), 0.0);
548566
// Float.
549567
assertEquals(Double.NaN, baseQuery.property(simpleFloat).avg(), 0.0);
550568
assertEquals(Double.NaN, baseQuery.property(simpleDouble).avg(), 0.0);
@@ -557,6 +575,10 @@ public void min_noData() {
557575
assertEquals(0, baseQuery.property(simpleShort).min());
558576
assertEquals(0, baseQuery.property(simpleInt).min());
559577
assertEquals(0, baseQuery.property(simpleLong).min());
578+
// Integer treated as unsigned.
579+
assertEquals(0, baseQuery.property(simpleShortU).min());
580+
assertEquals(0, baseQuery.property(simpleIntU).min());
581+
assertEquals(0, baseQuery.property(simpleLongU).min());
560582
}
561583

562584
@Test
@@ -573,6 +595,10 @@ public void max_noData() {
573595
assertEquals(0, baseQuery.property(simpleShort).max());
574596
assertEquals(0, baseQuery.property(simpleInt).max());
575597
assertEquals(0, baseQuery.property(simpleLong).max());
598+
// Integer treated as unsigned.
599+
assertEquals(0, baseQuery.property(simpleShortU).max());
600+
assertEquals(0, baseQuery.property(simpleIntU).max());
601+
assertEquals(0, baseQuery.property(simpleLongU).max());
576602
}
577603

578604
@Test
@@ -589,6 +615,10 @@ public void sum_noData() {
589615
assertEquals(0, baseQuery.property(simpleShort).sum());
590616
assertEquals(0, baseQuery.property(simpleInt).sum());
591617
assertEquals(0, baseQuery.property(simpleLong).sum());
618+
// Integer treated as unsigned.
619+
assertEquals(0, baseQuery.property(simpleShortU).sum());
620+
assertEquals(0, baseQuery.property(simpleIntU).sum());
621+
assertEquals(0, baseQuery.property(simpleLongU).sum());
592622
}
593623

594624
@Test
@@ -639,6 +669,16 @@ public void sum_byteShortIntOverflow() {
639669
assertEquals(Integer.MAX_VALUE + 1L, baseQuery.property(simpleInt).sum());
640670
}
641671

672+
@Test
673+
public void sum_unsignedShortIntOverflow() {
674+
putTestEntityUnsignedInteger((short) -1, -1, 0);
675+
putTestEntityUnsignedInteger((short) 1, 1, 0);
676+
677+
Query<TestEntity> baseQuery = box.query().build();
678+
assertEquals(0x1_0000, baseQuery.property(simpleShortU).sum());
679+
assertEquals(0x1_0000_0000L, baseQuery.property(simpleIntU).sum());
680+
}
681+
642682
@Test
643683
public void sum_longOverflow_exception() {
644684
exceptionRule.expect(NumericOverflowException.class);
@@ -650,6 +690,28 @@ public void sum_longOverflow_exception() {
650690
box.query().build().property(simpleLong).sum();
651691
}
652692

693+
@Test
694+
public void sum_longUnderflow_exception() {
695+
exceptionRule.expect(NumericOverflowException.class);
696+
exceptionRule.expectMessage("Numeric overflow");
697+
698+
putTestEntityInteger((byte) 0, (short) 0, 0, Long.MIN_VALUE);
699+
putTestEntityInteger((byte) 0, (short) 0, 0, -1);
700+
701+
box.query().build().property(simpleLong).sum();
702+
}
703+
704+
@Test
705+
public void sum_unsignedLongOverflow_exception() {
706+
exceptionRule.expect(NumericOverflowException.class);
707+
exceptionRule.expectMessage("Numeric overflow");
708+
709+
putTestEntityUnsignedInteger((short) 0, 0, -1);
710+
putTestEntityUnsignedInteger((short) 0, 0, 1);
711+
712+
box.query().build().property(simpleLongU).sum();
713+
}
714+
653715
@Test
654716
public void sumDouble_positiveOverflow_exception() {
655717
putTestEntityFloat(Float.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
@@ -691,6 +753,9 @@ public void testAggregates() {
691753
PropertyQuery longQuery = query.property(simpleLong);
692754
PropertyQuery floatQuery = query.property(simpleFloat);
693755
PropertyQuery doubleQuery = query.property(simpleDouble);
756+
PropertyQuery shortUQuery = query.property(simpleShortU);
757+
PropertyQuery intUQuery = query.property(simpleIntU);
758+
PropertyQuery longUQuery = query.property(simpleLongU);
694759
// avg
695760
assertEquals(0.5, booleanQuery.avg(), 0.0001);
696761
assertEquals(-37.5, byteQuery.avg(), 0.0001);
@@ -699,20 +764,29 @@ public void testAggregates() {
699764
assertEquals(3000.5, longQuery.avg(), 0.0001);
700765
assertEquals(400.05, floatQuery.avg(), 0.0001);
701766
assertEquals(2020.005, doubleQuery.avg(), 0.0001);
767+
assertEquals(2100.5, shortUQuery.avg(), 0.0001);
768+
assertEquals(2000.5, intUQuery.avg(), 0.0001);
769+
assertEquals(3000.5, longUQuery.avg(), 0.0001);
702770
// min
703771
assertEquals(-38, byteQuery.min());
704772
assertEquals(2100, shortQuery.min());
705773
assertEquals(2000, intQuery.min());
706774
assertEquals(3000, longQuery.min());
707775
assertEquals(400, floatQuery.minDouble(), 0.001);
708776
assertEquals(2020, doubleQuery.minDouble(), 0.001);
777+
assertEquals(2100, shortUQuery.min());
778+
assertEquals(2000, intUQuery.min());
779+
assertEquals(3000, longUQuery.min());
709780
// max
710781
assertEquals(-37, byteQuery.max());
711782
assertEquals(2101, shortQuery.max());
712783
assertEquals(2001, intQuery.max());
713784
assertEquals(3001, longQuery.max());
714785
assertEquals(400.1, floatQuery.maxDouble(), 0.001);
715786
assertEquals(2020.01, doubleQuery.maxDouble(), 0.001);
787+
assertEquals(2101, shortUQuery.max());
788+
assertEquals(2001, intUQuery.max());
789+
assertEquals(3001, longUQuery.max());
716790
// sum
717791
assertEquals(1, booleanQuery.sum());
718792
assertEquals(1, booleanQuery.sumDouble(), 0.001);
@@ -726,6 +800,12 @@ public void testAggregates() {
726800
assertEquals(6001, longQuery.sumDouble(), 0.001);
727801
assertEquals(800.1, floatQuery.sumDouble(), 0.001);
728802
assertEquals(4040.01, doubleQuery.sumDouble(), 0.001);
803+
assertEquals(4201, shortUQuery.sum());
804+
assertEquals(4201, shortUQuery.sumDouble(), 0.001);
805+
assertEquals(4001, intUQuery.sum());
806+
assertEquals(4001, intUQuery.sumDouble(), 0.001);
807+
assertEquals(6001, longUQuery.sum());
808+
assertEquals(6001, longUQuery.sumDouble(), 0.001);
729809
}
730810

731811
@Test

0 commit comments

Comments
 (0)