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
|
#include <gtest/gtest.h>
#include "fspp/impl/IdList.h"
#include <stdexcept>
using cpputils::make_unique_ref;
using namespace fspp;
class MyObj {
public:
MyObj(int val_): val(val_) {}
int val;
};
struct IdListTest: public ::testing::Test {
static constexpr int OBJ1 = 3;
static constexpr int OBJ2 = 10;
static constexpr int OBJ3 = 8;
IdListTest(): list() {}
IdList<MyObj> list;
int add(int num) {
return list.add(make_unique_ref<MyObj>(num));
}
int add() {
return add(OBJ1);
}
void check(int id, int num) {
EXPECT_EQ(num, list.get(id)->val);
}
void checkConst(int id, int num) {
const IdList<MyObj> &constList = list;
EXPECT_EQ(num, constList.get(id)->val);
}
};
TEST_F(IdListTest, EmptyList1) {
ASSERT_THROW(list.get(0), std::out_of_range);
}
TEST_F(IdListTest, EmptyList2) {
ASSERT_THROW(list.get(3), std::out_of_range);
}
TEST_F(IdListTest, InvalidId) {
const int valid_id = add();
const int invalid_id = valid_id + 1;
ASSERT_THROW(list.get(invalid_id), std::out_of_range);
}
TEST_F(IdListTest, GetRemovedItemOnEmptyList) {
const int id = add();
list.remove(id);
ASSERT_THROW(list.get(id), std::out_of_range);
}
TEST_F(IdListTest, GetRemovedItemOnNonEmptyList) {
const int id = add();
add();
list.remove(id);
ASSERT_THROW(list.get(id), std::out_of_range);
}
TEST_F(IdListTest, RemoveOnEmptyList1) {
ASSERT_THROW(list.remove(0), std::out_of_range);
}
TEST_F(IdListTest, RemoveOnEmptyList2) {
ASSERT_THROW(list.remove(4), std::out_of_range);
}
TEST_F(IdListTest, RemoveInvalidId) {
const int valid_id = add();
const int invalid_id = valid_id + 1;
ASSERT_THROW(list.remove(invalid_id), std::out_of_range);
}
TEST_F(IdListTest, Add1AndGet) {
const int id = add(OBJ1);
check(id, OBJ1);
}
TEST_F(IdListTest, Add2AndGet) {
const int id1 = add(OBJ1);
const int id2 = add(OBJ2);
check(id1, OBJ1);
check(id2, OBJ2);
}
TEST_F(IdListTest, Add3AndGet) {
const int id1 = add(OBJ1);
const int id2 = add(OBJ2);
const int id3 = add(OBJ3);
check(id1, OBJ1);
check(id3, OBJ3);
check(id2, OBJ2);
}
TEST_F(IdListTest, Add3AndConstGet) {
const int id1 = add(OBJ1);
const int id2 = add(OBJ2);
const int id3 = add(OBJ3);
checkConst(id1, OBJ1);
checkConst(id3, OBJ3);
checkConst(id2, OBJ2);
}
|