본문 바로가기
1 Day 1 Algorithms

[2019.01.21] Testing

by 곰돌찌 2019. 1. 21.

Problem


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 class TestDataEmptyArray has to return an empty array.
  • get_array() method in class TestDataUniqueValues has to return an array of size at least 2 with all unique elements, while method get_expected_result() of this class has to return the expected minimum value index for this array.
  • get_array() method in class TestDataExactlyTwoDifferentMinimums has to return an array where there are exactly two different minimum values, while method get_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.


How I solved the problem


# Testing 하는 부분인데 해석을 잘 하면 풀수 있는 문제.

# 아래의 specifications 를 읽고 이 부분을 코드로 변환시킬 수 있으면 가능함!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
    }
 
    static class TestDataEmptyArray {
        public static int[] get_array() {
            // complete this function
            int[] testDataEmptyArray = new int[0];
            return testDataEmptyArray;
        }
    }
 
    static class TestDataUniqueValues {
        public static int[] get_array() {
            // complete this function
            int[] uniqueValueArr = new int[3];
            uniqueValueArr[0= 1;
            uniqueValueArr[1= 2;
            uniqueValueArr[2= 3;
 
            return uniqueValueArr;
        }
 
        public static int get_expected_result() {
            // complete this function
            int[] arr = get_array();
            int minimum = minimum_index(arr);
 
            return minimum;
        }
    }
 
    static class TestDataExactlyTwoDifferentMinimums {
        public static int[] get_array() {
            // complete this function
            int[] differentMinimums = new int[2];
            differentMinimums[0= 1;
            differentMinimums[1= 1;
 
            return differentMinimums;
        }
 
        public static int get_expected_result() {
            // complete this function
            int[] arr = get_array();
            int result = minimum_index(arr);
 
            return result;
        }
    }
 
    
    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");
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.01.23] Bitwise AND  (1) 2019.01.23
[2019.01.22] RegEx, Patterns, and Intro to Databases  (0) 2019.01.22
[2019.01.17] Nested Logic  (0) 2019.01.17
[2019.01.16] Running Time and Complexity  (0) 2019.01.16
[2019.01.15] More Linked Lists  (0) 2019.01.15

댓글