@@ -42,6 +42,9 @@ private void putTestEntityInteger(byte vByte, short vShort, int vInt, long vLong
4242 entity .setSimpleShort (vShort );
4343 entity .setSimpleInt (vInt );
4444 entity .setSimpleLong (vLong );
45+ entity .setSimpleShortU (vShort );
46+ entity .setSimpleIntU (vInt );
47+ entity .setSimpleLongU (vLong );
4548 box .put (entity );
4649 }
4750
@@ -479,6 +482,18 @@ public void avg_notSupported() {
479482 assertUnsupported (() -> query .property (simpleString ).avg (), exceptionMessage );
480483 }
481484
485+ @ Test
486+ public void avgLong_notSupported () {
487+ Query <TestEntity > query = box .query ().build ();
488+ String exceptionMessage = "Cannot calculate sum. This function is for integer types only. This operation is not supported for Property " ;
489+ assertUnsupported (() -> query .property (simpleByteArray ).avgLong (), exceptionMessage );
490+ assertUnsupported (() -> query .property (simpleString ).avgLong (), exceptionMessage );
491+
492+ String exceptionMessage2 = "Please use the double based average instead. This operation is not supported for Property " ;
493+ assertUnsupported (() -> query .property (simpleFloat ).avgLong (), exceptionMessage2 );
494+ assertUnsupported (() -> query .property (simpleDouble ).avgLong (), exceptionMessage2 );
495+ }
496+
482497 @ Test
483498 public void min_notSupported () {
484499 Query <TestEntity > query = box .query ().build ();
@@ -568,6 +583,20 @@ public void avg_noData() {
568583 assertEquals (Double .NaN , baseQuery .property (simpleDouble ).avg (), 0.0 );
569584 }
570585
586+ @ Test
587+ public void avgLong_noData () {
588+ Query <TestEntity > baseQuery = box .query ().build ();
589+ // Integer.
590+ assertEquals (0 , baseQuery .property (simpleByte ).avgLong ());
591+ assertEquals (0 , baseQuery .property (simpleShort ).avgLong ());
592+ assertEquals (0 , baseQuery .property (simpleInt ).avgLong ());
593+ assertEquals (0 , baseQuery .property (simpleLong ).avgLong ());
594+ // Integer treated as unsigned.
595+ assertEquals (0 , baseQuery .property (simpleShortU ).avgLong ());
596+ assertEquals (0 , baseQuery .property (simpleIntU ).avgLong ());
597+ assertEquals (0 , baseQuery .property (simpleLongU ).avgLong ());
598+ }
599+
571600 @ Test
572601 public void min_noData () {
573602 Query <TestEntity > baseQuery = box .query ().build ();
@@ -668,6 +697,39 @@ public void avg_NaN() {
668697 assertEquals (Double .NaN , baseQuery .property (simpleDouble ).avg (), 0.001 );
669698 }
670699
700+ @ Test
701+ public void avgLong_positiveOverflow () {
702+ putTestEntityInteger ((byte ) 0 , (short ) 0 , 0 , Long .MAX_VALUE );
703+ putTestEntityInteger ((byte ) 0 , (short ) 0 , 0 , 1 );
704+
705+ Query <TestEntity > baseQuery = box .query ().build ();
706+ assertEquals (Long .MAX_VALUE / 2 + 1 , baseQuery .property (simpleLong ).avgLong ());
707+ // Should not change if treated as unsigned.
708+ assertEquals (Long .MAX_VALUE / 2 + 1 , baseQuery .property (simpleLongU ).avgLong ());
709+ }
710+
711+ @ Test
712+ public void avgLong_negativeOverflow () {
713+ putTestEntityInteger ((byte ) 0 , (short ) 0 , 0 , Long .MIN_VALUE );
714+ putTestEntityInteger ((byte ) 0 , (short ) 0 , 0 , -1 );
715+
716+ Query <TestEntity > baseQuery = box .query ().build ();
717+ assertEquals (Long .MIN_VALUE / 2 , baseQuery .property (simpleLong ).avgLong ());
718+ // Should not change if treated as unsigned.
719+ assertEquals (Long .MIN_VALUE / 2 , baseQuery .property (simpleLongU ).avgLong ());
720+ }
721+
722+ @ Test
723+ public void avgLong_unsignedOverflow () {
724+ putTestEntityInteger ((byte ) 0 , (short ) 0 , 0 , -1 );
725+ putTestEntityInteger ((byte ) 0 , (short ) 0 , 0 , 1 );
726+
727+ Query <TestEntity > baseQuery = box .query ().build ();
728+ assertEquals (Long .MIN_VALUE , baseQuery .property (simpleLongU ).avgLong ());
729+ // Should be different if treated as signed.
730+ assertEquals (0 , baseQuery .property (simpleLong ).avgLong ());
731+ }
732+
671733 @ Test
672734 public void sum_byteShortIntOverflow () {
673735 putTestEntityInteger (Byte .MAX_VALUE , Short .MAX_VALUE , Integer .MAX_VALUE , 0 );
@@ -777,6 +839,15 @@ public void testAggregates() {
777839 assertEquals (2100.5 , shortUQuery .avg (), 0.0001 );
778840 assertEquals (2000.5 , intUQuery .avg (), 0.0001 );
779841 assertEquals (3000.5 , longUQuery .avg (), 0.0001 );
842+ // avgLong
843+ assertEquals (1 , booleanQuery .avgLong ());
844+ assertEquals (-38 , byteQuery .avgLong ());
845+ assertEquals (2101 , shortQuery .avgLong ());
846+ assertEquals (2001 , intQuery .avgLong ());
847+ assertEquals (3001 , longQuery .avgLong ());
848+ assertEquals (2101 , shortUQuery .avgLong ());
849+ assertEquals (2001 , intUQuery .avgLong ());
850+ assertEquals (3001 , longUQuery .avgLong ());
780851 // min
781852 assertEquals (-38 , byteQuery .min ());
782853 assertEquals (2100 , shortQuery .min ());
0 commit comments