Day 27: Testing - HackerRank 30 days of code solution
This problem is all about unit testing.
Your company needs a function that meets the following requirements:
- For a given array of integers, the function returns the index of the element with the minimum value in the array. If there is more than one element with the minimum value, the returned index should be the smallest one.
- If an empty array is passed to the function, it should raise an Exception.
Note: The arrays are indexed from .
A colleague has written that function, and your task is to design separated unit tests, testing if the function behaves correctly. The implementation in Python is listed below (Implementations in other languages can be found in the code template):
def minimum_index(seq):
if len(seq) == 0:
raise ValueError("Cannot get the minimum value index from an empty sequence")
min_idx = 0
for i in range(1, len(seq)):
if a[i] < a[min_idx]:
min_idx = i
return min_idx
Another co-worker has prepared functions that will perform the testing and validate returned results with expectations. Your task is to implement classes that will produce test data and the expected results for the testing functions. More specifically: function
get_array()
in TestDataEmptyArray
class and functions get_array()
and get_expected_result()
in classes TestDataUniqueValues
and TestDataExactlyTwoDifferentMinimums
following the below specifications:get_array()
method in classTestDataEmptyArray
has to return an empty array.get_array()
method in classTestDataUniqueValues
has to return an array of size at least 2 with all unique elements, while methodget_expected_result()
of this class has to return the expected minimum value index for this array.get_array()
method in classTestDataExactlyTwoDifferentMinimums
has to return an array where there are exactly two different minimum values, while methodget_expected_result()
of this class has to return the expected minimum value index for this array.
Take a look at the code template to see the exact implementation of functions that your colleagues already implemented.
Solution :
import java.util.*;
public class Solution {
public static int minimum_index(int[] seq) {
if (seq.length == 0) {
throw new IllegalArgumentException("Cannot get the minimum value index from an empty sequence");
}
int min_idx = 0;
for (int i = 1; i < seq.length; ++i) {
if (seq[i] < seq[min_idx]) {
min_idx = i;
}
}
return min_idx;
}
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
System.out.println("5");
System.out.println("4 4");
System.out.println("-1 0 3 3");
System.out.println("6 2");
System.out.println("-1 0 1 2 3 2");
System.out.println("7 4");
System.out.println("-1 -2 0 3 3 9 6");
System.out.println("3 2");
System.out.println("-2 0 1");
System.out.println("8 5");
System.out.println("-2 -1 0 2 3 5 6 99");
}
}
public static void TestWithEmptyArray() {
try {
int[] seq = TestDataEmptyArray.get_array();
int result = minimum_index(seq);
} catch (IllegalArgumentException e) {
return;
}
throw new AssertionError("Exception wasn't thrown as expected");
}
public static void TestWithUniqueValues() {
int[] seq = TestDataUniqueValues.get_array();
if (seq.length < 2) {
throw new AssertionError("less than 2 elements in the array");
}
Integer[] tmp = new Integer[seq.length];
for (int i = 0; i < seq.length; ++i) {
tmp[i] = Integer.valueOf(seq[i]);
}
if (!((new LinkedHashSet<Integer>(Arrays.asList(tmp))).size() == seq.length)) {
throw new AssertionError("not all values are unique");
}
int expected_result = TestDataUniqueValues.get_expected_result();
int result = minimum_index(seq);
if (result != expected_result) {
throw new AssertionError("result is different than the expected result");
}
}
public static void TestWithExactlyTwoDifferentMinimums() {
int[] seq = TestDataExactlyTwoDifferentMinimums.get_array();
if (seq.length < 2) {
throw new AssertionError("less than 2 elements in the array");
}
int[] tmp = seq.clone();
Arrays.sort(tmp);
if (!(tmp[0] == tmp[1] && (tmp.length == 2 || tmp[1] < tmp[2]))) {
throw new AssertionError("there are not exactly two minimums in the array");
}
int expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result();
int result = minimum_index(seq);
if (result != expected_result) {
throw new AssertionError("result is different than the expected result");
}
}
public static void main(String[] args) {
TestWithEmptyArray();
TestWithUniqueValues();
TestWithExactlyTwoDifferentMinimums();
System.out.println("OK");
}
}
Really nice topics you had discussed above. I am much impressed. Thank you for providing this nice information here
ReplyDeleteSoftware Testing Company
Console Game Testing
Game Testing Company
Video Game QA
Thank you. I'm glad it helped :)
Delete
ReplyDeleteI like visiting your site since I always come across interesting articles like this one. Keep sharing! Regards. Read more about..
selenium training in chennai |
Selenium Training in Chennai | Certification | Online Training Course | Selenium Training in Bangalore | Certification | Online Training Course | Selenium Training in Hyderabad | Certification | Online Training Course | Selenium Training in Coimbatore | Certification | Online Training Course | Selenium Training in Online | Certification | Online Training Course
ReplyDeleteI am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
Top Software Testing Companies
Top Security Testing Companies
Top Mobile Testing Companies
Top Test Automation Companies
Top Performance Testing Companies
Website testing services