Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

David s pumpkins patch 1 #15

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/randomness/ExtendedRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,28 @@ public static <E> E nextObject(E[] array) {

// TODO: Write tests for this
public static <E> E nextObject(List<E> list) {
return list.get(0);
if (list.size() == 0) {
String excMsg = "List " + list.toString()
+ " has no elements to choose from";
throw new NoSuchElementException(excMsg);
}
int index = RANDOM.nextInt(list.size());
return list.get(index);
}

// TODO: Write tests for this
public static <E> E nextObject(Set<E> set) {
if (set.size() == 0) {
String excMsg = "Set " + set.toString()
+ " has no elements to choose from";
throw new NoSuchElementException(excMsg);
}
int index = RANDOM.nextInt(set.size());
Iterator<E> iterator = set.iterator();
int curr = 0;
while (curr < index) {
iterator.next();
}
return iterator.next();
}

Expand Down
72 changes: 71 additions & 1 deletion test/randomness/ExtendedRandomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.math.BigInteger;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -274,4 +276,72 @@ void testNextObjectEmptyArrayThrowsException() {
System.out.println("\"" + excMsg + "\"");
}

}
@Test
void testNextObjectList() {
int capacity = RANDOM.nextInt(32) + 8;
List<LocalDateTime> list = new ArrayList<>(capacity);
Set<LocalDateTime> expected = new HashSet<>(capacity);
LocalDateTime curr = LocalDateTime.now();
for (int i = 0; i < capacity; i++) {
curr = curr.minusHours((long) i * RANDOM.nextInt(24));
list.add(curr);
expected.add(curr);
}
Set<LocalDateTime> actual = new HashSet<>(capacity);
int numberOfCalls = 20 * capacity;
for (int j = 0; j < numberOfCalls; j++) {
actual.add(ExtendedRandom.nextObject(list));
}
String msg = "After " + numberOfCalls
+ " nextObject() calls for list with " + capacity
+ " elements, all elements should have been given";
assertEquals(expected, actual, msg);
}

@Test
void testNextObjectEmptyListThrowsException() {
List<Statement> list = new ArrayList<>(capacity);
Throwable t = assertThrows(NoSuchElementException.class, () -> {
Statement badStatement = ExtendedRandom.nextObject(list);
System.out.println("Calling nextObject() on empty list gave "
+ badStatement.toString());
});
String excMsg = t.getMessage();
assert excMsg != null : "Message should not be null";
System.out.println("\"" + excMsg + "\"");
}

@Test
void testNextObjectSet() {
int capacity = RANDOM.nextInt(32) + 8;
Set<LocalDateTime> expected = new HashSet<>(capacity);
LocalDateTime curr = LocalDateTime.now();
for (int i = 0; i < capacity; i++) {
curr = curr.minusHours((long) i * RANDOM.nextInt(24));
expected.add(curr);
}
Set<LocalDateTime> actual = new HashSet<>(capacity);
int numberOfCalls = 20 * capacity;
for (int j = 0; j < numberOfCalls; j++) {
actual.add(ExtendedRandom.nextObject(expected));
}
String msg = "After " + numberOfCalls
+ " nextObject() calls for set with " + capacity
+ " elements, all elements should have been given";
assertEquals(expected, actual, msg);
}

@Test
void testNextObjectEmptySetThrowsException() {
Set<Statement> set = new HashSet<>(capacity);
Throwable t = assertThrows(NoSuchElementException.class, () -> {
Statement badStatement = ExtendedRandom.nextObject(set);
System.out.println("Calling nextObject() on empty set gave "
+ badStatement.toString());
});
String excMsg = t.getMessage();
assert excMsg != null : "Message should not be null";
System.out.println("\"" + excMsg + "\"");
}

}