File: test_util.cc

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (357 lines) | stat: -rw-r--r-- 11,263 bytes parent folder | download | duplicates (3)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "gtest/gtest.h"
#include "node_options-inl.h"
#include "node_test_fixture.h"
#include "simdutf.h"
#include "util-inl.h"
#include "v8-function-callback.h"
#include "v8-primitive.h"
#include "v8.h"

using node::Calloc;
using node::Malloc;
using node::MaybeStackBuffer;
using node::SPrintF;
using node::StringEqualNoCase;
using node::StringEqualNoCaseN;
using node::ToLower;
using node::UncheckedCalloc;
using node::UncheckedMalloc;

class UtilTest : public EnvironmentTestFixture {};

TEST_F(UtilTest, ListHead) {
  struct Item { node::ListNode<Item> node_; };
  typedef node::ListHead<Item, &Item::node_> List;

  List list;
  EXPECT_TRUE(list.IsEmpty());
  EXPECT_TRUE(list.PopFront() == nullptr);

  Item one;
  EXPECT_TRUE(one.node_.IsEmpty());

  list.PushBack(&one);
  EXPECT_FALSE(list.IsEmpty());
  EXPECT_FALSE(one.node_.IsEmpty());

  {
    List::Iterator it = list.begin();
    EXPECT_NE(list.end(), it);
    EXPECT_EQ(&one, *it);
    ++it;
    EXPECT_FALSE(it != list.end());  // Iterator only implements != operator.
  }

  Item two;
  list.PushBack(&two);

  {
    List::Iterator it = list.begin();
    EXPECT_NE(list.end(), it);
    EXPECT_EQ(&one, *it);
    ++it;
    EXPECT_NE(list.end(), it);
    EXPECT_EQ(&two, *it);
    ++it;
    EXPECT_FALSE(it != list.end());  // Iterator only implements != operator.
  }

  EXPECT_EQ(&one, list.PopFront());
  EXPECT_TRUE(one.node_.IsEmpty());
  EXPECT_FALSE(list.IsEmpty());

  {
    List::Iterator it = list.begin();
    EXPECT_NE(list.end(), it);
    EXPECT_EQ(&two, *it);
    ++it;
    EXPECT_FALSE(it != list.end());  // Iterator only implements != operator.
  }

  EXPECT_EQ(&two, list.PopFront());
  EXPECT_TRUE(two.node_.IsEmpty());
  EXPECT_TRUE(list.IsEmpty());
  EXPECT_FALSE(list.begin() != list.end());
}

TEST_F(UtilTest, StringEqualNoCase) {
  EXPECT_FALSE(StringEqualNoCase("a", "b"));
  EXPECT_TRUE(StringEqualNoCase("", ""));
  EXPECT_TRUE(StringEqualNoCase("equal", "equal"));
  EXPECT_TRUE(StringEqualNoCase("equal", "EQUAL"));
  EXPECT_TRUE(StringEqualNoCase("EQUAL", "EQUAL"));
  EXPECT_FALSE(StringEqualNoCase("equal", "equals"));
  EXPECT_FALSE(StringEqualNoCase("equals", "equal"));
}

TEST_F(UtilTest, StringEqualNoCaseN) {
  EXPECT_FALSE(StringEqualNoCaseN("a", "b", strlen("a")));
  EXPECT_TRUE(StringEqualNoCaseN("", "", strlen("")));
  EXPECT_TRUE(StringEqualNoCaseN("equal", "equal", strlen("equal")));
  EXPECT_TRUE(StringEqualNoCaseN("equal", "EQUAL", strlen("equal")));
  EXPECT_TRUE(StringEqualNoCaseN("EQUAL", "EQUAL", strlen("equal")));
  EXPECT_TRUE(StringEqualNoCaseN("equal", "equals", strlen("equal")));
  EXPECT_FALSE(StringEqualNoCaseN("equal", "equals", strlen("equals")));
  EXPECT_TRUE(StringEqualNoCaseN("equals", "equal", strlen("equal")));
  EXPECT_FALSE(StringEqualNoCaseN("equals", "equal", strlen("equals")));
  EXPECT_TRUE(StringEqualNoCaseN("abc\0abc", "abc\0efg", strlen("abcdefgh")));
  EXPECT_FALSE(StringEqualNoCaseN("abc\0abc", "abcd\0efg", strlen("abcdefgh")));
}

TEST_F(UtilTest, ToLower) {
  EXPECT_EQ('0', ToLower('0'));
  EXPECT_EQ('a', ToLower('a'));
  EXPECT_EQ('a', ToLower('A'));
}

#define TEST_AND_FREE(expression, size)                                        \
  do {                                                                         \
    auto pointer = expression(size);                                           \
    EXPECT_EQ(pointer == nullptr, size == 0);                                  \
    free(pointer);                                                             \
  } while (0)

TEST_F(UtilTest, Malloc) {
  TEST_AND_FREE(Malloc<char>, 0);
  TEST_AND_FREE(Malloc<char>, 1);
  TEST_AND_FREE(Malloc, 0);
  TEST_AND_FREE(Malloc, 1);
}

TEST_F(UtilTest, Calloc) {
  TEST_AND_FREE(Calloc<char>, 0);
  TEST_AND_FREE(Calloc<char>, 1);
  TEST_AND_FREE(Calloc, 0);
  TEST_AND_FREE(Calloc, 1);
}

TEST_F(UtilTest, UncheckedMalloc) {
  TEST_AND_FREE(UncheckedMalloc<char>, 0);
  TEST_AND_FREE(UncheckedMalloc<char>, 1);
  TEST_AND_FREE(UncheckedMalloc, 0);
  TEST_AND_FREE(UncheckedMalloc, 1);
}

TEST_F(UtilTest, UncheckedCalloc) {
  TEST_AND_FREE(UncheckedCalloc<char>, 0);
  TEST_AND_FREE(UncheckedCalloc<char>, 1);
  TEST_AND_FREE(UncheckedCalloc, 0);
  TEST_AND_FREE(UncheckedCalloc, 1);
}

template <typename T>
static void MaybeStackBufferBasic() {
  MaybeStackBuffer<T> buf;
  size_t old_length;
  size_t old_capacity;

  // Default constructor.
  EXPECT_EQ(0U, buf.length());
  EXPECT_FALSE(buf.IsAllocated());
  EXPECT_GT(buf.capacity(), buf.length());

  // SetLength() expansion.
  buf.SetLength(buf.capacity());
  EXPECT_EQ(buf.capacity(), buf.length());
  EXPECT_FALSE(buf.IsAllocated());

  // Means of accessing raw buffer.
  EXPECT_EQ(buf.out(), *buf);
  EXPECT_EQ(&buf[0], *buf);

  // Basic I/O.
  for (size_t i = 0; i < buf.length(); i++)
    buf[i] = static_cast<T>(i);
  for (size_t i = 0; i < buf.length(); i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);

  // SetLengthAndZeroTerminate().
  buf.SetLengthAndZeroTerminate(buf.capacity() - 1);
  EXPECT_EQ(buf.capacity() - 1, buf.length());
  for (size_t i = 0; i < buf.length(); i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);
  buf.SetLength(buf.capacity());
  EXPECT_EQ(0, buf[buf.length() - 1]);

  // Initial Realloc.
  old_length = buf.length() - 1;
  old_capacity = buf.capacity();
  buf.AllocateSufficientStorage(buf.capacity() * 2);
  EXPECT_EQ(buf.capacity(), buf.length());
  EXPECT_TRUE(buf.IsAllocated());
  for (size_t i = 0; i < old_length; i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);
  EXPECT_EQ(0, buf[old_length]);

  // SetLength() reduction and expansion.
  for (size_t i = 0; i < buf.length(); i++)
    buf[i] = static_cast<T>(i);
  buf.SetLength(10);
  for (size_t i = 0; i < buf.length(); i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);
  buf.SetLength(buf.capacity());
  for (size_t i = 0; i < buf.length(); i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);

  // Subsequent Realloc.
  old_length = buf.length();
  old_capacity = buf.capacity();
  buf.AllocateSufficientStorage(old_capacity * 1.5);
  EXPECT_EQ(buf.capacity(), buf.length());
  EXPECT_EQ(static_cast<size_t>(old_capacity * 1.5), buf.length());
  EXPECT_TRUE(buf.IsAllocated());
  for (size_t i = 0; i < old_length; i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);

  // Basic I/O on Realloc'd buffer.
  for (size_t i = 0; i < buf.length(); i++)
    buf[i] = static_cast<T>(i);
  for (size_t i = 0; i < buf.length(); i++)
    EXPECT_EQ(static_cast<T>(i), buf[i]);

  // Release().
  T* rawbuf = buf.out();
  buf.Release();
  EXPECT_EQ(0U, buf.length());
  EXPECT_FALSE(buf.IsAllocated());
  EXPECT_GT(buf.capacity(), buf.length());
  free(rawbuf);
}

TEST_F(UtilTest, MaybeStackBuffer) {
  MaybeStackBufferBasic<uint8_t>();
  MaybeStackBufferBasic<uint16_t>();

  // Constructor with size parameter.
  {
    MaybeStackBuffer<unsigned char> buf(100);
    EXPECT_EQ(100U, buf.length());
    EXPECT_FALSE(buf.IsAllocated());
    EXPECT_GT(buf.capacity(), buf.length());
    buf.SetLength(buf.capacity());
    EXPECT_EQ(buf.capacity(), buf.length());
    EXPECT_FALSE(buf.IsAllocated());
    for (size_t i = 0; i < buf.length(); i++)
      buf[i] = static_cast<unsigned char>(i);
    for (size_t i = 0; i < buf.length(); i++)
      EXPECT_EQ(static_cast<unsigned char>(i), buf[i]);

    MaybeStackBuffer<unsigned char> bigbuf(10000);
    EXPECT_EQ(10000U, bigbuf.length());
    EXPECT_TRUE(bigbuf.IsAllocated());
    EXPECT_EQ(bigbuf.length(), bigbuf.capacity());
    for (size_t i = 0; i < bigbuf.length(); i++)
      bigbuf[i] = static_cast<unsigned char>(i);
    for (size_t i = 0; i < bigbuf.length(); i++)
      EXPECT_EQ(static_cast<unsigned char>(i), bigbuf[i]);
  }

  // Invalidated buffer.
  {
    MaybeStackBuffer<char> buf;
    buf.Invalidate();
    EXPECT_TRUE(buf.IsInvalidated());
    EXPECT_FALSE(buf.IsAllocated());
    EXPECT_EQ(0U, buf.length());
    EXPECT_EQ(0U, buf.capacity());
    buf.Invalidate();
    EXPECT_TRUE(buf.IsInvalidated());
  }
}

TEST_F(UtilTest, SPrintF) {
  // %d, %u and %s all do the same thing. The actual C++ type is used to infer
  // the right representation.
  EXPECT_EQ(SPrintF("%s", false), "false");
  EXPECT_EQ(SPrintF("%s", true), "true");
  EXPECT_EQ(SPrintF("%d", true), "true");
  EXPECT_EQ(SPrintF("%u", true), "true");
  EXPECT_EQ(SPrintF("%d", 10000000000LL), "10000000000");
  EXPECT_EQ(SPrintF("%d", -10000000000LL), "-10000000000");
  EXPECT_EQ(SPrintF("%u", 10000000000LL), "10000000000");
  EXPECT_EQ(SPrintF("%u", -10000000000LL), "-10000000000");
  EXPECT_EQ(SPrintF("%i", 10), "10");
  EXPECT_EQ(SPrintF("%d", 10), "10");
  EXPECT_EQ(SPrintF("%x", 15), "f");
  EXPECT_EQ(SPrintF("%x", 16), "10");
  EXPECT_EQ(SPrintF("%X", 15), "F");
  EXPECT_EQ(SPrintF("%X", 16), "10");
  EXPECT_EQ(SPrintF("%o", 7), "7");
  EXPECT_EQ(SPrintF("%o", 8), "10");

  EXPECT_EQ(atof(SPrintF("%s", 0.5).c_str()), 0.5);
  EXPECT_EQ(atof(SPrintF("%s", -0.5).c_str()), -0.5);

  void (*fn)() = []() {};
  void* p = reinterpret_cast<void*>(&fn);
  EXPECT_GE(SPrintF("%p", fn).size(), 4u);
  EXPECT_GE(SPrintF("%p", p).size(), 4u);

  const std::string foo = "foo";
  const char* bar = "bar";
  EXPECT_EQ(SPrintF("%s %s", foo, "bar"), "foo bar");
  EXPECT_EQ(SPrintF("%s %s", foo, bar), "foo bar");
  EXPECT_EQ(SPrintF("%s", nullptr), "(null)");

  EXPECT_EQ(SPrintF("[%% %s %%]", foo), "[% foo %]");

  struct HasToString {
    std::string ToString() const {
      return "meow";
    }
  };
  EXPECT_EQ(SPrintF("%s", HasToString{}), "meow");

  const std::string with_zero = std::string("a") + '\0' + 'b';
  EXPECT_EQ(SPrintF("%s", with_zero), with_zero);
}

TEST_F(UtilTest, DumpJavaScriptStackWithNoIsolate) {
  node::DumpJavaScriptBacktrace(stderr);
}

TEST_F(UtilTest, DetermineSpecificErrorType) {
  const v8::HandleScope handle_scope(isolate_);
  Argv argv;
  Env env{handle_scope, argv, node::EnvironmentFlags::kNoBrowserGlobals};

  // Boolean
  EXPECT_EQ(
      node::DetermineSpecificErrorType(*env, v8::Boolean::New(isolate_, true)),
      "type boolean (true)");

  // BigInt
  EXPECT_EQ(
      node::DetermineSpecificErrorType(*env, v8::BigInt::New(isolate_, 255)),
      "type bigint (255)");

  // String
  EXPECT_EQ(
      node::DetermineSpecificErrorType(
          *env, v8::String::NewFromUtf8(isolate_, "input").ToLocalChecked()),
      "type string ('input')");
  // String that calls JSONStringify
  EXPECT_EQ(
      node::DetermineSpecificErrorType(
          *env, v8::String::NewFromUtf8(isolate_, "'input'").ToLocalChecked()),
      "type string (\"'input'\")");
  EXPECT_EQ(node::DetermineSpecificErrorType(
                *env,
                v8::String::NewFromUtf8(isolate_,
                                        "string with more than 26 characters")
                    .ToLocalChecked()),
            "type string ('string with more than 26 ...')");

  // Number, Int32, Uint32
  EXPECT_EQ(
      node::DetermineSpecificErrorType(*env, v8::Number::New(isolate_, 10)),
      "type number (10)");
  EXPECT_EQ(
      node::DetermineSpecificErrorType(*env, v8::Int32::New(isolate_, -255)),
      "type number (-255)");
  EXPECT_EQ(
      node::DetermineSpecificErrorType(*env, v8::Uint32::New(isolate_, 255)),
      "type number (255)");
}