From 648517f6081c5ab0929dd19ecbdac28a11672ed6 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 20 Dec 2015 10:51:30 -0500 Subject: [PATCH] Test: Added rx.t --- test/.gitignore | 1 + test/CMakeLists.txt | 4 +- test/rx.t.cpp | 133 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 test/rx.t.cpp diff --git a/test/.gitignore b/test/.gitignore index 669135d6..934fbf25 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,3 +1,4 @@ all.log color.t +rx.t utf8.t diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ec34c8ca..68e82b26 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,11 +5,11 @@ endif() include_directories (${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src - ${CMAKE_SOURCE_DIR}/common + ${CMAKE_SOURCE_DIR}/src/common ${CMAKE_SOURCE_DIR}/test ${TIMEW_INCLUDE_DIRS}) -set (test_SRCS color.t utf8.t) +set (test_SRCS color.t rx.t utf8.t) add_custom_target (test ./run_all --verbose DEPENDS ${test_SRCS} timew_executable diff --git a/test/rx.t.cpp b/test/rx.t.cpp new file mode 100644 index 00000000..e658c786 --- /dev/null +++ b/test/rx.t.cpp @@ -0,0 +1,133 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// +// http://www.opensource.org/licenses/mit-license.php +// +//////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +int main (int, char**) +{ + UnitTest ut (26); + + std::string text = "This is a test."; + RX r1 ("i. ", true); + ut.ok (r1.match (text), text + " =~ /i. /"); + + std::vector matches; + ut.ok (r1.match (matches, text), text + " =~ /i. /"); + ut.ok (matches.size () == 2, "2 match"); + ut.is (matches[0], "is ", "$1 == is\\s"); + ut.is (matches[1], "is ", "$1 == is\\s"); + + text = "abcdefghijklmnopqrstuvwxyz"; + + RX r3 ("t..", true); + ut.ok (r3.match (text), "t.."); + + RX r4 ("T..", false); + ut.ok (r4.match (text), "T.."); + + RX r5 ("T..", true); + ut.ok (!r5.match (text), "! T.."); + + text = "this is a test of the regex engine."; + // |...:....|....:....|....:....|....: + + RX r6 ("^this"); + ut.ok (r6.match (text), "^this matches"); + + RX r7 ("engine\\.$"); + ut.ok (r7.match (text), "engine\\.$ matches"); + + std::vector results; + std::vector start; + std::vector end; + RX r8 ("e..", true); + ut.ok (r8.match (results, text), "e.. there are matches"); + ut.ok (r8.match (start, end, text), "e.. there are matches"); + ut.is (results.size (), (size_t) 4, "e.. == 4 matches"); + ut.is (results[0], "est", "e..[0] == 'est'"); + ut.is (start[0], 11, "e..[0] == 11->"); + ut.is (end[0], 14, "e..[0] == ->14"); + + results.clear (); + RX r9 ("e", true); + ut.ok (r9.match (results, text), "e there are matches"); + ut.is (results.size (), (size_t) 6, "e == 6 matches"); + + start.clear (); + end.clear (); + ut.ok (r9.match (start, end, text), "e there are matches"); + ut.is (start.size (), (size_t) 6, "e == 6 matches"); + +#if defined(DARWIN) || defined(CYGWIN) || defined(FREEBSD) || defined(OPENBSD) + text = "this is the end."; + ut.pass (text + " =~ /\\bthe/"); + ut.pass (text + " =~ /the\\b/"); + ut.pass (text + " =~ /\\bthe\\b/"); +#elif defined(SOLARIS) + RX r10 ("\\"); + ut.ok (r11.match (text), text + " =~ /the\\>/"); + + RX r12 ("\\"); + ut.ok (r12.match (text), text + " =~ /\\/"); +#else + RX r10 ("\\bthe"); + text = "this is the end."; + ut.ok (r10.match (text), text + " =~ /\\bthe/"); + + RX r11 ("the\\b"); + ut.ok (r11.match (text), text + " =~ /the\\b/"); + + RX r12 ("\\bthe\\b"); + ut.ok (r12.match (text), text + " =~ /\\bthe\\b/"); +#endif + +#if defined(DARWIN) + text = "D0"; + RX r13 ("D\\d"); + ut.ok (r13.match (text), text + " =~ /D\\d/"); +#else + ut.skip (" =~ /D\\d/"); +#endif + + text = "D0"; + RX r14 ("D[[:digit:]]"); + ut.ok (r14.match (text), text + " =~ /D[[:digit:]]/"); + + text = "D0"; + RX r15 ("D[0-9]"); + ut.ok (r15.match (text), text + " =~ /D[0-9]/"); + + return 0; +} + +//////////////////////////////////////////////////////////////////////////////// +