"git@developer.sourcefind.cn:SIYIXNI/vllm.git" did not exist on "311490a720951f322977d811eacea685a623b5a1"
load_node_test.cpp 8.88 KB
Newer Older
Jesse Beder's avatar
Jesse Beder committed
1
2
3
4
5
6
7
#include "yaml-cpp/yaml.h"  // IWYU pragma: keep

#include "gtest/gtest.h"

namespace YAML {
namespace {
TEST(LoadNodeTest, Reassign) {
8
9
  Node node = Load("foo");
  node = Node();
10
  EXPECT_TRUE(node.IsNull());
Jesse Beder's avatar
Jesse Beder committed
11
12
13
}

TEST(LoadNodeTest, FallbackValues) {
14
  Node node = Load("foo: bar\nx: 2");
Jesse Beder's avatar
Jesse Beder committed
15
16
17
18
19
20
21
22
23
  EXPECT_EQ("bar", node["foo"].as<std::string>());
  EXPECT_EQ("bar", node["foo"].as<std::string>("hello"));
  EXPECT_EQ("hello", node["baz"].as<std::string>("hello"));
  EXPECT_EQ(2, node["x"].as<int>());
  EXPECT_EQ(2, node["x"].as<int>(5));
  EXPECT_EQ(5, node["y"].as<int>(5));
}

TEST(LoadNodeTest, NumericConversion) {
24
  Node node = Load("[1.5, 1, .nan, .inf, -.inf, 0x15, 015]");
Jesse Beder's avatar
Jesse Beder committed
25
26
  EXPECT_EQ(1.5f, node[0].as<float>());
  EXPECT_EQ(1.5, node[0].as<double>());
27
  EXPECT_THROW(node[0].as<int>(), TypedBadConversion<int>);
Jesse Beder's avatar
Jesse Beder committed
28
29
30
31
32
33
34
  EXPECT_EQ(1, node[1].as<int>());
  EXPECT_EQ(1.0f, node[1].as<float>());
  EXPECT_NE(node[2].as<float>(), node[2].as<float>());
  EXPECT_EQ(std::numeric_limits<float>::infinity(), node[3].as<float>());
  EXPECT_EQ(-std::numeric_limits<float>::infinity(), node[4].as<float>());
  EXPECT_EQ(21, node[5].as<int>());
  EXPECT_EQ(13, node[6].as<int>());
35
36
37
38
39
  // Throw exception: convert a negative number to an unsigned number.
  EXPECT_THROW(node[7].as<unsigned>(), TypedBadConversion<unsigned int>);
  EXPECT_THROW(node[7].as<unsigned short>(), TypedBadConversion<unsigned short>);
  EXPECT_THROW(node[7].as<unsigned long>(), TypedBadConversion<unsigned long>);
  EXPECT_THROW(node[7].as<unsigned long long>(), TypedBadConversion<unsigned long long>);
Jesse Beder's avatar
Jesse Beder committed
40
41
42
}

TEST(LoadNodeTest, Binary) {
43
  Node node = Load(
Jesse Beder's avatar
Jesse Beder committed
44
45
46
47
48
49
50
      "[!!binary \"SGVsbG8sIFdvcmxkIQ==\", !!binary "
      "\"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS"
      "B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG"
      "x1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbi"
      "B0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZG"
      "dlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS"
      "4K\"]");
51
52
53
54
55
56
57
58
59
60
61
  EXPECT_EQ(Binary(reinterpret_cast<const unsigned char*>("Hello, World!"), 13),
            node[0].as<Binary>());
  EXPECT_EQ(Binary(reinterpret_cast<const unsigned char*>(
                       "Man is distinguished, not only by his reason, "
                       "but by this singular passion from other "
                       "animals, which is a lust of the mind, that by "
                       "a perseverance of delight in the continued and "
                       "indefatigable generation of knowledge, exceeds "
                       "the short vehemence of any carnal pleasure.\n"),
                   270),
            node[1].as<Binary>());
Jesse Beder's avatar
Jesse Beder committed
62
63
}

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
TEST(LoadNodeTest, BinaryWithWhitespaces) {
  Node node = Load(
      "binaryText: !binary |-\n"
      "  TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieS\n"
      "  B0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG\n"
      "  x1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbi\n"
      "  B0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZG\n"
      "  dlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS\n"
      "  4K");
  EXPECT_EQ(Binary(reinterpret_cast<const unsigned char*>(
                       "Man is distinguished, not only by his reason, "
                       "but by this singular passion from other "
                       "animals, which is a lust of the mind, that by "
                       "a perseverance of delight in the continued and "
                       "indefatigable generation of knowledge, exceeds "
                       "the short vehemence of any carnal pleasure.\n"),
                   270),
            node["binaryText"].as<Binary>());
}

Jesse Beder's avatar
Jesse Beder committed
84
TEST(LoadNodeTest, IterateSequence) {
85
  Node node = Load("[1, 3, 5, 7]");
Jesse Beder's avatar
Jesse Beder committed
86
87
  int seq[] = {1, 3, 5, 7};
  int i = 0;
88
  for (const_iterator it = node.begin(); it != node.end(); ++it) {
Jesse Beder's avatar
Jesse Beder committed
89
90
91
92
93
94
95
96
    EXPECT_TRUE(i < 4);
    int x = seq[i++];
    EXPECT_EQ(x, it->as<int>());
  }
  EXPECT_EQ(4, i);
}

TEST(LoadNodeTest, IterateMap) {
97
  Node node = Load("{a: A, b: B, c: C}");
Jesse Beder's avatar
Jesse Beder committed
98
  int i = 0;
99
  for (const_iterator it = node.begin(); it != node.end(); ++it) {
Jesse Beder's avatar
Jesse Beder committed
100
101
102
103
104
105
106
107
108
    EXPECT_TRUE(i < 3);
    i++;
    EXPECT_EQ(it->second.as<char>(), it->first.as<char>() + 'A' - 'a');
  }
  EXPECT_EQ(3, i);
}

#ifdef BOOST_FOREACH
TEST(LoadNodeTest, ForEach) {
109
  Node node = Load("[1, 3, 5, 7]");
Jesse Beder's avatar
Jesse Beder committed
110
111
  int seq[] = {1, 3, 5, 7};
  int i = 0;
112
  BOOST_FOREACH (const Node& item, node) {
Jesse Beder's avatar
Jesse Beder committed
113
114
115
116
117
118
    int x = seq[i++];
    EXPECT_EQ(x, item.as<int>());
  }
}

TEST(LoadNodeTest, ForEachMap) {
119
120
  Node node = Load("{a: A, b: B, c: C}");
  BOOST_FOREACH (const const_iterator::value_type& p, node) {
Jesse Beder's avatar
Jesse Beder committed
121
122
123
124
125
126
    EXPECT_EQ(p.second.as<char>(), p.first.as<char>() + 'A' - 'a');
  }
}
#endif

TEST(LoadNodeTest, CloneScalar) {
127
128
  Node node = Load("!foo monkey");
  Node clone = Clone(node);
Jesse Beder's avatar
Jesse Beder committed
129
130
131
132
133
134
  EXPECT_FALSE(clone == node);
  EXPECT_EQ(clone.as<std::string>(), node.as<std::string>());
  EXPECT_EQ(clone.Tag(), node.Tag());
}

TEST(LoadNodeTest, CloneSeq) {
135
136
  Node node = Load("[1, 3, 5, 7]");
  Node clone = Clone(node);
Jesse Beder's avatar
Jesse Beder committed
137
  EXPECT_FALSE(clone == node);
138
  EXPECT_EQ(NodeType::Sequence, clone.Type());
Jesse Beder's avatar
Jesse Beder committed
139
140
141
142
143
144
145
  EXPECT_EQ(clone.size(), node.size());
  for (std::size_t i = 0; i < node.size(); i++) {
    EXPECT_EQ(clone[i].as<int>(), node[i].as<int>());
  }
}

TEST(LoadNodeTest, CloneMap) {
146
147
  Node node = Load("{foo: bar}");
  Node clone = Clone(node);
Jesse Beder's avatar
Jesse Beder committed
148
  EXPECT_FALSE(clone == node);
149
  EXPECT_EQ(NodeType::Map, clone.Type());
Jesse Beder's avatar
Jesse Beder committed
150
151
152
153
154
  EXPECT_EQ(clone.size(), node.size());
  EXPECT_EQ(clone["foo"].as<std::string>(), node["foo"].as<std::string>());
}

TEST(LoadNodeTest, CloneAlias) {
155
156
  Node node = Load("&foo [*foo]");
  Node clone = Clone(node);
Jesse Beder's avatar
Jesse Beder committed
157
  EXPECT_FALSE(clone == node);
158
  EXPECT_EQ(NodeType::Sequence, clone.Type());
Jesse Beder's avatar
Jesse Beder committed
159
160
161
162
163
  EXPECT_EQ(clone.size(), node.size());
  EXPECT_EQ(clone[0], clone);
}

TEST(LoadNodeTest, ForceInsertIntoMap) {
164
  Node node;
Jesse Beder's avatar
Jesse Beder committed
165
166
167
168
  node["a"] = "b";
  node.force_insert("x", "y");
  node.force_insert("a", 5);
  EXPECT_EQ(3, node.size());
169
  EXPECT_EQ(NodeType::Map, node.Type());
Jesse Beder's avatar
Jesse Beder committed
170
171
172
  bool ab = false;
  bool a5 = false;
  bool xy = false;
173
  for (const_iterator it = node.begin(); it != node.end(); ++it) {
Jesse Beder's avatar
Jesse Beder committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    if (it->first.as<std::string>() == "a") {
      if (it->second.as<std::string>() == "b")
        ab = true;
      else if (it->second.as<std::string>() == "5")
        a5 = true;
    } else if (it->first.as<std::string>() == "x" &&
               it->second.as<std::string>() == "y")
      xy = true;
  }
  EXPECT_TRUE(ab);
  EXPECT_TRUE(a5);
  EXPECT_TRUE(xy);
}

TEST(LoadNodeTest, ResetNode) {
189
  Node node = Load("[1, 2, 3]");
Jesse Beder's avatar
Jesse Beder committed
190
  EXPECT_TRUE(!node.IsNull());
191
  Node other = node;
Jesse Beder's avatar
Jesse Beder committed
192
193
194
195
196
197
198
199
  node.reset();
  EXPECT_TRUE(node.IsNull());
  EXPECT_TRUE(!other.IsNull());
  node.reset(other);
  EXPECT_TRUE(!node.IsNull());
  EXPECT_EQ(node, other);
}

200
201
202
203
204
TEST(LoadNodeTest, EmptyString) {
  Node node = Load("\"\"");
  EXPECT_TRUE(!node.IsNull());
}

Jesse Beder's avatar
Jesse Beder committed
205
TEST(LoadNodeTest, DereferenceIteratorError) {
206
207
  Node node = Load("[{a: b}, 1, 2]");
  EXPECT_THROW(node.begin()->first.as<int>(), InvalidNode);
Jesse Beder's avatar
Jesse Beder committed
208
209
  EXPECT_EQ(true, (*node.begin()).IsMap());
  EXPECT_EQ(true, node.begin()->IsMap());
210
211
  EXPECT_THROW((*node.begin()->begin()).Type(), InvalidNode);
  EXPECT_THROW(node.begin()->begin()->Type(), InvalidNode);
Jesse Beder's avatar
Jesse Beder committed
212
213
214
}

TEST(NodeTest, EmitEmptyNode) {
215
216
  Node node;
  Emitter emitter;
Jesse Beder's avatar
Jesse Beder committed
217
218
219
  emitter << node;
  EXPECT_EQ("", std::string(emitter.c_str()));
}
220
221
222
223
224
225
226

TEST(NodeTest, ParseNodeStyle) {
  EXPECT_EQ(EmitterStyle::Flow, Load("[1, 2, 3]").Style());
  EXPECT_EQ(EmitterStyle::Flow, Load("{foo: bar}").Style());
  EXPECT_EQ(EmitterStyle::Block, Load("- foo\n- bar").Style());
  EXPECT_EQ(EmitterStyle::Block, Load("foo: bar").Style());
}
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

struct ParserExceptionTestCase {
  std::string name;
  std::string input;
  std::string expected_exception;
};

TEST(NodeTest, IncompleteJson) {
  std::vector<ParserExceptionTestCase> tests = {
      {"JSON map without value", "{\"access\"", ErrorMsg::END_OF_MAP_FLOW},
      {"JSON map with colon but no value", "{\"access\":",
       ErrorMsg::END_OF_MAP_FLOW},
      {"JSON map with unclosed value quote", "{\"access\":\"",
       ErrorMsg::END_OF_MAP_FLOW},
      {"JSON map without end brace", "{\"access\":\"abc\"",
       ErrorMsg::END_OF_MAP_FLOW},
  };
  for (const ParserExceptionTestCase test : tests) {
    try {
      Load(test.input);
      FAIL() << "Expected exception " << test.expected_exception << " for "
             << test.name << ", input: " << test.input;
    } catch (const ParserException& e) {
      EXPECT_EQ(test.expected_exception, e.msg);
    }
  }
Jesse Beder's avatar
Jesse Beder committed
253
}
254

255
256
257
258
TEST(NodeTest, LoadTildeAsNull) {
  Node node = Load("~");
  ASSERT_TRUE(node.IsNull());
}
259
260
261
262
263
264
    
TEST(NodeTest, LoadTagWithParenthesis) {
    Node node = Load("!Complex(Tag) foo");
    EXPECT_EQ(node.Tag(), "!Complex(Tag)");
    EXPECT_EQ(node.as<std::string>(), "foo");
}
265

266
267
268
269
270
TEST(NodeTest, LoadTagWithNullScalar) {
  Node node = Load("!2");
  EXPECT_TRUE(node.IsNull());
}

271
272
}  // namespace
}  // namespace YAML