Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Created February 12, 2017 22:02
Show Gist options
  • Save bhnascar/e34faeea210a3efbfc0bb927bc307157 to your computer and use it in GitHub Desktop.
Save bhnascar/e34faeea210a3efbfc0bb927bc307157 to your computer and use it in GitHub Desktop.
Tests for MoreArrayList
import java.util.*;
class MoreArrayListTest
{
public static void main(String[] args)
{
// Run all the tests.
testNormalize();
testShuffle();
testRemoveDuplicates();
testFindLongestIncreasingSequence();
testBuildHistogram();
testSimpleSort();
testInsertionSort();
}
private static void testNormalize() {
// Build an ArrayList of random doubles.
ArrayList<Double> arr = GenerateSortedDoubles(20);
System.out.println("\nOriginal ArrayList: " + arr);
// Call |normalize| and display the result.
MoreArrayListSolutions.normalize(arr);
System.out.println("\nNormalized ArrayList: " + arr);
// Verify result.
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) < 0 || arr.get(i) > 1) {
System.out.println("\nDetected a normalized value outside [0, 1]. " +
"Looks like something went wrong!");
}
}
}
private static void testShuffle() {
// Build a sorted ArrayList of integers.
ArrayList<Integer> sorted = GenerateSortedInts(20);
HashSet<Integer> originalItems = new HashSet<Integer>(sorted);
System.out.println("\nSorted ArrayList: " + sorted);
// Call |shuffle| and display the result.
MoreArrayListSolutions.shuffle(sorted);
System.out.println("\nShuffled ArrayList: " + sorted);
// Verify result.
for (Integer i : originalItems) {
if (!sorted.contains(i)) {
System.out.println("\nLooks like " + i + " got lost in the shuffle!");
}
}
}
private static void testRemoveDuplicates() {
// Generate a list of words with some duplicates.
ArrayList<String> words = new ArrayList<String>();
words.add("Hi");
words.add("Hi");
words.add("Bye");
words.add("Bye");
words.add("Bye");
words.add("Sayonara");
words.add("A demain");
words.add("Hasta la vista");
words.add("Hasta la vista");
words.add("Zai jian");
System.out.println("\nOriginal words: " + words);
// Call |removeDuplicates| and display the result.
MoreArrayListSolutions.removeDuplicates(words);
System.out.println("\nWords w/o duplicates: " + words);
// Verify result.
HashSet<String> noDupes = new HashSet<String>();
for (String word : words) {
if (noDupes.contains(word)) {
System.out.println("\n" + word + " is still duplicated!");
}
noDupes.add(word);
}
}
private static void testBuildHistogram() {
// Generate some random numbers where some numbers appear multiple times.
ArrayList<Integer> numbers = GenerateUnsortedInts(20, 5);
System.out.println("\nTest numbers: " + numbers);
// Call |buildHistogram| and display the result.
ArrayList<MoreArrayListSolutions.Item> items =
MoreArrayListSolutions.buildHistogram(numbers);
System.out.println("\nHistogram: " + items);
// Verify result.
int totalCount = 0;
for (MoreArrayListSolutions.Item item : items) {
totalCount += item.count;
// Check item count.
int itemCount = 0;
for (int i = 0; i < numbers.size(); i++) {
if (numbers.get(i) == item.value) {
itemCount++;
}
}
if (itemCount != item.count) {
System.out.println("\nWrong count for item " + item.value);
}
}
if (totalCount < numbers.size()) {
System.out.println("\nHistogram has too few numbers compared to " +
"the original set!");
}
}
private static void testFindLongestIncreasingSequence() {
// Build an ArrayList of random integers.
ArrayList<Integer> numbers = GenerateUnsortedInts(20);
System.out.println("\nTest numbers: " + numbers);
// Call |findLongestIncreasingSequence| and display the result.
int length = MoreArrayListSolutions.findLongestIncreasingSequence(numbers);
System.out.println("\nLongest increasing sequence is: " + length);
}
private static void testSimpleSort() {
// Build an ArrayList of unsorted integers.
ArrayList<Integer> unsorted = GenerateUnsortedInts(20);
System.out.println("\nUnsorted numbers: " + unsorted);
// Call |simpleSort| and display the result.
ArrayList<Integer> sorted = MoreArrayListSolutions.simpleSort(unsorted);
System.out.println("\nSorted numbers: " + sorted);
// Verify result.
for (int i = 1; i < sorted.size(); i++) {
if (sorted.get(i) < sorted.get(i - 1)) {
System.out.println("\nNumbers at index " + (i - 1) + " and " + i +
" are out of order!");
}
}
}
private static void testInsertionSort() {
// Build an ArrayList of unsorted integers.
ArrayList<Integer> numbers = GenerateUnsortedInts(20);
System.out.println("\nUnsorted numbers: " + numbers);
// Call |insertionSort| and display the result.
MoreArrayListSolutions.insertionSort(numbers);
System.out.println("\nSorted numbers: " + numbers);
// Verify result.
for (int i = 1; i < numbers.size(); i++) {
if (numbers.get(i) < numbers.get(i - 1)) {
System.out.println("\nNumbers at index " + (i - 1) + " and " + i +
" are out of order!");
}
}
}
/* Helper methods for generating test data. */
private static ArrayList<Integer> GenerateSortedInts(int size) {
ArrayList<Integer> numbers = new ArrayList<Integer>(size);
for (int i = 0; i < size; i++) {
numbers.add(i);
}
return numbers;
}
private static ArrayList<Integer> GenerateUnsortedInts(int size) {
ArrayList<Integer> numbers = new ArrayList<Integer>(size);
for (int i = 0; i < size; i++) {
numbers.add((int)(Math.random() * 100.0));
}
return numbers;
}
private static ArrayList<Integer> GenerateUnsortedInts(int size, int range) {
ArrayList<Integer> numbers = new ArrayList<Integer>(size);
for (int i = 0; i < size; i++) {
numbers.add((int)(Math.random() * range));
}
return numbers;
}
private static ArrayList<Double> GenerateSortedDoubles(int size) {
ArrayList<Double> numbers = new ArrayList<Double>(size);
for (double i = 0.0; i < size; i++) {
numbers.add(i);
}
return numbers;
}
private static ArrayList<Double> GenerateUnsortedDoubles(int size) {
ArrayList<Double> numbers = new ArrayList<Double>(size);
for (int i = 0; i < size; i++) {
numbers.add(Math.random());
}
return numbers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment