/
githubmirror
/
nbs
Обзор
Документация
Войти
/
githubmirror
/
nbs
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
library/cpp/monlib/encode/buffered/string_pool_ut.cpp
84 строки
2 KB
qkrorlqr
Initial NBS OpenSource export
28 апр 2023, 21:54
28 апр 2023, 21:54
783a858
Код
Авторство
О чём код?
#include "string_pool.h" #include <library/cpp/testing/unittest/registar.h> using namespace NMonitoring; Y_UNIT_TEST_SUITE(TStringPoolTest) { Y_UNIT_TEST(PutIfAbsent) { TStringPoolBuilder strPool; strPool.SetSorted(true); auto* h1 = strPool.PutIfAbsent("one"); auto* h2 = strPool.PutIfAbsent("two"); auto* h3 = strPool.PutIfAbsent("two"); UNIT_ASSERT(h1 != h2); UNIT_ASSERT(h2 == h3); UNIT_ASSERT_VALUES_EQUAL(h1->Frequency, 1); UNIT_ASSERT_VALUES_EQUAL(h1->Index, 0); UNIT_ASSERT_VALUES_EQUAL(h2->Frequency, 2); UNIT_ASSERT_VALUES_EQUAL(h2->Index, 1); UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 6); UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 2); } Y_UNIT_TEST(SortByFrequency) { TStringPoolBuilder strPool; strPool.SetSorted(true); auto* h1 = strPool.PutIfAbsent("one"); auto* h2 = strPool.PutIfAbsent("two"); auto* h3 = strPool.PutIfAbsent("two"); UNIT_ASSERT(h1 != h2); UNIT_ASSERT(h2 == h3); strPool.Build(); UNIT_ASSERT_VALUES_EQUAL(h1->Frequency, 1); UNIT_ASSERT_VALUES_EQUAL(h1->Index, 1); UNIT_ASSERT_VALUES_EQUAL(h2->Frequency, 2); UNIT_ASSERT_VALUES_EQUAL(h2->Index, 0); UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 6); UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 2); } Y_UNIT_TEST(ForEach) { TStringPoolBuilder strPool; strPool.SetSorted(true); strPool.PutIfAbsent("one"); strPool.PutIfAbsent("two"); strPool.PutIfAbsent("two"); strPool.PutIfAbsent("three"); strPool.PutIfAbsent("three"); strPool.PutIfAbsent("three"); UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 11); UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 3); strPool.Build(); TVector<TString> strings; TVector<ui32> indexes; TVector<ui32> frequences; strPool.ForEach([&](TStringBuf str, ui32 index, ui32 freq) { strings.emplace_back(str); indexes.push_back(index); frequences.push_back(freq); }); TVector<TString> expectedStrings = {"three", "two", "one"}; UNIT_ASSERT_EQUAL(strings, expectedStrings); TVector<ui32> expectedIndexes = {0, 1, 2}; UNIT_ASSERT_EQUAL(indexes, expectedIndexes); TVector<ui32> expectedFrequences = {3, 2, 1}; UNIT_ASSERT_EQUAL(frequences, expectedFrequences); } }