1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 description("Test the conversion performed by the function Number.prototype.toString."); 25 26 var numberIndex = 0; 27 var base10StringIndex = 1; 28 var base2StringIndex = 2; 29 var base36StringIndex = 3; 30 31 var validNumberData = [ 32 // Regular Integers: 33 [0, '0', '0', '0'], 34 [-1, '-1', '-1', '-1'], 35 [1, '1', '1', '1'], 36 [1984, '1984', '11111000000', '1j4'], 37 [-1984, '-1984', '-11111000000', '-1j4'], 38 // Limits: 39 [2147483647, '2147483647', '1111111111111111111111111111111', 'zik0zj'], // INT_MAX. 40 [-2147483648, '-2147483648', '-10000000000000000000000000000000', '-zik0zk'], // INT_MIN 41 [9007199254740992, '9007199254740992', '100000000000000000000000000000000000000000000000000000', '2gosa7pa2gw'], // Max Integer in a double. 42 [-9007199254740992, '-9007199254740992', '-100000000000000000000000000000000000000000000000000000', '-2gosa7pa2gw'], // Min Integer in a double. 43 44 // Integers represented as double. 45 [0.0, '0', '0', '0'], 46 [-1.0, '-1', '-1', '-1'], 47 [1.0, '1', '1', '1'], 48 [1984.0, '1984', '11111000000', '1j4'], 49 [-1984.0, '-1984', '-11111000000', '-1j4'], 50 // Limits: 51 [2147483647.0, '2147483647', '1111111111111111111111111111111', 'zik0zj'], // INT_MAX. 52 [-2147483648.0, '-2147483648', '-10000000000000000000000000000000', '-zik0zk'], // INT_MIN 53 [9007199254740992.0, '9007199254740992', '100000000000000000000000000000000000000000000000000000', '2gosa7pa2gw'], // Max Integer in a double. 54 [-9007199254740992.0, '-9007199254740992', '-100000000000000000000000000000000000000000000000000000', '-2gosa7pa2gw'], // Min Integer in a double. 55 56 // Double. 57 [0.1, '0.1', '0.0001100110011001100110011001100110011001100110011001101', '0.3lllllllllm'], 58 [-1.1, '-1.1', '-1.000110011001100110011001100110011001100110011001101', '-1.3llllllllm'], 59 [1.1, '1.1', '1.000110011001100110011001100110011001100110011001101', '1.3llllllllm'], 60 [1984.1, '1984.1', '11111000000.00011001100110011001100110011001100110011', '1j4.3lllllllc'], 61 [-1984.1, '-1984.1', '-11111000000.00011001100110011001100110011001100110011', '-1j4.3lllllllc'], 62 // Limits: 63 [2147483647.1, '2147483647.1', '1111111111111111111111111111111.000110011001100110011', 'zik0zj.3lllg'], 64 [-2147483648.1, '-2147483648.1', '-10000000000000000000000000000000.000110011001100110011', '-zik0zk.3lllg'], 65 [9007199254740992.1, '9007199254740992', '100000000000000000000000000000000000000000000000000000', '2gosa7pa2gw'], 66 [-9007199254740992.1, '-9007199254740992', '-100000000000000000000000000000000000000000000000000000', '-2gosa7pa2gw'], 67 ]; 68 69 for (var i = 0; i < validNumberData.length; ++i) { 70 number = validNumberData[i][numberIndex]; 71 72 // Base 10: 73 stringBase10 = validNumberData[i][base10StringIndex]; 74 shouldBeEqualToString('Number(' + number + ').toString()', stringBase10); 75 shouldBeEqualToString('Number.prototype.toString.call(' + number + ')', stringBase10); 76 shouldBeEqualToString('Number.prototype.toString.call(new Number(' + number + '))', stringBase10); 77 // Passing the string to number should also lead to valid conversion. 78 shouldBeEqualToString('Number("' + number + '").toString()', stringBase10); 79 // Passing the base explicitly. 80 shouldBeEqualToString('Number(' + number + ').toString(10)', stringBase10); 81 82 // Base 2: 83 stringBase2 = validNumberData[i][base2StringIndex]; 84 shouldBeEqualToString('Number(' + number + ').toString(2)', stringBase2); 85 shouldBeEqualToString('Number.prototype.toString.call(' + number + ', 2)', stringBase2); 86 shouldBeEqualToString('Number.prototype.toString.call(new Number(' + number + '), 2)', stringBase2); 87 88 // Base 36: 89 stringBase36 = validNumberData[i][base36StringIndex]; 90 shouldBeEqualToString('Number(' + number + ').toString(36)', stringBase36); 91 shouldBeEqualToString('Number.prototype.toString.call(' + number + ', 36)', stringBase36); 92 shouldBeEqualToString('Number.prototype.toString.call(new Number(' + number + '), 36)', stringBase36); 93 } 94 successfullyParsed = true; 95