1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma/Expressions/11.10-3.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,234 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/** 1.11 + File Name: 11.10-3.js 1.12 + ECMA Section: 11.10-3 Binary Bitwise Operators: ^ 1.13 + Description: 1.14 + Semantics 1.15 + 1.16 + The production A : A @ B, where @ is one of the bitwise operators in the 1.17 + productions &, ^, | , is evaluated as follows: 1.18 + 1.19 + 1. Evaluate A. 1.20 + 2. Call GetValue(Result(1)). 1.21 + 3. Evaluate B. 1.22 + 4. Call GetValue(Result(3)). 1.23 + 5. Call ToInt32(Result(2)). 1.24 + 6. Call ToInt32(Result(4)). 1.25 + 7. Apply the bitwise operator @ to Result(5) and Result(6). The result is 1.26 + a signed 32 bit integer. 1.27 + 8. Return Result(7). 1.28 + 1.29 + Author: christine@netscape.com 1.30 + Date: 12 november 1997 1.31 +*/ 1.32 +var SECTION = "11.10-3"; 1.33 +var VERSION = "ECMA_1"; 1.34 +startTest(); 1.35 + 1.36 +writeHeaderToLog( SECTION + " Binary Bitwise Operators: ^"); 1.37 + 1.38 +var shiftexp = 0; 1.39 +var addexp = 0; 1.40 + 1.41 +for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) { 1.42 + shiftexp += Math.pow( 2, shiftpow ); 1.43 + 1.44 + for ( addpow = 0; addpow < 33; addpow++ ) { 1.45 + addexp += Math.pow(2, addpow); 1.46 + 1.47 + new TestCase( SECTION, 1.48 + shiftexp + " ^ " + addexp, 1.49 + Xor( shiftexp, addexp ), 1.50 + shiftexp ^ addexp ); 1.51 + } 1.52 +} 1.53 + 1.54 +test(); 1.55 + 1.56 +function ToInteger( n ) { 1.57 + n = Number( n ); 1.58 + var sign = ( n < 0 ) ? -1 : 1; 1.59 + 1.60 + if ( n != n ) { 1.61 + return 0; 1.62 + } 1.63 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { 1.64 + return n; 1.65 + } 1.66 + return ( sign * Math.floor(Math.abs(n)) ); 1.67 +} 1.68 +function ToInt32( n ) { 1.69 + n = Number( n ); 1.70 + var sign = ( n < 0 ) ? -1 : 1; 1.71 + 1.72 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 1.73 + return 0; 1.74 + } 1.75 + 1.76 + n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); 1.77 + n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; 1.78 + 1.79 + return ( n ); 1.80 +} 1.81 +function ToUint32( n ) { 1.82 + n = Number( n ); 1.83 + var sign = ( n < 0 ) ? -1 : 1; 1.84 + 1.85 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 1.86 + return 0; 1.87 + } 1.88 + n = sign * Math.floor( Math.abs(n) ) 1.89 + 1.90 + n = n % Math.pow(2,32); 1.91 + 1.92 + if ( n < 0 ){ 1.93 + n += Math.pow(2,32); 1.94 + } 1.95 + 1.96 + return ( n ); 1.97 +} 1.98 +function ToUint16( n ) { 1.99 + var sign = ( n < 0 ) ? -1 : 1; 1.100 + 1.101 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 1.102 + return 0; 1.103 + } 1.104 + 1.105 + n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); 1.106 + 1.107 + if (n <0) { 1.108 + n += Math.pow(2,16); 1.109 + } 1.110 + 1.111 + return ( n ); 1.112 +} 1.113 +function Mask( b, n ) { 1.114 + b = ToUint32BitString( b ); 1.115 + b = b.substring( b.length - n ); 1.116 + b = ToUint32Decimal( b ); 1.117 + return ( b ); 1.118 +} 1.119 +function ToUint32BitString( n ) { 1.120 + var b = ""; 1.121 + for ( p = 31; p >=0; p-- ) { 1.122 + if ( n >= Math.pow(2,p) ) { 1.123 + b += "1"; 1.124 + n -= Math.pow(2,p); 1.125 + } else { 1.126 + b += "0"; 1.127 + } 1.128 + } 1.129 + return b; 1.130 +} 1.131 +function ToInt32BitString( n ) { 1.132 + var b = ""; 1.133 + var sign = ( n < 0 ) ? -1 : 1; 1.134 + 1.135 + b += ( sign == 1 ) ? "0" : "1"; 1.136 + 1.137 + for ( p = 30; p >=0; p-- ) { 1.138 + if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { 1.139 + b += ( sign == 1 ) ? "1" : "0"; 1.140 + n -= sign * Math.pow( 2, p ); 1.141 + } else { 1.142 + b += ( sign == 1 ) ? "0" : "1"; 1.143 + } 1.144 + } 1.145 + 1.146 + return b; 1.147 +} 1.148 +function ToInt32Decimal( bin ) { 1.149 + var r = 0; 1.150 + var sign; 1.151 + 1.152 + if ( Number(bin.charAt(0)) == 0 ) { 1.153 + sign = 1; 1.154 + r = 0; 1.155 + } else { 1.156 + sign = -1; 1.157 + r = -(Math.pow(2,31)); 1.158 + } 1.159 + 1.160 + for ( var j = 0; j < 31; j++ ) { 1.161 + r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); 1.162 + } 1.163 + 1.164 + return r; 1.165 +} 1.166 +function ToUint32Decimal( bin ) { 1.167 + var r = 0; 1.168 + 1.169 + for ( l = bin.length; l < 32; l++ ) { 1.170 + bin = "0" + bin; 1.171 + } 1.172 + 1.173 + for ( j = 0; j < 31; j++ ) { 1.174 + r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); 1.175 + 1.176 + } 1.177 + 1.178 + return r; 1.179 +} 1.180 +function And( s, a ) { 1.181 + s = ToInt32( s ); 1.182 + a = ToInt32( a ); 1.183 + 1.184 + var bs = ToInt32BitString( s ); 1.185 + var ba = ToInt32BitString( a ); 1.186 + 1.187 + var result = ""; 1.188 + 1.189 + for ( var bit = 0; bit < bs.length; bit++ ) { 1.190 + if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) { 1.191 + result += "1"; 1.192 + } else { 1.193 + result += "0"; 1.194 + } 1.195 + } 1.196 + return ToInt32Decimal(result); 1.197 +} 1.198 +function Xor( s, a ) { 1.199 + s = ToInt32( s ); 1.200 + a = ToInt32( a ); 1.201 + 1.202 + var bs = ToInt32BitString( s ); 1.203 + var ba = ToInt32BitString( a ); 1.204 + 1.205 + var result = ""; 1.206 + 1.207 + for ( var bit = 0; bit < bs.length; bit++ ) { 1.208 + if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") || 1.209 + (bs.charAt(bit) == "0" && ba.charAt(bit) == "1") 1.210 + ) { 1.211 + result += "1"; 1.212 + } else { 1.213 + result += "0"; 1.214 + } 1.215 + } 1.216 + 1.217 + return ToInt32Decimal(result); 1.218 +} 1.219 +function Or( s, a ) { 1.220 + s = ToInt32( s ); 1.221 + a = ToInt32( a ); 1.222 + 1.223 + var bs = ToInt32BitString( s ); 1.224 + var ba = ToInt32BitString( a ); 1.225 + 1.226 + var result = ""; 1.227 + 1.228 + for ( var bit = 0; bit < bs.length; bit++ ) { 1.229 + if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) { 1.230 + result += "1"; 1.231 + } else { 1.232 + result += "0"; 1.233 + } 1.234 + } 1.235 + 1.236 + return ToInt32Decimal(result); 1.237 +}