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 26644 : 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 26644 : 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 26644 : 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 26644 : TEST(Nesting) {
79 5 : auto value = TracedValue::Create();
80 5 : auto v0 = TracedValue::Create();
81 5 : auto v2 = TracedValue::Create();
82 10 : v0->SetString("s1", std::string("Hello World!"));
83 5 : v2->SetValue("v0", v0.get());
84 5 : value->SetValue("v2", v2.get());
85 :
86 : std::string json;
87 5 : value->AppendAsTraceFormat(&json);
88 5 : CHECK_EQ("{\"v2\":{\"v0\":{\"s1\":\"Hello World!\"}}}", json);
89 5 : }
90 :
91 26644 : TEST(LongStrings) {
92 5 : std::string long_string = "supercalifragilisticexpialidocious";
93 5 : std::string long_string2 = "0123456789012345678901234567890123456789";
94 : char long_string3[4096];
95 40965 : for (size_t i = 0; i < sizeof(long_string3); ++i)
96 20480 : long_string3[i] = static_cast<char>('a' + (i % 26));
97 5 : long_string3[sizeof(long_string3) - 1] = '\0';
98 :
99 5 : auto value = TracedValue::Create();
100 5 : value->SetString("a", "short");
101 : value->SetString("b", long_string);
102 5 : value->BeginArray("c");
103 : value->AppendString(long_string2);
104 5 : value->AppendString("");
105 5 : value->BeginDictionary();
106 5 : value->SetString("a", long_string3);
107 5 : value->EndDictionary();
108 5 : value->EndArray();
109 :
110 : std::string json;
111 5 : value->AppendAsTraceFormat(&json);
112 35 : CHECK_EQ("{\"a\":\"short\",\"b\":\"" + long_string + "\",\"c\":[\"" +
113 : long_string2 + "\",\"\",{\"a\":\"" + long_string3 + "\"}]}",
114 : json);
115 5 : }
116 :
117 26644 : TEST(Escaping) {
118 : const char* string1 = "abc\"\'\\\\x\"y\'z\n\x09\x17";
119 : std::string chars127;
120 1275 : for (int i = 1; i <= 127; ++i) {
121 635 : chars127 += static_cast<char>(i);
122 : }
123 5 : auto value = TracedValue::Create();
124 5 : value->SetString("a", string1);
125 : value->SetString("b", chars127);
126 :
127 : std::string json;
128 5 : value->AppendAsTraceFormat(&json);
129 : // Cannot use the expected value literal directly in CHECK_EQ
130 : // as it fails to process the # character on Windows.
131 : const char* expected =
132 : R"({"a":"abc\"'\\\\x\"y'z\n\t\u0017","b":"\u0001\u0002\u0003\u0004\u0005)"
133 : R"(\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013)"
134 : R"(\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E)"
135 : R"(\u001F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ)"
136 : R"([\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007F"})";
137 5 : CHECK_EQ(expected, json);
138 5 : }
139 :
140 26644 : TEST(Utf8) {
141 : const char* string1 = "Люблю тебя, Петра творенье";
142 : const char* string2 = "☀\u2600\u26FF";
143 5 : auto value = TracedValue::Create();
144 5 : value->SetString("a", string1);
145 5 : value->SetString("b", string2);
146 : // Surrogate pair test. Smile emoji === U+1F601 === \xf0\x9f\x98\x81
147 5 : value->SetString("c", "\U0001F601");
148 : std::string json;
149 5 : value->AppendAsTraceFormat(&json);
150 : const char* expected =
151 : "{\"a\":\"\u041B\u044E\u0431\u043B\u044E \u0442\u0435\u0431\u044F, \u041F"
152 : "\u0435\u0442\u0440\u0430 \u0442\u0432\u043E\u0440\u0435\u043D\u044C"
153 : "\u0435\",\"b\":\"\u2600\u2600\u26FF\",\"c\":\"\xf0\x9f\x98\x81\"}";
154 5 : CHECK_EQ(expected, json);
155 79922 : }
|