1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* ES2015 24.2.2.1.1 */ 17try { 18 DataView (); 19 assert (false); 20} catch (e) { 21 assert (e instanceof TypeError); 22} 23 24/* ES2015 24.2.2.1.2 */ 25try { 26 new DataView (5); 27 assert (false); 28} catch (e) { 29 assert (e instanceof TypeError); 30} 31 32/* ES2015 24.2.2.1.3 */ 33try { 34 new DataView ({}); 35 assert (false); 36} catch (e) { 37 assert (e instanceof TypeError); 38} 39 40var buffer = new ArrayBuffer (16); 41 42/* ES2015 24.2.2.1.6 */ 43try { 44 new DataView (buffer, { toString: function () { throw new ReferenceError ('foo') } }); 45 assert (false); 46} catch (e) { 47 assert (e instanceof ReferenceError); 48 assert (e.message === 'foo'); 49} 50 51/* ES2015 24.2.2.1.7 (numberOffset != offset)*/ 52try { 53 new DataView (buffer, 1.5); 54 assert (false); 55} catch (e) { 56 assert (e instanceof RangeError); 57} 58 59 60/* ES2015 24.2.2.1.7 (offset < 0) */ 61try { 62 new DataView (buffer, -1); 63 assert (false); 64} catch (e) { 65 assert (e instanceof RangeError); 66} 67 68/* ES2015 24.2.2.1.10 */ 69try { 70 new DataView (buffer, 17); 71 assert (false); 72} catch (e) { 73 assert (e instanceof RangeError); 74} 75 76/* ES2015 24.2.2.1.12.b */ 77try { 78 new DataView (buffer, 0, { toString: function () { throw new ReferenceError ('bar') } }); 79 assert (false); 80} catch (e) { 81 assert (e instanceof ReferenceError); 82 assert (e.message === 'bar'); 83} 84 85/* ES2015 24.2.2.1.12.b */ 86try { 87 new DataView (buffer, 0, Infinity); 88 assert (false); 89} catch (e) { 90 assert (e instanceof RangeError); 91} 92 93/* ES2015 24.2.2.1.12.c */ 94try { 95 new DataView (buffer, 4, 13); 96 assert (false); 97} catch (e) { 98 assert (e instanceof RangeError); 99} 100 101/* Tests accessors: ES2015 24.2.2.1 - 24.2.2.3 */ 102var accessorList = ['buffer', 'byteOffset', 'byteLength']; 103 104accessorList.forEach (function (prop) { 105 /* ES2015 24.2.4.{1, 2, 3}.{2, 3} */ 106 try { 107 var obj = {}; 108 Object.setPrototypeOf (obj, DataView.prototype); 109 obj[prop]; 110 assert (false); 111 } catch (e) { 112 assert (e instanceof TypeError); 113 } 114}); 115 116buffer = new ArrayBuffer (32); 117var view1 = new DataView (buffer); 118 119assert (view1.buffer === buffer); 120assert (view1.byteOffset === 0); 121assert (view1.byteLength === buffer.byteLength); 122 123var view2 = new DataView (buffer, 8); 124assert (view2.buffer === buffer); 125assert (view2.byteOffset === 8); 126assert (view2.byteLength === buffer.byteLength - view2.byteOffset); 127 128var view3 = new DataView (buffer, 8, 16); 129assert (view3.buffer === buffer); 130assert (view3.byteOffset === 8); 131assert (view3.byteLength === 16); 132 133/* Test get/set routines */ 134var getters = ['getInt8', 'getUint8', 'getInt16', 'getUint16', 'getInt32', 'getUint32', 'getFloat32', 'getFloat64']; 135var setters = ['setInt8', 'setUint8', 'setInt16', 'setUint16', 'setInt32', 'setUint32', 'setFloat32', 'setFloat64']; 136var gettersSetters = getters.concat (setters); 137 138gettersSetters.forEach (function (propName) { 139 /* ES2015 24.2.1.{1, 2}.1 */ 140 var routine = DataView.prototype[propName]; 141 try { 142 DataView.prototype[propName].call (5); 143 assert (false); 144 } catch (e) { 145 assert (e instanceof TypeError); 146 } 147 148 /* ES2015 24.2.1.{1, 2}.2 */ 149 try { 150 DataView.prototype[propName].call ({}); 151 assert (false); 152 } catch (e) { 153 assert (e instanceof TypeError); 154 } 155 156 /* ES2015 24.2.1.{1, 2}.5 */ 157 try { 158 var buffer = new ArrayBuffer (16); 159 var view = new DataView (buffer) 160 view[propName] ({ toString : function () { throw new ReferenceError ('fooBar') } }); 161 assert (false); 162 } catch (e) { 163 assert (e instanceof ReferenceError); 164 assert (e.message == 'fooBar'); 165 } 166 167 var buffer = new ArrayBuffer (16); 168 var view = new DataView (buffer) 169 170 /* ES2015 24.2.1.{1, 2}.6 (numberIndex != getIndex) */ 171 if (propName.indexOf("get") !== -1) { 172 assert(view[propName] (1.5) === 0); 173 } else { 174 assert(view[propName] (1.5) === undefined); 175 } 176 177 /* ES2015 24.2.1.{1, 2}.6 (getIndex < 0) */ 178 try { 179 view[propName] (-1); 180 assert (false); 181 } catch (e) { 182 assert (e instanceof RangeError); 183 } 184 185 /* ES2015 24.2.1.{1, 2}.13 */ 186 try { 187 view[propName] (20); 188 assert (false); 189 } catch (e) { 190 assert (e instanceof RangeError); 191 } 192}); 193 194/* Test the endianness */ 195function validateResult (view, offset, isLitteEndian, results) { 196 for (var i = 0; i < getters.length; i++) { 197 assert (results[i] === view[getters[i]](offset, isLitteEndian)); 198 } 199} 200 201buffer = new ArrayBuffer (24); 202view1 = new DataView (buffer); 203view2 = new DataView (buffer, 4, 12); 204view3 = new DataView (buffer, 6, 18); 205 206view1.setUint8 (0, 255); 207validateResult(view1, 0, false, [-1, 255, -256, 65280, -16777216, 4278190080, -1.7014118346046924e+38, -5.486124068793689e+303]); 208validateResult(view1, 0, true, [-1, 255, 255, 255, 255, 255, 3.5733110840282835e-43, 1.26e-321]); 209validateResult(view1, 2, false, [0, 0, 0, 0, 0, 0, 0, 0]); 210validateResult(view1, 2, true, [0, 0, 0, 0, 0, 0, 0, 0]); 211 212view1.setInt16 (4, 40000); 213validateResult(view1, 4, false, [-100, 156, -25536, 40000, -1673527296, 2621440000, -6.352747104407253e-22, -1.2938158758247024e-172]); 214validateResult(view2, 0, false, [-100, 156, -25536, 40000, -1673527296, 2621440000, -6.352747104407253e-22, -1.2938158758247024e-172]); 215validateResult(view1, 4, true, [-100, 156, 16540, 16540, 16540, 16540, 2.3177476599932474e-41, 8.172e-320]); 216validateResult(view2, 0, true, [-100, 156, 16540, 16540, 16540, 16540, 2.3177476599932474e-41, 8.172e-320]); 217 218view2.setUint32 (2, 3000000000, true); 219validateResult(view2, 2, false, [0, 0, 94, 94, 6213810, 6213810, 8.707402410606192e-39, 6.856613170926581e-307]); 220validateResult(view3, 0, false, [0, 0, 94, 94, 6213810, 6213810, 8.707402410606192e-39, 6.856613170926581e-307]); 221validateResult(view2, 2, true, [0, 0, 24064, 24064, -1294967296, 3000000000, -2.425713319098577e-8, 1.4821969375e-314]); 222validateResult(view3, 0, true, [0, 0, 24064, 24064, -1294967296, 3000000000, -2.425713319098577e-8, 1.4821969375e-314]); 223 224view3.setFloat32 (4, 8.5); 225validateResult(view3, 4, false, [65, 65, 16648, 16648, 1091043328, 1091043328, 8.5, 196608]); 226validateResult(view3, 4, true, [65, 65, 2113, 2113, 2113, 2113, 2.9609436551183385e-42, 1.044e-320]); 227validateResult(view2, 4, false, [-48, 208, -12110, 53426, -793624312, 3501342984, -23924850688, -5.411000515087672e+80]); 228validateResult(view2, 4, true, [-48, 208, -19760, 45776, 138523344, 138523344, 5.828901796903399e-34, 6.84396254e-316]); 229