Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/tracing/traced-value.h"
6 : #include "test/cctest/cctest.h"
7 :
8 : using v8::tracing::TracedValue;
9 :
10 28342 : TEST(FlatDictionary) {
11 5 : auto value = TracedValue::Create();
12 5 : value->SetInteger("int", 2014);
13 5 : value->SetDouble("double", 0.0);
14 5 : value->SetBoolean("bool", true);
15 5 : value->SetString("string", "string");
16 5 : std::string json = "PREFIX";
17 5 : value->AppendAsTraceFormat(&json);
18 5 : CHECK_EQ(
19 : "PREFIX{\"int\":2014,\"double\":0,\"bool\":true,\"string\":"
20 : "\"string\"}",
21 : json);
22 5 : }
23 :
24 28342 : TEST(NoDotPathExpansion) {
25 5 : auto value = TracedValue::Create();
26 5 : value->SetInteger("in.t", 2014);
27 5 : value->SetDouble("doub.le", -20.25);
28 5 : value->SetBoolean("bo.ol", true);
29 5 : value->SetString("str.ing", "str.ing");
30 : std::string json;
31 5 : value->AppendAsTraceFormat(&json);
32 5 : CHECK_EQ(
33 : "{\"in.t\":2014,\"doub.le\":-20.25,\"bo.ol\":true,\"str.ing\":\"str."
34 : "ing\"}",
35 : json);
36 5 : }
37 :
38 28342 : TEST(Hierarchy) {
39 5 : auto value = TracedValue::Create();
40 5 : value->SetInteger("i0", 2014);
41 5 : value->BeginDictionary("dict1");
42 5 : value->SetInteger("i1", 2014);
43 5 : value->BeginDictionary("dict2");
44 5 : value->SetBoolean("b2", false);
45 5 : value->EndDictionary();
46 5 : value->SetString("s1", "foo");
47 5 : value->EndDictionary();
48 5 : value->SetDouble("d0", 0.0);
49 5 : value->SetDouble("d1", 10.5);
50 5 : value->SetBoolean("b0", true);
51 5 : value->BeginArray("a1");
52 5 : value->AppendInteger(1);
53 5 : value->AppendBoolean(true);
54 5 : value->BeginDictionary();
55 5 : value->SetInteger("i2", 3);
56 5 : value->EndDictionary();
57 5 : value->EndArray();
58 5 : value->SetString("s0", "foo");
59 :
60 5 : value->BeginArray("arr1");
61 5 : value->BeginDictionary();
62 5 : value->EndDictionary();
63 5 : value->BeginArray();
64 5 : value->EndArray();
65 5 : value->BeginDictionary();
66 5 : value->EndDictionary();
67 5 : value->EndArray();
68 :
69 : std::string json;
70 5 : value->AppendAsTraceFormat(&json);
71 5 : CHECK_EQ(
72 : "{\"i0\":2014,\"dict1\":{\"i1\":2014,\"dict2\":{\"b2\":false},"
73 : "\"s1\":\"foo\"},\"d0\":0,\"d1\":10.5,\"b0\":true,\"a1\":[1,true,{\"i2\":"
74 : "3}],\"s0\":\"foo\",\"arr1\":[{},[],{}]}",
75 : json);
76 5 : }
77 :
78 28342 : TEST(LongStrings) {
79 5 : std::string long_string = "supercalifragilisticexpialidocious";
80 5 : std::string long_string2 = "0123456789012345678901234567890123456789";
81 : char long_string3[4096];
82 20485 : for (size_t i = 0; i < sizeof(long_string3); ++i)
83 20480 : long_string3[i] = static_cast<char>('a' + (i % 26));
84 5 : long_string3[sizeof(long_string3) - 1] = '\0';
85 :
86 5 : auto value = TracedValue::Create();
87 5 : value->SetString("a", "short");
88 : value->SetString("b", long_string);
89 5 : value->BeginArray("c");
90 : value->AppendString(long_string2);
91 5 : value->AppendString("");
92 5 : value->BeginDictionary();
93 5 : value->SetString("a", long_string3);
94 5 : value->EndDictionary();
95 5 : value->EndArray();
96 :
97 : std::string json;
98 5 : value->AppendAsTraceFormat(&json);
99 35 : CHECK_EQ("{\"a\":\"short\",\"b\":\"" + long_string + "\",\"c\":[\"" +
100 : long_string2 + "\",\"\",{\"a\":\"" + long_string3 + "\"}]}",
101 : json);
102 5 : }
103 :
104 28342 : TEST(Escaping) {
105 : const char* string1 = "abc\"\'\\\\x\"y\'z\n\x09\x17";
106 : std::string chars127;
107 640 : for (int i = 1; i <= 127; ++i) {
108 635 : chars127 += static_cast<char>(i);
109 : }
110 5 : auto value = TracedValue::Create();
111 5 : value->SetString("a", string1);
112 : value->SetString("b", chars127);
113 :
114 : std::string json;
115 5 : value->AppendAsTraceFormat(&json);
116 : // Cannot use the expected value literal directly in CHECK_EQ
117 : // as it fails to process the # character on Windows.
118 : const char* expected =
119 : R"({"a":"abc\"'\\\\x\"y'z\n\t\u0017","b":"\u0001\u0002\u0003\u0004\u0005)"
120 : R"(\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013)"
121 : R"(\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E)"
122 : R"(\u001F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ)"
123 : R"([\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007F"})";
124 5 : CHECK_EQ(expected, json);
125 5 : }
126 :
127 28342 : TEST(Utf8) {
128 : const char* string1 = "Люблю тебя, Петра творенье";
129 : const char* string2 = "☀\u2600\u26FF";
130 5 : auto value = TracedValue::Create();
131 5 : value->SetString("a", string1);
132 5 : value->SetString("b", string2);
133 : // Surrogate pair test. Smile emoji === U+1F601 === \xf0\x9f\x98\x81
134 5 : value->SetString("c", "\U0001F601");
135 : std::string json;
136 5 : value->AppendAsTraceFormat(&json);
137 : const char* expected =
138 : "{\"a\":\"\u041B\u044E\u0431\u043B\u044E \u0442\u0435\u0431\u044F, \u041F"
139 : "\u0435\u0442\u0440\u0430 \u0442\u0432\u043E\u0440\u0435\u043D\u044C"
140 : "\u0435\",\"b\":\"\u2600\u2600\u26FF\",\"c\":\"\xf0\x9f\x98\x81\"}";
141 5 : CHECK_EQ(expected, json);
142 85016 : }
|