dataview_new.js revision 765:91ef0e039d91
1214455Srpaulo/*
2214455Srpaulo * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3214455Srpaulo * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4214455Srpaulo *
5214455Srpaulo * This code is free software; you can redistribute it and/or modify it
6214455Srpaulo * under the terms of the GNU General Public License version 2 only, as
7214455Srpaulo * published by the Free Software Foundation.
8214455Srpaulo *
9214455Srpaulo * This code is distributed in the hope that it will be useful, but WITHOUT
10214455Srpaulo * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11214455Srpaulo * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12214455Srpaulo * version 2 for more details (a copy is included in the LICENSE file that
13214455Srpaulo * accompanied this code).
14214455Srpaulo *
15214455Srpaulo * You should have received a copy of the GNU General Public License version
16214455Srpaulo * 2 along with this work; if not, write to the Free Software Foundation,
17214455Srpaulo * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18214455Srpaulo *
19214455Srpaulo * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20214455Srpaulo * or visit www.oracle.com if you need additional information or have any
21214455Srpaulo * questions.
22214455Srpaulo */
23214455Srpaulo
24214455Srpaulo/**
25214455Srpaulo * JDK-8015958: DataView constructor is not defined
26214455Srpaulo *
27214455Srpaulo * @test
28214455Srpaulo * @run
29214455Srpaulo */
30214455Srpaulo
31214455Srpaulo// basic DataView constructor checks.
32214455Srpaulo
33214455Srpaulo// check ArrayBufferView property values of DataView instance
34214455Srpaulofunction check(dv, buf, offset, length) {
35214455Srpaulo    if (dv.buffer !== buf) {
36214455Srpaulo        fail("DataView.buffer is wrong");
37214455Srpaulo    }
38214455Srpaulo
39214455Srpaulo    if (dv.byteOffset != offset) {
40214455Srpaulo        fail("DataView.byteOffset = " + dv.byteOffset + ", expected " + offset);
41214455Srpaulo    }
42214455Srpaulo
43214455Srpaulo    if (dv.byteLength != length) {
44214455Srpaulo        fail("DataView.byteLength = " + dv.byteLength + ", expected " + length);
45214455Srpaulo    }
46214455Srpaulo}
47214455Srpaulo
48214455Srpaulovar buffer = new ArrayBuffer(12);
49214455Srpaulocheck(new DataView(buffer), buffer, 0, 12);
50214455Srpaulocheck(new DataView(buffer, 2), buffer, 2, 10);
51214455Srpaulocheck(new DataView(buffer, 4, 8), buffer, 4, 8);
52214455Srpaulo
53214455Srpaulo// make sure expected error is thrown
54214455Srpaulofunction checkError(callback, ErrorType) {
55214455Srpaulo    try {
56214455Srpaulo        callback();
57214455Srpaulo        fail("Should have thrown " + ErrorType.name);
58214455Srpaulo    } catch (e) {
59214455Srpaulo        if (! (e instanceof ErrorType)) {
60214455Srpaulo            fail("Expected " + ErrorType.name + " got " + e);
61214455Srpaulo        }
62214455Srpaulo    }
63214455Srpaulo}
64214455Srpaulo
65214455Srpaulo// non ArrayBuffer as first arg
66214455SrpaulocheckError(function() { new DataView(344) }, TypeError);
67214455Srpaulo
68214455Srpaulo// illegal offset/length values
69214455SrpaulocheckError(function() { new DataView(buffer, -1) }, RangeError);
70214455SrpaulocheckError(function() { new DataView(buffer, 15) }, RangeError);
71214455SrpaulocheckError(function() { new DataView(buffer, 1, 32) }, RangeError);
72214455Srpaulo