GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestBuffersFloatDoubleConversion.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
29package com.jogamp.common.nio;
30
31import java.io.IOException;
32import org.junit.Assert;
33
34import org.junit.Test;
35
36import com.jogamp.junit.util.SingletonJunitCase;
37
38import org.junit.FixMethodOrder;
39import org.junit.runners.MethodSorters;
40
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
43
44 public static boolean cmpFloatArray(final float[] d1, final int d1_offset, final float[] d2, final int d2_offset, final int len) {
45 if( d1.length - d1_offset < len) {
46 throw new RuntimeException("d1 too small len "+len+" > "+d1.length+" - "+d1_offset);
47 }
48 if( d2.length - d2_offset < len) {
49 throw new RuntimeException("d2 too small len "+len+" > "+d2.length+" - "+d2_offset);
50 }
51 boolean ok = true;
52 for(int i=0; ok && i<len; i++) {
53 ok = d1[d1_offset+i] == d2[d2_offset+i] ;
54 }
55 return ok;
56 }
57
58 public static boolean cmpDoubleArray(final double[] d1, final int d1_offset, final double[] d2, final int d2_offset, final int len) {
59 if( d1.length - d1_offset < len) {
60 throw new RuntimeException("d1 too small len "+len+" > "+d1.length+" - "+d1_offset);
61 }
62 if( d2.length - d2_offset < len) {
63 throw new RuntimeException("d2 too small len "+len+" > "+d2.length+" - "+d2_offset);
64 }
65 boolean ok = true;
66 for(int i=0; ok && i<len; i++) {
67 ok = d1[d1_offset+i] == d2[d2_offset+i] ;
68 }
69 return ok;
70 }
71
72 public static void incrFloatArray(final float[] data, final int offset, final int len) {
73 if( data.length - offset < len) {
74 throw new RuntimeException("data too small len "+len+" > "+data.length+" - "+offset);
75 }
76 for(int i=0; i<len; i++) {
77 data[offset+i] += 1;
78 }
79 }
80
81 public static void incrDoubleArray(final double[] data, final int offset, final int len) {
82 if( data.length - offset < len) {
83 throw new RuntimeException("data too small len "+len+" > "+data.length+" - "+offset);
84 }
85 for(int i=0; i<len; i++) {
86 data[offset+i] += 1;
87 }
88 }
89
90 public static void setFloatArray(final float[] data, final int offset, final int len) {
91 if( data.length - offset < len) {
92 throw new RuntimeException("data too small len "+len+" > "+data.length+" - "+offset);
93 }
94 for(int i=0; i<len; i++) {
95 data[offset+i] = i;
96 }
97 }
98
99 public static void setDoubleArray(final double[] data, final int offset, final int len) {
100 if( data.length - offset < len) {
101 throw new RuntimeException("data too small len "+len+" > "+data.length+" - "+offset);
102 }
103 for(int i=0; i<len; i++) {
104 data[offset+i] = i;
105 }
106 }
107
108 public static void doItDoubleArray01(final double[] data, final int offset, final int len) {
109 final float[] f_data = Buffers.getFloatArray(data, offset, null, 0, len);
110 incrFloatArray(f_data, 0, len);
111 Buffers.getDoubleArray(f_data, 0, data, offset, len);
112 }
113
114 @Test
116 final int offset = 50;
117 final int len = 20;
118
119 // reference 1
120 final float[] fa_ref = new float[100];
121 setFloatArray(fa_ref, offset, len);
122 incrFloatArray(fa_ref, offset, len);
123
124 // reference 2
125 final double[] da_ref = new double[100];
126 setDoubleArray(da_ref, offset, len);
127 incrDoubleArray(da_ref, offset, len);
128
129 // test 1: forth and back .. double -> float -> double
130 {
131 final double[] da1 = new double[100];
132 setDoubleArray(da1, offset, len);
133 incrDoubleArray(da1, offset, len);
134
135 // conv_forth: double[offset..len] -> float[0..len]
136 final float[] f_da1 = Buffers.getFloatArray(da1, offset, null, 0, len);
137 Assert.assertTrue(cmpFloatArray(fa_ref, offset, f_da1, 0, len));
138
139 // conv_back: float[0..len] -> double[offset..len]
140 Buffers.getDoubleArray(f_da1, 0, da1, offset, len);
141 Assert.assertTrue(cmpDoubleArray(da_ref, offset, da1, offset, len));
142 }
143
144 // test 2: forth, incr, back .. double -> float -> incr -> double
145 {
146 final double[] da1 = new double[100];
147 setDoubleArray(da1, offset, len);
148
149 doItDoubleArray01(da1, offset, len);
150 Assert.assertTrue(cmpDoubleArray(da_ref, offset, da1, offset, len));
151 }
152 }
153
154 public static void main(final String args[]) throws IOException {
155 final String tstname = TestBuffersFloatDoubleConversion.class.getName();
156 org.junit.runner.JUnitCore.main(tstname);
157 }
158
159}
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
static double[] getDoubleArray(final float[] source, final int soffset, double[] dest, int doffset, int len)
Definition: Buffers.java:1085
static float[] getFloatArray(final double[] source, final int soffset, float[] dest, int doffset, int len)
Definition: Buffers.java:1038
static void doItDoubleArray01(final double[] data, final int offset, final int len)
static void incrDoubleArray(final double[] data, final int offset, final int len)
static boolean cmpFloatArray(final float[] d1, final int d1_offset, final float[] d2, final int d2_offset, final int len)
static void setFloatArray(final float[] data, final int offset, final int len)
static void incrFloatArray(final float[] data, final int offset, final int len)
static void setDoubleArray(final double[] data, final int offset, final int len)
static boolean cmpDoubleArray(final double[] d1, final int d1_offset, final double[] d2, final int d2_offset, final int len)