• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25  * @bug 8065570
26  * @summary Unit test for X-Buffer.order methods
27  */
28 package test.java.nio.Buffer;
29 
30 import java.nio.*;
31 import org.testng.annotations.Test;
32 
33 
34 public class Order {
35 
36     static final ByteOrder be = ByteOrder.BIG_ENDIAN;
37     static final ByteOrder le = ByteOrder.LITTLE_ENDIAN;
38     static final ByteOrder nord = ByteOrder.nativeOrder();
39 
40     protected static final int LENGTH = 16;
41 
ck(ByteOrder ord, ByteOrder expected)42     static void ck(ByteOrder ord, ByteOrder expected) {
43         if (ord != expected)
44             throw new RuntimeException("Got " + ord + ", expected " + expected);
45     }
46 
ckViews(ByteBuffer bb)47     private static void ckViews(ByteBuffer bb) {
48         ck(bb.asCharBuffer().order(), bb.order());
49         ck(bb.asShortBuffer().order(), bb.order());
50         ck(bb.asIntBuffer().order(), bb.order());
51         ck(bb.asLongBuffer().order(), bb.order());
52         ck(bb.asFloatBuffer().order(), bb.order());
53         ck(bb.asDoubleBuffer().order(), bb.order());
54     }
55 
ckCopyViews(ByteBuffer bb)56     private static void ckCopyViews(ByteBuffer bb) {
57         ck(bb.asReadOnlyBuffer().order(), be);
58         ck(bb.duplicate().order(), be);
59         ck(bb.slice().order(), be);
60     }
61 
ckByteBuffer(ByteBuffer bb)62     private static void ckByteBuffer(ByteBuffer bb) {
63         ckViews(bb);
64         ckCopyViews(bb);
65         bb.order(be);
66         ckViews(bb);
67         ckCopyViews(bb);
68         bb.order(le);
69         ckViews(bb);
70         ckCopyViews(bb);
71     }
72 
73     // Android-changed: Use TsetNg.
74     // public static void main(String args[]) throws Exception {
75     @Test
testOrder()76     public void testOrder() throws Exception {
77 
78         ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(), be);
79         ck(ByteBuffer.wrap(new byte[LENGTH]).order(), be);
80         ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(be).order(), be);
81         ck(ByteBuffer.wrap(new byte[LENGTH]).order(be).order(), be);
82         ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(le).order(), le);
83         ck(ByteBuffer.wrap(new byte[LENGTH]).order(le).order(), le);
84         ck(ByteBuffer.allocate(LENGTH).order(), be);
85         ck(ByteBuffer.allocateDirect(LENGTH).order(), be);
86         ck(ByteBuffer.allocate(LENGTH).order(be).order(), be);
87         ck(ByteBuffer.allocate(LENGTH).order(le).order(), le);
88         ck(ByteBuffer.allocateDirect(LENGTH).order(be).order(), be);
89         ck(ByteBuffer.allocateDirect(LENGTH).order(le).order(), le);
90 
91         ckByteBuffer(ByteBuffer.allocate(LENGTH));
92         ckByteBuffer(ByteBuffer.allocateDirect(LENGTH));
93 
94         OrderChar.ckCharBuffer();
95         OrderShort.ckShortBuffer();
96         OrderInt.ckIntBuffer();
97         OrderLong.ckLongBuffer();
98         OrderFloat.ckFloatBuffer();
99         OrderDouble.ckDoubleBuffer();
100     }
101 }