js/src/jit-test/tests/basic/string-endswith.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/basic/string-endswith.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,262 @@
     1.4 +/*
     1.5 +* Copyright (c) 2013 Mathias Bynens. All rights reserved.
     1.6 +*
     1.7 +* Permission is hereby granted, free of charge, to any person obtaining a
     1.8 +* copy of this software and associated documentation files (the "Software"),
     1.9 +* to deal in the Software without restriction, including without limitation
    1.10 +* the rights to use, copy, modify, merge, publish, distribute, sublicense,
    1.11 +* and/or sell copies of the Software, and to permit persons to whom the
    1.12 +* Software is furnished to do so, subject to the following conditions:
    1.13 +*
    1.14 +* The above copyright notice and this permission notice shall be included in
    1.15 +* all copies or substantial portions of the Software.
    1.16 +*
    1.17 +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.18 +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.19 +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.20 +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.21 +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    1.22 +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    1.23 +* DEALINGS IN THE SOFTWARE.
    1.24 +*/
    1.25 +
    1.26 +function assertThrows(fun, errorType) {
    1.27 +  try {
    1.28 +    fun();
    1.29 +    assertEq(true, false, "Expected error, but none was thrown");
    1.30 +  } catch (e) {
    1.31 +    assertEq(e instanceof errorType, true, "Wrong error type thrown");
    1.32 +  }
    1.33 +}
    1.34 +assertEq(String.prototype.endsWith.length, 1);
    1.35 +assertEq(String.prototype.propertyIsEnumerable('endsWith'), false);
    1.36 +
    1.37 +assertEq('undefined'.endsWith(), true);
    1.38 +assertEq('undefined'.endsWith(undefined), true);
    1.39 +assertEq('undefined'.endsWith(null), false);
    1.40 +assertEq('null'.endsWith(), false);
    1.41 +assertEq('null'.endsWith(undefined), false);
    1.42 +assertEq('null'.endsWith(null), true);
    1.43 +
    1.44 +assertEq('abc'.endsWith(), false);
    1.45 +assertEq('abc'.endsWith(''), true);
    1.46 +assertEq('abc'.endsWith('\0'), false);
    1.47 +assertEq('abc'.endsWith('c'), true);
    1.48 +assertEq('abc'.endsWith('b'), false);
    1.49 +assertEq('abc'.endsWith('a'), false);
    1.50 +assertEq('abc'.endsWith('ab'), false);
    1.51 +assertEq('abc'.endsWith('bc'), true);
    1.52 +assertEq('abc'.endsWith('abc'), true);
    1.53 +assertEq('abc'.endsWith('bcd'), false);
    1.54 +assertEq('abc'.endsWith('abcd'), false);
    1.55 +assertEq('abc'.endsWith('bcde'), false);
    1.56 +
    1.57 +assertEq('abc'.endsWith('', NaN), true);
    1.58 +assertEq('abc'.endsWith('\0', NaN), false);
    1.59 +assertEq('abc'.endsWith('c', NaN), false);
    1.60 +assertEq('abc'.endsWith('b', NaN), false);
    1.61 +assertEq('abc'.endsWith('a', NaN), false);
    1.62 +assertEq('abc'.endsWith('ab', NaN), false);
    1.63 +assertEq('abc'.endsWith('bc', NaN), false);
    1.64 +assertEq('abc'.endsWith('abc', NaN), false);
    1.65 +assertEq('abc'.endsWith('bcd', NaN), false);
    1.66 +assertEq('abc'.endsWith('abcd', NaN), false);
    1.67 +assertEq('abc'.endsWith('bcde', NaN), false);
    1.68 +
    1.69 +assertEq('abc'.endsWith('', false), true);
    1.70 +assertEq('abc'.endsWith('\0', false), false);
    1.71 +assertEq('abc'.endsWith('c', false), false);
    1.72 +assertEq('abc'.endsWith('b', false), false);
    1.73 +assertEq('abc'.endsWith('a', false), false);
    1.74 +assertEq('abc'.endsWith('ab', false), false);
    1.75 +assertEq('abc'.endsWith('bc', false), false);
    1.76 +assertEq('abc'.endsWith('abc', false), false);
    1.77 +assertEq('abc'.endsWith('bcd', false), false);
    1.78 +assertEq('abc'.endsWith('abcd', false), false);
    1.79 +assertEq('abc'.endsWith('bcde', false), false);
    1.80 +
    1.81 +assertEq('abc'.endsWith('', undefined), true);
    1.82 +assertEq('abc'.endsWith('\0', undefined), false);
    1.83 +assertEq('abc'.endsWith('c', undefined), true);
    1.84 +assertEq('abc'.endsWith('b', undefined), false);
    1.85 +assertEq('abc'.endsWith('a', undefined), false);
    1.86 +assertEq('abc'.endsWith('ab', undefined), false);
    1.87 +assertEq('abc'.endsWith('bc', undefined), true);
    1.88 +assertEq('abc'.endsWith('abc', undefined), true);
    1.89 +assertEq('abc'.endsWith('bcd', undefined), false);
    1.90 +assertEq('abc'.endsWith('abcd', undefined), false);
    1.91 +assertEq('abc'.endsWith('bcde', undefined), false);
    1.92 +
    1.93 +assertEq('abc'.endsWith('', null), true);
    1.94 +assertEq('abc'.endsWith('\0', null), false);
    1.95 +assertEq('abc'.endsWith('c', null), false);
    1.96 +assertEq('abc'.endsWith('b', null), false);
    1.97 +assertEq('abc'.endsWith('a', null), false);
    1.98 +assertEq('abc'.endsWith('ab', null), false);
    1.99 +assertEq('abc'.endsWith('bc', null), false);
   1.100 +assertEq('abc'.endsWith('abc', null), false);
   1.101 +assertEq('abc'.endsWith('bcd', null), false);
   1.102 +assertEq('abc'.endsWith('abcd', null), false);
   1.103 +assertEq('abc'.endsWith('bcde', null), false);
   1.104 +
   1.105 +assertEq('abc'.endsWith('', -Infinity), true);
   1.106 +assertEq('abc'.endsWith('\0', -Infinity), false);
   1.107 +assertEq('abc'.endsWith('c', -Infinity), false);
   1.108 +assertEq('abc'.endsWith('b', -Infinity), false);
   1.109 +assertEq('abc'.endsWith('a', -Infinity), false);
   1.110 +assertEq('abc'.endsWith('ab', -Infinity), false);
   1.111 +assertEq('abc'.endsWith('bc', -Infinity), false);
   1.112 +assertEq('abc'.endsWith('abc', -Infinity), false);
   1.113 +assertEq('abc'.endsWith('bcd', -Infinity), false);
   1.114 +assertEq('abc'.endsWith('abcd', -Infinity), false);
   1.115 +assertEq('abc'.endsWith('bcde', -Infinity), false);
   1.116 +
   1.117 +assertEq('abc'.endsWith('', -1), true);
   1.118 +assertEq('abc'.endsWith('\0', -1), false);
   1.119 +assertEq('abc'.endsWith('c', -1), false);
   1.120 +assertEq('abc'.endsWith('b', -1), false);
   1.121 +assertEq('abc'.endsWith('a', -1), false);
   1.122 +assertEq('abc'.endsWith('ab', -1), false);
   1.123 +assertEq('abc'.endsWith('bc', -1), false);
   1.124 +assertEq('abc'.endsWith('abc', -1), false);
   1.125 +assertEq('abc'.endsWith('bcd', -1), false);
   1.126 +assertEq('abc'.endsWith('abcd', -1), false);
   1.127 +assertEq('abc'.endsWith('bcde', -1), false);
   1.128 +
   1.129 +assertEq('abc'.endsWith('', -0), true);
   1.130 +assertEq('abc'.endsWith('\0', -0), false);
   1.131 +assertEq('abc'.endsWith('c', -0), false);
   1.132 +assertEq('abc'.endsWith('b', -0), false);
   1.133 +assertEq('abc'.endsWith('a', -0), false);
   1.134 +assertEq('abc'.endsWith('ab', -0), false);
   1.135 +assertEq('abc'.endsWith('bc', -0), false);
   1.136 +assertEq('abc'.endsWith('abc', -0), false);
   1.137 +assertEq('abc'.endsWith('bcd', -0), false);
   1.138 +assertEq('abc'.endsWith('abcd', -0), false);
   1.139 +assertEq('abc'.endsWith('bcde', -0), false);
   1.140 +
   1.141 +assertEq('abc'.endsWith('', +0), true);
   1.142 +assertEq('abc'.endsWith('\0', +0), false);
   1.143 +assertEq('abc'.endsWith('c', +0), false);
   1.144 +assertEq('abc'.endsWith('b', +0), false);
   1.145 +assertEq('abc'.endsWith('a', +0), false);
   1.146 +assertEq('abc'.endsWith('ab', +0), false);
   1.147 +assertEq('abc'.endsWith('bc', +0), false);
   1.148 +assertEq('abc'.endsWith('abc', +0), false);
   1.149 +assertEq('abc'.endsWith('bcd', +0), false);
   1.150 +assertEq('abc'.endsWith('abcd', +0), false);
   1.151 +assertEq('abc'.endsWith('bcde', +0), false);
   1.152 +
   1.153 +assertEq('abc'.endsWith('', 1), true);
   1.154 +assertEq('abc'.endsWith('\0', 1), false);
   1.155 +assertEq('abc'.endsWith('c', 1), false);
   1.156 +assertEq('abc'.endsWith('b', 1), false);
   1.157 +assertEq('abc'.endsWith('ab', 1), false);
   1.158 +assertEq('abc'.endsWith('bc', 1), false);
   1.159 +assertEq('abc'.endsWith('abc', 1), false);
   1.160 +assertEq('abc'.endsWith('bcd', 1), false);
   1.161 +assertEq('abc'.endsWith('abcd', 1), false);
   1.162 +assertEq('abc'.endsWith('bcde', 1), false);
   1.163 +
   1.164 +assertEq('abc'.endsWith('', 2), true);
   1.165 +assertEq('abc'.endsWith('\0', 2), false);
   1.166 +assertEq('abc'.endsWith('c', 2), false);
   1.167 +assertEq('abc'.endsWith('b', 2), true);
   1.168 +assertEq('abc'.endsWith('ab', 2), true);
   1.169 +assertEq('abc'.endsWith('bc', 2), false);
   1.170 +assertEq('abc'.endsWith('abc', 2), false);
   1.171 +assertEq('abc'.endsWith('bcd', 2), false);
   1.172 +assertEq('abc'.endsWith('abcd', 2), false);
   1.173 +assertEq('abc'.endsWith('bcde', 2), false);
   1.174 +
   1.175 +assertEq('abc'.endsWith('', +Infinity), true);
   1.176 +assertEq('abc'.endsWith('\0', +Infinity), false);
   1.177 +assertEq('abc'.endsWith('c', +Infinity), true);
   1.178 +assertEq('abc'.endsWith('b', +Infinity), false);
   1.179 +assertEq('abc'.endsWith('a', +Infinity), false);
   1.180 +assertEq('abc'.endsWith('ab', +Infinity), false);
   1.181 +assertEq('abc'.endsWith('bc', +Infinity), true);
   1.182 +assertEq('abc'.endsWith('abc', +Infinity), true);
   1.183 +assertEq('abc'.endsWith('bcd', +Infinity), false);
   1.184 +assertEq('abc'.endsWith('abcd', +Infinity), false);
   1.185 +assertEq('abc'.endsWith('bcde', +Infinity), false);
   1.186 +
   1.187 +assertEq('abc'.endsWith('', true), true);
   1.188 +assertEq('abc'.endsWith('\0', true), false);
   1.189 +assertEq('abc'.endsWith('c', true), false);
   1.190 +assertEq('abc'.endsWith('b', true), false);
   1.191 +assertEq('abc'.endsWith('ab', true), false);
   1.192 +assertEq('abc'.endsWith('bc', true), false);
   1.193 +assertEq('abc'.endsWith('abc', true), false);
   1.194 +assertEq('abc'.endsWith('bcd', true), false);
   1.195 +assertEq('abc'.endsWith('abcd', true), false);
   1.196 +assertEq('abc'.endsWith('bcde', true), false);
   1.197 +
   1.198 +assertEq('abc'.endsWith('', 'x'), true);
   1.199 +assertEq('abc'.endsWith('\0', 'x'), false);
   1.200 +assertEq('abc'.endsWith('c', 'x'), false);
   1.201 +assertEq('abc'.endsWith('b', 'x'), false);
   1.202 +assertEq('abc'.endsWith('a', 'x'), false);
   1.203 +assertEq('abc'.endsWith('ab', 'x'), false);
   1.204 +assertEq('abc'.endsWith('bc', 'x'), false);
   1.205 +assertEq('abc'.endsWith('abc', 'x'), false);
   1.206 +assertEq('abc'.endsWith('bcd', 'x'), false);
   1.207 +assertEq('abc'.endsWith('abcd', 'x'), false);
   1.208 +assertEq('abc'.endsWith('bcde', 'x'), false);
   1.209 +
   1.210 +assertEq('[a-z]+(bar)?'.endsWith('(bar)?'), true);
   1.211 +assertThrows(function() { '[a-z]+(bar)?'.endsWith(/(bar)?/); }, TypeError);
   1.212 +assertEq('[a-z]+(bar)?'.endsWith('[a-z]+', 6), true);
   1.213 +assertThrows(function() { '[a-z]+(bar)?'.endsWith(/(bar)?/); }, TypeError);
   1.214 +assertThrows(function() { '[a-z]+/(bar)?/'.endsWith(/(bar)?/); }, TypeError);
   1.215 +var global = newGlobal();
   1.216 +global.eval('this.re = /(bar)?/');
   1.217 +assertThrows(function() { '[a-z]+/(bar)?/'.endsWith(global.re); }, TypeError);
   1.218 +
   1.219 +// http://mathiasbynens.be/notes/javascript-unicode#poo-test
   1.220 +var string = 'I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9';
   1.221 +assertEq(string.endsWith(''), true);
   1.222 +assertEq(string.endsWith('\xF1t\xEBr'), false);
   1.223 +assertEq(string.endsWith('\xF1t\xEBr', 5), true);
   1.224 +assertEq(string.endsWith('\xE0liz\xE6'), false);
   1.225 +assertEq(string.endsWith('\xE0liz\xE6', 16), true);
   1.226 +assertEq(string.endsWith('\xF8n\u2603\uD83D\uDCA9'), true);
   1.227 +assertEq(string.endsWith('\xF8n\u2603\uD83D\uDCA9', 23), true);
   1.228 +assertEq(string.endsWith('\u2603'), false);
   1.229 +assertEq(string.endsWith('\u2603', 21), true);
   1.230 +assertEq(string.endsWith('\uD83D\uDCA9'), true);
   1.231 +assertEq(string.endsWith('\uD83D\uDCA9', 23), true);
   1.232 +
   1.233 +assertThrows(function() { String.prototype.endsWith.call(undefined); }, TypeError);
   1.234 +assertThrows(function() { String.prototype.endsWith.call(undefined, 'b'); }, TypeError);
   1.235 +assertThrows(function() { String.prototype.endsWith.call(undefined, 'b', 4); }, TypeError);
   1.236 +assertThrows(function() { String.prototype.endsWith.call(null); }, TypeError);
   1.237 +assertThrows(function() { String.prototype.endsWith.call(null, 'b'); }, TypeError);
   1.238 +assertThrows(function() { String.prototype.endsWith.call(null, 'b', 4); }, TypeError);
   1.239 +assertEq(String.prototype.endsWith.call(42, '2'), true);
   1.240 +assertEq(String.prototype.endsWith.call(42, '4'), false);
   1.241 +assertEq(String.prototype.endsWith.call(42, 'b', 4), false);
   1.242 +assertEq(String.prototype.endsWith.call(42, '2', 1), false);
   1.243 +assertEq(String.prototype.endsWith.call(42, '2', 4), true);
   1.244 +assertEq(String.prototype.endsWith.call({ 'toString': function() { return 'abc'; } }, 'b', 0), false);
   1.245 +assertEq(String.prototype.endsWith.call({ 'toString': function() { return 'abc'; } }, 'b', 1), false);
   1.246 +assertEq(String.prototype.endsWith.call({ 'toString': function() { return 'abc'; } }, 'b', 2), true);
   1.247 +assertThrows(function() { String.prototype.endsWith.call({ 'toString': function() { throw RangeError(); } }, /./); }, RangeError);
   1.248 +assertThrows(function() { String.prototype.endsWith.call({ 'toString': function() { return 'abc' } }, /./); }, TypeError);
   1.249 +
   1.250 +assertThrows(function() { String.prototype.endsWith.apply(undefined); }, TypeError);
   1.251 +assertThrows(function() { String.prototype.endsWith.apply(undefined, ['b']); }, TypeError);
   1.252 +assertThrows(function() { String.prototype.endsWith.apply(undefined, ['b', 4]); }, TypeError);
   1.253 +assertThrows(function() { String.prototype.endsWith.apply(null); }, TypeError);
   1.254 +assertThrows(function() { String.prototype.endsWith.apply(null, ['b']); }, TypeError);
   1.255 +assertThrows(function() { String.prototype.endsWith.apply(null, ['b', 4]); }, TypeError);
   1.256 +assertEq(String.prototype.endsWith.apply(42, ['2']), true);
   1.257 +assertEq(String.prototype.endsWith.apply(42, ['4']), false);
   1.258 +assertEq(String.prototype.endsWith.apply(42, ['b', 4]), false);
   1.259 +assertEq(String.prototype.endsWith.apply(42, ['2', 1]), false);
   1.260 +assertEq(String.prototype.endsWith.apply(42, ['2', 4]), true);
   1.261 +assertEq(String.prototype.endsWith.apply({ 'toString': function() { return 'abc'; } }, ['b', 0]), false);
   1.262 +assertEq(String.prototype.endsWith.apply({ 'toString': function() { return 'abc'; } }, ['b', 1]), false);
   1.263 +assertEq(String.prototype.endsWith.apply({ 'toString': function() { return 'abc'; } }, ['b', 2]), true);
   1.264 +assertThrows(function() { String.prototype.endsWith.apply({ 'toString': function() { throw RangeError(); } }, [/./]); }, RangeError);
   1.265 +assertThrows(function() { String.prototype.endsWith.apply({ 'toString': function() { return 'abc' } }, [/./]); }, TypeError);

mercurial