From da17cddd8fe5ab873de06eede24a6faa13e5eab9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 5 Jul 2011 18:08:01 +0200 Subject: Actual regex implementation --- tests/re/driver.cxx | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/re/driver.cxx (limited to 'tests/re/driver.cxx') diff --git a/tests/re/driver.cxx b/tests/re/driver.cxx new file mode 100644 index 0000000..4f5683d --- /dev/null +++ b/tests/re/driver.cxx @@ -0,0 +1,84 @@ +// file : tests/re/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include +#include +#include + +#include + +using namespace cutl::re; + +int +main () +{ + // empty() and str(). + // + { + regex r; + assert (r.empty ()); + r = "['`]foo([^ ]*)bar['`]"; + assert (!r.empty ()); + assert (r.str () == "['`]foo([^ ]*)bar['`]"); + } + + // Error handling. + // + try + { + regex r ("['`]foo([^ ]*bar['`]"); + assert (false); + } + catch (format const& e) + { + assert (e.regex () == "['`]foo([^ ]*bar['`]"); + assert (!e.description ().empty ()); + //std::cerr << e.description () << std::endl; + } + + // match(), search(), and replace(). + // + { + regex r ("['`]foo([^ ]*)bar['`]"); + + assert (r.match ("'foofoxbar'")); + assert (!r.match ("'foof xbar'")); + + assert (r.search ("prefix 'foofoxbar' suffix")); + assert (!r.search ("prefix 'foof xbar' suffix")); + + assert (r.replace ("'foofoxbar'", "\\u$1") == "Fox"); + } + + // regexsub + // + { + regexsub r ("/['`]foo([^ ]*)bar['`]/\\u$1/"); + + assert (r.replace ("'foofoxbar'") == "Fox"); + } + + // regexsub escaping + // + { + regexsub r ("#a\\#\\\\\\\\#a?\\\\\\\\#"); + + assert (r.replace ("a#\\") == "a?\\"); + } + + // regexsub error handling. + // + try + { + regexsub r ("/['`]foo([^ ]*)bar['`]#\\u$1/"); + assert (false); + } + catch (format const& e) + { + assert (e.regex () == "/['`]foo([^ ]*)bar['`]#\\u$1/"); + assert (!e.description ().empty ()); + //std::cerr << e.description () << std::endl; + } +} -- cgit v1.1