8000 [BTS-705] Add more decay tests (#15490) · strogo/arangodb@0bc7e73 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bc7e73

Browse files
authored
[BTS-705] Add more decay tests (arangodb#15490)
1 parent 825f8fc commit 0bc7e73

File tree

2 files changed

+135
-57
lines changed

2 files changed

+135
-57
lines changed

tests/Aql/DecaysFunctionTest.cpp

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ SmallVector<AqlValue> createArgVec(const VPackSlice slice) {
6767
}
6868

6969
void expectEqSlices(const VPackSlice actualSlice,
70-
const VPackSlice expectedSlice) {
70+
const VPackSlice expectedSlice, char const* args) {
7171
ASSERT_TRUE((actualSlice.isNumber() && expectedSlice.isNumber()) ||
7272
(actualSlice.isArray() && expectedSlice.isArray()));
7373

@@ -76,16 +76,16 @@ void expectEqSlices(const VPackSlice actualSlice,
7676
VPackValueLength expectedSize = expectedSlice.length();
7777
ASSERT_EQ(actualSize, expectedSize);
7878

79-
double lhs, rhs;
8079
for (VPackValueLength i = 0; i < actualSize; ++i) {
81-
lhs = actualSlice.at(i).getNumber<decltype(lhs)>();
82-
rhs = expectedSlice.at(i).getNumber<decltype(rhs)>();
83-
ASSERT_DOUBLE_EQ(lhs, rhs);
80+
double actual = actualSlice.at(i).getNumber<double>();
81+
double expected = expectedSlice.at(i).getNumber<double>();
82+
EXPECT_DOUBLE_EQ(expected, actual)
83+
<< "index: " << i << "; args: " << args;
8484
}
8585
} else {
86-
double lhs = actualSlice.getNumber<decltype(lhs)>();
87-
double rhs = expectedSlice.getNumber<decltype(rhs)>();
88-
ASSERT_DOUBLE_EQ(lhs, rhs);
86+
double actual = actualSlice.getNumber<double>();
87+
double expected = expectedSlice.getNumber<double>();
88+
EXPECT_DOUBLE_EQ(expected, actual) << "args: " << args;
8989
}
9090

9191
return;
@@ -135,7 +135,7 @@ void assertDecayFunction(char const* expected, char const* args,
135135
auto actual_value = evaluateDecayFunction(params, node);
136136

137137
// check equality
138-
expectEqSlices(actual_value.slice(), expectedSlice);
138+
expectEqSlices(actual_value.slice(), expectedSlice, args);
139139

140140
// destroy AqlValues
141141
for (auto& p : params) {
@@ -174,6 +174,26 @@ TEST(GaussDecayFunctionTest, test) {
174174
assertDecayFunction("1.0", "[40, 40, 5, 5, 0.5]", node);
175175
assertDecayFunction("1.0", "[49.987, 49.987, 0.001, 0.001, 0.2]", node);
176176

177+
// expecting <decay>
178+
assertDecayFunction("0.7", "[50, 40, 5, 5, 0.7]", node);
179+
assertDecayFunction("0.7", "[50, 40, 6, 4, 0.7]", node);
180+
assertDecayFunction("0.7", "[50, 40, 4, 6, 0.7]", node);
181+
assertDecayFunction("0.7", "[50, 40, 7, 3, 0.7]", node);
182+
assertDecayFunction("0.7", "[50, 40, 3, 7, 0.7]", node);
183+
assertDecayFunction("0.7", "[46, 40, 5, 1, 0.7]", node);
184+
assertDecayFunction("0.7", "[46, 40, 1, 5, 0.7]", node);
185+
assertDecayFunction("0.7", "[34, 40, 5, 1, 0.7]", node);
186+
assertDecayFunction("0.7", "[34, 40, 1, 5, 0.7]", node);
187+
assertDecayFunction("0.5", "[10, 0, 5, 5, 0.5]", node);
188+
assertDecayFunction("0.5", "[-10, 0, 5, 5, 0.5]", node);
189+
assertDecayFunction("0.4", "[-5, 0, 4, 1, 0.4]", node);
190+
assertDecayFunction("0.4", "[5, 0, 1, 4, 0.4]", node);
191+
assertDecayFunction("0.4", "[-5, 0, 4, 1, 0.4]", node);
192+
assertDecayFunction("0.4", "[5, 0, 1, 4, 0.4]", node);
193+
assertDecayFunction("0.2", "[2.5, 2.0, 0.25, 0.25, 0.2]", node);
194+
assertDecayFunction("0.2", "[2.5, 2.0, 0.4, 0.1, 0.2]", node);
195+
assertDecayFunction("0.20000000000000023", "[2.5, 2.0, 0.1, 0.4, 0.2]", node);
196+
177197
// test range input
178198
assertDecayFunction(
179199
"[0.5, 0.6417129487814521, 0.7791645796604999, 0.8950250709279725, "
@@ -224,6 +244,26 @@ TEST(ExpDecayFunctionTest, test) {
224244
assertDecayFunction("1.0", "[40, 40, 5, 5, 0.5]", node);
225245
assertDecayFunction("1.0", "[49.987, 49.987, 0.001, 0.001, 0.2]", node);
226246

247+
// expecting <decay>
248+
assertDecayFunction("0.7", "[50, 40, 5, 5, 0.7]", node);
249+
assertDecayFunction("0.7", "[50, 40, 6, 4, 0.7]", node);
250+
assertDecayFunction("0.7", "[50, 40, 4, 6, 0.7]", node);
251+
assertDecayFunction("0.7", "[50, 40, 7, 3, 0.7]", node);
252+
assertDecayFunction("0.7", "[50, 40, 3, 7, 0.7]", node);
253+
assertDecayFunction("0.7", "[46, 40, 5, 1, 0.7]", node);
254+
assertDecayFunction("0.7", "[46, 40, 1, 5, 0.7]", node);
255+
assertDecayFunction("0.7", "[34, 40, 5, 1, 0.7]", node);
256+
assertDecayFunction("0.7", "[34, 40, 1, 5, 0.7]", node);
257+
assertDecayFunction("0.5", "[10, 0, 5, 5, 0.5]", node);
258+
assertDecayFunction("0.5", "[-10, 0, 5, 5, 0.5]", node);
259+
assertDecayFunction("0.4", "[-5, 0, 4, 1, 0.4]", node);
260+
assertDecayFunction("0.4", "[5, 0, 1, 4, 0.4]", node);
261+
assertDecayFunction("0.4", "[-5, 0, 4, 1, 0.4]", node);
262+
assertDecayFunction("0.4", "[5, 0, 1, 4, 0.4]", node);
263+
assertDecayFunction("0.2", "[2.5, 2.0, 0.25, 0.25, 0.2]", node);
264+
assertDecayFunction("0.2", "[2.5, 2.0, 0.4, 0.1, 0.2]", node);
265+
assertDecayFunction("0.2", "[2.5, 2.0, 0.1, 0.4, 0.2]", node);
266+
227267
// with offset=0
228268
assertDecayFunction("0.8513399225207846", "[1, 0, 10, 0, 0.2]", node);
229269
assertDecayFunction("0.7247796636776955", "[2, 0, 10, 0, 0.2]", node);
@@ -270,6 +310,26 @@ TEST(LinDecayFunctionTest, test) {
270310
assertDecayFunction("1.0", "[40, 40, 5, 5, 0.5]", node);
271311
assertDecayFunction("1.0", "[49.987, 49.987, 0.001, 0.001, 0.2]", node);
272312

313+
// expecting <decay>
314+
assertDecayFunction("0.7", "[50, 40, 5, 5, 0.7]", node);
315+
assertDecayFunction("0.7", "[50, 40, 6, 4, 0.7]", node);
316+
assertDecayFunction("0.7", "[50, 40, 4, 6, 0.7]", node);
317+
assertDecayFunction("0.7", "[50, 40, 7, 3, 0.7]", node);
318+
assertDecayFunction("0.7", "[50, 40, 3, 7, 0.7]", node);
319+
assertDecayFunction("0.7", "[46, 40, 5, 1, 0.7]", node);
320+
assertDecayFunction("0.7", "[46, 40, 1, 5, 0.7]", node);
321+
assertDecayFunction("0.7", "[34, 40, 5, 1, 0.7]", node);
322+
assertDecayFunction("0.7", "[34, 40, 1, 5, 0.7]", node);
323+
assertDecayFunction("0.5", "[10, 0, 5, 5, 0.5]", node);
324+
assertDecayFunction("0.5", "[-10, 0, 5, 5, 0.5]", node);
325+
assertDecayFunction("0.4", "[-5, 0, 4, 1, 0.4]", node);
326+
assertDecayFunction("0.4", "[5, 0, 1, 4, 0.4]", node);
327+
assertDecayFunction("0.4", "[-5, 0, 4, 1, 0.4]", node);
328+
assertDecayFunction("0.4", "[5, 0, 1, 4, 0.4]", node);
329+
assertDecayFunction("0.2", "[2.5, 2.0, 0.25, 0.25, 0.2]", node);
330+
assertDecayFunction("0.2", "[2.5, 2.0, 0.4, 0.1, 0.2]", node);
331+
assertDecayFunction("0.20000000000000018", "[2.5, 2.0, 0.1, 0.4, 0.2]", node);
332+
273333
// with offset=0
274334
assertDecayFunction("0.92", "[1, 0, 10, 0, 0.2]", node);
275335
assertDecayFunction("0.84", "[2, 0, 10, 0, 0.2]", node);

tests/js/server/aql/aql-functions-numeric.js

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
////////////////////////////////////////////////////////////////////////////////
2929

3030
var internal = require("internal");
31+
var arangodb = require('@arangodb');
3132
var errors = internal.errors;
3233
var jsunity = require("jsunity");
3334
var helper = require("@arangodb/aql-helper");
@@ -5767,27 +5768,19 @@ function ahuacatlNumericFunctionsTestSuite () {
57675768
////////////////////////////////////////////////////////////////////////////////
57685769

57695770
testDecayGauss : function () {
5770-
var data = [
5771+
[
57715772
[1, 0, 10, 0, 0.2, 0.9840344433634576],
57725773
[-10, 40, 5, 0, 0.1, 1.0000000000000458e-100],
57735774
[41, 40, 5, 5, 0.7, 1]
5774-
];
5775+
].forEach(function (value) {
5776+
const [arg, origin, scale, offset, decay, expected] = value;
57755777

5776-
var valgrind = require("internal").valgrind;
5778+
const query = `RETURN DECAY_GAUSS(${arg}, ${origin}, ${scale}, ${offset}, ${decay})`;
5779+
let actual = getQueryResults(query);
5780+
assertAlmostEqual(expected, actual[0], query + " " + JSON.stringify(value));
57775781

5778-
data.forEach(function (value) {
5779-
var arg = JSON.stringify(value[0]);
5780-
var origin = JSON.stringify(value[1]);
5781-
var scale = JSON.stringify(value[2]);
5782-
var offset = JSON.stringify(value[3]);
5783-
var decay = JSON.stringify(value[4]);
5784-
5785-
var query = "RETURN DECAY_GAUSS(" + arg + ", " + origin + ", " + scale + ", " + offset + ", " + decay + ")";
5786-
var actual = getQueryResults(query);
5787-
assertAlmostEqual(value[5], actual[0], query + " " + JSON.stringify(value));
5788-
5789-
actual = getQueryResults("RETURN NOOPT(DECAY_GAUSS(" + arg + ", " + origin + ", " + scale + ", " + offset + ", " + decay +"))");
5790-
assertAlmostEqual(value[5], actual[0], value, query + " " + JSON.stringify(value));
5782+
actual = getQueryResults(`RETURN NOOPT(DECAY_GAUSS(${arg}, ${origin}, ${scale}, ${offset}, ${decay}))`);
5783+
assertAlmostEqual(expected, actual[0], query + " " + JSON.stringify(value));
57915784
});
57925785
},
57935786

@@ -5796,58 +5789,83 @@ function ahuacatlNumericFunctionsTestSuite () {
57965789
////////////////////////////////////////////////////////////////////////////////
57975790

57985791
testDecayExp : function () {
5799-
var data = [
5792+
[
58005793
[1, 0, 10, 0, 0.2, 0.8513399225207846],
58015794
[0, 0, 0.001, 10, 0.2, 1],
58025795
[41, 40, 5, 5, 0.7, 1]
5803-
];
5804-
5805-
var valgrind = require("internal").valgrind;
5806-
5807-
data.forEach(function (value) {
5808-
var arg = JSON.stringify(value[0]);
5809-
var origin = JSON.stringify(value[1]);
5810-
var scale = JSON.stringify(value[2]);
5811-
var offset = JSON.stringify(value[3]);
5812-
var decay = JSON.stringify(value[4]);
5796+
].forEach(function (value) {
5797+
const [arg, origin, scale, offset, decay, expected] = value;
58135798

5814-
var query = "RETURN DECAY_EXP(" + arg + ", " + origin + ", " + scale + ", " + offset + ", " + decay + ")";
5815-
var actual = getQueryResults(query);
5816-
assertAlmostEqual(value[5], actual[0], query + " " + JSON.stringify(value));
5799+
const query = `RETURN DECAY_EXP(${arg}, ${origin}, ${scale}, ${offset}, ${decay})`;
5800+
let actual = getQueryResults(query);
5801+
assertAlmostEqual(expected, actual[0], query + " " + JSON.stringify(value));
58175802

5818-
actual = getQueryResults("RETURN NOOPT(DECAY_EXP(" + arg + ", " + origin + ", " + scale + ", " + offset + ", " + decay +"))");
5819-
assertAlmostEqual(value[5], actual[0], value, query + " " + JSON.stringify(value));
5803+
actual = getQueryResults(`RETURN NOOPT(DECAY_EXP(${arg}, ${origin}, ${scale}, ${offset}, ${decay}))`);
5804+
assertAlmostEqual(expected, actual[0], query + " " + JSON.stringify(value));
58205805
});
58215806
},
5807+
58225808
////////////////////////////////////////////////////////////////////////////////
58235809
/// @brief test DECAY_LINEAR function
58245810
////////////////////////////////////////////////////////////////////////////////
58255811

58265812
testDecayLin : function () {
5827-
var data = [
5813+
[
58285814
[41, 40, 5, 5, 0.5, 1],
58295815
[1, 0, 10, 0, 0.2, 0.92],
58305816
[-10, 40, 5, 0, 0.1, 0]
5831-
];
5832-
5833-
var valgrind = require("internal").valgrind;
5817+
].forEach(function (value) {
5818+
const [arg, origin, scale, offset, decay, expected] = value;
58345819

5835-
data.forEach(function (value) {
5836-
var arg = JSON.stringify(value[0]);
5837-
var origin = JSON.stringify(value[1]);
5838-
var scale = JSON.stringify(value[2]);
5839-
var offset = JSON.stringify(value[3]);
5840-
var decay = JSON.stringify(value[4]);
5820+
const query = `RETURN DECAY_LINEAR(${arg}, ${origin}, ${scale}, ${offset}, ${decay})`;
5821+
let actual = getQueryResults(query);
5822+
assertAlmostEqual(expected, actual[0], query + " " + JSON.stringify(value));
58415823

5842-
var query = "RETURN DECAY_LINEAR(" + arg + ", " + origin + ", " + scale + ", " + offset + ", " + decay + ")";
5843-
var actual = getQueryResults(query);
5844-
assertAlmostEqual(value[5], actual[0], query + " " + JSON.stringify(value));
5845-
5846-
actual = getQueryResults("RETURN NOOPT(DECAY_LINEAR(" + arg + ", " + origin + ", " + scale + ", " + offset + ", " + decay +"))");
5847-
assertAlmostEqual(value[5], actual[0], value, query + " " + JSON.stringify(value));
5824+
actual = getQueryResults(`RETURN NOOPT(DECAY_LINEAR(${arg}, ${origin}, ${scale}, ${offset}, ${decay}))`);
5825+
assertAlmostEqual(expected, actual[0], query + " " + JSON.stringify(value));
58485826
});
58495827
},
5828+
5829+
testAllDecayOutOfRange : function () {
5830+
["DECAY_GAUSS", "DECAY_EXP", "DECAY_LINEAR"].forEach(func => {
5831+
[
5832+
[1, 1, 1, 1, -1], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 2], // decay
5833+
[1, 1, 1, -1, 0.5], // offset
5834+
[1, 1, -1, 1, 0.5], [1, 1, 0, 1, 0.5], // scale
5835+
].forEach(function (value) {
5836+
const [arg, origin, scale, offset, decay] = value;
5837+
const query = `RETURN ${func}(${arg}, ${origin}, ${scale}, ${offset}, ${decay})`;
5838+
const result = internal.db._query(query);
5839+
assertEqual(1, result._extra.warnings.length);
5840+
assertEqual(arangodb.errors.ERROR_QUERY_NUMBER_OUT_OF_RANGE.code, result._extra.warnings[0].code);
5841+
});
5842+
});
5843+
},
58505844

5845+
testAllDecayInvalidType : function () {
5846+
["DECAY_GAUSS", "DECAY_EXP", "DECAY_LINEAR"].forEach(func => {
5847+
[1, 1, 1, 1, 0.5]
5848+
.flatMap((_, idx, array) => {
5849+
const invalidValues = ["foo", null, false, {}];
5850+
if (idx !== 0) {
5851+
invalidValues.push({});
5852+
}
5853+
return invalidValues.map(v => {
5854+
const result = [...array];
5855+
result[idx] = v;
5856+
return result;
5857+
});
5858+
})
5859+
.forEach(function (value) {
5860+
const [arg, origin, scale, offset, decay] = value;
5861+
const query =
5862+
`RETURN ${func}(${JSON.stringify(arg)}, ${JSON.stringify(origin)}, ${JSON.stringify(scale)}, ${JSON.stringify(offset)}, ${JSON.stringify(decay)})`;
5863+
const result = internal.db._query(query);
5864+
assertEqual(1, result._extra.warnings.length, value);
5865+
assertEqual(arangodb.errors.ERROR_QUERY_FUNCTION_ARGUMENT_TYPE_MISMATCH.code, result._extra.warnings[0].code, value);
5866+
});
5867+
});
5868+
},
58515869
////////////////////////////////////////////////////////////////////////////////
58525870
/// @brief test COSINE_SIMILARITY function
58535871
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)
0