-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAssetEngine.java
More file actions
117 lines (99 loc) · 3.57 KB
/
TestAssetEngine.java
File metadata and controls
117 lines (99 loc) · 3.57 KB
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
package util;
import java.util.Arrays;
import java.util.Random;
/**
* Contains tools used to automate the creation of test assets.
*/
public class TestAssetEngine {
private Random rand = new Random();
public int upperBoundValue = Integer.MAX_VALUE;
public int upperBoundSize = 100000;
public TestAssetEngine() {}
public TestAssetEngine(TestAssetEngineBuilder builder) {
this.upperBoundValue = builder.upperBoundValue;
this.upperBoundSize = builder.upperBoundSize;
}
/**
* Generates a random signed integer.
* @return the randomly generated integer.
*/
public int generateRandomInteger() {
return generateSign() ? this.rand.nextInt(upperBoundValue) : -1 * this.rand.nextInt(upperBoundValue);
}
/**
* Generates a random signed integer given a specified upper bound.
* @param bound the upper bound to use during integer generation.
* @return the randomly generated integer.
*/
public int generateRandomIntegerWithBound(int bound) {
return generateSign() ? this.rand.nextInt(bound) : -1 * this.rand.nextInt(bound);
}
/**
* Generates an array of randomly-generated unordered integers.
* @return the generated array.
*/
public int[] generateUnorderedIntegerArray() {
int arraySize = this.rand.nextInt(this.upperBoundSize);
int[] unorderedArr = new int[arraySize];
for(int i = 0; i < unorderedArr.length; i++) {
unorderedArr[i] = generateRandomInteger();
}
return unorderedArr;
}
/**
* Generates an array of randomly-generated ordered integers.
* @return the generated array.
*/
public int[] generateOrderedIntegerArray() {
int arraySize = this.rand.nextInt(this.upperBoundSize);
int[] orderedArr = new int[arraySize];
for(int i = 0; i < orderedArr.length; i++) {
orderedArr[i] = generateRandomInteger();
}
Arrays.sort(orderedArr);
return orderedArr;
}
/**
* Randomly chooses an integer-index pair from a given array.
* @param arr the array to choose from.
* @return the randomly chosen ItemIndex value.
*/
public ItemIndex chooseRandomIntegerIndexFromArray(int[] arr) {
int index = this.rand.nextInt(arr.length);
return new ItemIndex(arr[index], index);
}
/**
* Randomly generates an integer that does not exist in a given array.
* @param arr the array to use for reference.
* @return the randomly generated integer.
*/
public int generateRandomIntegerNotInArray(int[] arr) {
int value = generateRandomIntegerWithBound(upperBoundValue);
while(Arrays.asList(arr).contains(value)) {
value = generateRandomIntegerWithBound(upperBoundValue);
}
return value;
}
/**
* Randomly generates a true or false value.
* @return the generated boolean.
*/
public boolean generateSign() {
return this.rand.nextInt(2) == 0 ? true : false;
}
/**
* Builder class used to quickly instantiate parameter boundaries for test assets.
*/
public static class TestAssetEngineBuilder {
public int upperBoundValue = Integer.MAX_VALUE;
public int upperBoundSize = 100000;
public TestAssetEngineBuilder upperBoundValue(int upperBoundValue){
this.upperBoundValue = upperBoundValue;
return this;
}
public TestAssetEngineBuilder upperBoundSize(int upperBoundSize) {
this.upperBoundSize = upperBoundSize;
return this;
}
}
}