unit: modernise test_util

This commit is contained in:
Justin Viiret 2017-03-01 16:55:24 +11:00 committed by Matthew Barr
parent 1376f3849a
commit bc7da2807a
2 changed files with 27 additions and 32 deletions

View File

@ -26,24 +26,24 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include "hs.h" #include "hs.h"
#include "test_util.h" #include "test_util.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "util/expressions.h" #include "util/expressions.h"
#include "util/ExpressionParser.h" #include "util/ExpressionParser.h"
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std; using namespace std;
int record_cb(unsigned id, unsigned long long, unsigned long long to, int record_cb(unsigned id, unsigned long long, unsigned long long to,
unsigned, void *ctxt) { unsigned, void *ctxt) {
CallBackContext *c = (CallBackContext *)ctxt; CallBackContext *c = (CallBackContext *)ctxt;
c->matches.push_back(MatchRecord(to, id)); c->matches.emplace_back(to, id);
return (int)c->halt; return (int)c->halt;
} }
@ -64,20 +64,20 @@ hs_database_t *buildDB(const vector<pattern> &patterns, unsigned int mode,
vector<unsigned int> ids; vector<unsigned int> ids;
vector<const hs_expr_ext *> ext; vector<const hs_expr_ext *> ext;
for (vector<pattern>::const_iterator it = patterns.begin(); for (const auto &pat : patterns) {
it != patterns.end(); ++it) { expressions.push_back(pat.expression.c_str());
expressions.push_back(it->expression.c_str()); flags.push_back(pat.flags);
flags.push_back(it->flags); ids.push_back(pat.id);
ids.push_back(it->id); ext.push_back(&pat.ext);
ext.push_back(&it->ext);
} }
hs_database_t *db = nullptr; hs_database_t *db = nullptr;
hs_compile_error_t *compile_err = nullptr; hs_compile_error_t *compile_err = nullptr;
hs_error_t err; hs_error_t err;
err = hs_compile_ext_multi(&expressions[0], &flags[0], &ids[0], &ext[0], err = hs_compile_ext_multi(expressions.data(), flags.data(), ids.data(),
patterns.size(), mode, plat, &db, &compile_err); ext.data(), patterns.size(), mode, plat, &db,
&compile_err);
if (err != HS_SUCCESS) { if (err != HS_SUCCESS) {
return nullptr; return nullptr;
@ -87,15 +87,13 @@ hs_database_t *buildDB(const vector<pattern> &patterns, unsigned int mode,
} }
hs_database_t *buildDB(const pattern &expr, unsigned int mode) { hs_database_t *buildDB(const pattern &expr, unsigned int mode) {
return buildDB(vector<pattern>(1, expr), mode); return buildDB(vector<pattern>({expr}), mode);
} }
hs_database_t *buildDB(const char *expression, unsigned int flags, hs_database_t *buildDB(const char *expression, unsigned int flags,
unsigned int id, unsigned int mode, unsigned int id, unsigned int mode,
hs_platform_info_t *plat) { hs_platform_info_t *plat) {
vector<pattern> patterns; return buildDB({pattern(expression, flags, id)}, mode, plat);
patterns.push_back(pattern(expression, flags, id));
return buildDB(patterns, mode, plat);
} }
hs_database_t *buildDB(const char *filename, unsigned int mode, hs_database_t *buildDB(const char *filename, unsigned int mode,
@ -104,16 +102,14 @@ hs_database_t *buildDB(const char *filename, unsigned int mode,
ExpressionMap expressions; ExpressionMap expressions;
loadExpressionsFromFile(filename, expressions); loadExpressionsFromFile(filename, expressions);
for (ExpressionMap::iterator it = expressions.begin(); for (const auto &expr : expressions) {
it != expressions.end(); ++it) {
unsigned int flags = 0; unsigned int flags = 0;
string regex; string regex;
hs_expr_ext ext; hs_expr_ext ext;
if (!readExpression(it->second, regex, &flags, &ext)) { if (!readExpression(expr.second, regex, &flags, &ext)) {
return nullptr; return nullptr;
} }
patterns.push_back(pattern(regex, flags | extra_flags, it->first, patterns.emplace_back(regex, flags | extra_flags, expr.first, ext);
ext));
} }
return buildDB(patterns, mode); return buildDB(patterns, mode);
} }
@ -150,13 +146,13 @@ hs_database_t *buildDB(const char *filename, unsigned int mode,
ExpressionMap expressions; ExpressionMap expressions;
loadExpressionsFromFile(filename, expressions); loadExpressionsFromFile(filename, expressions);
for (ExpressionMap::iterator it = expressions.begin(); for (const auto &expr : expressions) {
it != expressions.end(); ++it) {
unsigned int flags = 0; unsigned int flags = 0;
string regex; string regex;
hs_expr_ext ext; hs_expr_ext ext;
bool must_be_ordered; bool must_be_ordered;
if (!readExpression(it->second, regex, &flags, &ext, &must_be_ordered)) { if (!readExpression(expr.second, regex, &flags, &ext,
&must_be_ordered)) {
return nullptr; return nullptr;
} }
@ -164,7 +160,7 @@ hs_database_t *buildDB(const char *filename, unsigned int mode,
return nullptr; return nullptr;
} }
patterns.emplace_back(regex, flags, it->first, ext); patterns.emplace_back(regex, flags, expr.first, ext);
} }
return buildDB(patterns, mode); return buildDB(patterns, mode);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015-2016, Intel Corporation * Copyright (c) 2015-2017, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -29,13 +29,13 @@
#ifndef TEST_UTIL_H #ifndef TEST_UTIL_H
#define TEST_UTIL_H #define TEST_UTIL_H
#include "hs.h"
#include <cstring> #include <cstring>
#include <iosfwd> #include <iosfwd>
#include <string> #include <string>
#include <vector> #include <vector>
#include "hs.h"
#ifndef UNUSED #ifndef UNUSED
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
#define UNUSED #define UNUSED
@ -56,8 +56,7 @@ struct MatchRecord {
std::ostream &operator<<(std::ostream &o, const MatchRecord &m); std::ostream &operator<<(std::ostream &o, const MatchRecord &m);
struct CallBackContext { struct CallBackContext {
CallBackContext() : halt(false) {} bool halt = false;
bool halt;
std::vector<MatchRecord> matches; std::vector<MatchRecord> matches;
void clear() { void clear() {