-
Notifications
You must be signed in to change notification settings - Fork 56
/
TestUtil.java
220 lines (180 loc) · 5.11 KB
/
TestUtil.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*Copyright ©2019 TommyLemon(https://github.com/TommyLemon/UnitAuto)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package unitauto.test;
import java.util.Arrays;
/**测试工具类
* @author Lemon
*/
public class TestUtil {
public static long plus(long a, long b) {
return a + b;
}
public static double plus(double a, double b) {
return a + b;
}
public static double plus(Number a, Number b) {
return plus(a.doubleValue(), b.doubleValue());
}
public static long minus(long a, long b) {
return a - b;
}
public static double minus(double a, double b) {
return a - b;
}
public static double minus(Number a, Number b) {
return minus(a.doubleValue(), b.doubleValue());
}
public static long multiply(long a, long b) {
return a * b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double multiply(Number a, Number b) {
return multiply(a.doubleValue(), b.doubleValue());
}
public static double divide(long a, long b) {
return divide((double) a, (double) b); //直接相除会白自动强转为 long,1/2 = 0 ! a / b;
}
public static double divide(double a, double b) {
return a / b;
}
public static double divide(Number a, Number b) {
return divide(a.doubleValue(), b.doubleValue());
}
public static long pow(long a, long b) {
return (long) Math.pow(a, b);
}
public static double pow(double a, double b) {
return Math.pow(a, b);
}
public static double pow(Number a, Number b) {
return pow(a.doubleValue(), b.doubleValue());
}
public static double sqrt(long a) {
return Math.sqrt(a);
}
public static double sqrt(double a) {
return Math.sqrt(a);
}
public static double sqrt(Number a) {
return sqrt(a.doubleValue());
}
public static int[] sort(int[] arr) {
Arrays.sort(arr);
return arr;
}
protected static void sort(Number[] arr) {
Arrays.sort(arr);
}
private static void sort(String[] arr) {
Arrays.sort(arr);
}
public static TestBean findBean(TestBean[] beans, Long id) {
if (id == null || beans == null || beans.length <= 0) {
return null;
}
for (TestBean bean : beans) {
if (bean != null && bean.getId() == id) {
return bean;
}
}
return null;
}
public static void setPoint(Object[][] points, int x, int y, Object val) {
int rows = points == null ? 0 : points.length;
if (x < 0 || x >= rows) {
throw new ArrayIndexOutOfBoundsException("x = " + x + " 超出了矩阵行数!");
}
Object[] ps = points[x];
int cols = ps == null ? 0 : ps.length;
if (y < 0 || y >= cols) {
throw new ArrayIndexOutOfBoundsException("y = " + y + " 超出了矩阵宽度!");
}
points[x][y] = val;
}
public static Long computeAsync(long a, long b, TestInterface callback) {
callback.setData("Mock outer interface success!");
Boolean sort = callback.sort();
if (sort != null && sort && a > b) {
callback.minusAsId(b, a);
}
else {
callback.minusAsId(a, b);
}
return callback.getId();
}
public static Number computeAsync(long a, long b, Callback callback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (Exception e) {}
Boolean sort = callback.sort();
if (sort != null && sort && a > b) {
callback.minusAsId(b, a);
} else {
callback.minusAsId(a, b);
}
callback.setData("Mock inner interface success!");
}
}).start();
return callback.getId();
}
public static <D> D computeAsync(long a, long b, D d, Callback1<D> callback) {
callback.minusAsId(a, b);
callback.setData(d);
return callback.getData();
}
public static <L, D> Callback2<L, D> computeAsync(L a, L b, D d, Callback2<L, D> callback) {
callback.setA(a);
callback.setB(b);
callback.setData(d);
callback.append(a, b);
return callback;
}
//需要用 $ 隔开内部类与它所在的类 apijson.demo.server.MathUtil$Callback
public interface Callback2<L, D> {
void setId(L id);
L getId();
void setData(D data);
D getData();
void setA(L a);
L getA();
void setB(L b);
L getB();
default String append(L a, L b) {
return "a=" + a + "; b=" + b;
}
}
public interface Callback1<D> extends Callback2<Long, D> {
Boolean sort();
default void minusAsId(long a, long b) {
setA(a);
setB(b);
setId(a + b);
}
}
public interface Callback extends Callback1<String> {
static long currentTime() {
return System.currentTimeMillis();
}
@Override
default void minusAsId(long a, long b) {
System.out.println("minusAsId startTime: " + currentTime());
setA(a);
setB(b);
setId(a + b);
System.out.println("minusAsId endTime: " + currentTime());
}
}
}