From bb59964aa067459f50a09450329664a03a8e0b43 Mon Sep 17 00:00:00 2001 From: chenzizhan Date: Mon, 31 Jul 2023 13:18:43 +0800 Subject: fuzz test with benchmark --- test/utils.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'test/utils.cpp') diff --git a/test/utils.cpp b/test/utils.cpp index d54d701..b53e4e4 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -243,3 +245,51 @@ bool Fieldstat_tag_list_wrapper::operator==(const Fieldstat_tag_list_wrapper &ta } return true; } + +Fieldstat_tag_list_wrapper& Fieldstat_tag_list_wrapper::sort_tag_list() +{ + std::sort(tag_list_c.tag, tag_list_c.tag + tag_list_c.n_tag, [](const struct fieldstat_tag &a, const struct fieldstat_tag &b) { + return strcmp((char *)a.key, (char *)b.key) < 0; + }); + return *this; +} + +double test_cal_topk_accuracy(vector &test_result, unordered_map &expected_count) +{ + std::vector> countVector(expected_count.begin(), expected_count.end()); + std::sort(countVector.begin(), countVector.end(), [](const std::pair &a, const std::pair &b) { + return a.second > b.second; + }); + + std::set myset; + int min_in_max_count = 0; + size_t i; + for (i = 0; i < test_result.size(); ++i) { + myset.insert(countVector[i].first); + min_in_max_count = countVector[i].second; + } + while (i < countVector.size()) { + if (countVector[i].second != min_in_max_count) { + break; + } + myset.insert(countVector[i].first); + i++; + } + + // cout << "myset : " << endl; + // for (auto it = myset.begin(); it != myset.end(); it++) { + // cout << *it << endl; + // } + // cout << "------------------------- " << endl; + + int correct = 0; + for (size_t i = 0; i < test_result.size(); i++) { + string key = test_result[i]->to_string(); + if (myset.find(key) != myset.end()) { + correct++; + } + } + + double accuracy = (double)correct / test_result.size(); + return accuracy; +} -- cgit v1.2.3