Wed, 31 Dec 2014 06:09:35 +0100
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.4.8.js |
michael@0 | 9 | ECMA Section: 11.4.8 Bitwise NOT Operator |
michael@0 | 10 | Description: flip bits up to 32 bits |
michael@0 | 11 | no special cases |
michael@0 | 12 | Author: christine@netscape.com |
michael@0 | 13 | Date: 7 july 1997 |
michael@0 | 14 | |
michael@0 | 15 | Data File Fields: |
michael@0 | 16 | VALUE value passed as an argument to the ~ operator |
michael@0 | 17 | E_RESULT expected return value of ~ VALUE; |
michael@0 | 18 | |
michael@0 | 19 | Static variables: |
michael@0 | 20 | none |
michael@0 | 21 | |
michael@0 | 22 | */ |
michael@0 | 23 | |
michael@0 | 24 | var SECTION = "11.4.8"; |
michael@0 | 25 | var VERSION = "ECMA_1"; |
michael@0 | 26 | startTest(); |
michael@0 | 27 | |
michael@0 | 28 | writeHeaderToLog( SECTION + " Bitwise Not operator"); |
michael@0 | 29 | |
michael@0 | 30 | for ( var i = 0; i < 35; i++ ) { |
michael@0 | 31 | var p = Math.pow(2,i); |
michael@0 | 32 | |
michael@0 | 33 | new TestCase( SECTION, "~"+p, Not(p), ~p ); |
michael@0 | 34 | |
michael@0 | 35 | } |
michael@0 | 36 | for ( i = 0; i < 35; i++ ) { |
michael@0 | 37 | var p = -Math.pow(2,i); |
michael@0 | 38 | |
michael@0 | 39 | new TestCase( SECTION, "~"+p, Not(p), ~p ); |
michael@0 | 40 | |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | test(); |
michael@0 | 44 | |
michael@0 | 45 | function ToInteger( n ) { |
michael@0 | 46 | n = Number( n ); |
michael@0 | 47 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 48 | |
michael@0 | 49 | if ( n != n ) { |
michael@0 | 50 | return 0; |
michael@0 | 51 | } |
michael@0 | 52 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) { |
michael@0 | 53 | return n; |
michael@0 | 54 | } |
michael@0 | 55 | return ( sign * Math.floor(Math.abs(n)) ); |
michael@0 | 56 | } |
michael@0 | 57 | function ToInt32( n ) { |
michael@0 | 58 | n = Number( n ); |
michael@0 | 59 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 60 | |
michael@0 | 61 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 62 | return 0; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32); |
michael@0 | 66 | n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n; |
michael@0 | 67 | |
michael@0 | 68 | return ( n ); |
michael@0 | 69 | } |
michael@0 | 70 | function ToUint32( n ) { |
michael@0 | 71 | n = Number( n ); |
michael@0 | 72 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 73 | |
michael@0 | 74 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 75 | return 0; |
michael@0 | 76 | } |
michael@0 | 77 | n = sign * Math.floor( Math.abs(n) ) |
michael@0 | 78 | |
michael@0 | 79 | n = n % Math.pow(2,32); |
michael@0 | 80 | |
michael@0 | 81 | if ( n < 0 ){ |
michael@0 | 82 | n += Math.pow(2,32); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | return ( n ); |
michael@0 | 86 | } |
michael@0 | 87 | function ToUint16( n ) { |
michael@0 | 88 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 89 | |
michael@0 | 90 | if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) { |
michael@0 | 91 | return 0; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16); |
michael@0 | 95 | |
michael@0 | 96 | if (n <0) { |
michael@0 | 97 | n += Math.pow(2,16); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | return ( n ); |
michael@0 | 101 | } |
michael@0 | 102 | function Mask( b, n ) { |
michael@0 | 103 | b = ToUint32BitString( b ); |
michael@0 | 104 | b = b.substring( b.length - n ); |
michael@0 | 105 | b = ToUint32Decimal( b ); |
michael@0 | 106 | return ( b ); |
michael@0 | 107 | } |
michael@0 | 108 | function ToUint32BitString( n ) { |
michael@0 | 109 | var b = ""; |
michael@0 | 110 | for ( var p = 31; p >=0; p-- ) { |
michael@0 | 111 | if ( n >= Math.pow(2,p) ) { |
michael@0 | 112 | b += "1"; |
michael@0 | 113 | n -= Math.pow(2,p); |
michael@0 | 114 | } else { |
michael@0 | 115 | b += "0"; |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | return b; |
michael@0 | 119 | } |
michael@0 | 120 | function ToInt32BitString( n ) { |
michael@0 | 121 | var b = ""; |
michael@0 | 122 | var sign = ( n < 0 ) ? -1 : 1; |
michael@0 | 123 | |
michael@0 | 124 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 125 | |
michael@0 | 126 | for ( var p = 30; p >=0; p-- ) { |
michael@0 | 127 | if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) { |
michael@0 | 128 | b += ( sign == 1 ) ? "1" : "0"; |
michael@0 | 129 | n -= sign * Math.pow( 2, p ); |
michael@0 | 130 | } else { |
michael@0 | 131 | b += ( sign == 1 ) ? "0" : "1"; |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | return b; |
michael@0 | 136 | } |
michael@0 | 137 | function ToInt32Decimal( bin ) { |
michael@0 | 138 | var r = 0; |
michael@0 | 139 | var sign; |
michael@0 | 140 | |
michael@0 | 141 | if ( Number(bin.charAt(0)) == 0 ) { |
michael@0 | 142 | sign = 1; |
michael@0 | 143 | r = 0; |
michael@0 | 144 | } else { |
michael@0 | 145 | sign = -1; |
michael@0 | 146 | r = -(Math.pow(2,31)); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | for ( var j = 0; j < 31; j++ ) { |
michael@0 | 150 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | return r; |
michael@0 | 154 | } |
michael@0 | 155 | function ToUint32Decimal( bin ) { |
michael@0 | 156 | var r = 0; |
michael@0 | 157 | |
michael@0 | 158 | for ( var l = bin.length; l < 32; l++ ) { |
michael@0 | 159 | bin = "0" + bin; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | for ( var j = 0; j < 31; j++ ) { |
michael@0 | 163 | r += Math.pow( 2, j ) * Number(bin.charAt(31-j)); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | return r; |
michael@0 | 167 | } |
michael@0 | 168 | function Not( n ) { |
michael@0 | 169 | n = ToInt32(n); |
michael@0 | 170 | n = ToInt32BitString(n); |
michael@0 | 171 | |
michael@0 | 172 | var r = ""; |
michael@0 | 173 | |
michael@0 | 174 | for( var l = 0; l < n.length; l++ ) { |
michael@0 | 175 | r += ( n.charAt(l) == "0" ) ? "1" : "0"; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | n = ToInt32Decimal(r); |
michael@0 | 179 | |
michael@0 | 180 | return n; |
michael@0 | 181 | } |