libcryfs/test/fspp/impl/IdListTest.cpp

110 lines
2.2 KiB
C++
Raw Normal View History

2016-02-11 12:53:42 +01:00
#include <gtest/gtest.h>
2014-11-16 01:31:16 +01:00
2016-02-11 12:53:42 +01:00
#include "fspp/impl/IdList.h"
2014-11-16 01:31:16 +01:00
#include <stdexcept>
using cpputils::make_unique_ref;
2014-11-16 01:31:16 +01:00
using namespace fspp;
class MyObj {
public:
MyObj(int val_): val(val_) {}
int val;
};
2014-11-16 03:01:41 +01:00
struct IdListTest: public ::testing::Test {
2015-10-17 20:35:17 +02:00
static constexpr int OBJ1 = 3;
static constexpr int OBJ2 = 10;
static constexpr int OBJ3 = 8;
IdListTest(): list() {}
2014-11-16 03:01:41 +01:00
2014-11-16 01:31:16 +01:00
IdList<MyObj> list;
2015-10-17 20:35:17 +02:00
2014-11-16 03:01:41 +01:00
int add(int num) {
return list.add(make_unique_ref<MyObj>(num));
2014-11-16 03:01:41 +01:00
}
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) {
2014-11-16 01:31:16 +01:00
ASSERT_THROW(list.get(0), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, EmptyList2) {
2014-11-16 01:31:16 +01:00
ASSERT_THROW(list.get(3), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, InvalidId) {
int valid_id = add();
2014-11-16 01:31:16 +01:00
int invalid_id = valid_id + 1;
ASSERT_THROW(list.get(invalid_id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, GetRemovedItemOnEmptyList) {
int id = add();
2014-11-16 01:31:16 +01:00
list.remove(id);
ASSERT_THROW(list.get(id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, GetRemovedItemOnNonEmptyList) {
int id = add();
add();
2014-11-16 01:31:16 +01:00
list.remove(id);
ASSERT_THROW(list.get(id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, RemoveOnEmptyList1) {
2014-11-16 02:42:34 +01:00
ASSERT_THROW(list.remove(0), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, RemoveOnEmptyList2) {
2014-11-16 02:42:34 +01:00
ASSERT_THROW(list.remove(4), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, RemoveInvalidId) {
int valid_id = add();
2014-11-16 02:42:34 +01:00
int invalid_id = valid_id + 1;
ASSERT_THROW(list.remove(invalid_id), std::out_of_range);
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, Add1AndGet) {
int id = add(OBJ1);
check(id, OBJ1);
2014-11-16 01:31:16 +01:00
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, Add2AndGet) {
int id1 = add(OBJ1);
int id2 = add(OBJ2);
check(id1, OBJ1);
check(id2, OBJ2);
2014-11-16 01:31:16 +01:00
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, Add3AndGet) {
int id1 = add(OBJ1);
int id2 = add(OBJ2);
int id3 = add(OBJ3);
check(id1, OBJ1);
check(id3, OBJ3);
check(id2, OBJ2);
2014-11-16 01:31:16 +01:00
}
2014-11-16 03:01:41 +01:00
TEST_F(IdListTest, Add3AndConstGet) {
int id1 = add(OBJ1);
int id2 = add(OBJ2);
int id3 = add(OBJ3);
checkConst(id1, OBJ1);
checkConst(id3, OBJ3);
checkConst(id2, OBJ2);
2014-11-16 01:31:16 +01:00
}