Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: test/cctest/test-regexp.cc

Issue 9638: More automaton translation (Closed)
Patch Set: Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/jsregexp.cc ('K') | « src/parser.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 25 matching lines...) Expand all
36 #include "parser.h" 36 #include "parser.h"
37 #include "ast.h" 37 #include "ast.h"
38 #include "jsregexp-inl.h" 38 #include "jsregexp-inl.h"
39 #include "assembler-re2k.h" 39 #include "assembler-re2k.h"
40 #include "interpreter-re2k.h" 40 #include "interpreter-re2k.h"
41 41
42 42
43 using namespace v8::internal; 43 using namespace v8::internal;
44 44
45 45
46 class RegExpTestCase {
47 public:
48 RegExpTestCase()
49 : pattern_(NULL),
50 flags_(NULL),
51 input_(NULL),
52 compile_error_(NULL) { }
53 RegExpTestCase(const char* pattern,
54 const char* flags,
55 const char* input,
56 const char* compile_error)
57 : pattern_(pattern),
58 flags_(flags),
59 input_(input),
60 compile_error_(compile_error) { }
61 const char* pattern() const { return pattern_; }
62 bool expect_error() const { return compile_error_ != NULL; }
63 private:
64 const char* pattern_;
65 const char* flags_;
66 const char* input_;
67 const char* compile_error_;
68 };
69
70
71 static SmartPointer<char> Parse(const char* input) { 46 static SmartPointer<char> Parse(const char* input) {
72 v8::HandleScope scope; 47 v8::HandleScope scope;
73 unibrow::Utf8InputBuffer<> buffer(input, strlen(input)); 48 unibrow::Utf8InputBuffer<> buffer(input, strlen(input));
74 ZoneScope zone_scope(DELETE_ON_EXIT); 49 ZoneScope zone_scope(DELETE_ON_EXIT);
75 Handle<String> error; 50 RegExpParseResult result;
76 RegExpTree* node = v8::internal::ParseRegExp(&buffer, &error, NULL); 51 CHECK(v8::internal::ParseRegExp(&buffer, &result));
77 CHECK(node != NULL); 52 CHECK(result.tree != NULL);
78 CHECK(error.is_null()); 53 CHECK(result.error.is_null());
79 SmartPointer<char> output = node->ToString(); 54 SmartPointer<char> output = result.tree->ToString();
80 return output; 55 return output;
81 } 56 }
82 57
83 static bool ParseEscapes(const char* input) { 58 static bool ParseEscapes(const char* input) {
84 v8::HandleScope scope; 59 v8::HandleScope scope;
85 unibrow::Utf8InputBuffer<> buffer(input, strlen(input)); 60 unibrow::Utf8InputBuffer<> buffer(input, strlen(input));
86 ZoneScope zone_scope(DELETE_ON_EXIT); 61 ZoneScope zone_scope(DELETE_ON_EXIT);
87 Handle<String> error; 62 RegExpParseResult result;
88 bool has_escapes; 63 CHECK(v8::internal::ParseRegExp(&buffer, &result));
89 RegExpTree* node = v8::internal::ParseRegExp(&buffer, &error, &has_escapes); 64 CHECK(result.tree != NULL);
90 CHECK(node != NULL); 65 CHECK(result.error.is_null());
91 CHECK(error.is_null()); 66 return result.has_character_escapes;
92 return has_escapes;
93 } 67 }
94 68
95 69
96 #define CHECK_PARSE_EQ(input, expected) CHECK_EQ(expected, *Parse(input)) 70 #define CHECK_PARSE_EQ(input, expected) CHECK_EQ(expected, *Parse(input))
97 #define CHECK_ESCAPES(input, has_escapes) CHECK_EQ(has_escapes, \ 71 #define CHECK_ESCAPES(input, has_escapes) CHECK_EQ(has_escapes, \
98 ParseEscapes(input)); 72 ParseEscapes(input));
99 73
100 TEST(Parser) { 74 TEST(Parser) {
101 V8::Initialize(NULL); 75 V8::Initialize(NULL);
102 CHECK_PARSE_EQ("abc", "'abc'"); 76 CHECK_PARSE_EQ("abc", "'abc'");
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 CHECK_ESCAPES("\\0", true); 212 CHECK_ESCAPES("\\0", true);
239 CHECK_ESCAPES("(a)\\1", false); 213 CHECK_ESCAPES("(a)\\1", false);
240 } 214 }
241 215
242 216
243 static void ExpectError(const char* input, 217 static void ExpectError(const char* input,
244 const char* expected) { 218 const char* expected) {
245 v8::HandleScope scope; 219 v8::HandleScope scope;
246 unibrow::Utf8InputBuffer<> buffer(input, strlen(input)); 220 unibrow::Utf8InputBuffer<> buffer(input, strlen(input));
247 ZoneScope zone_scope(DELETE_ON_EXIT); 221 ZoneScope zone_scope(DELETE_ON_EXIT);
248 Handle<String> error; 222 RegExpParseResult result;
249 RegExpTree* node = v8::internal::ParseRegExp(&buffer, &error, NULL); 223 CHECK_EQ(false, v8::internal::ParseRegExp(&buffer, &result));
250 CHECK(node == NULL); 224 CHECK(result.tree == NULL);
251 CHECK(!error.is_null()); 225 CHECK(!result.error.is_null());
252 SmartPointer<char> str = error->ToCString(ALLOW_NULLS); 226 SmartPointer<char> str = result.error->ToCString(ALLOW_NULLS);
253 CHECK_EQ(expected, *str); 227 CHECK_EQ(expected, *str);
254 } 228 }
255 229
256 230
257 TEST(Errors) { 231 TEST(Errors) {
258 V8::Initialize(NULL); 232 V8::Initialize(NULL);
259 const char* kEndBackslash = "\\ at end of pattern"; 233 const char* kEndBackslash = "\\ at end of pattern";
260 ExpectError("\\", kEndBackslash); 234 ExpectError("\\", kEndBackslash);
261 const char* kInvalidQuantifier = "Invalid quantifier"; 235 const char* kInvalidQuantifier = "Invalid quantifier";
262 ExpectError("a{}", kInvalidQuantifier); 236 ExpectError("a{}", kInvalidQuantifier);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 TestCharacterClassEscapes('.', Dot); 317 TestCharacterClassEscapes('.', Dot);
344 TestCharacterClassEscapes('d', IsDigit); 318 TestCharacterClassEscapes('d', IsDigit);
345 TestCharacterClassEscapes('D', NotDigit); 319 TestCharacterClassEscapes('D', NotDigit);
346 TestCharacterClassEscapes('s', IsWhiteSpace); 320 TestCharacterClassEscapes('s', IsWhiteSpace);
347 TestCharacterClassEscapes('S', NotWhiteSpace); 321 TestCharacterClassEscapes('S', NotWhiteSpace);
348 TestCharacterClassEscapes('w', IsWord); 322 TestCharacterClassEscapes('w', IsWord);
349 TestCharacterClassEscapes('W', NotWord); 323 TestCharacterClassEscapes('W', NotWord);
350 } 324 }
351 325
352 326
353 static void Execute(bool expected, const char* input, const char* str) { 327 static void Execute(const char* input,
328 const char* str,
329 bool dot_output = false) {
354 v8::HandleScope scope; 330 v8::HandleScope scope;
355 unibrow::Utf8InputBuffer<> buffer(input, strlen(input)); 331 unibrow::Utf8InputBuffer<> buffer(input, strlen(input));
356 ZoneScope zone_scope(DELETE_ON_EXIT); 332 ZoneScope zone_scope(DELETE_ON_EXIT);
357 Handle<String> error; 333 RegExpParseResult result;
358 RegExpTree* tree = v8::internal::ParseRegExp(&buffer, &error, NULL); 334 if (!v8::internal::ParseRegExp(&buffer, &result))
359 CHECK(tree != NULL); 335 return;
360 CHECK(error.is_null()); 336 RegExpNode* node = RegExpEngine::Compile(&result);
361 RegExpNode<const char>* node = RegExpEngine::Compile<const char>(tree); 337 if (dot_output) {
362 bool outcome = RegExpEngine::Execute(node, CStrVector(str)); 338 RegExpEngine::DotPrint(input, node);
363 CHECK_EQ(outcome, expected); 339 exit(0);
340 }
364 } 341 }
365 342
366 343
367 TEST(Execution) { 344 TEST(Execution) {
368 V8::Initialize(NULL); 345 V8::Initialize(NULL);
369 Execute(true, ".*?(?:a[bc]d|e[fg]h)", "xxxabbegh"); 346 Execute(".*?(?:a[bc]d|e[fg]h)", "xxxabbegh");
370 Execute(true, ".*?(?:a[bc]d|e[fg]h)", "xxxabbefh"); 347 Execute(".*?(?:a[bc]d|e[fg]h)", "xxxabbefh");
371 Execute(false, ".*?(?:a[bc]d|e[fg]h)", "xxxabbefd"); 348 Execute(".*?(?:a[bc]d|e[fg]h)", "xxxabbefd");
372 } 349 }
373 350
374 351
375 class TestConfig { 352 class TestConfig {
376 public: 353 public:
377 typedef int Key; 354 typedef int Key;
378 typedef int Value; 355 typedef int Value;
379 static const int kNoKey; 356 static const int kNoKey;
380 static const int kNoValue; 357 static const int kNoValue;
381 static inline int Compare(int a, int b) { 358 static inline int Compare(int a, int b) {
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 for (int k = 0; !is_on && (k < 2 * kRangeSize); k += 2) 472 for (int k = 0; !is_on && (k < 2 * kRangeSize); k += 2)
496 is_on = (range[k] <= p && p <= range[k + 1]); 473 is_on = (range[k] <= p && p <= range[k + 1]);
497 CHECK_EQ(is_on, outs.Get(j)); 474 CHECK_EQ(is_on, outs.Get(j));
498 } 475 }
499 } 476 }
500 } 477 }
501 478
502 479
503 TEST(Assembler) { 480 TEST(Assembler) {
504 V8::Initialize(NULL); 481 V8::Initialize(NULL);
505
506 byte codes[1024]; 482 byte codes[1024];
507 Re2kAssembler assembler(Vector<byte>(codes, 1024)); 483 Re2kAssembler assembler(Vector<byte>(codes, 1024));
508 #define __ assembler. 484 #define __ assembler.
509 Label advance; 485 Label advance;
510 Label look_for_foo; 486 Label look_for_foo;
511 __ GoTo(&look_for_foo); 487 __ GoTo(&look_for_foo);
512 __ Bind(&advance); 488 __ Bind(&advance);
513 __ AdvanceCP(); 489 __ AdvanceCP();
514 __ Bind(&look_for_foo); 490 __ Bind(&look_for_foo);
515 __ FailIfWithin(3); 491 __ FailIfWithin(3);
(...skipping 18 matching lines...) Expand all
534 Handle<String> f2 = Factory::NewStringFromAscii(CStrVector("foo bar baz")); 510 Handle<String> f2 = Factory::NewStringFromAscii(CStrVector("foo bar baz"));
535 CHECK(Re2kInterpreter::Match(*array, *f2, captures, 0)); 511 CHECK(Re2kInterpreter::Match(*array, *f2, captures, 0));
536 CHECK_EQ(0, captures[0]); 512 CHECK_EQ(0, captures[0]);
537 CHECK_EQ(2, captures[1]); 513 CHECK_EQ(2, captures[1]);
538 514
539 Handle<String> f3 = Factory::NewStringFromAscii(CStrVector("tomfoolery")); 515 Handle<String> f3 = Factory::NewStringFromAscii(CStrVector("tomfoolery"));
540 CHECK(Re2kInterpreter::Match(*array, *f3, captures, 0)); 516 CHECK(Re2kInterpreter::Match(*array, *f3, captures, 0));
541 CHECK_EQ(3, captures[0]); 517 CHECK_EQ(3, captures[0]);
542 CHECK_EQ(5, captures[1]); 518 CHECK_EQ(5, captures[1]);
543 } 519 }
544
545
546
OLDNEW
« src/jsregexp.cc ('K') | « src/parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698