GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
TestValueConversion.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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.util;
30
31import org.junit.Assert;
32
33import org.junit.Test;
34
35import static com.jogamp.common.util.ValueConv.*;
36
37/**
38 * Testing ValueConv's value conversion of primitive types
39 */
40import org.junit.FixMethodOrder;
41import org.junit.runners.MethodSorters;
42
43@FixMethodOrder(MethodSorters.NAME_ASCENDING)
44public class TestValueConversion {
45
46 @Test
47 public void testBaseFloat() {
48 Assert.assertEquals(Byte.MAX_VALUE, float_to_byte( 1.0f, true));
49 Assert.assertEquals(Byte.MIN_VALUE, float_to_byte(-1.0f, true));
50 Assert.assertEquals( 1.0f, byte_to_float( Byte.MAX_VALUE, true), 0.0);
51 Assert.assertEquals(-1.0f, byte_to_float( Byte.MIN_VALUE, true), 0.0);
52
53 Assert.assertEquals(Short.MAX_VALUE, float_to_short( 1.0f, true));
54 Assert.assertEquals(Short.MIN_VALUE, float_to_short(-1.0f, true));
55 Assert.assertEquals( 1.0f, short_to_float( Short.MAX_VALUE, true), 0.0);
56 Assert.assertEquals(-1.0f, short_to_float( Short.MIN_VALUE, true), 0.0);
57
58 Assert.assertEquals(Integer.MAX_VALUE, float_to_int( 1.0f, true));
59 Assert.assertEquals(Integer.MIN_VALUE, float_to_int(-1.0f, true));
60 Assert.assertEquals( 1.0f, int_to_float( Integer.MAX_VALUE, true), 0.0);
61 Assert.assertEquals(-1.0f, int_to_float( Integer.MIN_VALUE, true), 0.0);
62
63 Assert.assertEquals((byte)0xff, float_to_byte( 1.0f, false));
64 Assert.assertEquals( 1.0f, byte_to_float( (byte)0xff, false), 0.0);
65
66 Assert.assertEquals((short)0xffff, float_to_short( 1.0f, false));
67 Assert.assertEquals( 1.0f, short_to_float( (short)0xffff, false), 0.0);
68
69 Assert.assertEquals(0xffffffff, float_to_int( 1.0f, false));
70 Assert.assertEquals( 1.0f, int_to_float( 0xffffffff, false), 0.0);
71 }
72
73 @Test
74 public void testBaseDouble() {
75 Assert.assertEquals(Byte.MAX_VALUE, double_to_byte( 1.0, true));
76 Assert.assertEquals(Byte.MIN_VALUE, double_to_byte(-1.0, true));
77 Assert.assertEquals( 1.0, byte_to_double( Byte.MAX_VALUE, true), 0.0);
78 Assert.assertEquals(-1.0, byte_to_double( Byte.MIN_VALUE, true), 0.0);
79
80 Assert.assertEquals(Short.MAX_VALUE, double_to_short( 1.0, true));
81 Assert.assertEquals(Short.MIN_VALUE, double_to_short(-1.0, true));
82 Assert.assertEquals( 1.0, short_to_double( Short.MAX_VALUE, true), 0.0);
83 Assert.assertEquals(-1.0, short_to_double( Short.MIN_VALUE, true), 0.0);
84
85 Assert.assertEquals(Integer.MAX_VALUE, double_to_int( 1.0, true));
86 Assert.assertEquals(Integer.MIN_VALUE, double_to_int(-1.0, true));
87 Assert.assertEquals( 1.0, int_to_double( Integer.MAX_VALUE, true), 0.0);
88 Assert.assertEquals(-1.0, int_to_double( Integer.MIN_VALUE, true), 0.0);
89
90 Assert.assertEquals((byte)0xff, double_to_byte( 1.0, false));
91 Assert.assertEquals( 1.0, byte_to_double( (byte)0xff, false), 0.0);
92
93 Assert.assertEquals((short)0xffff, double_to_short( 1.0, false));
94 Assert.assertEquals( 1.0, short_to_double( (short)0xffff, false), 0.0);
95
96 Assert.assertEquals(0xffffffff, double_to_int( 1.0, false));
97 Assert.assertEquals( 1.0, int_to_double( 0xffffffff, false), 0.0);
98 }
99
100 @Test
101 public void testConversion() {
102 final byte sb0 = 127;
103 final byte sb1 = -128;
104
105 final float sf0 = byte_to_float(sb0, true);
106 final float sf1 = byte_to_float(sb1, true);
107 final short ss0 = byte_to_short(sb0, true, true);
108 final short ss1 = byte_to_short(sb1, true, true);
109 final int si0 = byte_to_int(sb0, true, true);
110 final int si1 = byte_to_int(sb1, true, true);
111
112 Assert.assertEquals(1.0f, sf0, 0.0);
113 Assert.assertEquals(-1.0f, sf1, 0.0);
114 Assert.assertEquals(Short.MAX_VALUE, ss0);
115 Assert.assertEquals(Short.MIN_VALUE, ss1);
116 Assert.assertEquals(Integer.MAX_VALUE, si0);
117 Assert.assertEquals(Integer.MIN_VALUE, si1);
118
119 Assert.assertEquals(sb0, short_to_byte(ss0, true, true));
120 Assert.assertEquals(sb1, short_to_byte(ss1, true, true));
121 Assert.assertEquals(sb0, int_to_byte(si0, true, true));
122 Assert.assertEquals(sb1, int_to_byte(si1, true, true));
123
124 final byte ub0 = (byte) 0xff;
125 final float uf0 = byte_to_float(ub0, false);
126 final short us0 = byte_to_short(ub0, false, false);
127 final int ui0 = byte_to_int(ub0, false, false);
128
129 Assert.assertEquals(1.0f, uf0, 0.0);
130 Assert.assertEquals((short)0xffff, us0);
131 Assert.assertEquals(0xffffffff, ui0);
132
133 Assert.assertEquals(ub0, short_to_byte(us0, false, false));
134 Assert.assertEquals(us0, int_to_short(ui0, false, false));
135 }
136
137 public static void main(final String args[]) {
138 org.junit.runner.JUnitCore.main(TestValueConversion.class.getName());
139 }
140
141}