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