Java Day 27: Testing




This problem is all about unit testing.

Your company needs a function that meets the following requirements:

For a given array of n 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 0.



  1. import java.util.*;
  2. public class Solution {
  3. public static int minimum_index(int[] seq) {
  4. if (seq.length == 0) {
  5. throw new IllegalArgumentException("Cannot get the minimum value index from an empty sequence");
  6. }
  7. int min_idx = 0;
  8. for (int i = 1; i < seq.length; ++i) {
  9. if (seq[i] < seq[min_idx]) {
  10. min_idx = i;
  11. }
  12. }
  13. return min_idx;
  14. }
  15. static class TestDataEmptyArray {
  16. public static int[] get_array() {
  17. return new int[] {};
  18. }
  19. }
  20. static class TestDataUniqueValues {
  21. public static int[] get_array() {
  22. return new int[] {1,2,3,4};
  23. }
  24. public static int get_expected_result() {
  25. return 0;
  26. }
  27. }
  28. static class TestDataExactlyTwoDifferentMinimums {
  29. public static int[] get_array() {
  30. return new int[] {1, 2, 3, 4, 1};
  31. }
  32. public static int get_expected_result() {
  33. return 0;
  34. }
  35. }
  36. public static void TestWithEmptyArray() {
  37. try {
  38. int[] seq = TestDataEmptyArray.get_array();
  39. int result = minimum_index(seq);
  40. } catch (IllegalArgumentException e) {
  41. return;
  42. }
  43. throw new AssertionError("Exception wasn't thrown as expected");
  44. }
  45. public static void TestWithUniqueValues() {
  46. int[] seq = TestDataUniqueValues.get_array();
  47. if (seq.length < 2) {
  48. throw new AssertionError("less than 2 elements in the array");
  49. }
  50. Integer[] tmp = new Integer[seq.length];
  51. for (int i = 0; i < seq.length; ++i) {
  52. tmp[i] = Integer.valueOf(seq[i]);
  53. }
  54. if (!((new LinkedHashSet<Integer>(Arrays.asList(tmp))).size() == seq.length)) {
  55. throw new AssertionError("not all values are unique");
  56. }
  57. int expected_result = TestDataUniqueValues.get_expected_result();
  58. int result = minimum_index(seq);
  59. if (result != expected_result) {
  60. throw new AssertionError("result is different than the expected result");
  61. }
  62. }
  63. public static void TestWithExactlyTwoDifferentMinimums() {
  64. int[] seq = TestDataExactlyTwoDifferentMinimums.get_array();
  65. if (seq.length < 2) {
  66. throw new AssertionError("less than 2 elements in the array");
  67. }
  68. int[] tmp = seq.clone();
  69. Arrays.sort(tmp);
  70. if (!(tmp[0] == tmp[1] && (tmp.length == 2 || tmp[1] < tmp[2]))) {
  71. throw new AssertionError("there are not exactly two minimums in the array");
  72. }
  73. int expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result();
  74. int result = minimum_index(seq);
  75. if (result != expected_result) {
  76. throw new AssertionError("result is different than the expected result");
  77. }
  78. }
  79. public static void main(String[] args) {
  80. TestWithEmptyArray();
  81. TestWithUniqueValues();
  82. TestWithExactlyTwoDifferentMinimums();
  83. System.out.println("OK");
  84. }
  85. }
download android app

Popular HackerRank Java Solutions








codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube