-
Notifications
You must be signed in to change notification settings - Fork 858
Apm 55 #14364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Apm 55 #14364
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9517969
gauss decay
alexbakharew 114b775
Merge remote-tracking branch 'origin/devel' into APM-55
alexbakharew 15a4a16
3 functions are ready
alexbakharew 00a52c1
fix for win build
alexbakharew 4161a8f
changes after code review
alexbakharew 87cf166
changelog
alexbakharew 35b14a3
changes in functions + js tests + range test
alexbakharew 2c02bbe
Merge remote-tracking branch 'origin/devel' into APM-55
alexbakharew 542ed44
renamed functions
alexbakharew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
gauss decay
- Loading branch information
commit 9517969690bd5fbc7e6189f3f795be9bad6232d7
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ build | |
|
||
*.deb | ||
*.rpm | ||
*.user | ||
|
||
.DS_Store | ||
*.swp | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
/// DISCLAIMER | ||
/// | ||
/// Copyright 2014-2020 ArangoDB GmbH, Cologne, Germany | ||
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany | ||
/// | ||
/// Licensed under the Apache License, Version 2.0 (the "License"); | ||
/// you may not use this file except in compliance with the License. | ||
/// You may obtain a copy of the License at | ||
/// | ||
6D47 | /// http://www.apache.org/licenses/LICENSE-2.0 | |
/// | ||
/// Unless required by applicable law or agreed to in writing, software | ||
/// distributed under the License is distributed on an "AS IS" BASIS, | ||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
/// See the License for the specific language governing permissions and | ||
/// limitations under the License. | ||
/// | ||
/// Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
/// | ||
/// @author Alexey Bakharew | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "fakeit.hpp" | ||
|
||
#include <vector> | ||
|
||
#include "Aql/AstNode.h" | ||
#include "Aql/ExpressionContext.h" | ||
#include "Aql/Function.h" | ||
#include "Aql/Functions.h" | ||
#include "Containers/SmallVector.h" | ||
#include "Transaction/Context.h" | ||
#include "Transaction/Methods.h" | ||
#include "IResearch/IResearchQueryCommon.h" | ||
#include <velocypack/Builder.h> | ||
#include <velocypack/Iterator.h> | ||
#include <velocypack/Parser.h> | ||
#include <velocypack/Slice.h> | ||
#include <velocypack/velocypack-aliases.h> | ||
|
||
using namespace arangodb; | ||
using namespace arangodb::aql; | ||
using namespace arangodb::containers; | ||
|
||
namespace { | ||
|
||
|
||
// helper function | ||
SmallVector<AqlValue> create_arg_vec(const VPackSlice slice) { | ||
alexbakharew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
SmallVector<AqlValue>::allocator_type::arena_type arena; | ||
SmallVector<AqlValue> params{arena}; | ||
|
||
for (const auto arg : VPackArrayIterator(slice)) { | ||
params.emplace_back(AqlValue(arg)); | ||
} | ||
|
||
return params; | ||
} | ||
|
||
void expect_eq_slices(const VPackSlice actual_slice, const VPackSlice expected_slice) { | ||
alexbakharew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (actual_slice.isArray() && expected_slice.isArray()) { | ||
VPackValueLength actual_size = actual_slice.length(); | ||
VPackValueLength expected_size = actual_slice.length(); | ||
ASSERT_EQ(actual_size, expected_size); | ||
|
||
double lhs, rhs; | ||
for(VPackValueLength i = 0; i < actual_size; ++i) { | ||
lhs = actual_slice.at(i).getNumber<decltype (lhs)>(); | ||
rhs = actual_slice.at(i).getNumber<decltype (rhs)>(); | ||
ASSERT_DOUBLE_EQ(lhs, rhs); | ||
} | ||
} else if (actual_slice.isNumber() && expected_slice.isNumber()) { | ||
double lhs = actual_slice.getNumber<decltype (lhs)>(); | ||
double rhs = expected_slice.getNumber<decltype (rhs)>(); | ||
ASSERT_DOUBLE_EQ(lhs, rhs); | ||
} else { | ||
ASSERT_TRUE(false); | ||
} | ||
|
||
return; | ||
} | ||
|
||
AqlValue evaluate_gauss(const SmallVector<AqlValue> params) { | ||
fakeit::Mock<ExpressionContext> expressionContextMock; | ||
ExpressionContext& expressionContext = expressionContextMock.get(); | ||
fakeit::When(Method(expressionContextMock, registerWarning)).AlwaysDo([](ErrorCode, char const*){ }); | ||
|
||
VPackOptions options; | ||
fakeit::Mock<transaction::Context> trxCtxMock; | ||
fakeit::When(Method(trxCtxMock, getVPackOptions)).AlwaysReturn(&options); | ||
transaction::Context& trxCtx = trxCtxMock.get(); | ||
|
||
fakeit::Mock<transaction::Methods> trxMock; | ||
fakeit::When(Method(trxMock, transactionContextPtr)).AlwaysReturn(&trxCtx); | ||
fakeit::When(Method(trxMock, vpackOptions)).AlwaysReturn(options); | ||
transaction::Methods& trx = trxMock.get(); | ||
|
||
fakeit::When(Method(expressionContextMock, trx)).AlwaysDo([&trx]() -> transaction::Methods& { | ||
return trx; | ||
}); | ||
|
||
arangodb::aql::Function f("GAUSS_DECAY", &Functions::GaussDecay); | ||
arangodb::aql::AstNode node(NODE_TYPE_FCALL); | ||
node.setData(static_cast<void const*>(&f)); | ||
|
||
return Functions::GaussDecay(&expressionContext, node, params); | ||
} | ||
|
||
void assertGauss(char const* expected, char const* args) { | ||
|
||
// get slice for expected value | ||
auto const expected_json = VPackParser::fromJson(expected); | ||
alexbakharew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto const expected_slice = expected_json->slice(); | ||
ASSERT_TRUE(expected_slice.isArray() || expected_slice.isNumber()); | ||
|
||
// get slice for args value | ||
auto const args_json = VPackParser::fromJson(args); | ||
auto const args_slice = args_json->slice(); | ||
ASSERT_TRUE(args_slice.isArray()); | ||
|
||
// create params vector from args slice | ||
SmallVector<AqlValue> params = create_arg_vec(args_slice); | ||
|
||
// evaluate | ||
auto const actual_value = evaluate_gauss(params); | ||
ASSERT_TRUE(actual_value.isNumber() || actual_value.isArray()); | ||
|
||
// check equality | ||
expect_eq_slices(actual_value.slice(), expected_slice); | ||
return; | ||
alexbakharew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void assertGaussFail(char const* args) { | ||
// get slice for args value | ||
auto const args_json = VPackParser::fromJson(args); | ||
auto const args_slice = args_json->slice(); | ||
ASSERT_TRUE(args_slice.isArray()); | ||
|
||
// create params vector from args slice | ||
SmallVector<AqlValue> params = create_arg_vec(args_slice); | ||
|
||
ASSERT_TRUE(evaluate_gauss(params).isNull(false)); | ||
} | ||
|
||
TEST(GaussDecayFunctionTest, test) { | ||
alexbakharew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertGauss("0.5", "[20, 40, 5, 5, 0.5]"); | ||
assertGauss("1", "[41, 40, 5, 5, 0.5]"); | ||
assertGauss("[0.5, 1.0]", "[[20.0, 41], 40, 5, 5, 0.5]"); | ||
assertGauss("1.0", "[40, 40, 5, 5, 0.5]"); | ||
assertGauss("1.0", "[49.987, 49.987, 0.001, 0.001, 0.2]"); | ||
assertGauss("0.2715403018822964", "[49.9889, 49.987, 0.001, 0.001, 0.2]"); | ||
assertGauss("0.1", "[-10, 40, 5, 0, 0.1]"); | ||
assertGaussFail("[30, 40, 5]"); | ||
assertGaussFail("[30, 40, 5, 100]"); | ||
assertGaussFail("[30, 40, 5, 100, -100]"); | ||
} | ||
|
||
} // namespase |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.