GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestBuffers.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29/*
30 * Created on Sunday, July 04 2010 20:00
31 */
32package com.jogamp.common.nio;
33
34import java.io.IOException;
35import java.nio.ByteBuffer;
36import java.nio.CharBuffer;
37import java.nio.DoubleBuffer;
38import java.nio.FloatBuffer;
39import java.nio.IntBuffer;
40import java.nio.LongBuffer;
41import java.nio.ShortBuffer;
42
43import org.junit.Test;
44
45import com.jogamp.junit.util.SingletonJunitCase;
46
47import static org.junit.Assert.*;
48
49/**
50 * @author Michael Bien
51 */
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
56public class TestBuffers extends SingletonJunitCase {
57
58 @Test
60 final byte[] byteData = { 1, 2, 3, 4, 5, 6, 7, 8 };
61 final ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(byteData);
62 assertEquals(0, byteBuffer.position());
63 assertEquals(8, byteBuffer.limit());
64 assertEquals(8, byteBuffer.capacity());
65 assertEquals(8, byteBuffer.remaining());
66 assertEquals(5, byteBuffer.get(4));
67
68 final double[] doubleData = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
69 final DoubleBuffer doubleBuffer = Buffers.newDirectDoubleBuffer(doubleData);
70 assertEquals(0, doubleBuffer.position());
71 assertEquals(8, doubleBuffer.limit());
72 assertEquals(8, doubleBuffer.capacity());
73 assertEquals(8, doubleBuffer.remaining());
74 assertEquals(5.0, doubleBuffer.get(4), 0.1);
75
76 final float[] floatData = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
77 final FloatBuffer floatBuffer = Buffers.newDirectFloatBuffer(floatData);
78 assertEquals(0, floatBuffer.position());
79 assertEquals(8, floatBuffer.limit());
80 assertEquals(8, floatBuffer.capacity());
81 assertEquals(8, floatBuffer.remaining());
82 assertEquals(5.0f, floatBuffer.get(4), 0.1f);
83
84 final int[] intData = { 1, 2, 3, 4, 5, 6, 7, 8 };
85 final IntBuffer intBuffer = Buffers.newDirectIntBuffer(intData);
86 assertEquals(0, intBuffer.position());
87 assertEquals(8, intBuffer.limit());
88 assertEquals(8, intBuffer.capacity());
89 assertEquals(8, intBuffer.remaining());
90 assertEquals(5, intBuffer.get(4));
91
92 final long[] longData = { 1, 2, 3, 4, 5, 6, 7, 8 };
93 final LongBuffer longBuffer = Buffers.newDirectLongBuffer(longData);
94 assertEquals(0, longBuffer.position());
95 assertEquals(8, longBuffer.limit());
96 assertEquals(8, longBuffer.capacity());
97 assertEquals(8, longBuffer.remaining());
98 assertEquals(5, longBuffer.get(4));
99
100 final short[] shortData = { 1, 2, 3, 4, 5, 6, 7, 8 };
101 final ShortBuffer shortBuffer = Buffers.newDirectShortBuffer(shortData);
102 assertEquals(0, shortBuffer.position());
103 assertEquals(8, shortBuffer.limit());
104 assertEquals(8, shortBuffer.capacity());
105 assertEquals(8, shortBuffer.remaining());
106 assertEquals(5, shortBuffer.get(4));
107
108 final char[] charData = { 1, 2, 3, 4, 5, 6, 7, 8 };
109 final CharBuffer charBuffer = Buffers.newDirectCharBuffer(charData);
110 assertEquals(0, charBuffer.position());
111 assertEquals(8, charBuffer.limit());
112 assertEquals(8, charBuffer.capacity());
113 assertEquals(8, charBuffer.remaining());
114 assertEquals(5, charBuffer.get(4));
115 }
116
117 @Test
118 public void test10Slice() {
119
120 final IntBuffer buffer = Buffers.newDirectIntBuffer(6);
121 buffer.put(new int[]{1,2,3,4,5,6}).rewind();
122
123 final IntBuffer threefour = Buffers.slice(buffer, 2, 2);
124
125 assertEquals(3, threefour.get(0));
126 assertEquals(4, threefour.get(1));
127 assertEquals(2, threefour.capacity());
128
129 assertEquals(0, buffer.position());
130 assertEquals(6, buffer.limit());
131
132 final IntBuffer fourfivesix = Buffers.slice(buffer, 3, 3);
133
134 assertEquals(4, fourfivesix.get(0));
135 assertEquals(5, fourfivesix.get(1));
136 assertEquals(6, fourfivesix.get(2));
137 assertEquals(3, fourfivesix.capacity());
138
139 assertEquals(0, buffer.position());
140 assertEquals(6, buffer.limit());
141
142 final IntBuffer onetwothree = Buffers.slice(buffer, 0, 3);
143
144 assertEquals(1, onetwothree.get(0));
145 assertEquals(2, onetwothree.get(1));
146 assertEquals(3, onetwothree.get(2));
147 assertEquals(3, onetwothree.capacity());
148
149 assertEquals(0, buffer.position());
150 assertEquals(6, buffer.limit());
151
152 // is it really sliced?
153 buffer.put(2, 42);
154
155 assertEquals(42, buffer.get(2));
156 assertEquals(42, onetwothree.get(2));
157 }
158
159 @Test
160 public void test20Cleaner() {
161 final ByteBuffer byteBuffer = Buffers.newDirectByteBuffer(1024);
162 Buffers.Cleaner.clean(byteBuffer);
163 }
164
165 public static void main(final String args[]) throws IOException {
166 final String tstname = TestBuffers.class.getName();
167 org.junit.runner.JUnitCore.main(tstname);
168 }
169}
Access to NIO sun.misc.Cleaner, allowing caller to deterministically clean a given sun....
Definition: Buffers.java:1988
static boolean clean(final ByteBuffer bb)
If b is an direct NIO buffer, i.e sun.nio.ch.DirectBuffer, calls it's sun.misc.Cleaner instance clean...
Definition: Buffers.java:2043
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
static LongBuffer newDirectLongBuffer(final int numElements)
Allocates a new direct LongBuffer with the specified number of elements.
Definition: Buffers.java:176
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
Definition: Buffers.java:92
static FloatBuffer newDirectFloatBuffer(final int numElements)
Allocates a new direct FloatBuffer with the specified number of elements.
Definition: Buffers.java:134
static DoubleBuffer newDirectDoubleBuffer(final int numElements)
Allocates a new direct DoubleBuffer with the specified number of elements.
Definition: Buffers.java:113
static< B extends Buffer > B slice(final B buffer)
Calls slice on the specified buffer while maintaining the byteorder.
Definition: Buffers.java:501
static CharBuffer newDirectCharBuffer(final int numElements)
Allocates a new direct CharBuffer with the specified number of elements.
Definition: Buffers.java:218
static ShortBuffer newDirectShortBuffer(final int numElements)
Allocates a new direct ShortBuffer with the specified number of elements.
Definition: Buffers.java:197
static IntBuffer newDirectIntBuffer(final int numElements)
Allocates a new direct IntBuffer with the specified number of elements.
Definition: Buffers.java:155
static void main(final String args[])