-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
74 lines (62 loc) · 2.25 KB
/
example.cpp
File metadata and controls
74 lines (62 loc) · 2.25 KB
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
#include "gut.h"
#include <algorithm>
#include <stdexcept>
#include <vector>
class RecentlyUsedList {
std::vector<std::string> items_;
public:
bool empty() const { return items_.empty(); }
size_t size() const { return items_.size(); }
std::string operator[](size_t index) const {
if (index >= size())
throw std::out_of_range("invalid subscript");
return items_[size() - index - 1];
}
void insert(const std::string& item) {
auto found = std::find(begin(items_), end(items_), item);
if (found != items_.end())
items_.erase(found);
items_.push_back(item);
}
};
GUT_ENABLE_COLORINCONSOLE
TEST("Initial list is empty") {
RecentlyUsedList anEmptyList;
CHECK(anEmptyList.empty());
CHECK(anEmptyList.size() == 0);
}
TEST("Insertion to empty list is retained") {
RecentlyUsedList aListWithOneElement;
aListWithOneElement.insert("one");
CHECK(!aListWithOneElement.empty());
CHECK(aListWithOneElement.size() == 1);
CHECK(aListWithOneElement[0] == "one");
}
TEST("Distinct insertions are retained in stack order") {
RecentlyUsedList aListWithManyElements;
aListWithManyElements.insert("one");
aListWithManyElements.insert("two");
aListWithManyElements.insert("three");
CHECK(!aListWithManyElements.empty());
CHECK(aListWithManyElements.size() == 3);
CHECK(aListWithManyElements[0] == "three");
REQUIRE(aListWithManyElements[1] == "two");
CHECK(aListWithManyElements[2] == "one");
}
TEST("Duplicate insertions are moved to the front but not inserted") {
RecentlyUsedList aListWithDuplicatedElements;
aListWithDuplicatedElements.insert("one");
aListWithDuplicatedElements.insert("two");
aListWithDuplicatedElements.insert("three");
aListWithDuplicatedElements.insert("two");
CHECK(!aListWithDuplicatedElements.empty());
CHECK(aListWithDuplicatedElements.size() == 3);
CHECK(aListWithDuplicatedElements[0] == "two");
CHECK(aListWithDuplicatedElements[1] == "three");
CHECK(aListWithDuplicatedElements[2] == "one");
}
TEST("Out of range indexing throws exception") {
RecentlyUsedList aListWithOneElement;
aListWithOneElement.insert("one");
THROWS(aListWithOneElement[1], std::out_of_range);
}