| 457 | } |
| 458 | |
| 459 | static bool handcrafted_check_kv(const gguf_context * gguf_ctx, const unsigned int seed, const bool has_tensors, const bool alignment_defined) { |
| 460 | if (!gguf_ctx) { |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | std::mt19937 rng(seed); |
| 465 | |
| 466 | std::vector<tensor_config_t> tensor_configs; |
| 467 | if (has_tensors) { |
| 468 | tensor_configs = get_tensor_configs(rng); |
| 469 | } |
| 470 | |
| 471 | std::vector<std::pair<enum gguf_type, enum gguf_type>> kv_types = get_kv_types(rng); |
| 472 | |
| 473 | bool ok = true; |
| 474 | |
| 475 | for (int i = 0; i < int(kv_types.size()); ++i) { |
| 476 | const enum gguf_type type = gguf_type(kv_types[i].first); |
| 477 | const enum gguf_type type_arr = gguf_type(kv_types[i].second); |
| 478 | |
| 479 | const std::string key = "my_key_" + std::to_string(i); |
| 480 | |
| 481 | uint32_t data[16]; |
| 482 | for (int j = 0; j < 16; ++j) { |
| 483 | data[j] = rng(); |
| 484 | if (type == GGUF_TYPE_STRING || type_arr == GGUF_TYPE_STRING) { |
| 485 | data[j] |= 0x01010101; // avoid random null-termination of string |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | const char * data8 = reinterpret_cast<const char *>(data); |
| 490 | const int id = gguf_find_key(gguf_ctx, key.c_str()); |
| 491 | |
| 492 | if (type == GGUF_TYPE_STRING) { |
| 493 | const char * str = gguf_get_val_str(gguf_ctx, id); |
| 494 | const uint64_t n = strlen(str); |
| 495 | const uint64_t n_expected = rng() % sizeof(data); |
| 496 | if (n != n_expected) { |
| 497 | ok = false; |
| 498 | continue; |
| 499 | } |
| 500 | if (!std::equal(str, str + n, data8)) { |
| 501 | ok = false; |
| 502 | } |
| 503 | continue; |
| 504 | } |
| 505 | |
| 506 | if (type == GGUF_TYPE_ARRAY) { |
| 507 | const size_t type_size = gguf_type_size(type_arr); |
| 508 | const uint64_t arr_n = gguf_get_arr_n(gguf_ctx, id); |
| 509 | |
| 510 | if (type_arr == GGUF_TYPE_STRING) { |
| 511 | const uint64_t nstr_expected = rng() % (16 + 1); |
| 512 | if (arr_n != nstr_expected) { |
| 513 | ok = false; |
| 514 | continue; |
| 515 | } |
| 516 | for (uint64_t istr = 0; istr < nstr_expected; ++istr) { |
no test coverage detected