Browse Source

Tests: PityTest randomInt() now using RangeInt

pull/6/head
heck 4 years ago
parent
commit
94939c3dff
  1. 3
      test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java
  2. 18
      test/java/foundation/pEp/pitytest/utils/TestUtils.java

3
test/java/foundation/pEp/jniadapter/test/jni118/TestAlice.java

@ -4,6 +4,7 @@ import static foundation.pEp.pitytest.TestLogger.*;
import foundation.pEp.jniadapter.*; import foundation.pEp.jniadapter.*;
import foundation.pEp.pitytest.*; import foundation.pEp.pitytest.*;
import foundation.pEp.pitytest.utils.RangeInt;
import foundation.pEp.pitytest.utils.TestUtils; import foundation.pEp.pitytest.utils.TestUtils;
import foundation.pEp.jniadapter.test.utils.*; 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 -> { 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())); AdapterTestUtils.addRcptsToOptFields(ctx.msgToBobDecrypted,Identity.toXKeyList(ctx.msgToBobDecrypted.getTo()));
log("running re_evaluate_message_rating() on:\n" + AdapterTestUtils.msgToString(ctx.msgToBobDecrypted, false)); log("running re_evaluate_message_rating() on:\n" + AdapterTestUtils.msgToString(ctx.msgToBobDecrypted, false));
Rating rat = ctx.engine.re_evaluate_message_rating(ctx.msgToBobDecrypted); Rating rat = ctx.engine.re_evaluate_message_rating(ctx.msgToBobDecrypted);

18
test/java/foundation/pEp/pitytest/utils/TestUtils.java

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

Loading…
Cancel
Save