1 package eu.simuline.testhelpers;
2
3 import java.util.List;
4 import java.util.ArrayList;
5
6
7
8
9
10
11
12
13
14
15 public abstract class DTestHelper {
16
17
18
19
20
21
22
23
24
25
26
27
28
29 static double random(boolean signed) {
30 return signed ? 2. * (Math.random() - 0.5) : Math.random();
31 }
32
33 public static double createArgD(boolean isSigned) {
34 double cand;
35 cand = Math.random();
36 cand *= createPower2();
37 cand *= isSigned ? Math.signum(Math.random() - 0.5) : 1;
38 return cand;
39 }
40
41
42
43
44
45
46 public static double createArgD(boolean isSigned, int exp0, int exp1) {
47 double cand;
48 cand = Math.random();
49 cand *= Math.pow(2.0, (exp1 - exp0) * Math.random() + exp0);
50 cand *= isSigned ? Math.signum(Math.random() - 0.5) : 1;
51 return cand;
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public static List<Double> createMultArgsSumD(int numArgs,
74 boolean signed,
75 boolean inRange) {
76
77
78 List<Double> resultD = new ArrayList<Double>(numArgs);
79
80 if (numArgs == 0) {
81 return resultD;
82 }
83
84 double sum = 0;
85 double max = 0;
86 double cand;
87 for (int i = 0; i < numArgs; i++) {
88 cand = random(signed);
89
90 max = Math.max(max, Math.abs(cand));
91 resultD.add(cand);
92 sum += cand;
93 }
94
95
96
97 if (inRange) {
98 int sgn = (int) Math.signum(sum);
99
100
101 max = Math.max(max, Math.abs(sum));
102
103 double shift = Math.pow(2,
104
105 Math.ceil(Math.log(max) / Math.log(2)));
106
107
108
109
110 assert -1 < sum / shift && sum / shift < 1;
111 assert -1 < max / shift && max / shift < 1;
112
113 for (int i = 0; i < numArgs; i++) {
114
115
116 resultD.set(i, (double) resultD.get(i) / shift * sgn);
117 }
118 }
119
120 return resultD;
121 }
122
123 private static final int MAX_NUM_ARGS = 10;
124
125
126 private static int createNumArgs() {
127 return 1 + (int) Math.round(MAX_NUM_ARGS * Math.random());
128 }
129
130 public static List<Double> createMultArgsSumD(boolean signed,
131 boolean inRange) {
132 return createMultArgsSumD(createNumArgs(), signed, inRange);
133 }
134
135 private static final double FRAC_NON_NAN = 0.95;
136
137 public static List<Double> createMultArgsD(int numArgs,
138 boolean allowsSigned,
139 boolean inRange,
140 boolean allowsNaN) {
141
142 List<Double> resultD = new ArrayList<Double>(numArgs);
143 double num;
144 for (int i = 0; i < numArgs; i++) {
145 if (allowsNaN && Math.random() > FRAC_NON_NAN) {
146 num = Double.NaN;
147 } else {
148 num = Math.random();
149 num *= inRange ? 1 : createPower2();
150 num *= allowsSigned ? Math.signum(Math.random() - 0.5) : 1;
151 }
152
153 resultD.add(num);
154 }
155
156 return resultD;
157 }
158
159 private static final int RANGE_POW2 = 50;
160
161 private static double createPower2() {
162 return Math.pow(2.0, 2 * RANGE_POW2 * Math.random() - RANGE_POW2);
163
164
165 }
166
167 public static List<Double> createMultArgsD(boolean signed,
168 boolean inRange,
169 boolean allowsNaN) {
170 return createMultArgsD(createNumArgs(), signed, inRange, allowsNaN);
171 }
172
173 }