Skip to content

Commit

Permalink
Swap random to use Collections.List instead of native arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
antithesis-shomik committed Sep 4, 2024
1 parent 0d13d3e commit 421183e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
25 changes: 11 additions & 14 deletions sdk/src/main/java/com/antithesis/sdk/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.antithesis.sdk.internal.Internal;

import java.util.Optional;
import java.util.List;

/**
* The Random class provides methods that request both structured and unstructured randomness from the Antithesis environment.
Expand Down Expand Up @@ -42,23 +42,20 @@ public static long getRandom() {
* in a structured way enables it to provide more interesting
* choices over time.
*
* @param array An array of items to select from
* @param <T> Type of the array member items
* @return Randomly selected item from the provided array.
* @param list An array of items to select from
* @param <T> Type of the array member items
* @return Randomly selected item from the provided list.
*/
//public static <T> Optional<T> randomChoice(T[] array) {
public static <T> T randomChoice(T[] array) {
if (array.length == 0) {
return null; // Optional.empty();
} else if (array.length == 1) {
// return Optional.of(array[0]);
return array[0];
public static <T> T randomChoice(List<T> list) {
if (list.isEmpty()) {
return null;
} else if (list.size() == 1) {
return list.get(0);
} else {
// Safety: Result of modulo is always less than the divisor
// and will always fit into an integer
int idx = (int) Long.remainderUnsigned(getRandom(), array.length);
// return Optional.of(array[idx]);
return array[idx];
int idx = (int) Long.remainderUnsigned(getRandom(), list.size());
return list.get(idx);
}
}
}
25 changes: 14 additions & 11 deletions sdk/src/test/java/com/antithesis/sdk/RandomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import static org.junit.jupiter.api.Assertions.*;

public class RandomTest {
@Test
void randomNoChoice() {
String[] arr = {};
assertEquals(Random.randomChoice(arr), null);
List<String> list = new ArrayList<>();
assertNull(Random.randomChoice(list));
}

@Test
void randomOneChoice() {
String[] arr = {"ABc"};
assertEquals(Random.randomChoice(arr), "ABc");
List<String> list = new LinkedList<String>();
list.add("ABc");
assertEquals(Random.randomChoice(list), "ABc");
}

@Test
Expand All @@ -31,13 +35,12 @@ void randomFewChoices() {
counted_items.put('c', 0);
counted_items.put('d', 0);

Character[] all_keys = counted_items.keySet().toArray(new Character[0]);
assertEquals(counted_items.size(), all_keys.length);
List<Character> all_keys = new Vector<>(counted_items.keySet());
assertEquals(counted_items.size(), all_keys.size());
for (int i = 0; i < 25; i++) {
Character rc = Random.randomChoice(all_keys);
if (rc != null) {
Character choice = rc;
counted_items.compute(choice, (key, val) -> val + 1);
Character choice = Random.randomChoice(all_keys);
if (choice != null) {
counted_items.computeIfPresent(choice, (key, val) -> val + 1);
}
}
counted_items.forEach((key, val) -> assertNotEquals(val, 0, String.format("Did not produce the choice: %c", key)));
Expand Down

0 comments on commit 421183e

Please sign in to comment.