1// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// The flags below are to test the trace-calls functionality and the
29// preallocated meessage memory.
30// Flags: --trace-calls --preallocate-message-memory
31
32/**
33 * @fileoverview Check that various regexp constructs work as intended.
34 * Particularly those regexps that use ^ and $.
35 */
36
37assertTrue(/^bar/.test("bar"));
38assertTrue(/^bar/.test("bar\nfoo"));
39assertFalse(/^bar/.test("foo\nbar"));
40assertTrue(/^bar/m.test("bar"));
41assertTrue(/^bar/m.test("bar\nfoo"));
42assertTrue(/^bar/m.test("foo\nbar"));
43
44assertTrue(/bar$/.test("bar"));
45assertFalse(/bar$/.test("bar\nfoo"));
46assertTrue(/bar$/.test("foo\nbar"));
47assertTrue(/bar$/m.test("bar"));
48assertTrue(/bar$/m.test("bar\nfoo"));
49assertTrue(/bar$/m.test("foo\nbar"));
50
51assertFalse(/^bxr/.test("bar"));
52assertFalse(/^bxr/.test("bar\nfoo"));
53assertFalse(/^bxr/m.test("bar"));
54assertFalse(/^bxr/m.test("bar\nfoo"));
55assertFalse(/^bxr/m.test("foo\nbar"));
56
57assertFalse(/bxr$/.test("bar"));
58assertFalse(/bxr$/.test("foo\nbar"));
59assertFalse(/bxr$/m.test("bar"));
60assertFalse(/bxr$/m.test("bar\nfoo"));
61assertFalse(/bxr$/m.test("foo\nbar"));
62
63
64assertTrue(/^.*$/.test(""));
65assertTrue(/^.*$/.test("foo"));
66assertFalse(/^.*$/.test("\n"));
67assertTrue(/^.*$/m.test("\n"));
68
69assertTrue(/^[\s]*$/.test(" "));
70assertTrue(/^[\s]*$/.test("\n"));
71
72assertTrue(/^[^]*$/.test(""));
73assertTrue(/^[^]*$/.test("foo"));
74assertTrue(/^[^]*$/.test("\n"));
75
76assertTrue(/^([()\s]|.)*$/.test("()\n()"));
77assertTrue(/^([()\n]|.)*$/.test("()\n()"));
78assertFalse(/^([()]|.)*$/.test("()\n()"));
79assertTrue(/^([()]|.)*$/m.test("()\n()"));
80assertTrue(/^([()]|.)*$/m.test("()\n"));
81assertTrue(/^[()]*$/m.test("()\n."));
82
83assertTrue(/^[\].]*$/.test("...]..."));
84
85
86function check_case(lc, uc) {
87  var a = new RegExp("^" + lc + "$");
88  assertFalse(a.test(uc));
89  a = new RegExp("^" + lc + "$", "i");
90  assertTrue(a.test(uc));
91
92  var A = new RegExp("^" + uc + "$");
93  assertFalse(A.test(lc));
94  A = new RegExp("^" + uc + "$", "i");
95  assertTrue(A.test(lc));
96
97  a = new RegExp("^[" + lc + "]$");
98  assertFalse(a.test(uc));
99  a = new RegExp("^[" + lc + "]$", "i");
100  assertTrue(a.test(uc));
101
102  A = new RegExp("^[" + uc + "]$");
103  assertFalse(A.test(lc));
104  A = new RegExp("^[" + uc + "]$", "i");
105  assertTrue(A.test(lc));
106}
107
108
109check_case("a", "A");
110// Aring
111check_case(String.fromCharCode(229), String.fromCharCode(197));
112// Russian G
113check_case(String.fromCharCode(0x413), String.fromCharCode(0x433));
114
115
116assertThrows("a = new RegExp('[z-a]');");
117