Skip to content

Commit 8ae73d3

Browse files
author
Hana Dusíková
committed
benchmark and tests updated
1 parent 93a1953 commit 8ae73d3

File tree

6 files changed

+65
-47
lines changed

6 files changed

+65
-47
lines changed

tests/benchmark-exec/baseline.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <cstdio>
2+
#include <iostream>
3+
#include <fstream>
4+
#include <regex>
5+
#include "ctre.hpp"
6+
7+
extern "C" {
8+
9+
#define PCRE2_CODE_UNIT_WIDTH 8
10+
#define PCRE2_STATIC
11+
#include <pcre2.h>
12+
13+
}
14+
15+
int main (int argc, char ** argv)
16+
{
17+
return 0;
18+
}

tests/benchmark-exec/ctre.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
#include <ctre.hpp>
21
#include <cstdio>
32
#include <iostream>
43
#include <fstream>
4+
#include <regex>
5+
#include "ctre.hpp"
6+
7+
extern "C" {
8+
9+
#define PCRE2_CODE_UNIT_WIDTH 8
10+
#define PCRE2_STATIC
11+
#include <pcre2.h>
12+
13+
}
514

615
int main (int argc, char ** argv)
716
{
817
using namespace ctre::literals;
9-
//constexpr auto re1 = "ABCD|DEFGH|EFGHI|A{4,}+"_pcre;
10-
constexpr auto re1 = "^[0-9]{4,16}?[aA]"_pcre;
18+
constexpr auto re = "([aAbB]{4,}|[xXyY]{4,}|[1234]{4,})0"_ctre;
1119

1220
auto grep = [&](auto && stream) {
1321
std::string line;
1422
while (std::getline(stream, line)) {
15-
if (bool(re1.match(line))) {
23+
if (bool(re.search(line))) {
1624
std::cout << line << '\n';
1725
}
1826
}
@@ -24,7 +32,5 @@ int main (int argc, char ** argv)
2432
std::string fname{std::string(argv[i])};
2533
grep(std::ifstream(fname, std::ifstream::in));
2634
}
27-
28-
return 0;
2935
}
3036

tests/benchmark-exec/pcre.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <cstdio>
22
#include <iostream>
33
#include <fstream>
4+
#include <regex>
5+
#include "ctre.hpp"
46

57
extern "C" {
68

@@ -14,26 +16,15 @@ int main (int argc, char ** argv)
1416
{
1517
int errornumber = 0;
1618
size_t erroroffset = 0;
17-
//const unsigned char * pattern = "^[0-9]{4,16}?[aA]";
18-
auto * pattern = reinterpret_cast<const unsigned char *>("ABCD|DEFGH|EFGHI|A{4,}+");
19+
auto * pattern = reinterpret_cast<const unsigned char *>("([aAbB]{4,}|[xXyY]{4,}|[1234]{4,})0");
1920
pcre2_code * re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
20-
if (!re) {
21-
std::cerr << "can't compile RE\n";
22-
return 1;
23-
}
24-
25-
size_t subject_length;
26-
pcre2_match_data * match_data = pcre2_match_data_create_from_pattern(re, NULL);
2721

2822
auto grep = [&](auto && stream) {
2923
std::string line;
3024
while (std::getline(stream, line)) {
31-
if (pcre2_match(re, reinterpret_cast<const unsigned char *>(line.c_str()), line.length(), 0, 0, match_data, NULL) >= 0) {
25+
if (pcre2_match(re, reinterpret_cast<const unsigned char *>(line.c_str()), line.length(), 0, 0, NULL, NULL) >= 0) {
3226
std::cout << line << '\n';
3327
}
34-
//if (bool(re.match(line))) {
35-
// std::cout << line << '\n';
36-
//}
3728
}
3829
};
3930

@@ -44,8 +35,5 @@ int main (int argc, char ** argv)
4435
grep(std::ifstream(fname, std::ifstream::in));
4536
}
4637

47-
pcre2_match_data_free(match_data);
4838
pcre2_code_free(re);
49-
50-
return 0;
5139
}

tests/benchmark-exec/std.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22
#include <iostream>
33
#include <fstream>
44
#include <regex>
5+
#include "ctre.hpp"
6+
7+
extern "C" {
8+
9+
#define PCRE2_CODE_UNIT_WIDTH 8
10+
#define PCRE2_STATIC
11+
#include <pcre2.h>
12+
13+
}
514

615
int main (int argc, char ** argv)
716
{
8-
//using namespace ctre::literals;
9-
//constexpr auto re = "ABCD|DEFGH|EFGHI|A{4,}"_pcre;
10-
std::regex re1("^[0-9]{4,16}?[aA]");
17+
std::regex re("([aAbB]{4,}|[xXyY]{4,}|[1234]{4,})0");
1118

1219
auto grep = [&](auto && stream) {
1320
std::string line;
1421
while (std::getline(stream, line)) {
15-
if (std::regex_search(line, re1)) {
22+
if (std::regex_search(line, re)) {
1623
std::cout << line << '\n';
1724
}
18-
//if (bool(re.match(line))) {
19-
// std::cout << line << '\n';
20-
//}
2125
}
2226
};
2327

@@ -27,6 +31,4 @@ int main (int argc, char ** argv)
2731
std::string fname{std::string(argv[i])};
2832
grep(std::ifstream(fname, std::ifstream::in));
2933
}
30-
31-
return 0;
3234
}

tests/matching.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ template <typename Pattern> constexpr bool match(std::string_view input, Pattern
55
return bool(ctre::match_re(input.begin(), input.end(), pattern));
66
}
77

8+
template <typename Pattern> constexpr bool search(std::string_view input, Pattern pattern) {
9+
return bool(ctre::search_re(input.begin(), input.end(), pattern));
10+
}
11+
812
using namespace std::string_view_literals;
913

1014
static_assert(match("a"sv, ctre::character<'a'>()));
11-
static_assert(match("abc"sv, ctre::character<'a'>())); // only match from start
15+
static_assert(search("abc"sv, ctre::character<'a'>())); // only match from start
1216
static_assert(!match("abc"sv, ctre::character<'b'>()));
1317
static_assert(!match("a"sv, ctre::character<'b'>()));
1418
static_assert(match("a"sv, ctre::any()));
@@ -67,7 +71,7 @@ static_assert(match("aba"sv, ctre::sequence<ctre::possessive_plus<ctre::characte
6771

6872
static_assert(match("aaax"sv, ctre::sequence<ctre::lazy_repeat<3,0,ctre::character<'a'>>, ctre::character<'x'>>()));
6973

70-
static_assert(match("aaaaaa"sv, ctre::repeat<0,5,ctre::character<'a'>>()));
74+
static_assert(search("aaaaaa"sv, ctre::repeat<0,5,ctre::character<'a'>>()));
7175
static_assert(!match("aaaaaa"sv, ctre::sequence<ctre::repeat<0,5,ctre::character<'a'>>, ctre::assert_end>()));
7276
static_assert(match("aaaaa"sv, ctre::sequence<ctre::repeat<0,5,ctre::character<'a'>>, ctre::assert_end>()));
7377

tests/matching2.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ using namespace ctre::literals;
55
using namespace ctre::test_literals;
66
using namespace std::string_view_literals;
77

8-
static_assert(""_pcre.match("abc"sv));
8+
static_assert(""_pcre.search("abc"sv));
99
static_assert("abc"_pcre.match("abc"sv));
1010

1111
static_assert("a"_pcre.match("a"sv));
12-
static_assert("a"_pcre.match("abc"sv));
12+
static_assert("a"_pcre.search("abc"sv));
1313
static_assert("b"_pcre.search("abc"sv));
1414
static_assert(!"^b"_pcre.match("abc"sv));
1515
static_assert(!"b"_pcre.match("a"sv));
1616
static_assert("."_pcre.match("a"sv));
17-
static_assert("."_pcre.match("abc"sv));
17+
static_assert("."_pcre.search("abc"sv));
1818
static_assert("[a-z]"_pcre.match("a"sv));
1919
static_assert("[a-z]"_pcre.match("f"sv));
2020
static_assert("[a-z]"_pcre.match("z"sv));
@@ -37,7 +37,7 @@ static_assert("(?:a|b|c)"_pcre.match("c"sv));
3737
static_assert(!"(?:a|b|c)"_pcre.match("d"sv));
3838
static_assert("(?:xy)?"_pcre.match("xy"sv));
3939
static_assert("(?:xy)?"_pcre.match(""sv));
40-
static_assert("(?:xy)?"_pcre.match("zxy"sv));
40+
static_assert("(?:xy)?"_pcre.search("zxy"sv));
4141
static_assert("(?:xy)?$"_pcre.search("zxy"sv));
4242
static_assert(!"~(?:xy)?$"_pcre.match("zxy"sv));
4343

@@ -46,12 +46,12 @@ static_assert("^def$"_pcre.match("def"sv));
4646
static_assert(!"a^"_pcre.match("a"sv));
4747
static_assert(!"$a"_pcre.match("a"sv));
4848

49-
static_assert("a+?"_pcre.match("aaax"sv));
50-
static_assert("a+?"_pcre.match("ax"sv));
49+
static_assert("a+?"_pcre.search("aaax"sv));
50+
static_assert("a+?"_pcre.search("ax"sv));
5151
static_assert(!"a+?"_pcre.match("x"sv));
5252

53-
static_assert("a++"_pcre.match("aaax"sv));
54-
static_assert("a++"_pcre.match("ax"sv));
53+
static_assert("a++"_pcre.search("aaax"sv));
54+
static_assert("a++"_pcre.search("ax"sv));
5555
static_assert(!"a++"_pcre.match("x"sv));
5656

5757
static_assert("a*?x"_pcre.match("aaax"sv));
@@ -78,7 +78,7 @@ static_assert("a{3,}x"_pcre.match("aaax"sv));
7878
static_assert("a{3,}x"_pcre.match("aaaax"sv));
7979

8080
static_assert("^a{5}"_pcre.match("aaaaa"sv));
81-
static_assert("^a{5}"_pcre.match("aaaaaa"sv));
81+
static_assert("^a{5}"_pcre.search("aaaaaa"sv));
8282
static_assert(!"^a{5}$"_pcre.match("aaaaaa"sv));
8383

8484
static_assert("a*"_pcre.match("aaa"sv));
@@ -119,9 +119,9 @@ static_assert("(?<name>abc)+"_pcre.match("abcabc"sv));
119119
static_assert("(?<name>abc)+"_pcre.match("abcabcabc"sv));
120120
static_assert(!"(?<name>abc)+"_pcre.match("name"sv));
121121

122-
static_assert(std::string_view{"^([a-z]+)"_pcre.match("abcdef1234"sv)} == "abcdef"sv);
122+
static_assert(std::string_view{"^([a-z]+)"_pcre.search("abcdef1234"sv)} == "abcdef"sv);
123123
static_assert(std::string_view{"^([a-z]+)1234"_pcre.match("abcdef1234"sv)} == "abcdef1234"sv);
124-
static_assert(std::string_view{"^([a-z])"_pcre.match("abcdef1234"sv)} == "a"sv);
124+
static_assert(std::string_view{"^([a-z])"_pcre.search("abcdef1234"sv)} == "a"sv);
125125

126126
static_assert("^([0-9]+[a-z]+)+"_pcre.match("123abc456def"sv));
127127
static_assert("^([0-9]+[a-z]+)+"_pcre.match("123abc456def"sv).template get<1>() == "456def"sv);
@@ -131,9 +131,9 @@ static_assert("^([0-9]++[a-z]++)+"_pcre.match("123abc456def"sv));
131131
static_assert("^([0-9]++[a-z]++)+"_pcre.match("123abc456def"sv).template get<1>() == "456def"sv);
132132
static_assert("^([0-9]++[a-z]++)+"_pcre.match("123abc456def"sv).template get<0>() == "123abc456def"sv);
133133

134-
static_assert("^([0-9]+?[a-z]+?)+"_pcre.match("123abc456def"sv));
135-
static_assert("^([0-9]+?[a-z]+?)+"_pcre.match("123abc456def"sv).template get<1>() == "123a"sv);
136-
static_assert("^([0-9]+?[a-z]+?)+"_pcre.match("123abc456def"sv).template get<0>() == "123a"sv);
134+
static_assert("^([0-9]+?[a-z]+?)+"_pcre.search("123abc456def"sv));
135+
static_assert("^([0-9]+?[a-z]+?)+"_pcre.search("123abc456def"sv).template get<1>() == "123a"sv);
136+
static_assert("^([0-9]+?[a-z]+?)+"_pcre.search("123abc456def"sv).template get<0>() == "123a"sv);
137137

138138
static_assert("^([0-9]+?[a-z]++)+"_pcre.match("123abc456def"sv));
139139
static_assert("^([0-9]+?[a-z]++)+"_pcre.match("123abc456def"sv).template get<1>() == "456def"sv);

0 commit comments

Comments
 (0)