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 /**
     29  * @fileoverview Check that the global escape and unescape functions work
     30  * right.
     31  */
     32 
     33 // Section B.2.1 of ECMAScript 3
     34 var unescaped = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";
     35 
     36 // Check the unescape chars are not escaped
     37 assertEquals(unescaped, escape(unescaped));
     38 // Check spaces are escaped
     39 assertEquals("%20/%20", escape(" / "));
     40 // Check that null chars are escaped and do not terminate the string
     41 assertEquals("%000", escape("\0" + "0"));
     42 // Check a unicode escape
     43 assertEquals("A%20B%u1234%00%20C", escape(String.fromCharCode(0x41, 0x20, 0x42, 0x1234, 0, 0x20, 0x43)));
     44 // Check unicode escapes have a leading zero to pad to 4 digits
     45 assertEquals("%u0123", escape(String.fromCharCode(0x123)));
     46 // Check escapes are upper case
     47 assertEquals("%uABCD", escape(String.fromCharCode(0xabcd)));
     48 assertEquals("%AB", escape(String.fromCharCode(0xab)));
     49 assertEquals("%0A", escape("\n"));
     50 
     51 // Check first 1000 chars individually for escaped/not escaped
     52 for (var i = 0; i < 1000; i++) {
     53   var s = String.fromCharCode(i);
     54   if (unescaped.indexOf(s, 0) == -1) {
     55     assertFalse(s == escape(s));
     56   } else {
     57     assertTrue(s == escape(s));
     58   }
     59 }
     60 
     61 // Check all chars up to 1000 in groups of 10 using unescape as a check
     62 for (var i = 0; i < 1000; i += 10) {
     63   var s = String.fromCharCode(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9);
     64   assertEquals(s, unescape(escape(s)));
     65 }
     66 
     67 // Benchmark
     68 var example = "Now is the time for all good men to come to the aid of the party.";
     69 example = example + String.fromCharCode(267, 0x1234, 0x6667, 0xabcd);
     70 example = example + " The quick brown fox jumps over the lazy dog."
     71 example = example + String.fromCharCode(171, 172, 173, 174, 175, 176, 178, 179);
     72 
     73 for (var i = 0; i < 3000; i++) {
     74   assertEquals(example, unescape(escape(example)));
     75 }
     76 
     77 // Check unescape can cope with upper and lower case
     78 assertEquals(unescape("%41%4A%4a"), "AJJ");
     79 
     80 // Check upper case U
     81 assertEquals("%U1234", unescape("%U1234"));
     82 
     83 // Check malformed unescapes
     84 assertEquals("%", unescape("%"));
     85 assertEquals("%4", unescape("%4"));
     86 assertEquals("%u", unescape("%u"));
     87 assertEquals("%u4", unescape("%u4"));
     88 assertEquals("%u44", unescape("%u44"));
     89 assertEquals("%u444", unescape("%u444"));
     90 assertEquals("%4z", unescape("%4z"));
     91 assertEquals("%uzzzz", unescape("%uzzzz"));
     92 assertEquals("%u4zzz", unescape("%u4zzz"));
     93 assertEquals("%u44zz", unescape("%u44zz"));
     94 assertEquals("%u444z", unescape("%u444z"));
     95 assertEquals("%4<", unescape("%4<"));
     96 assertEquals("%u<<<<", unescape("%u<<<<"));
     97 assertEquals("%u4<<<", unescape("%u4<<<"));
     98 assertEquals("%u44<<", unescape("%u44<<"));
     99 assertEquals("%u444<", unescape("%u444<"));
    100 assertEquals("foo%4<", unescape("foo%4<"));
    101 assertEquals("foo%u<<<<", unescape("foo%u<<<<"));
    102 assertEquals("foo%u4<<<", unescape("foo%u4<<<"));
    103 assertEquals("foo%u44<<", unescape("foo%u44<<"));
    104 assertEquals("foo%u444<", unescape("foo%u444<"));
    105 assertEquals("foo%4<bar", unescape("foo%4<bar"));
    106 assertEquals("foo%u<<<<bar", unescape("foo%u<<<<bar"));
    107 assertEquals("foo%u4<<<bar", unescape("foo%u4<<<bar"));
    108 assertEquals("foo%u44<<bar", unescape("foo%u44<<bar"));
    109 assertEquals("foo%u444<bar", unescape("foo%u444<bar"));
    110 assertEquals("% ", unescape("%%20"));
    111 assertEquals("%% ", unescape("%%%20"));
    112 
    113 // Unescape stress
    114 var eexample = escape(example);
    115 
    116 for (var i = 1; i < 3000; i++) {
    117   assertEquals(example, unescape(eexample));
    118 }
    119