Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestValueConversion.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2012 Gothel Software e.K.
5 * Copyright (c) 2012 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util;
28
29import static org.jau.util.ValueConv.*;
30
31import org.junit.Assert;
32/**
33 * Testing ValueConv's value conversion of primitive types
34 */
35import org.junit.FixMethodOrder;
36import org.junit.Test;
37import org.junit.runners.MethodSorters;
38
39@FixMethodOrder(MethodSorters.NAME_ASCENDING)
40public class TestValueConversion {
41
42 @Test
43 public void testBaseFloat() {
44 Assert.assertEquals(Byte.MAX_VALUE, float_to_byte( 1.0f, true));
45 Assert.assertEquals(Byte.MIN_VALUE, float_to_byte(-1.0f, true));
46 Assert.assertEquals( 1.0f, byte_to_float( Byte.MAX_VALUE, true), 0.0);
47 Assert.assertEquals(-1.0f, byte_to_float( Byte.MIN_VALUE, true), 0.0);
48
49 Assert.assertEquals(Short.MAX_VALUE, float_to_short( 1.0f, true));
50 Assert.assertEquals(Short.MIN_VALUE, float_to_short(-1.0f, true));
51 Assert.assertEquals( 1.0f, short_to_float( Short.MAX_VALUE, true), 0.0);
52 Assert.assertEquals(-1.0f, short_to_float( Short.MIN_VALUE, true), 0.0);
53
54 Assert.assertEquals(Integer.MAX_VALUE, float_to_int( 1.0f, true));
55 Assert.assertEquals(Integer.MIN_VALUE, float_to_int(-1.0f, true));
56 Assert.assertEquals( 1.0f, int_to_float( Integer.MAX_VALUE, true), 0.0);
57 Assert.assertEquals(-1.0f, int_to_float( Integer.MIN_VALUE, true), 0.0);
58
59 Assert.assertEquals((byte)0xff, float_to_byte( 1.0f, false));
60 Assert.assertEquals( 1.0f, byte_to_float( (byte)0xff, false), 0.0);
61
62 Assert.assertEquals((short)0xffff, float_to_short( 1.0f, false));
63 Assert.assertEquals( 1.0f, short_to_float( (short)0xffff, false), 0.0);
64
65 Assert.assertEquals(0xffffffff, float_to_int( 1.0f, false));
66 Assert.assertEquals( 1.0f, int_to_float( 0xffffffff, false), 0.0);
67 }
68
69 @Test
70 public void testBaseDouble() {
71 Assert.assertEquals(Byte.MAX_VALUE, double_to_byte( 1.0, true));
72 Assert.assertEquals(Byte.MIN_VALUE, double_to_byte(-1.0, true));
73 Assert.assertEquals( 1.0, byte_to_double( Byte.MAX_VALUE, true), 0.0);
74 Assert.assertEquals(-1.0, byte_to_double( Byte.MIN_VALUE, true), 0.0);
75
76 Assert.assertEquals(Short.MAX_VALUE, double_to_short( 1.0, true));
77 Assert.assertEquals(Short.MIN_VALUE, double_to_short(-1.0, true));
78 Assert.assertEquals( 1.0, short_to_double( Short.MAX_VALUE, true), 0.0);
79 Assert.assertEquals(-1.0, short_to_double( Short.MIN_VALUE, true), 0.0);
80
81 Assert.assertEquals(Integer.MAX_VALUE, double_to_int( 1.0, true));
82 Assert.assertEquals(Integer.MIN_VALUE, double_to_int(-1.0, true));
83 Assert.assertEquals( 1.0, int_to_double( Integer.MAX_VALUE, true), 0.0);
84 Assert.assertEquals(-1.0, int_to_double( Integer.MIN_VALUE, true), 0.0);
85
86 Assert.assertEquals((byte)0xff, double_to_byte( 1.0, false));
87 Assert.assertEquals( 1.0, byte_to_double( (byte)0xff, false), 0.0);
88
89 Assert.assertEquals((short)0xffff, double_to_short( 1.0, false));
90 Assert.assertEquals( 1.0, short_to_double( (short)0xffff, false), 0.0);
91
92 Assert.assertEquals(0xffffffff, double_to_int( 1.0, false));
93 Assert.assertEquals( 1.0, int_to_double( 0xffffffff, false), 0.0);
94 }
95
96 @Test
97 public void testConversion() {
98 final byte sb0 = 127;
99 final byte sb1 = -128;
100
101 final float sf0 = byte_to_float(sb0, true);
102 final float sf1 = byte_to_float(sb1, true);
103 final short ss0 = byte_to_short(sb0, true, true);
104 final short ss1 = byte_to_short(sb1, true, true);
105 final int si0 = byte_to_int(sb0, true, true);
106 final int si1 = byte_to_int(sb1, true, true);
107
108 Assert.assertEquals(1.0f, sf0, 0.0);
109 Assert.assertEquals(-1.0f, sf1, 0.0);
110 Assert.assertEquals(Short.MAX_VALUE, ss0);
111 Assert.assertEquals(Short.MIN_VALUE, ss1);
112 Assert.assertEquals(Integer.MAX_VALUE, si0);
113 Assert.assertEquals(Integer.MIN_VALUE, si1);
114
115 Assert.assertEquals(sb0, short_to_byte(ss0, true, true));
116 Assert.assertEquals(sb1, short_to_byte(ss1, true, true));
117 Assert.assertEquals(sb0, int_to_byte(si0, true, true));
118 Assert.assertEquals(sb1, int_to_byte(si1, true, true));
119
120 final byte ub0 = (byte) 0xff;
121 final float uf0 = byte_to_float(ub0, false);
122 final short us0 = byte_to_short(ub0, false, false);
123 final int ui0 = byte_to_int(ub0, false, false);
124
125 Assert.assertEquals(1.0f, uf0, 0.0);
126 Assert.assertEquals((short)0xffff, us0);
127 Assert.assertEquals(0xffffffff, ui0);
128
129 Assert.assertEquals(ub0, short_to_byte(us0, false, false));
130 Assert.assertEquals(us0, int_to_short(ui0, false, false));
131 }
132
133 public static void main(final String args[]) {
134 org.junit.runner.JUnitCore.main(TestValueConversion.class.getName());
135 }
136
137}
static void main(final String args[])