| 338 | } |
| 339 | |
| 340 | void Fuzz(random::SimplePhilox* rng) { |
| 341 | // Generate keys. |
| 342 | auto num_keys = 1 + rng->Rand32() % 100; |
| 343 | std::unordered_set<string> unique_keys; |
| 344 | for (auto i = 0; i < num_keys; ++i) { |
| 345 | unique_keys.emplace(RandStr(rng)); |
| 346 | } |
| 347 | |
| 348 | // Generate serialized example. |
| 349 | Example example; |
| 350 | string serialized_example; |
| 351 | auto num_concats = 1 + rng->Rand32() % 4; |
| 352 | std::vector<Feature::KindCase> feat_types( |
| 353 | {Feature::kBytesList, Feature::kFloatList, Feature::kInt64List}); |
| 354 | std::vector<string> all_keys(unique_keys.begin(), unique_keys.end()); |
| 355 | while (num_concats--) { |
| 356 | example.Clear(); |
| 357 | auto num_active_keys = 1 + rng->Rand32() % all_keys.size(); |
| 358 | |
| 359 | // Generate features. |
| 360 | for (auto i = 0; i < num_active_keys; ++i) { |
| 361 | auto fkey = all_keys[rng->Rand32() % all_keys.size()]; |
| 362 | auto ftype_idx = rng->Rand32() % feat_types.size(); |
| 363 | auto num_features = 1 + rng->Rand32() % 5; |
| 364 | switch (static_cast<Feature::KindCase>(feat_types[ftype_idx])) { |
| 365 | case Feature::kBytesList: { |
| 366 | BytesList* bytes_list = |
| 367 | (*example.mutable_features()->mutable_feature())[fkey] |
| 368 | .mutable_bytes_list(); |
| 369 | while (num_features--) { |
| 370 | bytes_list->add_value(RandStr(rng)); |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | case Feature::kFloatList: { |
| 375 | FloatList* float_list = |
| 376 | (*example.mutable_features()->mutable_feature())[fkey] |
| 377 | .mutable_float_list(); |
| 378 | while (num_features--) { |
| 379 | float_list->add_value(rng->RandFloat()); |
| 380 | } |
| 381 | break; |
| 382 | } |
| 383 | case Feature::kInt64List: { |
| 384 | Int64List* int64_list = |
| 385 | (*example.mutable_features()->mutable_feature())[fkey] |
| 386 | .mutable_int64_list(); |
| 387 | while (num_features--) { |
| 388 | int64_list->add_value(rng->Rand64()); |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | default: { |
| 393 | LOG(QFATAL); |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | } |
no test coverage detected