Home | History | Annotate | Download | only in mjsunit
      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 
     37 assertTrue(/^bar/.test("bar"));
     38 assertTrue(/^bar/.test("bar\nfoo"));
     39 assertFalse(/^bar/.test("foo\nbar"));
     40 assertTrue(/^bar/m.test("bar"));
     41 assertTrue(/^bar/m.test("bar\nfoo"));
     42 assertTrue(/^bar/m.test("foo\nbar"));
     43 
     44 assertTrue(/bar$/.test("bar"));
     45 assertFalse(/bar$/.test("bar\nfoo"));
     46 assertTrue(/bar$/.test("foo\nbar"));
     47 assertTrue(/bar$/m.test("bar"));
     48 assertTrue(/bar$/m.test("bar\nfoo"));
     49 assertTrue(/bar$/m.test("foo\nbar"));
     50 
     51 assertFalse(/^bxr/.test("bar"));
     52 assertFalse(/^bxr/.test("bar\nfoo"));
     53 assertFalse(/^bxr/m.test("bar"));
     54 assertFalse(/^bxr/m.test("bar\nfoo"));
     55 assertFalse(/^bxr/m.test("foo\nbar"));
     56 
     57 assertFalse(/bxr$/.test("bar"));
     58 assertFalse(/bxr$/.test("foo\nbar"));
     59 assertFalse(/bxr$/m.test("bar"));
     60 assertFalse(/bxr$/m.test("bar\nfoo"));
     61 assertFalse(/bxr$/m.test("foo\nbar"));
     62 
     63 
     64 assertTrue(/^.*$/.test(""));
     65 assertTrue(/^.*$/.test("foo"));
     66 assertFalse(/^.*$/.test("\n"));
     67 assertTrue(/^.*$/m.test("\n"));
     68 
     69 assertTrue(/^[\s]*$/.test(" "));
     70 assertTrue(/^[\s]*$/.test("\n"));
     71 
     72 assertTrue(/^[^]*$/.test(""));
     73 assertTrue(/^[^]*$/.test("foo"));
     74 assertTrue(/^[^]*$/.test("\n"));
     75 
     76 assertTrue(/^([()\s]|.)*$/.test("()\n()"));
     77 assertTrue(/^([()\n]|.)*$/.test("()\n()"));
     78 assertFalse(/^([()]|.)*$/.test("()\n()"));
     79 assertTrue(/^([()]|.)*$/m.test("()\n()"));
     80 assertTrue(/^([()]|.)*$/m.test("()\n"));
     81 assertTrue(/^[()]*$/m.test("()\n."));
     82 
     83 assertTrue(/^[\].]*$/.test("...]..."));
     84 
     85 
     86 function 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 
    109 check_case("a", "A");
    110 // Aring
    111 check_case(String.fromCharCode(229), String.fromCharCode(197));
    112 // Russian G
    113 check_case(String.fromCharCode(0x413), String.fromCharCode(0x433));
    114 
    115 
    116 assertThrows("a = new RegExp('[z-a]');");
    117