Skip to content

Commit

Permalink
fixup! extraction unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
milroy committed Jan 14, 2025
1 parent 16ff6b8 commit e248f48
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 50 deletions.
19 changes: 8 additions & 11 deletions resource/evaluators/expr_eval_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ int expr_eval_api_t::evaluate_paren (const std::string &e,

int expr_eval_api_t::extract_leaf (const std::string &e,
const expr_eval_target_base_t &target,
unsigned long &jobid,
bool &agfilter)
std::vector<std::pair<std::string, std::string>> &predicates)
{
int rc = -1;
std::size_t delim;
Expand All @@ -249,7 +248,7 @@ int expr_eval_api_t::extract_leaf (const std::string &e,
}
key = e.substr (0, delim);
value = e.substr (delim + 1);
if ((rc = (&target)->extract (key, value, jobid, agfilter)) < 0) {
if ((rc = (&target)->extract (key, value, predicates)) < 0) {
errno = EINVAL;
goto done;
}
Expand Down Expand Up @@ -279,8 +278,7 @@ int expr_eval_api_t::extract_paren (const std::string &e,
const expr_eval_target_base_t &target,
size_t at,
size_t &nx,
unsigned long &jobid,
bool &agfilter)
std::vector<std::pair<std::string, std::string>> &predicates)
{
int rc = -1;
std::size_t tok;
Expand All @@ -289,12 +287,12 @@ int expr_eval_api_t::extract_paren (const std::string &e,
if (!is_paren (e, at)) {
if ((rc = parse_expr_leaf (e, at, tok, len)) < 0)
goto done;
if ((rc = extract_leaf (e.substr (tok, len), target, jobid, agfilter)) < 0)
if ((rc = extract_leaf (e.substr (tok, len), target, predicates)) < 0)
goto done;
} else {
if ((rc = parse_expr_paren (e, at, tok, len)) < 0)
goto done;
if ((rc = extract (e.substr (tok + 1, len - 2), target, jobid, agfilter)) < 0)
if ((rc = extract (e.substr (tok + 1, len - 2), target, predicates)) < 0)
goto done;
}
nx = tok + len;
Expand Down Expand Up @@ -369,14 +367,13 @@ int expr_eval_api_t::evaluate (const std::string &e,

int expr_eval_api_t::extract (const std::string &e,
const expr_eval_target_base_t &target,
unsigned long &jobid,
bool &agfilter)
std::vector<std::pair<std::string, std::string>> &predicates)
{
int rc = -1;
pred_op_t op;
std::size_t next, at;

if ((rc = extract_paren (e, target, 0, next, jobid, agfilter)) < 0)
if ((rc = extract_paren (e, target, 0, next, predicates)) < 0)
goto done;
at = next;

Expand All @@ -386,7 +383,7 @@ int expr_eval_api_t::extract (const std::string &e,
goto done;
}
at = next;
if ((rc = extract_paren (e, target, at, next, jobid, agfilter)) < 0)
if ((rc = extract_paren (e, target, at, next, predicates)) < 0)
goto done;
at = next;
}
Expand Down
10 changes: 4 additions & 6 deletions resource/evaluators/expr_eval_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define EXPR_EVAL_API_HPP

#include <string>
#include <vector>
#include "resource/evaluators/expr_eval_target.hpp"

namespace Flux {
Expand Down Expand Up @@ -58,8 +59,7 @@ class expr_eval_api_t {
*/
int extract (const std::string &expr,
const expr_eval_target_base_t &target,
unsigned long &jobid,
bool &agfilter);
std::vector<std::pair<std::string, std::string>> &predicates);

private:
enum class pred_op_t : int { AND = 0, OR = 1, UNKNOWN = 2 };
Expand Down Expand Up @@ -93,14 +93,12 @@ class expr_eval_api_t {
/* Extract methods */
int extract_leaf (const std::string &expr,
const expr_eval_target_base_t &target,
unsigned long &jobid,
bool &agfilter);
std::vector<std::pair<std::string, std::string>> &predicates);
int extract_paren (const std::string &expr,
const expr_eval_target_base_t &target,
size_t at,
size_t &next,
unsigned long &jobid,
bool &agfilter);
std::vector<std::pair<std::string, std::string>> &predicates);
};

} // namespace resource_model
Expand Down
4 changes: 2 additions & 2 deletions resource/evaluators/expr_eval_target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define EXPR_EVAL_TARGET_HPP

#include <string>
#include <vector>

namespace Flux {
namespace resource_model {
Expand Down Expand Up @@ -59,8 +60,7 @@ class expr_eval_target_base_t {
*/
virtual int extract (const std::string &p,
const std::string &x,
unsigned long &jobid,
bool &agfilter) const = 0;
std::vector<std::pair<std::string, std::string>> &predicates) const = 0;
};

} // namespace resource_model
Expand Down
17 changes: 2 additions & 15 deletions resource/evaluators/expr_eval_vtx_target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ int expr_eval_vtx_target_t::evaluate (const std::string &p,

int expr_eval_vtx_target_t::extract (const std::string &p,
const std::string &x,
unsigned long &jobid,
bool &agfilter) const
std::vector<std::pair<std::string, std::string>> &predicates) const
{
int rc = 0;
std::string lcx = x;
Expand All @@ -146,19 +145,7 @@ int expr_eval_vtx_target_t::extract (const std::string &p,
goto done;
}
std::transform (x.begin (), x.end (), lcx.begin (), ::tolower);
if (p == "jobid-alloc" || p == "jobid-span" || p == "jobid-tag" || p == "jobid-reserved") {
try {
jobid = std::stoul (lcx);
} catch (std::invalid_argument) {
errno = EINVAL;
rc = -1;
} catch (std::out_of_range) {
errno = EINVAL;
rc = -1;
}
} else if (p == "agfilter") {
agfilter = true;
}
predicates.push_back ({p, lcx});
done:
return rc;
}
Expand Down
4 changes: 2 additions & 2 deletions resource/evaluators/expr_eval_vtx_target.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <string>
#include <memory>
#include <vector>
#include "resource/schema/resource_graph.hpp"
#include "resource/evaluators/expr_eval_target.hpp"

Expand Down Expand Up @@ -82,8 +83,7 @@ class expr_eval_vtx_target_t : public expr_eval_target_base_t {
*/
virtual int extract (const std::string &p,
const std::string &x,
unsigned long &jobid,
bool &agfilter) const override;
std::vector<std::pair<std::string, std::string>> &predicates) const override;

/*! Initialize the object of this class with a resource vertex.
* This must be called before the validate and evaluate interfaces
Expand Down
42 changes: 28 additions & 14 deletions resource/evaluators/test/expr_eval_test01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C" {

#include <vector>
#include <iostream>
#include <algorithm>
#include "resource/evaluators/expr_eval_api.hpp"
#include "resource/evaluators/expr_eval_target.hpp"
#include "src/common/libtap/tap.h"
Expand All @@ -31,8 +32,7 @@ class expr_eval_test_target_t : public expr_eval_target_base_t {

virtual int extract (const std::string &p,
const std::string &x,
unsigned long &jobid,
bool &agfilter) const override;
std::vector<std::pair<std::string, std::string>> &predicates) const override;
};

int expr_eval_test_target_t::validate (const std::string &p, const std::string &x) const
Expand Down Expand Up @@ -69,20 +69,22 @@ int expr_eval_test_target_t::evaluate (const std::string &p,

int expr_eval_test_target_t::extract (const std::string &p,
const std::string &x,
unsigned long &jobid,
bool &agfilter) const
std::vector<std::pair<std::string, std::string>> &predicates) const
{
std::string lcx = x;
if (validate (p, x) < 0)
return -1;
if (p == "jobid-alloc") {
if (x != "") {
jobid = std::stoul (x);
} else {
jobid = 0;
}
} else if (p == "agfilter") {
agfilter = true;
}
std::transform (x.begin (), x.end (), lcx.begin (), ::tolower);
predicates.push_back ({p, lcx});
// if (p == "jobid-alloc") {
// if (x != "") {
// jobid = std::stoul (x);
// } else {
// jobid = 0;
// }
// } else if (p == "agfilter") {
// agfilter = true;
// }
return 0;
}

Expand Down Expand Up @@ -224,7 +226,19 @@ void test_extraction (std::vector<std::string> &expr_vector,
for (i = 0; i < expr_vector.size (); ++i) {
agfilter = false;
jobid = 0;
rc = evaluator.extract (expr_vector.at (i), expr_eval_test_target, jobid, agfilter);
std::vector<std::pair<std::string, std::string>> predicates;
rc = evaluator.extract (expr_vector.at (i), expr_eval_test_target, predicates);
for (auto const &p : predicates) {
if (p.first == "jobid-alloc" || p.first == "jobid-span" || p.first == "jobid-tag" || p.first == "jobid-reserved") {
jobid = std::stoul (p.second);
} else if (p.first == "agfilter") {
if (p.second == "true" || p.second == "t") {
agfilter = true;
} else {
agfilter = false;
}
}
}
expected = must_success ? rc == 0 : rc < 0;
expected_jobid = (jobids.at (i) == jobid);
expected_agfilter = (agfilters.at (i) == agfilter);
Expand Down

0 comments on commit e248f48

Please sign in to comment.