Ci test
This commit is contained in:
Matthieu Gautier 2019-08-12 12:39:51 +02:00 committed by GitHub
commit 44bec86f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 123 additions and 4 deletions

View File

@ -30,7 +30,7 @@
std::map<std::string, std::shared_ptr<icu::RegexPattern>> regexCache; std::map<std::string, std::shared_ptr<icu::RegexPattern>> regexCache;
static pthread_mutex_t regexLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t regexLock = PTHREAD_MUTEX_INITIALIZER;
std::unique_ptr<icu::RegexMatcher> buildMatcher(const std::string& regex, const icu::UnicodeString& content) std::unique_ptr<icu::RegexMatcher> buildMatcher(const std::string& regex, icu::UnicodeString& content)
{ {
std::shared_ptr<icu::RegexPattern> pattern; std::shared_ptr<icu::RegexPattern> pattern;
/* Regex is in cache */ /* Regex is in cache */
@ -56,7 +56,8 @@ std::unique_ptr<icu::RegexMatcher> buildMatcher(const std::string& regex, const
bool matchRegex(const std::string& content, const std::string& regex) bool matchRegex(const std::string& content, const std::string& regex)
{ {
ucnv_setDefaultName("UTF-8"); ucnv_setDefaultName("UTF-8");
auto matcher = buildMatcher(regex, content.c_str()); icu::UnicodeString ucontent(content.c_str());
auto matcher = buildMatcher(regex, ucontent);
return matcher->find(); return matcher->find();
} }

View File

@ -2,7 +2,8 @@
tests = [ tests = [
'parseUrl', 'parseUrl',
'library' 'library',
'regex'
] ]

109
test/regex.cpp Normal file
View File

@ -0,0 +1,109 @@
/*
* Copyright (C) 2019 Matthieu Gautier
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "gtest/gtest.h"
#include <string>
#include "../include/tools/regexTools.h"
namespace
{
TEST(MatchRegex, match)
{
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "f"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "ef"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "efg"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "mno"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "m*o"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "m*v"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "M"));
EXPECT_TRUE(matchRegex("abcdefghijklmnopqrstuvwxyz", "STU"));
EXPECT_TRUE(matchRegex("Mythology & Folklore Stack Exchange", "folklore"));
EXPECT_TRUE(matchRegex("Mythology &amp; Folklore Stack Exchange", "Folklore"));
EXPECT_TRUE(matchRegex("Mythology &amp; Folklore Stack Exchange", "folklore"));
}
TEST(MatchRegex, nomatch)
{
EXPECT_FALSE(matchRegex("abcdefghijklmnopqrstuvwxyz", "vu"));
}
TEST(ReplaceRegex, beginning)
{
EXPECT_EQ(replaceRegex("abcdefghij", "----", "abcd"), "----efghij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "abcde"), "----fghij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "a.*i"), "----j");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "AbCd"), "----efghij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "A"), "----bcdefghij");
}
TEST(ReplaceRegex, end)
{
EXPECT_EQ(replaceRegex("abcdefghij", "----", "ghij"), "abcdef----");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "fghij"), "abcde----");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "c.*j"), "ab----");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "GhIj"), "abcdef----");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "J"), "abcdefghi----");
}
TEST(ReplaceRegex, middle)
{
EXPECT_EQ(replaceRegex("abcdefghij", "----", "cdef"), "ab----ghij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "cdefgh"), "ab----ij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "c.*f"), "ab----ghij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "DeFg"), "abc----hij");
EXPECT_EQ(replaceRegex("abcdefghij", "----", "F"), "abcde----ghij");
}
TEST(append, beggining)
{
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "abcd", "----"), "abcd----efghij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "abcde", "----"), "abcde----fghij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "a.*i", "----"), "abcdefghi----j");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "AbCd", "----"), "abcd----efghij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "A", "----"), "a----bcdefghij");
}
TEST(append, end)
{
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "ghij", "----"), "abcdefghij----");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "fghij", "----"), "abcdefghij----");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "c.*j", "----"), "abcdefghij----");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "GhIj", "----"), "abcdefghij----");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "J", "----"), "abcdefghij----");
}
TEST(append, middle)
{
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "cdef", "----"), "abcdef----ghij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "cdefgh", "----"), "abcdefgh----ij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "c.*f", "----"), "abcdef----ghij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "DeFg", "----"), "abcdefg----hij");
EXPECT_EQ(appendToFirstOccurence("abcdefghij", "F", "----"), "abcdef----ghij");
}
};
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -5,13 +5,15 @@ set -e
BUILD_DIR=${HOME}/BUILD_${PLATFORM} BUILD_DIR=${HOME}/BUILD_${PLATFORM}
INSTALL_DIR=${BUILD_DIR}/INSTALL INSTALL_DIR=${BUILD_DIR}/INSTALL
TEST=0
case ${PLATFORM} in case ${PLATFORM} in
"native_static") "native_static")
MESON_OPTION="--default-library=static" MESON_OPTION="--default-library=static"
TEST=1
;; ;;
"native_dyn") "native_dyn")
MESON_OPTION="--default-library=shared" MESON_OPTION="--default-library=shared"
TEST=1
;; ;;
"win32_static") "win32_static")
MESON_OPTION="--default-library=static --cross-file ${BUILD_DIR}/meson_cross_file.txt" MESON_OPTION="--default-library=static --cross-file ${BUILD_DIR}/meson_cross_file.txt"
@ -39,3 +41,9 @@ export CPPFLAGS="-I${INSTALL_DIR}/include"
meson . build ${MESON_OPTION} meson . build ${MESON_OPTION}
cd build cd build
ninja ninja
if [[ "$TEST" == "1" ]]
then
echo "Running test"
export LD_LIBRARY_PATH=${INSTALL_DIR}/lib:${INSTALL_DIR}/lib64:${INSTALL_DIR}/lib/x86_64-linux-gnu
ninja test
fi