forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_string_bytes.cc
More file actions
100 lines (79 loc) · 2.86 KB
/
test_string_bytes.cc
File metadata and controls
100 lines (79 loc) · 2.86 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
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
#include "gtest/gtest.h"
#include "node.h"
#include "node_test_fixture.h"
#include "string_bytes.h"
#include "util-inl.h"
using node::MaybeStackBuffer;
using node::StringBytes;
using v8::HandleScope;
using v8::Local;
using v8::Maybe;
using v8::String;
class StringBytesTest : public EnvironmentTestFixture {};
// Data "Hello, ÆÊÎÖÿ"
static const char latin1_data[] = "Hello, \xC6\xCA\xCE\xD6\xFF";
static const char utf8_data[] = "Hello, ÆÊÎÖÿ";
TEST_F(StringBytesTest, WriteLatin1WithOneByteString) {
const HandleScope handle_scope(isolate_);
const Argv argv;
Env env_{handle_scope, argv};
Local<String> one_byte_str =
String::NewFromOneByte(isolate_,
reinterpret_cast<const uint8_t*>(latin1_data))
.ToLocalChecked();
Maybe<size_t> size_maybe =
StringBytes::StorageSize(isolate_, one_byte_str, node::LATIN1);
ASSERT_TRUE(size_maybe.IsJust());
size_t size = size_maybe.FromJust();
ASSERT_EQ(size, 12u);
MaybeStackBuffer<char> buf;
size_t written = StringBytes::Write(
isolate_, buf.out(), buf.capacity(), one_byte_str, node::LATIN1);
ASSERT_EQ(written, 12u);
// Null-terminate the buffer and compare the contents.
buf.SetLength(13);
buf[12] = '\0';
ASSERT_STREQ(latin1_data, buf.out());
}
TEST_F(StringBytesTest, WriteLatin1WithUtf8String) {
const HandleScope handle_scope(isolate_);
const Argv argv;
Env env_{handle_scope, argv};
Local<String> utf8_str =
String::NewFromUtf8(isolate_, utf8_data).ToLocalChecked();
Maybe<size_t> size_maybe =
StringBytes::StorageSize(isolate_, utf8_str, node::LATIN1);
ASSERT_TRUE(size_maybe.IsJust());
size_t size = size_maybe.FromJust();
ASSERT_EQ(size, 12u);
MaybeStackBuffer<char> buf;
size_t written = StringBytes::Write(
isolate_, buf.out(), buf.capacity(), utf8_str, node::LATIN1);
ASSERT_EQ(written, 12u);
// Null-terminate the buffer and compare the contents.
buf.SetLength(13);
buf[12] = '\0';
ASSERT_STREQ(latin1_data, buf.out());
}
// Verify that StringBytes::Write converts two-byte characters to one-byte
// characters, even if there is no valid one-byte representation.
TEST_F(StringBytesTest, WriteLatin1WithInvalidChar) {
const HandleScope handle_scope(isolate_);
const Argv argv;
Env env_{handle_scope, argv};
Local<String> utf8_str =
String::NewFromUtf8(isolate_, "Hello, 世界").ToLocalChecked();
Maybe<size_t> size_maybe =
StringBytes::StorageSize(isolate_, utf8_str, node::LATIN1);
ASSERT_TRUE(size_maybe.IsJust());
size_t size = size_maybe.FromJust();
ASSERT_EQ(size, 9u);
MaybeStackBuffer<char> buf;
size_t written = StringBytes::Write(
isolate_, buf.out(), buf.capacity(), utf8_str, node::LATIN1);
ASSERT_EQ(written, 9u);
// Null-terminate the buffer and compare the contents.
buf.SetLength(10);
buf[9] = '\0';
ASSERT_STREQ("Hello, \x16\x4C", buf.out());
}