summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/accumulator_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/pipeline/accumulator_test.cpp')
-rw-r--r--src/mongo/db/pipeline/accumulator_test.cpp89
1 files changed, 53 insertions, 36 deletions
diff --git a/src/mongo/db/pipeline/accumulator_test.cpp b/src/mongo/db/pipeline/accumulator_test.cpp
index 928eb2e868e..e354d71d8f9 100644
--- a/src/mongo/db/pipeline/accumulator_test.cpp
+++ b/src/mongo/db/pipeline/accumulator_test.cpp
@@ -96,38 +96,55 @@ static void assertExpectedResults(
TEST(Accumulators, Avg) {
assertExpectedResults(
"$avg",
- {// No documents evaluated.
- {{}, Value(BSONNULL)},
-
- // One int value is converted to double.
- {{Value(3)}, Value(3.0)},
- // One long value is converted to double.
- {{Value(-4LL)}, Value(-4.0)},
- // One double value.
- {{Value(22.6)}, Value(22.6)},
-
- // Averaging two ints.
- {{Value(10), Value(11)}, Value(10.5)},
- // Averaging two longs.
- {{Value(10LL), Value(11LL)}, Value(10.5)},
- // Averaging two doubles.
- {{Value(10.0), Value(11.0)}, Value(10.5)},
-
- // The average of an int and a double is a double.
- {{Value(10), Value(11.0)}, Value(10.5)},
- // The average of a long and a double is a double.
- {{Value(5LL), Value(1.0)}, Value(3.0)},
- // The average of an int and a long is a double.
- {{Value(5), Value(3LL)}, Value(4.0)},
- // Averaging an int, long, and double.
- {{Value(1), Value(2LL), Value(6.0)}, Value(3.0)},
-
- // Unlike $sum, two ints do not overflow in the 'total' portion of the average.
- {{Value(numeric_limits<int>::max()), Value(numeric_limits<int>::max())},
- Value(static_cast<double>(numeric_limits<int>::max()))},
- // Two longs do overflow in the 'total' portion of the average.
- {{Value(numeric_limits<long long>::max()), Value(numeric_limits<long long>::max())},
- Value(static_cast<double>(numeric_limits<long long>::max()))}});
+ {
+ // No documents evaluated.
+ {{}, Value(BSONNULL)},
+
+ // One int value is converted to double.
+ {{Value(3)}, Value(3.0)},
+ // One long value is converted to double.
+ {{Value(-4LL)}, Value(-4.0)},
+ // One double value.
+ {{Value(22.6)}, Value(22.6)},
+
+ // Averaging two ints.
+ {{Value(10), Value(11)}, Value(10.5)},
+ // Averaging two longs.
+ {{Value(10LL), Value(11LL)}, Value(10.5)},
+ // Averaging two doubles.
+ {{Value(10.0), Value(11.0)}, Value(10.5)},
+
+ // The average of an int and a double is a double.
+ {{Value(10), Value(11.0)}, Value(10.5)},
+ // The average of a long and a double is a double.
+ {{Value(5LL), Value(1.0)}, Value(3.0)},
+ // The average of an int and a long is a double.
+ {{Value(5), Value(3LL)}, Value(4.0)},
+ // Averaging an int, long, and double.
+ {{Value(1), Value(2LL), Value(6.0)}, Value(3.0)},
+
+ // Unlike $sum, two ints do not overflow in the 'total' portion of the average.
+ {{Value(numeric_limits<int>::max()), Value(numeric_limits<int>::max())},
+ Value(static_cast<double>(numeric_limits<int>::max()))},
+ // Two longs do overflow in the 'total' portion of the average.
+ {{Value(numeric_limits<long long>::max()), Value(numeric_limits<long long>::max())},
+ Value(static_cast<double>(numeric_limits<long long>::max()))},
+
+ // Averaging two decimals.
+ {{Value(Decimal128("-1234567890.1234567889")),
+ Value(Decimal128("-1234567890.1234567891"))},
+ Value(Decimal128("-1234567890.1234567890"))},
+
+ // Averaging two longs and a decimal results in an accurate decimal result.
+ {{Value(1234567890123456788LL),
+ Value(1234567890123456789LL),
+ Value(Decimal128("1234567890123456790.037037036703702"))},
+ Value(Decimal128("1234567890123456789.012345678901234"))},
+
+ // Averaging a double and a decimal
+ {{Value(1.0E22), Value(Decimal128("9999999999999999999999.9999999999"))},
+ Value(Decimal128("9999999999999999999999.99999999995"))},
+ });
}
TEST(Accumulators, First) {
@@ -249,12 +266,12 @@ TEST(Accumulators, Sum) {
// An int and a double do not trigger an int overflow.
{{Value(numeric_limits<int>::max()), Value(1.0)},
Value(static_cast<long long>(numeric_limits<int>::max()) + 1.0)},
- // An int and a long overflow.
+ // An int and a long overflow into a double.
{{Value(1), Value(numeric_limits<long long>::max())},
- Value(numeric_limits<long long>::min())},
- // Two longs overflow.
+ Value(-static_cast<double>(numeric_limits<long long>::min()))},
+ // Two longs overflow into a double.
{{Value(numeric_limits<long long>::max()), Value(numeric_limits<long long>::max())},
- Value(-2LL)},
+ Value(static_cast<double>(numeric_limits<long long>::max()) * 2)},
// A long and a double do not trigger a long overflow.
{{Value(numeric_limits<long long>::max()), Value(1.0)},
Value(numeric_limits<long long>::max() + 1.0)},