Skip to content

Commit e1852b5

Browse files
author
Mert Can Altin
committed
util: add fast path for Latin1 decoding
1 parent bbdfeeb commit e1852b5

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "encoding_binding.h"
2+
#include "node_test_fixture.h"
3+
#include "env-inl.h"
4+
#include "v8.h"
5+
#include "gtest/gtest.h"
6+
7+
namespace node {
8+
namespace encoding_binding {
9+
10+
bool RunDecodeLatin1(Environment* env, Local<Value> args[], Local<Value>* result) {
11+
Isolate* isolate = env->isolate();
12+
TryCatch try_catch(isolate);
13+
14+
BindingData::DecodeLatin1(FunctionCallbackInfo<Value>(args));
15+
16+
if (try_catch.HasCaught()) {
17+
return false;
18+
}
19+
20+
*result = try_catch.Exception();
21+
return true;
22+
}
23+
24+
class EncodingBindingTest : public NodeTestFixture {};
25+
26+
TEST_F(EncodingBindingTest, DecodeLatin1_ValidInput) {
27+
Environment* env = CreateEnvironment();
28+
Isolate* isolate = env->isolate();
29+
HandleScope handle_scope(isolate);
30+
31+
const uint8_t latin1_data[] = {0xC1, 0xE9, 0xF3};
32+
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(latin1_data));
33+
memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data));
34+
35+
Local<Uint8Array> array = Uint8Array::New(ab, 0, sizeof(latin1_data));
36+
Local<Value> args[] = { array };
37+
38+
Local<Value> result;
39+
EXPECT_TRUE(RunDecodeLatin1(env, args, &result));
40+
41+
String::Utf8Value utf8_result(isolate, result);
42+
EXPECT_STREQ(*utf8_result, "Áéó");
43+
}
44+
45+
TEST_F(EncodingBindingTest, DecodeLatin1_EmptyInput) {
46+
Environment* env = CreateEnvironment();
47+
Isolate* isolate = env->isolate();
48+
HandleScope handle_scope(isolate);
49+
50+
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, 0);
51+
Local<Uint8Array> array = Uint8Array::New(ab, 0, 0);
52+
Local<Value> args[] = { array };
53+
54+
Local<Value> result;
55+
EXPECT_TRUE(RunDecodeLatin1(env, args, &result));
56+
57+
String::Utf8Value utf8_result(isolate, result);
58+
EXPECT_STREQ(*utf8_result, "");
59+
}
60+
61+
TEST_F(EncodingBindingTest, DecodeLatin1_InvalidInput) {
62+
Environment* env = CreateEnvironment();
63+
Isolate* isolate = env->isolate();
64+
HandleScope handle_scope(isolate);
65+
66+
Local<Value> args[] = { String::NewFromUtf8Literal(isolate, "Invalid input") };
67+
68+
Local<Value> result;
69+
EXPECT_FALSE(RunDecodeLatin1(env, args, &result));
70+
}
71+
72+
} // namespace encoding_binding
73+
} // namespace node

0 commit comments

Comments
 (0)