Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestMemBuffers00.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package jau.test.io;
26
27import java.nio.ByteBuffer;
28import java.nio.ByteOrder;
29
30import org.jau.io.Buffers;
31import org.jau.io.MemUtil;
32import org.junit.Assert;
33import org.junit.FixMethodOrder;
34import org.junit.Test;
35import org.junit.runners.MethodSorters;
36
37import jau.pkg.PlatformRuntime;
38import jau.test.junit.util.JunitTracer;
39
40@FixMethodOrder(MethodSorters.NAME_ASCENDING)
41public class TestMemBuffers00 extends JunitTracer {
42 static final boolean DEBUG = false;
43
44 @Test(timeout = 10000)
45 public final void test01_ByteBuffer() {
46 PlatformRuntime.checkInitialized();
47 final int len = 10;
48
49 final ByteBuffer b1 = Buffers.newDirectByteBuffer(len * 2);
50 Assert.assertEquals(len * 2, b1.capacity());
51 Assert.assertEquals(len * 2, b1.limit());
52 Assert.assertEquals(0, b1.position());
53 Assert.assertEquals(len * 2, b1.remaining());
54 Assert.assertEquals(ByteOrder.nativeOrder(), b1.order());
55
56 for(int i=0; i<len; ++i) {
57 b1.put((byte)0xaa);
58 }
59 Assert.assertEquals(len, b1.position());
60 b1.flip();
61 Assert.assertEquals(len, b1.limit());
62 Assert.assertEquals(0, b1.position());
63 Assert.assertEquals(len, b1.remaining());
64
65 Assert.assertEquals((byte)0xaa, b1.get(0));
66 Assert.assertEquals((byte)0xaa, b1.get(len-1));
67 MemUtil.zeroByteBuffer(b1);
68 Assert.assertEquals((byte)0x00, b1.get(0));
69 Assert.assertEquals((byte)0x00, b1.get(len-1));
70 }
71
72/**
73 @Test(timeout = 10000)
74 public final void test02_String_ByteBuffer() {
75 PlatformRuntime.checkInitialized();
76 final ByteBuffer b0;
77 {
78 final String s1 = "0123456789"; // len = 10
79 b0 = CPUtils.to_ByteBuffer(s1, false);
80 final String s2 = new String(s1.getBytes()); // true copy before deletion
81 final boolean r2 = CPUtils.zeroString(s2);
82 // Assert.assertTrue( r1 );
83 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1a: s1: '%s', len %d\n", s1, s1.length());
84 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1a: s2: '%s', len %d, zeroString result %b\n", s2, s2.length(), r2);
85
86 final boolean r1 = CPUtils.zeroString(s1);
87 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1b: s1: '%s', len %d, zeroString result %b\n", s1, s1.length(), r1);
88 }
89 {
90 // Ooops, the internalized string at compile time got zeroed out
91 final String s1 = "0123456789"; // len = 10
92 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1c: s1: '%s', len %d\n", s1, s1.length());
93 }
94 {
95 // That works, using dynamic storage
96 final String s1 = CPUtils.to_String(b0, StandardCharsets.UTF_8);
97 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1d: s1: '%s', len %d\n", s1, s1.length());
98 }
99 {
100 final String s1 = CPUtils.to_String(b0, StandardCharsets.UTF_8);
101 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.2a: s1: '%s', len %d\n", s1, s1.length());
102
103 final ByteBuffer b1 = CPUtils.newDirectByteBuffer(s1.length() * 2);
104 Assert.assertEquals(s1.length() * 2, b1.capacity());
105 Assert.assertEquals(s1.length() * 2, b1.limit());
106 Assert.assertEquals(0, b1.position());
107 Assert.assertEquals(s1.length() * 2, b1.remaining());
108
109 b1.put(s1.getBytes(StandardCharsets.UTF_8));
110 Assert.assertEquals(s1.length(), b1.position());
111 b1.flip();
112 Assert.assertEquals(s1.length(), b1.limit());
113 Assert.assertEquals(0, b1.position());
114 Assert.assertEquals(s1.length(), b1.remaining());
115 {
116 final String b1_str = CPUtils.to_String(b1, StandardCharsets.UTF_8);
117 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.2b: b1: '%s', len %d/%d\n", b1_str, b1_str.length(), b1.limit());
118 }
119 Assert.assertEquals(s1.length(), b1.limit());
120 Assert.assertEquals(0, b1.position());
121 Assert.assertEquals(s1.length(), b1.remaining());
122
123 final String s2 = new String(s1.getBytes()); // true copy
124 final ByteBuffer b2 = CPUtils.to_ByteBuffer(s2, true);
125 Assert.assertEquals(s1.length(), b2.capacity());
126 Assert.assertEquals(s1.length(), b2.limit());
127 Assert.assertEquals(0, b2.position());
128 Assert.assertEquals(s1.length(), b2.remaining());
129 {
130 final byte[] b2_bytes = new byte[b2.remaining()];
131 b2.get(b2_bytes);
132 b2.rewind(); // yada yada yada (relative get, absolute @ JDK13
133 final String b2_str = new String(b2_bytes, StandardCharsets.UTF_8);
134 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.2c: b2: '%s', len %d/%d\n", b2_str, b2_str.length(), b2.limit());
135 }
136 }
137 }
138*/
139 public static void main(final String args[]) {
140 org.junit.runner.JUnitCore.main(TestMemBuffers00.class.getName());
141 }
142}
static void main(final String args[])