1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma/Expressions/11.4.8.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 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.4.8.js 1.12 + ECMA Section: 11.4.8 Bitwise NOT Operator 1.13 + Description: flip bits up to 32 bits 1.14 + no special cases 1.15 + Author: christine@netscape.com 1.16 + Date: 7 july 1997 1.17 + 1.18 + Data File Fields: 1.19 + VALUE value passed as an argument to the ~ operator 1.20 + E_RESULT expected return value of ~ VALUE; 1.21 + 1.22 + Static variables: 1.23 + none 1.24 + 1.25 +*/ 1.26 + 1.27 +var SECTION = "11.4.8"; 1.28 +var VERSION = "ECMA_1"; 1.29 +startTest(); 1.30 + 1.31 +writeHeaderToLog( SECTION + " Bitwise Not operator"); 1.32 + 1.33 +for ( var i = 0; i < 35; i++ ) { 1.34 + var p = Math.pow(2,i); 1.35 + 1.36 + new TestCase( SECTION, "~"+p, Not(p), ~p ); 1.37 + 1.38 +} 1.39 +for ( i = 0; i < 35; i++ ) { 1.40 + var p = -Math.pow(2,i); 1.41 + 1.42 + new TestCase( SECTION, "~"+p, Not(p), ~p ); 1.43 + 1.44 +} 1.45 + 1.46 +test(); 1.47 + 1.48 +function ToInteger( n ) { 1.49 + n = Number( n ); 1.50 + var sign = ( n < 0 ) ? -1 : 1; 1.51 + 1.52 + if ( n != n ) { 1.53 + return 0; 1.54 + } 1.55 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { 1.56 + return n; 1.57 + } 1.58 + return ( sign * Math.floor(Math.abs(n)) ); 1.59 +} 1.60 +function ToInt32( n ) { 1.61 + n = Number( n ); 1.62 + var sign = ( n < 0 ) ? -1 : 1; 1.63 + 1.64 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 1.65 + return 0; 1.66 + } 1.67 + 1.68 + n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); 1.69 + n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; 1.70 + 1.71 + return ( n ); 1.72 +} 1.73 +function ToUint32( n ) { 1.74 + n = Number( n ); 1.75 + var sign = ( n < 0 ) ? -1 : 1; 1.76 + 1.77 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 1.78 + return 0; 1.79 + } 1.80 + n = sign * Math.floor( Math.abs(n) ) 1.81 + 1.82 + n = n % Math.pow(2,32); 1.83 + 1.84 + if ( n < 0 ){ 1.85 + n += Math.pow(2,32); 1.86 + } 1.87 + 1.88 + return ( n ); 1.89 +} 1.90 +function ToUint16( n ) { 1.91 + var sign = ( n < 0 ) ? -1 : 1; 1.92 + 1.93 + if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { 1.94 + return 0; 1.95 + } 1.96 + 1.97 + n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); 1.98 + 1.99 + if (n <0) { 1.100 + n += Math.pow(2,16); 1.101 + } 1.102 + 1.103 + return ( n ); 1.104 +} 1.105 +function Mask( b, n ) { 1.106 + b = ToUint32BitString( b ); 1.107 + b = b.substring( b.length - n ); 1.108 + b = ToUint32Decimal( b ); 1.109 + return ( b ); 1.110 +} 1.111 +function ToUint32BitString( n ) { 1.112 + var b = ""; 1.113 + for ( var p = 31; p >=0; p-- ) { 1.114 + if ( n >= Math.pow(2,p) ) { 1.115 + b += "1"; 1.116 + n -= Math.pow(2,p); 1.117 + } else { 1.118 + b += "0"; 1.119 + } 1.120 + } 1.121 + return b; 1.122 +} 1.123 +function ToInt32BitString( n ) { 1.124 + var b = ""; 1.125 + var sign = ( n < 0 ) ? -1 : 1; 1.126 + 1.127 + b += ( sign == 1 ) ? "0" : "1"; 1.128 + 1.129 + for ( var p = 30; p >=0; p-- ) { 1.130 + if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { 1.131 + b += ( sign == 1 ) ? "1" : "0"; 1.132 + n -= sign * Math.pow( 2, p ); 1.133 + } else { 1.134 + b += ( sign == 1 ) ? "0" : "1"; 1.135 + } 1.136 + } 1.137 + 1.138 + return b; 1.139 +} 1.140 +function ToInt32Decimal( bin ) { 1.141 + var r = 0; 1.142 + var sign; 1.143 + 1.144 + if ( Number(bin.charAt(0)) == 0 ) { 1.145 + sign = 1; 1.146 + r = 0; 1.147 + } else { 1.148 + sign = -1; 1.149 + r = -(Math.pow(2,31)); 1.150 + } 1.151 + 1.152 + for ( var j = 0; j < 31; j++ ) { 1.153 + r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); 1.154 + } 1.155 + 1.156 + return r; 1.157 +} 1.158 +function ToUint32Decimal( bin ) { 1.159 + var r = 0; 1.160 + 1.161 + for ( var l = bin.length; l < 32; l++ ) { 1.162 + bin = "0" + bin; 1.163 + } 1.164 + 1.165 + for ( var j = 0; j < 31; j++ ) { 1.166 + r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); 1.167 + } 1.168 + 1.169 + return r; 1.170 +} 1.171 +function Not( n ) { 1.172 + n = ToInt32(n); 1.173 + n = ToInt32BitString(n); 1.174 + 1.175 + var r = ""; 1.176 + 1.177 + for( var l = 0; l < n.length; l++ ) { 1.178 + r += ( n.charAt(l) == "0" ) ? "1" : "0"; 1.179 + } 1.180 + 1.181 + n = ToInt32Decimal(r); 1.182 + 1.183 + return n; 1.184 +}