js/src/tests/ecma/Expressions/11.10-3.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 /**
michael@0 8 File Name: 11.10-3.js
michael@0 9 ECMA Section: 11.10-3 Binary Bitwise Operators: ^
michael@0 10 Description:
michael@0 11 Semantics
michael@0 12
michael@0 13 The production A : A @ B, where @ is one of the bitwise operators in the
michael@0 14 productions &, ^, | , is evaluated as follows:
michael@0 15
michael@0 16 1. Evaluate A.
michael@0 17 2. Call GetValue(Result(1)).
michael@0 18 3. Evaluate B.
michael@0 19 4. Call GetValue(Result(3)).
michael@0 20 5. Call ToInt32(Result(2)).
michael@0 21 6. Call ToInt32(Result(4)).
michael@0 22 7. Apply the bitwise operator @ to Result(5) and Result(6). The result is
michael@0 23 a signed 32 bit integer.
michael@0 24 8. Return Result(7).
michael@0 25
michael@0 26 Author: christine@netscape.com
michael@0 27 Date: 12 november 1997
michael@0 28 */
michael@0 29 var SECTION = "11.10-3";
michael@0 30 var VERSION = "ECMA_1";
michael@0 31 startTest();
michael@0 32
michael@0 33 writeHeaderToLog( SECTION + " Binary Bitwise Operators: ^");
michael@0 34
michael@0 35 var shiftexp = 0;
michael@0 36 var addexp = 0;
michael@0 37
michael@0 38 for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
michael@0 39 shiftexp += Math.pow( 2, shiftpow );
michael@0 40
michael@0 41 for ( addpow = 0; addpow < 33; addpow++ ) {
michael@0 42 addexp += Math.pow(2, addpow);
michael@0 43
michael@0 44 new TestCase( SECTION,
michael@0 45 shiftexp + " ^ " + addexp,
michael@0 46 Xor( shiftexp, addexp ),
michael@0 47 shiftexp ^ addexp );
michael@0 48 }
michael@0 49 }
michael@0 50
michael@0 51 test();
michael@0 52
michael@0 53 function ToInteger( n ) {
michael@0 54 n = Number( n );
michael@0 55 var sign = ( n < 0 ) ? -1 : 1;
michael@0 56
michael@0 57 if ( n != n ) {
michael@0 58 return 0;
michael@0 59 }
michael@0 60 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
michael@0 61 return n;
michael@0 62 }
michael@0 63 return ( sign * Math.floor(Math.abs(n)) );
michael@0 64 }
michael@0 65 function ToInt32( n ) {
michael@0 66 n = Number( n );
michael@0 67 var sign = ( n < 0 ) ? -1 : 1;
michael@0 68
michael@0 69 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
michael@0 70 return 0;
michael@0 71 }
michael@0 72
michael@0 73 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
michael@0 74 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
michael@0 75
michael@0 76 return ( n );
michael@0 77 }
michael@0 78 function ToUint32( n ) {
michael@0 79 n = Number( n );
michael@0 80 var sign = ( n < 0 ) ? -1 : 1;
michael@0 81
michael@0 82 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
michael@0 83 return 0;
michael@0 84 }
michael@0 85 n = sign * Math.floor( Math.abs(n) )
michael@0 86
michael@0 87 n = n % Math.pow(2,32);
michael@0 88
michael@0 89 if ( n < 0 ){
michael@0 90 n += Math.pow(2,32);
michael@0 91 }
michael@0 92
michael@0 93 return ( n );
michael@0 94 }
michael@0 95 function ToUint16( n ) {
michael@0 96 var sign = ( n < 0 ) ? -1 : 1;
michael@0 97
michael@0 98 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
michael@0 99 return 0;
michael@0 100 }
michael@0 101
michael@0 102 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
michael@0 103
michael@0 104 if (n <0) {
michael@0 105 n += Math.pow(2,16);
michael@0 106 }
michael@0 107
michael@0 108 return ( n );
michael@0 109 }
michael@0 110 function Mask( b, n ) {
michael@0 111 b = ToUint32BitString( b );
michael@0 112 b = b.substring( b.length - n );
michael@0 113 b = ToUint32Decimal( b );
michael@0 114 return ( b );
michael@0 115 }
michael@0 116 function ToUint32BitString( n ) {
michael@0 117 var b = "";
michael@0 118 for ( p = 31; p >=0; p-- ) {
michael@0 119 if ( n >= Math.pow(2,p) ) {
michael@0 120 b += "1";
michael@0 121 n -= Math.pow(2,p);
michael@0 122 } else {
michael@0 123 b += "0";
michael@0 124 }
michael@0 125 }
michael@0 126 return b;
michael@0 127 }
michael@0 128 function ToInt32BitString( n ) {
michael@0 129 var b = "";
michael@0 130 var sign = ( n < 0 ) ? -1 : 1;
michael@0 131
michael@0 132 b += ( sign == 1 ) ? "0" : "1";
michael@0 133
michael@0 134 for ( p = 30; p >=0; p-- ) {
michael@0 135 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
michael@0 136 b += ( sign == 1 ) ? "1" : "0";
michael@0 137 n -= sign * Math.pow( 2, p );
michael@0 138 } else {
michael@0 139 b += ( sign == 1 ) ? "0" : "1";
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 return b;
michael@0 144 }
michael@0 145 function ToInt32Decimal( bin ) {
michael@0 146 var r = 0;
michael@0 147 var sign;
michael@0 148
michael@0 149 if ( Number(bin.charAt(0)) == 0 ) {
michael@0 150 sign = 1;
michael@0 151 r = 0;
michael@0 152 } else {
michael@0 153 sign = -1;
michael@0 154 r = -(Math.pow(2,31));
michael@0 155 }
michael@0 156
michael@0 157 for ( var j = 0; j < 31; j++ ) {
michael@0 158 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
michael@0 159 }
michael@0 160
michael@0 161 return r;
michael@0 162 }
michael@0 163 function ToUint32Decimal( bin ) {
michael@0 164 var r = 0;
michael@0 165
michael@0 166 for ( l = bin.length; l < 32; l++ ) {
michael@0 167 bin = "0" + bin;
michael@0 168 }
michael@0 169
michael@0 170 for ( j = 0; j < 31; j++ ) {
michael@0 171 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
michael@0 172
michael@0 173 }
michael@0 174
michael@0 175 return r;
michael@0 176 }
michael@0 177 function And( s, a ) {
michael@0 178 s = ToInt32( s );
michael@0 179 a = ToInt32( a );
michael@0 180
michael@0 181 var bs = ToInt32BitString( s );
michael@0 182 var ba = ToInt32BitString( a );
michael@0 183
michael@0 184 var result = "";
michael@0 185
michael@0 186 for ( var bit = 0; bit < bs.length; bit++ ) {
michael@0 187 if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
michael@0 188 result += "1";
michael@0 189 } else {
michael@0 190 result += "0";
michael@0 191 }
michael@0 192 }
michael@0 193 return ToInt32Decimal(result);
michael@0 194 }
michael@0 195 function Xor( s, a ) {
michael@0 196 s = ToInt32( s );
michael@0 197 a = ToInt32( a );
michael@0 198
michael@0 199 var bs = ToInt32BitString( s );
michael@0 200 var ba = ToInt32BitString( a );
michael@0 201
michael@0 202 var result = "";
michael@0 203
michael@0 204 for ( var bit = 0; bit < bs.length; bit++ ) {
michael@0 205 if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") ||
michael@0 206 (bs.charAt(bit) == "0" && ba.charAt(bit) == "1")
michael@0 207 ) {
michael@0 208 result += "1";
michael@0 209 } else {
michael@0 210 result += "0";
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 return ToInt32Decimal(result);
michael@0 215 }
michael@0 216 function Or( s, a ) {
michael@0 217 s = ToInt32( s );
michael@0 218 a = ToInt32( a );
michael@0 219
michael@0 220 var bs = ToInt32BitString( s );
michael@0 221 var ba = ToInt32BitString( a );
michael@0 222
michael@0 223 var result = "";
michael@0 224
michael@0 225 for ( var bit = 0; bit < bs.length; bit++ ) {
michael@0 226 if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
michael@0 227 result += "1";
michael@0 228 } else {
michael@0 229 result += "0";
michael@0 230 }
michael@0 231 }
michael@0 232
michael@0 233 return ToInt32Decimal(result);
michael@0 234 }

mercurial