OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/globals.h" |
| 6 |
| 7 #include "vm/isolate.h" |
| 8 #include "vm/object.h" |
| 9 #include "vm/regexp.h" |
| 10 #include "vm/unit_test.h" |
| 11 |
| 12 namespace dart { |
| 13 |
| 14 static RawArray* Match(const String& pat, const String& str) { |
| 15 Isolate* isolate = Isolate::Current(); |
| 16 const JSRegExp& regexp = JSRegExp::Handle( |
| 17 RegExpEngine::CreateJSRegExp(isolate, pat, false, false)); |
| 18 const intptr_t cid = str.GetClassId(); |
| 19 const Function& fn = Function::Handle(regexp.function(cid)); |
| 20 EXPECT(!fn.IsNull()); |
| 21 const Smi& idx = Smi::Handle(Smi::New(0)); |
| 22 return IRRegExpMacroAssembler::Execute(fn, str, idx, Isolate::Current()); |
| 23 } |
| 24 |
| 25 TEST_CASE(RegExp_OneByteString) { |
| 26 uint8_t chars[] = { 'a', 'b', 'c', 'b', 'a' }; |
| 27 intptr_t len = ARRAY_SIZE(chars); |
| 28 const String& str = String::Handle( |
| 29 OneByteString::New(chars, len, Heap::kNew)); |
| 30 |
| 31 const String& pat = String::Handle(String::New("bc")); |
| 32 const Array& res = Array::Handle(Match(pat, str)); |
| 33 EXPECT_EQ(2, res.Length()); |
| 34 |
| 35 const Object& res_1 = Object::Handle(res.At(0)); |
| 36 const Object& res_2 = Object::Handle(res.At(1)); |
| 37 EXPECT(res_1.IsSmi()); |
| 38 EXPECT(res_2.IsSmi()); |
| 39 |
| 40 const Smi& smi_1 = Smi::Cast(res_1); |
| 41 const Smi& smi_2 = Smi::Cast(res_2); |
| 42 EXPECT_EQ(1, smi_1.Value()); |
| 43 EXPECT_EQ(3, smi_2.Value()); |
| 44 } |
| 45 |
| 46 TEST_CASE(RegExp_TwoByteString) { |
| 47 uint16_t chars[] = { 'a', 'b', 'c', 'b', 'a' }; |
| 48 intptr_t len = ARRAY_SIZE(chars); |
| 49 const String& str = String::Handle( |
| 50 TwoByteString::New(chars, len, Heap::kNew)); |
| 51 |
| 52 const String& pat = String::Handle(String::New("bc")); |
| 53 const Array& res = Array::Handle(Match(pat, str)); |
| 54 EXPECT_EQ(2, res.Length()); |
| 55 |
| 56 const Object& res_1 = Object::Handle(res.At(0)); |
| 57 const Object& res_2 = Object::Handle(res.At(1)); |
| 58 EXPECT(res_1.IsSmi()); |
| 59 EXPECT(res_2.IsSmi()); |
| 60 |
| 61 const Smi& smi_1 = Smi::Cast(res_1); |
| 62 const Smi& smi_2 = Smi::Cast(res_2); |
| 63 EXPECT_EQ(1, smi_1.Value()); |
| 64 EXPECT_EQ(3, smi_2.Value()); |
| 65 } |
| 66 |
| 67 TEST_CASE(RegExp_ExternalOneByteString) { |
| 68 uint8_t chars[] = { 'a', 'b', 'c', 'b', 'a' }; |
| 69 intptr_t len = ARRAY_SIZE(chars); |
| 70 const String& str = String::Handle( |
| 71 ExternalOneByteString::New(chars, len, NULL, NULL, Heap::kNew)); |
| 72 |
| 73 const String& pat = String::Handle(String::New("bc")); |
| 74 const Array& res = Array::Handle(Match(pat, str)); |
| 75 EXPECT_EQ(2, res.Length()); |
| 76 |
| 77 const Object& res_1 = Object::Handle(res.At(0)); |
| 78 const Object& res_2 = Object::Handle(res.At(1)); |
| 79 EXPECT(res_1.IsSmi()); |
| 80 EXPECT(res_2.IsSmi()); |
| 81 |
| 82 const Smi& smi_1 = Smi::Cast(res_1); |
| 83 const Smi& smi_2 = Smi::Cast(res_2); |
| 84 EXPECT_EQ(1, smi_1.Value()); |
| 85 EXPECT_EQ(3, smi_2.Value()); |
| 86 } |
| 87 |
| 88 TEST_CASE(RegExp_ExternalTwoByteString) { |
| 89 uint16_t chars[] = { 'a', 'b', 'c', 'b', 'a' }; |
| 90 intptr_t len = ARRAY_SIZE(chars); |
| 91 const String& str = String::Handle( |
| 92 ExternalTwoByteString::New(chars, len, NULL, NULL, Heap::kNew)); |
| 93 |
| 94 const String& pat = String::Handle(String::New("bc")); |
| 95 const Array& res = Array::Handle(Match(pat, str)); |
| 96 EXPECT_EQ(2, res.Length()); |
| 97 |
| 98 const Object& res_1 = Object::Handle(res.At(0)); |
| 99 const Object& res_2 = Object::Handle(res.At(1)); |
| 100 EXPECT(res_1.IsSmi()); |
| 101 EXPECT(res_2.IsSmi()); |
| 102 |
| 103 const Smi& smi_1 = Smi::Cast(res_1); |
| 104 const Smi& smi_2 = Smi::Cast(res_2); |
| 105 EXPECT_EQ(1, smi_1.Value()); |
| 106 EXPECT_EQ(3, smi_2.Value()); |
| 107 } |
| 108 |
| 109 } // namespace dart |
OLD | NEW |