-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkstring_test.cpp
162 lines (137 loc) · 4.31 KB
/
kstring_test.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "kstring.hpp"
#include <gtest/gtest.h>
template <typename T> class kallocator : public std::allocator<T> {
public:
static size_t allocate_size;
static size_t deallocate_size;
template <typename _Tp1> struct rebind {
typedef kallocator<_Tp1> other;
};
T* allocate(size_t n, const void *hint = 0) {
allocate_size += n*sizeof(T);
return std::allocator<T>::allocate(n);
}
void deallocate(T* p, std::size_t n) {
deallocate_size += n*sizeof(T);
return std::allocator<T>::deallocate(p, n);
}
static void reset_stats() {
allocate_size = 0;
deallocate_size = 0;
}
kallocator() throw() : std::allocator<T>() {
}
kallocator(const kallocator &a) throw() : std::allocator<T>(a) {}
template <class U>
kallocator(const kallocator<U> &a) throw() : std::allocator<T>(a) {}
~kallocator() throw() {}
};
template<typename T> size_t kallocator<T>::allocate_size = 0;
template<typename T> size_t kallocator<T>::deallocate_size = 0;
namespace std {
template<>
struct allocator_traits<kallocator<char>>
{
typedef char& reference;
typedef const char& const_reference;
typedef size_t size_type;
typedef char *pointer;
typedef const char *const_pointer;
};
}
// Demonstrate some basic assertions.
TEST(Kstring, StrLiteralConstructors) {
using kallocator = kallocator<char>;
ASSERT_EQ(sizeof(k::kstring<kallocator>), 24);
{
kallocator::reset_stats();
k::kstring<kallocator> ss("0123456789012345678901");
ASSERT_STREQ(ss.c_str(), "0123456789012345678901");
ASSERT_EQ(ss.length(), 22);
ASSERT_EQ(ss.capacity(), 23);
}
ASSERT_EQ(kallocator::allocate_size, 0);
ASSERT_EQ(kallocator::deallocate_size, 0);
{
kallocator::reset_stats();
k::kstring<kallocator> ms("012345678901234567890123456789");
ASSERT_STREQ(ms.c_str(), "012345678901234567890123456789");
ASSERT_EQ(ms.length(), 30);
ASSERT_EQ(ms.capacity(), 32);
}
ASSERT_EQ(kallocator::allocate_size, 32);
ASSERT_EQ(kallocator::deallocate_size, 32);
{
kallocator::reset_stats();
std::string long_str(300, 'c');
k::kstring<kallocator> ls(long_str.c_str());
ASSERT_STREQ(ls.c_str(), long_str.c_str());
ASSERT_EQ(ls.length(), 300);
ASSERT_EQ(ls.capacity(), 512);
}
ASSERT_EQ(kallocator::allocate_size, 512);
ASSERT_EQ(kallocator::deallocate_size, 512);
}
// Demonstrate some basic assertions.
TEST(Kstring, CopyConstructors) {
using kallocator = kallocator<char>;
ASSERT_EQ(sizeof(k::kstring<kallocator>), 24);
{
kallocator::reset_stats();
k::kstring<kallocator> ss1("0123456789012345678901");
auto ss2{ss1};
ASSERT_STREQ(ss2.c_str(), "0123456789012345678901");
ASSERT_EQ(ss2.length(), 22);
ASSERT_EQ(ss2.capacity(), 23);
}
ASSERT_EQ(kallocator::allocate_size, 0);
ASSERT_EQ(kallocator::deallocate_size, 0);
{
kallocator::reset_stats();
k::kstring<kallocator> ms1("012345678901234567890123456789");
auto ms2{ms1};
ASSERT_STREQ(ms2.c_str(), "012345678901234567890123456789");
ASSERT_EQ(ms2.length(), 30);
ASSERT_EQ(ms2.capacity(), 32);
}
ASSERT_EQ(kallocator::allocate_size, 64);
ASSERT_EQ(kallocator::deallocate_size, 64);
{
kallocator::reset_stats();
std::string long_str(300, 'c');
k::kstring<kallocator> ls1(long_str.c_str());
auto ls2{ls1};
ASSERT_STREQ(ls2.c_str(), long_str.c_str());
ASSERT_EQ(ls2.length(), 300);
ASSERT_EQ(ls2.capacity(), 512);
}
// Should not copy when there is no write
ASSERT_EQ(kallocator::allocate_size, 512);
ASSERT_EQ(kallocator::deallocate_size, 512);
}
TEST(Kstring, AccessCharOperator) {
using kallocator = kallocator<char>;
{
kallocator::reset_stats();
std::string long_str(300, 'c');
k::kstring<kallocator> ls1(long_str.c_str());
auto ls2{ls1};
ls2[150] = 'z';
ASSERT_STREQ(long_str.c_str(), ls1.c_str());
std::string expected_ls2(300, 'c');
expected_ls2[150] = 'z';
ASSERT_STREQ(ls2.c_str(), expected_ls2.c_str());
}
ASSERT_EQ(kallocator::allocate_size, 1024);
ASSERT_EQ(kallocator::deallocate_size, 1024);
{
kallocator::reset_stats();
std::string long_str(300, 'c');
k::kstring<kallocator> ls1(long_str.c_str());
const auto ls2{ls1};
char c = ls2[150];
ASSERT_EQ(c, 'c');
}
ASSERT_EQ(kallocator::allocate_size, 512);
ASSERT_EQ(kallocator::deallocate_size, 512);
}