-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumberUtilsTest.java
111 lines (92 loc) · 3.53 KB
/
NumberUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2013 Stephen Asbury
* Released under the MIT license
* http://opensource.org/licenses/MIT
*/
import junit.framework.*;
public class NumberUtilsTest extends TestCase
{
public void testConvertShort()
{
short[] numbers = {-10,10,0,124,-124,Short.MIN_VALUE,Short.MAX_VALUE};
for(int i=0,max=numbers.length;i<max;i++)
{
assertEquals(numbers[i],NumberUtils.convertToShort(NumberUtils.convertToBinary(numbers[i])));
}
}
public void testConvertInt()
{
int[] numbers = {-10,10,0,124,-124,Short.MIN_VALUE,Short.MAX_VALUE,Integer.MIN_VALUE,Integer.MAX_VALUE};
for(int i=0,max=numbers.length;i<max;i++)
{
assertEquals(numbers[i],NumberUtils.convertToInt(NumberUtils.convertToBinary(numbers[i])));
}
}
public void testConvertLong()
{
long[] numbers = {-10,10,0,124,-124,Short.MIN_VALUE,Short.MAX_VALUE,Integer.MIN_VALUE,Integer.MAX_VALUE,Long.MIN_VALUE,Long.MAX_VALUE};
for(int i=0,max=numbers.length;i<max;i++)
{
assertEquals(numbers[i],NumberUtils.convertToLong(NumberUtils.convertToBinary(numbers[i])));
}
}
public void testConvertFloat()
{
float[] numbers = {-10,10,0,124,-124,Short.MIN_VALUE,Short.MAX_VALUE,0.6f,-0.6f,Float.MIN_VALUE,Float.MAX_VALUE,Float.POSITIVE_INFINITY};
for(int i=0,max=numbers.length;i<max;i++)
{
assertTrue(numbers[i]==NumberUtils.convertToFloat(NumberUtils.convertToBinary(numbers[i])));
}
}
public void testConvertDouble()
{
double[] numbers = {-10,10,0,124,-124,Short.MIN_VALUE,Short.MAX_VALUE,0.666,-0.666,Double.MAX_VALUE,Double.MIN_VALUE,Double.POSITIVE_INFINITY};
for(int i=0,max=numbers.length;i<max;i++)
{
assertTrue(numbers[i]==NumberUtils.convertToDouble(NumberUtils.convertToBinary(numbers[i])));
}
}
public void testSum()
{
int total=0;
for(int i=0;i<101;i++)
{
total+=i;
assertEquals(total,NumberUtils.calculateSum(i));
}
}
public void testMapToIntervalInt()
{
assertEquals(0,NumberUtils.mapToInterval(0,0,100,10));
assertEquals(0,NumberUtils.mapToInterval(1,0,100,10));
assertEquals(10,NumberUtils.mapToInterval(14,0,100,10));
assertEquals(80,NumberUtils.mapToInterval(79,0,100,10));
assertEquals(100,NumberUtils.mapToInterval(99,0,100,10));
}
public void testEncodeDecodeInt()
{
int encoded,actual,mapped;
byte[] bits;
for(int i=0,max=101;i<max;i++)
{
actual = i;
mapped = NumberUtils.mapToInterval(actual,0,100,50);
bits = NumberUtils.encodeToInterval(actual,0,100,50);
encoded = NumberUtils.decodeFromInterval(bits,0,100,50);
assertEquals(mapped,encoded);
}
}
public void testEncodeDecodeDouble()
{
double encoded,actual,mapped;
byte[] bits;
for(int i=0,max=10100;i<max;i++)
{
actual = i*0.01;
mapped = NumberUtils.mapToInterval(actual,0.0,100.0,1000);
bits = NumberUtils.encodeToInterval(actual,0.0,100.0,1000);
encoded = NumberUtils.decodeFromInterval(bits,0.0,100.0,1000);
assertTrue(mapped==encoded);
}
}
}