Introduction:
Welcome to Day 27 of the 30 Days of Code challenge! Today, we're diving into unit testing. Your company needs a function that, given an array of integers, returns the index of the element with the minimum value. There are specific requirements for different test cases, and you need to implement classes to provide data and expected results for these tests.
The Challenge:
Your goal is to complete the implementation of classes to provide data and expected results for the unit tests. The tests will cover scenarios with an empty array, an array of unique values, and an array with exactly two different minimums.
Constraints:
The arrays are indexed.
Format & Sample:
Input Format:
The given code template includes implementations for the minimum_index function and three testing functions (TestWithEmptyArray, TestWithUniqueValues, TestWithExactlyTwoDifferentMinimums).
You need to complete the TestDataEmptyArray, TestDataUniqueValues, and TestDataExactlyTwoDifferentMinimums classes.
Input Format:
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <cassert>
#include <set>
using namespace std;
// Existing implementation of minimum_index function
void TestWithEmptyArray() {
try {
auto seq = TestDataEmptyArray::get_array();
auto result = minimum_index(seq);
} catch (invalid_argument& e) {
return;
}
assert(false);
}
void TestWithUniqueValues() {
auto seq = TestDataUniqueValues::get_array();
assert(seq.size() >= 2);
assert(set<int>(seq.begin(), seq.end()).size() == seq.size());
auto expected_result = TestDataUniqueValues::get_expected_result();
auto result = minimum_index(seq);
assert(result == expected_result);
}
void TestWithExactlyTwoDifferentMinimums() {
auto seq = TestDataExactlyTwoDifferentMinimums::get_array();
assert(seq.size() >= 2);
auto tmp = seq;
sort(tmp.begin(), tmp.end());
assert(tmp[0] == tmp[1] and (tmp.size() == 2 or tmp[1] < tmp[2]));
auto expected_result = TestDataExactlyTwoDifferentMinimums::get_expected_result();
auto result = minimum_index(seq);
assert(result == expected_result);
}
int main() {
TestWithEmptyArray();
TestWithUniqueValues();
TestWithExactlyTwoDifferentMinimums();
cout << "OK" << endl;
return 0;
}
Final Code:
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <cassert>
#include <set>
using namespace std;
int minimum_index(vector<int> seq) {
if (seq.empty()) {
throw invalid_argument("Cannot get the minimum value index from an empty sequence");
}
int min_idx = 0;
for (int i = 1; i < seq.size(); ++i) {
if (seq[i] < seq[min_idx]) {
min_idx = i;
}
}
return min_idx;
}
class TestDataEmptyArray {
public:
static vector<int> get_array(){
return {};
}
};
class TestDataUniqueValues {
public:
static vector<int> get_array(){
return {1, 2};
}
static int get_expected_result(){
return 0;
}
};
class TestDataExactlyTwoDifferentMinimums {
public:
static vector<int> get_array(){
return {1, 1, 2};
}
static int get_expected_result(){
return 0;
}
};
void TestWithEmptyArray() {
try {
auto seq = TestDataEmptyArray::get_array();
auto result = minimum_index(seq);
} catch (invalid_argument& e) {
return;
}
assert(false);
}
void TestWithUniqueValues() {
auto seq = TestDataUniqueValues::get_array();
assert(seq.size() >= 2);
assert(set<int>(seq.begin(), seq.end()).size() == seq.size());
auto expected_result = TestDataUniqueValues::get_expected_result();
auto result = minimum_index(seq);
assert(result == expected_result);
}
void TestWithExactlyTwoDifferentMinimums() {
auto seq = TestDataExactlyTwoDifferentMinimums::get_array();
assert(seq.size() >= 2);
auto tmp = seq;
sort(tmp.begin(), tmp.end());
assert(tmp[0] == tmp[1] and (tmp.size() == 2 or tmp[1] < tmp[2]));
auto expected_result = TestDataExactlyTwoDifferentMinimums::get_expected_result();
auto result = minimum_index(seq);
assert(result == expected_result);
}
int main() {
TestWithEmptyArray();
TestWithUniqueValues();
TestWithExactlyTwoDifferentMinimums();
cout << "OK" << endl;
return 0;
}
This concludes Day 27 of the 30 Days of Code challenge. You've successfully implemented classes to provide data and expected results for unit tests. Keep up the great work!