GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../test/cctest/test_util.cc Lines: 174 174 100.0 %
Date: 2019-01-07 12:15:22 Branches: 183 318 57.5 %

Line Branch Exec Source
1
#include "util.h"
2
#include "util-inl.h"
3
4
#include "gtest/gtest.h"
5
6
5
TEST(UtilTest, ListHead) {
7
4
  struct Item { node::ListNode<Item> node_; };
8
  typedef node::ListHead<Item, &Item::node_> List;
9
10
1
  List list;
11
1
  EXPECT_TRUE(list.IsEmpty());
12
13
1
  Item one;
14
1
  EXPECT_TRUE(one.node_.IsEmpty());
15
16
1
  list.PushBack(&one);
17
1
  EXPECT_FALSE(list.IsEmpty());
18
1
  EXPECT_FALSE(one.node_.IsEmpty());
19
20
  {
21
1
    List::Iterator it = list.begin();
22
1
    EXPECT_NE(list.end(), it);
23
1
    EXPECT_EQ(&one, *it);
24
1
    ++it;
25
1
    EXPECT_FALSE(it != list.end());  // Iterator only implements != operator.
26
  }
27
28
1
  Item two;
29
1
  list.PushBack(&two);
30
31
  {
32
1
    List::Iterator it = list.begin();
33
1
    EXPECT_NE(list.end(), it);
34
1
    EXPECT_EQ(&one, *it);
35
1
    ++it;
36
1
    EXPECT_NE(list.end(), it);
37
1
    EXPECT_EQ(&two, *it);
38
1
    ++it;
39
1
    EXPECT_FALSE(it != list.end());  // Iterator only implements != operator.
40
  }
41
42
1
  EXPECT_EQ(&one, list.PopFront());
43
1
  EXPECT_TRUE(one.node_.IsEmpty());
44
1
  EXPECT_FALSE(list.IsEmpty());
45
46
  {
47
1
    List::Iterator it = list.begin();
48
1
    EXPECT_NE(list.end(), it);
49
1
    EXPECT_EQ(&two, *it);
50
1
    ++it;
51
1
    EXPECT_FALSE(it != list.end());  // Iterator only implements != operator.
52
  }
53
54
1
  EXPECT_EQ(&two, list.PopFront());
55
1
  EXPECT_TRUE(two.node_.IsEmpty());
56
1
  EXPECT_TRUE(list.IsEmpty());
57
1
  EXPECT_FALSE(list.begin() != list.end());
58
1
}
59
60
5
TEST(UtilTest, StringEqualNoCase) {
61
  using node::StringEqualNoCase;
62
1
  EXPECT_FALSE(StringEqualNoCase("a", "b"));
63
1
  EXPECT_TRUE(StringEqualNoCase("", ""));
64
1
  EXPECT_TRUE(StringEqualNoCase("equal", "equal"));
65
1
  EXPECT_TRUE(StringEqualNoCase("equal", "EQUAL"));
66
1
  EXPECT_TRUE(StringEqualNoCase("EQUAL", "EQUAL"));
67
1
  EXPECT_FALSE(StringEqualNoCase("equal", "equals"));
68
1
  EXPECT_FALSE(StringEqualNoCase("equals", "equal"));
69
1
}
70
71
5
TEST(UtilTest, StringEqualNoCaseN) {
72
  using node::StringEqualNoCaseN;
73
1
  EXPECT_FALSE(StringEqualNoCaseN("a", "b", strlen("a")));
74
1
  EXPECT_TRUE(StringEqualNoCaseN("", "", strlen("")));
75
1
  EXPECT_TRUE(StringEqualNoCaseN("equal", "equal", strlen("equal")));
76
1
  EXPECT_TRUE(StringEqualNoCaseN("equal", "EQUAL", strlen("equal")));
77
1
  EXPECT_TRUE(StringEqualNoCaseN("EQUAL", "EQUAL", strlen("equal")));
78
1
  EXPECT_TRUE(StringEqualNoCaseN("equal", "equals", strlen("equal")));
79
1
  EXPECT_FALSE(StringEqualNoCaseN("equal", "equals", strlen("equals")));
80
1
  EXPECT_TRUE(StringEqualNoCaseN("equals", "equal", strlen("equal")));
81
1
  EXPECT_FALSE(StringEqualNoCaseN("equals", "equal", strlen("equals")));
82
1
  EXPECT_TRUE(StringEqualNoCaseN("abc\0abc", "abc\0efg", strlen("abcdefgh")));
83
1
  EXPECT_FALSE(StringEqualNoCaseN("abc\0abc", "abcd\0efg", strlen("abcdefgh")));
84
1
}
85
86
5
TEST(UtilTest, ToLower) {
87
  using node::ToLower;
88
1
  EXPECT_EQ('0', ToLower('0'));
89
1
  EXPECT_EQ('a', ToLower('a'));
90
1
  EXPECT_EQ('a', ToLower('A'));
91
1
}
92
93
#define TEST_AND_FREE(expression)                                             \
94
  do {                                                                        \
95
    auto pointer = expression;                                                \
96
    EXPECT_NE(nullptr, pointer);                                              \
97
    free(pointer);                                                            \
98
  } while (0)
99
100
5
TEST(UtilTest, Malloc) {
101
  using node::Malloc;
102
1
  TEST_AND_FREE(Malloc<char>(0));
103
1
  TEST_AND_FREE(Malloc<char>(1));
104
1
  TEST_AND_FREE(Malloc(0));
105
1
  TEST_AND_FREE(Malloc(1));
106
1
}
107
108
5
TEST(UtilTest, Calloc) {
109
  using node::Calloc;
110
1
  TEST_AND_FREE(Calloc<char>(0));
111
1
  TEST_AND_FREE(Calloc<char>(1));
112
1
  TEST_AND_FREE(Calloc(0));
113
1
  TEST_AND_FREE(Calloc(1));
114
1
}
115
116
5
TEST(UtilTest, UncheckedMalloc) {
117
  using node::UncheckedMalloc;
118
1
  TEST_AND_FREE(UncheckedMalloc<char>(0));
119
1
  TEST_AND_FREE(UncheckedMalloc<char>(1));
120
1
  TEST_AND_FREE(UncheckedMalloc(0));
121
1
  TEST_AND_FREE(UncheckedMalloc(1));
122
1
}
123
124
5
TEST(UtilTest, UncheckedCalloc) {
125
  using node::UncheckedCalloc;
126
1
  TEST_AND_FREE(UncheckedCalloc<char>(0));
127
1
  TEST_AND_FREE(UncheckedCalloc<char>(1));
128
1
  TEST_AND_FREE(UncheckedCalloc(0));
129
1
  TEST_AND_FREE(UncheckedCalloc(1));
130
1
}
131
132
template <typename T>
133
2
static void MaybeStackBufferBasic() {
134
  using node::MaybeStackBuffer;
135
136
2
  MaybeStackBuffer<T> buf;
137
  size_t old_length;
138
  size_t old_capacity;
139
140
  /* Default constructor */
141

2
  EXPECT_EQ(0U, buf.length());
142

2
  EXPECT_FALSE(buf.IsAllocated());
143

2
  EXPECT_GT(buf.capacity(), buf.length());
144
145
  /* SetLength() expansion */
146
2
  buf.SetLength(buf.capacity());
147

2
  EXPECT_EQ(buf.capacity(), buf.length());
148

2
  EXPECT_FALSE(buf.IsAllocated());
149
150
  /* Means of accessing raw buffer */
151

2
  EXPECT_EQ(buf.out(), *buf);
152

2
  EXPECT_EQ(&buf[0], *buf);
153
154
  /* Basic I/O */
155

2050
  for (size_t i = 0; i < buf.length(); i++)
156
2048
    buf[i] = static_cast<T>(i);
157

2050
  for (size_t i = 0; i < buf.length(); i++)
158

2048
    EXPECT_EQ(static_cast<T>(i), buf[i]);
159
160
  /* SetLengthAndZeroTerminate() */
161
2
  buf.SetLengthAndZeroTerminate(buf.capacity() - 1);
162

2
  EXPECT_EQ(buf.capacity() - 1, buf.length());
163

2048
  for (size_t i = 0; i < buf.length(); i++)
164

2046
    EXPECT_EQ(static_cast<T>(i), buf[i]);
165
2
  buf.SetLength(buf.capacity());
166

2
  EXPECT_EQ(0, buf[buf.length() - 1]);
167
168
  /* Initial Realloc */
169
2
  old_length = buf.length() - 1;
170
2
  old_capacity = buf.capacity();
171
2
  buf.AllocateSufficientStorage(buf.capacity() * 2);
172

2
  EXPECT_EQ(buf.capacity(), buf.length());
173

2
  EXPECT_TRUE(buf.IsAllocated());
174

2048
  for (size_t i = 0; i < old_length; i++)
175

2046
    EXPECT_EQ(static_cast<T>(i), buf[i]);
176

2
  EXPECT_EQ(0, buf[old_length]);
177
178
  /* SetLength() reduction and expansion */
179

4098
  for (size_t i = 0; i < buf.length(); i++)
180
4096
    buf[i] = static_cast<T>(i);
181
2
  buf.SetLength(10);
182

22
  for (size_t i = 0; i < buf.length(); i++)
183

20
    EXPECT_EQ(static_cast<T>(i), buf[i]);
184
2
  buf.SetLength(buf.capacity());
185

4098
  for (size_t i = 0; i < buf.length(); i++)
186

4096
    EXPECT_EQ(static_cast<T>(i), buf[i]);
187
188
  /* Subsequent Realloc */
189
2
  old_length = buf.length();
190
2
  old_capacity = buf.capacity();
191
2
  buf.AllocateSufficientStorage(old_capacity * 1.5);
192

2
  EXPECT_EQ(buf.capacity(), buf.length());
193

2
  EXPECT_EQ(static_cast<size_t>(old_capacity * 1.5), buf.length());
194

2
  EXPECT_TRUE(buf.IsAllocated());
195

4098
  for (size_t i = 0; i < old_length; i++)
196

4096
    EXPECT_EQ(static_cast<T>(i), buf[i]);
197
198
  /* Basic I/O on Realloc'd buffer */
199

6146
  for (size_t i = 0; i < buf.length(); i++)
200
6144
    buf[i] = static_cast<T>(i);
201

6146
  for (size_t i = 0; i < buf.length(); i++)
202

6144
    EXPECT_EQ(static_cast<T>(i), buf[i]);
203
204
  /* Release() */
205
2
  T* rawbuf = buf.out();
206
2
  buf.Release();
207

2
  EXPECT_EQ(0U, buf.length());
208

2
  EXPECT_FALSE(buf.IsAllocated());
209

2
  EXPECT_GT(buf.capacity(), buf.length());
210
2
  free(rawbuf);
211
2
}
212
213
5
TEST(UtilTest, MaybeStackBuffer) {
214
  using node::MaybeStackBuffer;
215
216
1
  MaybeStackBufferBasic<uint8_t>();
217
1
  MaybeStackBufferBasic<uint16_t>();
218
219
  // Constructor with size parameter
220
  {
221
1
    MaybeStackBuffer<unsigned char> buf(100);
222
1
    EXPECT_EQ(100U, buf.length());
223
1
    EXPECT_FALSE(buf.IsAllocated());
224
1
    EXPECT_GT(buf.capacity(), buf.length());
225
1
    buf.SetLength(buf.capacity());
226
1
    EXPECT_EQ(buf.capacity(), buf.length());
227
1
    EXPECT_FALSE(buf.IsAllocated());
228
1025
    for (size_t i = 0; i < buf.length(); i++)
229
1024
      buf[i] = static_cast<unsigned char>(i);
230
1025
    for (size_t i = 0; i < buf.length(); i++)
231
1024
      EXPECT_EQ(static_cast<unsigned char>(i), buf[i]);
232
233
1
    MaybeStackBuffer<unsigned char> bigbuf(10000);
234
1
    EXPECT_EQ(10000U, bigbuf.length());
235
1
    EXPECT_TRUE(bigbuf.IsAllocated());
236
1
    EXPECT_EQ(bigbuf.length(), bigbuf.capacity());
237
10001
    for (size_t i = 0; i < bigbuf.length(); i++)
238
10000
      bigbuf[i] = static_cast<unsigned char>(i);
239
10001
    for (size_t i = 0; i < bigbuf.length(); i++)
240
10001
      EXPECT_EQ(static_cast<unsigned char>(i), bigbuf[i]);
241
  }
242
243
  // Invalidated buffer
244
  {
245
1
    MaybeStackBuffer<char> buf;
246
1
    buf.Invalidate();
247
1
    EXPECT_TRUE(buf.IsInvalidated());
248
1
    EXPECT_FALSE(buf.IsAllocated());
249
1
    EXPECT_EQ(0U, buf.length());
250
1
    EXPECT_EQ(0U, buf.capacity());
251
1
    buf.Invalidate();
252
1
    EXPECT_TRUE(buf.IsInvalidated());
253
  }
254

4
}