/*===
-Infinity -Infinity string 0 
-Infinity -7 string 0 
-Infinity -6 string 0 
-Infinity -5 string 0 
-Infinity -4 string 0 
-Infinity -3 string 0 
-Infinity -2 string 0 
-Infinity -1 string 0 
-Infinity 0 string 0 
-Infinity 0 string 0 
-Infinity 1 string 1 f
-Infinity 2 string 2 fo
-Infinity 3 string 3 foo
-Infinity 4 string 4 foob
-Infinity 5 string 5 fooba
-Infinity 6 string 6 foobar
-Infinity 7 string 6 foobar
-Infinity Infinity string 6 foobar
-Infinity NaN string 0 
-7 -Infinity string 0 
-7 -7 string 0 
-7 -6 string 0 
-7 -5 string 0 
-7 -4 string 0 
-7 -3 string 0 
-7 -2 string 0 
-7 -1 string 0 
-7 0 string 0 
-7 0 string 0 
-7 1 string 1 f
-7 2 string 2 fo
-7 3 string 3 foo
-7 4 string 4 foob
-7 5 string 5 fooba
-7 6 string 6 foobar
-7 7 string 6 foobar
-7 Infinity string 6 foobar
-7 NaN string 0 
-6 -Infinity string 0 
-6 -7 string 0 
-6 -6 string 0 
-6 -5 string 0 
-6 -4 string 0 
-6 -3 string 0 
-6 -2 string 0 
-6 -1 string 0 
-6 0 string 0 
-6 0 string 0 
-6 1 string 1 f
-6 2 string 2 fo
-6 3 string 3 foo
-6 4 string 4 foob
-6 5 string 5 fooba
-6 6 string 6 foobar
-6 7 string 6 foobar
-6 Infinity string 6 foobar
-6 NaN string 0 
-5 -Infinity string 0 
-5 -7 string 0 
-5 -6 string 0 
-5 -5 string 0 
-5 -4 string 0 
-5 -3 string 0 
-5 -2 string 0 
-5 -1 string 0 
-5 0 string 0 
-5 0 string 0 
-5 1 string 1 o
-5 2 string 2 oo
-5 3 string 3 oob
-5 4 string 4 ooba
-5 5 string 5 oobar
-5 6 string 5 oobar
-5 7 string 5 oobar
-5 Infinity string 5 oobar
-5 NaN string 0 
-4 -Infinity string 0 
-4 -7 string 0 
-4 -6 string 0 
-4 -5 string 0 
-4 -4 string 0 
-4 -3 string 0 
-4 -2 string 0 
-4 -1 string 0 
-4 0 string 0 
-4 0 string 0 
-4 1 string 1 o
-4 2 string 2 ob
-4 3 string 3 oba
-4 4 string 4 obar
-4 5 string 4 obar
-4 6 string 4 obar
-4 7 string 4 obar
-4 Infinity string 4 obar
-4 NaN string 0 
-3 -Infinity string 0 
-3 -7 string 0 
-3 -6 string 0 
-3 -5 string 0 
-3 -4 string 0 
-3 -3 string 0 
-3 -2 string 0 
-3 -1 string 0 
-3 0 string 0 
-3 0 string 0 
-3 1 string 1 b
-3 2 string 2 ba
-3 3 string 3 bar
-3 4 string 3 bar
-3 5 string 3 bar
-3 6 string 3 bar
-3 7 string 3 bar
-3 Infinity string 3 bar
-3 NaN string 0 
-2 -Infinity string 0 
-2 -7 string 0 
-2 -6 string 0 
-2 -5 string 0 
-2 -4 string 0 
-2 -3 string 0 
-2 -2 string 0 
-2 -1 string 0 
-2 0 string 0 
-2 0 string 0 
-2 1 string 1 a
-2 2 string 2 ar
-2 3 string 2 ar
-2 4 string 2 ar
-2 5 string 2 ar
-2 6 string 2 ar
-2 7 string 2 ar
-2 Infinity string 2 ar
-2 NaN string 0 
-1 -Infinity string 0 
-1 -7 string 0 
-1 -6 string 0 
-1 -5 string 0 
-1 -4 string 0 
-1 -3 string 0 
-1 -2 string 0 
-1 -1 string 0 
-1 0 string 0 
-1 0 string 0 
-1 1 string 1 r
-1 2 string 1 r
-1 3 string 1 r
-1 4 string 1 r
-1 5 string 1 r
-1 6 string 1 r
-1 7 string 1 r
-1 Infinity string 1 r
-1 NaN string 0 
0 -Infinity string 0 
0 -7 string 0 
0 -6 string 0 
0 -5 string 0 
0 -4 string 0 
0 -3 string 0 
0 -2 string 0 
0 -1 string 0 
0 0 string 0 
0 0 string 0 
0 1 string 1 f
0 2 string 2 fo
0 3 string 3 foo
0 4 string 4 foob
0 5 string 5 fooba
0 6 string 6 foobar
0 7 string 6 foobar
0 Infinity string 6 foobar
0 NaN string 0 
0 -Infinity string 0 
0 -7 string 0 
0 -6 string 0 
0 -5 string 0 
0 -4 string 0 
0 -3 string 0 
0 -2 string 0 
0 -1 string 0 
0 0 string 0 
0 0 string 0 
0 1 string 1 f
0 2 string 2 fo
0 3 string 3 foo
0 4 string 4 foob
0 5 string 5 fooba
0 6 string 6 foobar
0 7 string 6 foobar
0 Infinity string 6 foobar
0 NaN string 0 
1 -Infinity string 0 
1 -7 string 0 
1 -6 string 0 
1 -5 string 0 
1 -4 string 0 
1 -3 string 0 
1 -2 string 0 
1 -1 string 0 
1 0 string 0 
1 0 string 0 
1 1 string 1 o
1 2 string 2 oo
1 3 string 3 oob
1 4 string 4 ooba
1 5 string 5 oobar
1 6 string 5 oobar
1 7 string 5 oobar
1 Infinity string 5 oobar
1 NaN string 0 
2 -Infinity string 0 
2 -7 string 0 
2 -6 string 0 
2 -5 string 0 
2 -4 string 0 
2 -3 string 0 
2 -2 string 0 
2 -1 string 0 
2 0 string 0 
2 0 string 0 
2 1 string 1 o
2 2 string 2 ob
2 3 string 3 oba
2 4 string 4 obar
2 5 string 4 obar
2 6 string 4 obar
2 7 string 4 obar
2 Infinity string 4 obar
2 NaN string 0 
3 -Infinity string 0 
3 -7 string 0 
3 -6 string 0 
3 -5 string 0 
3 -4 string 0 
3 -3 string 0 
3 -2 string 0 
3 -1 string 0 
3 0 string 0 
3 0 string 0 
3 1 string 1 b
3 2 string 2 ba
3 3 string 3 bar
3 4 string 3 bar
3 5 string 3 bar
3 6 string 3 bar
3 7 string 3 bar
3 Infinity string 3 bar
3 NaN string 0 
4 -Infinity string 0 
4 -7 string 0 
4 -6 string 0 
4 -5 string 0 
4 -4 string 0 
4 -3 string 0 
4 -2 string 0 
4 -1 string 0 
4 0 string 0 
4 0 string 0 
4 1 string 1 a
4 2 string 2 ar
4 3 string 2 ar
4 4 string 2 ar
4 5 string 2 ar
4 6 string 2 ar
4 7 string 2 ar
4 Infinity string 2 ar
4 NaN string 0 
5 -Infinity string 0 
5 -7 string 0 
5 -6 string 0 
5 -5 string 0 
5 -4 string 0 
5 -3 string 0 
5 -2 string 0 
5 -1 string 0 
5 0 string 0 
5 0 string 0 
5 1 string 1 r
5 2 string 1 r
5 3 string 1 r
5 4 string 1 r
5 5 string 1 r
5 6 string 1 r
5 7 string 1 r
5 Infinity string 1 r
5 NaN string 0 
6 -Infinity string 0 
6 -7 string 0 
6 -6 string 0 
6 -5 string 0 
6 -4 string 0 
6 -3 string 0 
6 -2 string 0 
6 -1 string 0 
6 0 string 0 
6 0 string 0 
6 1 string 0 
6 2 string 0 
6 3 string 0 
6 4 string 0 
6 5 string 0 
6 6 string 0 
6 7 string 0 
6 Infinity string 0 
6 NaN string 0 
7 -Infinity string 0 
7 -7 string 0 
7 -6 string 0 
7 -5 string 0 
7 -4 string 0 
7 -3 string 0 
7 -2 string 0 
7 -1 string 0 
7 0 string 0 
7 0 string 0 
7 1 string 0 
7 2 string 0 
7 3 string 0 
7 4 string 0 
7 5 string 0 
7 6 string 0 
7 7 string 0 
7 Infinity string 0 
7 NaN string 0 
Infinity -Infinity string 0 
Infinity -7 string 0 
Infinity -6 string 0 
Infinity -5 string 0 
Infinity -4 string 0 
Infinity -3 string 0 
Infinity -2 string 0 
Infinity -1 string 0 
Infinity 0 string 0 
Infinity 0 string 0 
Infinity 1 string 0 
Infinity 2 string 0 
Infinity 3 string 0 
Infinity 4 string 0 
Infinity 5 string 0 
Infinity 6 string 0 
Infinity 7 string 0 
Infinity Infinity string 0 
Infinity NaN string 0 
NaN -Infinity string 0 
NaN -7 string 0 
NaN -6 string 0 
NaN -5 string 0 
NaN -4 string 0 
NaN -3 string 0 
NaN -2 string 0 
NaN -1 string 0 
NaN 0 string 0 
NaN 0 string 0 
NaN 1 string 1 f
NaN 2 string 2 fo
NaN 3 string 3 foo
NaN 4 string 4 foob
NaN 5 string 5 fooba
NaN 6 string 6 foobar
NaN 7 string 6 foobar
NaN Infinity string 6 foobar
NaN NaN string 0 
===*/

function basicTest() {
    var numbers = [
        Number.NEGATIVE_INFINITY, -7, -6, -5, -4, -3, -2, -1,
        -0, +0, 1, 2, 3, 4, 5, 6, 7, Number.POSITIVE_INFINITY, Number.NaN
    ];
    var i, j;

    function test(str, start, length) {
        var t = str.substr(start, length);
        print(start, length, typeof t, t.length, t);
    }

    for (i = 0; i < numbers.length; i++) {
        for (j = 0; j < numbers.length; j++) {
            test('foobar', numbers[i], numbers[j]);
        }
    }
}

try {
    basicTest();
} catch (e) {
    print(e);
}

/*===
string 2 51966 98
===*/

/* Non-BMP test. */

function nonBmpTest() {
    var str = new String('foo\u1234\ucafebar');
    var t;

    t = str.substr(4, 2);
    print(typeof t, t.length, t.charCodeAt(0), t.charCodeAt(1));
}

try {
    nonBmpTest();
} catch (e) {
    print(e);
}

/*===
string 3 def
string 2 ll
string 2 ue
string 3 lse
string 3 345
string 3 oba
string 3 2,3
string 3 bje
toString() this
valueOf() start
valueOf() end
string 3 bar
===*/

function coercionTest() {
    function test(str, start, length) {
        var t;

        try {
            t = String.prototype.substr.call(str, start, length);
            print(typeof t, t.length, t);
        } catch (e) {
            print(e.name);
        }
    }

    test(undefined, 2, 3);  // unlike substring(), coerces to 'undefined'
    test(null, 2, 3);       // unlike substring(), coerces to 'null'
    test(true, 2, 3);
    test(false, 2, 3);
    test(12345, 2, 3);
    test('foobar', 2, 3);
    test([1,2,3,4], 2, 3);
    test({ foo: 1, bar: 2}, 2, 3);

    test({ toString: function() { print('toString() this'); return 'foobar' },
           valueOf: function() { print('valueOf() this'); return 'barfoo' } },
         { toString: function() { print('toString() start'); return 2; },
           valueOf: function() { print('valueOf() start'); return 3; } },
         { toString: function() { print('toString() end'); return 4; },
           valueOf: function() { print('valueOf() end'); return 5; } });
}

try {
    coercionTest();
} catch (e) {
    print(e);
}

