Browse Source

Tests: PityTest randomInt() now using RangeInt

pull/6/head
heck 1 year ago
parent
commit
94939c3dff
2 changed files with 10 additions and 11 deletions
  1. +2
    -1
      test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java
  2. +8
    -10
      test/java/foundation/pEp/pitytest/utils/TestUtils.java

+ 2
- 1
test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java View File

@ -4,6 +4,7 @@ import static foundation.pEp.pitytest.TestLogger.*;
import foundation.pEp.jniadapter.*;
import foundation.pEp.pitytest.*;
import foundation.pEp.pitytest.utils.RangeInt;
import foundation.pEp.pitytest.utils.TestUtils;
import foundation.pEp.jniadapter.test.utils.*;
@ -76,7 +77,7 @@ class TestAlice {
});
new TestUnit<JNI1118Context>("re_evaluate_message_rating() equal to decrypt_message_result.rating when Message has random rating string on XEncStatus", new JNI1118Context(), ctx -> {
AdapterTestUtils.addRatingToOptFields(ctx.msgToBobDecrypted, TestUtils.randomASCIIString(TestUtils.CharClass.Unbounded, TestUtils.randomInt(0,42)));
AdapterTestUtils.addRatingToOptFields(ctx.msgToBobDecrypted, TestUtils.randomASCIIString(TestUtils.CharClass.All, TestUtils.randomInt(new RangeInt(0,42))));
AdapterTestUtils.addRcptsToOptFields(ctx.msgToBobDecrypted,Identity.toXKeyList(ctx.msgToBobDecrypted.getTo()));
log("running re_evaluate_message_rating() on:\n" + AdapterTestUtils.msgToString(ctx.msgToBobDecrypted, false));
Rating rat = ctx.engine.re_evaluate_message_rating(ctx.msgToBobDecrypted);


+ 8
- 10
test/java/foundation/pEp/pitytest/utils/TestUtils.java View File

@ -319,6 +319,8 @@ public class TestUtils {
Math Utils
*/
private static Random rand = new Random();
public static int clip(int val, int min, int max) {
return Math.max(min, Math.min(max, val));
}
@ -331,24 +333,20 @@ public class TestUtils {
Random data generators
*/
public static int randomInt(int min, int max) {
public static int randomInt(RangeInt range) {
int ret = 0;
int range = max - min;
if (range <= 0) {
range = 1;
}
Random rand = new Random();
ret = rand.nextInt(range) + min;
ret = rand.nextInt(range.getSize()) + range.effectiveMin();
return ret;
}
public static String randomASCIIString(CharClass charClass, int len) {
byte[] array = new byte[len]; // length is bounded by 7
int rangeMin = 0;
int rangeMax = 0;
switch (charClass) {
case Unbounded: {
case All: {
rangeMin = 0;
rangeMax = 255;
break;
@ -371,14 +369,14 @@ public class TestUtils {
}
new Random().nextBytes(array);
for (int i = 0; i < array.length; i++) {
array[i] = (byte)clip(randomInt(rangeMin, rangeMax),0,255);
array[i] = (byte) clip(randomInt(new RangeInt(rangeMin, rangeMax)), 0, 255);
}
String generatedString = new String(array, Charset.forName("UTF-8"));
return generatedString;
}
public enum CharClass {
Unbounded(0) {
All(0) {
@Override
public String toString() {
return "Unbounded";


Loading…
Cancel
Save