To my best knowledge, unit testing should be done on each public API separately. But, I have been experiencing with a situation in which I have not found out a clear way to unit test each API independently as shown in the following example:
class MyStorage {
private:
std::vector<int> int_vec_;
public:
bool insert_int(int value);
int get_value_at(int idx);
}
I used GooogleTest framework and wrote unit tests as follows:
int int_tenth(int x) { return x * 10; }
TEST_F(MyStorageTest, insert_int) {
for(int i = 0; i < 10; i++) {
int value = int_tenth(i);
bool ret = my_storage.insert_int(value);
ASSERT_TRUE(ret);
}
}
TEST_F(MyStorageTest, get_value_at) {
for(int i = 0; i < 10; i++) {
int value = int_tenth(i);
my_storage.insert_int(value);
}
for(int i = 0; i < 10; i++) {
int expected_value = int_tenth(i);
int value = my_storage.get_value_at(i);
ASSERT_EQ(expected_value, value);
}
}
Am I doing right? If not, how can I make unit tests for this example?