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 *= Math.pow(2.0,100*Math.random()-50);
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
124 public static List<Double> createMultArgsSumD(boolean signed,
125 boolean inRange) {
126 int numArgs = 1+(int)Math.round(10*Math.random());
127 return createMultArgsSumD(numArgs, signed, inRange);
128 }
129
130 public static List<Double> createMultArgsD(int numArgs,
131 boolean allowsSigned,
132 boolean inRange,
133 boolean allowsNaN) {
134
135 List<Double> resultD = new ArrayList<Double>(numArgs);
136 double num;
137 for (int i = 0; i < numArgs; i++) {
138 if (allowsNaN && Math.random() > 0.95) {
139 num = Double.NaN;
140 } else {
141 num = Math.random();
142 num *= inRange ? 1 : Math.pow(2.0,100*Math.random()-50);
143 num *= allowsSigned ? Math.signum(Math.random() - 0.5) : 1;
144 }
145
146
147 resultD.add(num);
148 }
149
150 return resultD;
151 }
152
153 public static List<Double> createMultArgsD(boolean signed,
154 boolean inRange,
155 boolean allowsNaN) {
156 int numArgs = 1+(int)Math.round(10*Math.random());
157 return createMultArgsD(numArgs, signed, inRange, allowsNaN);
158 }
159
160 }