Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Expressions / 11.4.8.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          11.4.8.js
42    ECMA Section:       11.4.8 Bitwise NOT Operator
43    Description:        flip bits up to 32 bits
44    no special cases
45    Author:             christine@netscape.com
46    Date:               7 july 1997
47
48    Data File Fields:
49    VALUE           value passed as an argument to the ~ operator
50    E_RESULT        expected return value of ~ VALUE;
51
52    Static variables:
53    none
54
55 */
56
57 var SECTION = "11.4.8";
58 var VERSION = "ECMA_1";
59 startTest();
60
61 writeHeaderToLog( SECTION + " Bitwise Not operator");
62
63 for ( var i = 0; i < 35; i++ ) {
64   var p = Math.pow(2,i);
65
66   new TestCase( SECTION, "~"+p,   Not(p),     ~p );
67
68 }
69 for ( i = 0; i < 35; i++ ) {
70   var p = -Math.pow(2,i);
71
72   new TestCase( SECTION, "~"+p,   Not(p),     ~p );
73
74 }
75
76 test();
77
78 function ToInteger( n ) {
79   n = Number( n );
80   var sign = ( n < 0 ) ? -1 : 1;
81
82   if ( n != n ) {
83     return 0;
84   }
85   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
86     return n;
87   }
88   return ( sign * Math.floor(Math.abs(n)) );
89 }
90 function ToInt32( n ) {
91   n = Number( n );
92   var sign = ( n < 0 ) ? -1 : 1;
93
94   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
95     return 0;
96   }
97
98   n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
99   n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
100
101   return ( n );
102 }
103 function ToUint32( n ) {
104   n = Number( n );
105   var sign = ( n < 0 ) ? -1 : 1;
106
107   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
108     return 0;
109   }
110   n = sign * Math.floor( Math.abs(n) )
111
112     n = n % Math.pow(2,32);
113
114   if ( n < 0 ){
115     n += Math.pow(2,32);
116   }
117
118   return ( n );
119 }
120 function ToUint16( n ) {
121   var sign = ( n < 0 ) ? -1 : 1;
122
123   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
124     return 0;
125   }
126
127   n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
128
129   if (n <0) {
130     n += Math.pow(2,16);
131   }
132
133   return ( n );
134 }
135 function Mask( b, n ) {
136   b = ToUint32BitString( b );
137   b = b.substring( b.length - n );
138   b = ToUint32Decimal( b );
139   return ( b );
140 }
141 function ToUint32BitString( n ) {
142   var b = "";
143   for ( var p = 31; p >=0; p-- ) {
144     if ( n >= Math.pow(2,p) ) {
145       b += "1";
146       n -= Math.pow(2,p);
147     } else {
148       b += "0";
149     }
150   }
151   return b;
152 }
153 function ToInt32BitString( n ) {
154   var b = "";
155   var sign = ( n < 0 ) ? -1 : 1;
156
157   b += ( sign == 1 ) ? "0" : "1";
158
159   for ( var p = 30; p >=0; p-- ) {
160     if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
161       b += ( sign == 1 ) ? "1" : "0";
162       n -= sign * Math.pow( 2, p );
163     } else {
164       b += ( sign == 1 ) ? "0" : "1";
165     }
166   }
167
168   return b;
169 }
170 function ToInt32Decimal( bin ) {
171   var r = 0;
172   var sign;
173
174   if ( Number(bin.charAt(0)) == 0 ) {
175     sign = 1;
176     r = 0;
177   } else {
178     sign = -1;
179     r = -(Math.pow(2,31));
180   }
181
182   for ( var j = 0; j < 31; j++ ) {
183     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
184   }
185
186   return r;
187 }
188 function ToUint32Decimal( bin ) {
189   var r = 0;
190
191   for ( var l = bin.length; l < 32; l++ ) {
192     bin = "0" + bin;
193   }
194
195   for ( var j = 0; j < 31; j++ ) {
196     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
197   }
198
199   return r;
200 }
201 function Not( n ) {
202   n = ToInt32(n);
203   n = ToInt32BitString(n);
204
205   var r = "";
206
207   for( var l = 0; l < n.length; l++  ) {
208     r += ( n.charAt(l) == "0" ) ? "1" : "0";
209   }
210
211   n = ToInt32Decimal(r);
212
213   return n;
214 }