1 // Copyright 2009 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 // Test pre- and postfix count operations. 29 30 // Test value context. 31 var a = 42; 32 var b = {x:42}; 33 var c = "x"; 34 assertEquals(43, ++a); 35 assertEquals(43, a); 36 assertEquals(43, a++); 37 assertEquals(44, a); 38 assertEquals(43, ++b.x); 39 assertEquals(43, b.x); 40 assertEquals(43, b.x++); 41 assertEquals(44, b.x); 42 assertEquals(45, ++b[c]); 43 assertEquals(45, b[c]); 44 assertEquals(45, b[c]++); 45 assertEquals(46, b[c]); 46 47 // Test effect context. 48 a = 42; 49 b = {x:42}; 50 c = "x"; 51 assertEquals(1, eval("++a; 1")); 52 assertEquals(43, a); 53 assertEquals(1, eval("a++; 1")); 54 assertEquals(44, a); 55 assertEquals(1, eval("++b.x; 1")); 56 assertEquals(43, b.x); 57 assertEquals(1, eval("b.x++; 1")); 58 assertEquals(44, b.x); 59 assertEquals(1, eval("++b[c]; 1")); 60 assertEquals(45, b[c]); 61 assertEquals(1, eval("b[c]++; 1")); 62 assertEquals(46, b[c]); 63 64 // Test test context. 65 a = 42; 66 b = {x:42}; 67 c = "x"; 68 assertEquals(1, (++a) ? 1 : 0); 69 assertEquals(43, a); 70 assertEquals(1, (a++) ? 1 : 0); 71 assertEquals(44, a); 72 assertEquals(1, (++b.x) ? 1 : 0); 73 assertEquals(43, b.x); 74 assertEquals(1, (b.x++) ? 1 : 0); 75 assertEquals(44, b.x); 76 assertEquals(1, (++b[c]) ? 1 : 0); 77 assertEquals(45, b[c]); 78 assertEquals(1, (b[c]++) ? 1 : 0); 79 assertEquals(46, b[c]); 80 81 // Test value/test and test/value contexts. 82 a = 42; 83 b = {x:42}; 84 c = "x"; 85 assertEquals(43, ++a || 1); 86 assertEquals(43, a); 87 assertEquals(43, a++ || 1); 88 assertEquals(44, a); 89 assertEquals(43, ++b.x || 1); 90 assertEquals(43, b.x); 91 assertEquals(43, (b.x++) || 1); 92 assertEquals(44, b.x); 93 assertEquals(45, ++b[c] || 1); 94 assertEquals(45, b[c]); 95 assertEquals(45, b[c]++ || 1); 96 assertEquals(46, b[c]); 97 a = 42; 98 b = {x:42}; 99 c = "x"; 100 assertEquals(1, ++a && 1); 101 assertEquals(43, a); 102 assertEquals(1, a++ && 1); 103 assertEquals(44, a); 104 assertEquals(1, ++b.x && 1); 105 assertEquals(43, b.x); 106 assertEquals(1, (b.x++) && 1); 107 assertEquals(44, b.x); 108 assertEquals(1, ++b[c] && 1); 109 assertEquals(45, b[c]); 110 assertEquals(1, b[c]++ && 1); 111 assertEquals(46, b[c]); 112 113 // Test count operations with parameters. 114 function f(x) { x++; return x; } 115 assertEquals(43, f(42)); 116 117 function g(x) { ++x; return x; } 118 assertEquals(43, g(42)); 119 120 function h(x) { var y = x++; return y; } 121 assertEquals(42, h(42)); 122 123 function k(x) { var y = ++x; return y; } 124 assertEquals(43, k(42)); 125 126 // Test count operation in a test context. 127 function countTestPost(i) { var k = 0; while (i--) { k++; } return k; } 128 assertEquals(10, countTestPost(10)); 129 130 function countTestPre(i) { var k = 0; while (--i) { k++; } return k; } 131 assertEquals(9, countTestPre(10)); 132