From bc7da2807a054c92689b059e0615c444f27b055a Mon Sep 17 00:00:00 2001 From: Justin Viiret Date: Wed, 1 Mar 2017 16:55:24 +1100 Subject: [PATCH] unit: modernise test_util --- unit/hyperscan/test_util.cpp | 50 +++++++++++++++++------------------- unit/hyperscan/test_util.h | 9 +++---- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/unit/hyperscan/test_util.cpp b/unit/hyperscan/test_util.cpp index f3f6e610..f6c20a74 100644 --- a/unit/hyperscan/test_util.cpp +++ b/unit/hyperscan/test_util.cpp @@ -26,24 +26,24 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include -#include - #include "hs.h" #include "test_util.h" #include "gtest/gtest.h" #include "util/expressions.h" #include "util/ExpressionParser.h" +#include +#include +#include +#include + using namespace std; int record_cb(unsigned id, unsigned long long, unsigned long long to, unsigned, void *ctxt) { CallBackContext *c = (CallBackContext *)ctxt; - c->matches.push_back(MatchRecord(to, id)); + c->matches.emplace_back(to, id); return (int)c->halt; } @@ -64,20 +64,20 @@ hs_database_t *buildDB(const vector &patterns, unsigned int mode, vector ids; vector ext; - for (vector::const_iterator it = patterns.begin(); - it != patterns.end(); ++it) { - expressions.push_back(it->expression.c_str()); - flags.push_back(it->flags); - ids.push_back(it->id); - ext.push_back(&it->ext); + for (const auto &pat : patterns) { + expressions.push_back(pat.expression.c_str()); + flags.push_back(pat.flags); + ids.push_back(pat.id); + ext.push_back(&pat.ext); } hs_database_t *db = nullptr; hs_compile_error_t *compile_err = nullptr; hs_error_t err; - err = hs_compile_ext_multi(&expressions[0], &flags[0], &ids[0], &ext[0], - patterns.size(), mode, plat, &db, &compile_err); + err = hs_compile_ext_multi(expressions.data(), flags.data(), ids.data(), + ext.data(), patterns.size(), mode, plat, &db, + &compile_err); if (err != HS_SUCCESS) { return nullptr; @@ -87,15 +87,13 @@ hs_database_t *buildDB(const vector &patterns, unsigned int mode, } hs_database_t *buildDB(const pattern &expr, unsigned int mode) { - return buildDB(vector(1, expr), mode); + return buildDB(vector({expr}), mode); } hs_database_t *buildDB(const char *expression, unsigned int flags, unsigned int id, unsigned int mode, hs_platform_info_t *plat) { - vector patterns; - patterns.push_back(pattern(expression, flags, id)); - return buildDB(patterns, mode, plat); + return buildDB({pattern(expression, flags, id)}, mode, plat); } 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; loadExpressionsFromFile(filename, expressions); - for (ExpressionMap::iterator it = expressions.begin(); - it != expressions.end(); ++it) { + for (const auto &expr : expressions) { unsigned int flags = 0; string regex; hs_expr_ext ext; - if (!readExpression(it->second, regex, &flags, &ext)) { + if (!readExpression(expr.second, regex, &flags, &ext)) { return nullptr; } - patterns.push_back(pattern(regex, flags | extra_flags, it->first, - ext)); + patterns.emplace_back(regex, flags | extra_flags, expr.first, ext); } return buildDB(patterns, mode); } @@ -150,13 +146,13 @@ hs_database_t *buildDB(const char *filename, unsigned int mode, ExpressionMap expressions; loadExpressionsFromFile(filename, expressions); - for (ExpressionMap::iterator it = expressions.begin(); - it != expressions.end(); ++it) { + for (const auto &expr : expressions) { unsigned int flags = 0; string regex; hs_expr_ext ext; 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; } @@ -164,7 +160,7 @@ hs_database_t *buildDB(const char *filename, unsigned int mode, return nullptr; } - patterns.emplace_back(regex, flags, it->first, ext); + patterns.emplace_back(regex, flags, expr.first, ext); } return buildDB(patterns, mode); } diff --git a/unit/hyperscan/test_util.h b/unit/hyperscan/test_util.h index 9b963529..efa0570c 100644 --- a/unit/hyperscan/test_util.h +++ b/unit/hyperscan/test_util.h @@ -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 * modification, are permitted provided that the following conditions are met: @@ -29,13 +29,13 @@ #ifndef TEST_UTIL_H #define TEST_UTIL_H +#include "hs.h" + #include #include #include #include -#include "hs.h" - #ifndef UNUSED #if defined(_WIN32) || defined(_WIN64) #define UNUSED @@ -56,8 +56,7 @@ struct MatchRecord { std::ostream &operator<<(std::ostream &o, const MatchRecord &m); struct CallBackContext { - CallBackContext() : halt(false) {} - bool halt; + bool halt = false; std::vector matches; void clear() {