File size: 2,160 Bytes
684db41 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <gtest/gtest.h>
#include <chrono>
#include "kernel/rtlil.h"
#include "kernel/yosys.h"
YOSYS_NAMESPACE_BEGIN
namespace {
TEST(TwineHashTest, Fragmentation)
{
TwinePool pool;
IdString flat = pool.add(std::string("$abcdefghij"));
IdString base = pool.add(std::string("$abcde"));
IdString split = pool.add(TwineSpec::Suffix{base, "fghij"});
EXPECT_EQ(flat, split);
EXPECT_EQ(pool[flat].hash(), TwineNode::extend_hash(0, "$abcdefghij"));
}
TEST(TwineHashTest, SplitPoints)
{
const std::string content = "$0123456789abcdefghijklmnopqr";
const uint64_t want = TwineNode::extend_hash(0, content);
TwinePool pool;
IdString first;
for (size_t cut = 1; cut < content.size(); cut++) {
IdString base = pool.add(content.substr(0, cut));
IdString split = pool.add(TwineSpec::Suffix{base, content.substr(cut)});
if (first == IdString::Null)
first = split;
EXPECT_EQ(split, first) << "split after " << cut;
EXPECT_EQ(pool[split].hash(), want) << "split after " << cut;
}
}
TEST(TwineHashTest, DistinctContent)
{
TwinePool twines;
std::set<uint64_t> seen;
for (int i = 0; i < 4096; i++) {
IdString ref = twines.add(stringf("$name%d", i));
seen.insert(twines[ref].hash());
}
EXPECT_GT(seen.size(), 4000u);
}
TEST(TwineHashTest, BenchmarkIntern)
{
constexpr int kNames = 40000;
TwinePool pool;
auto t0 = std::chrono::steady_clock::now();
IdString prefix = pool.add(std::string("$bench"));
std::vector<IdString> refs;
refs.reserve(kNames);
for (int i = 0; i < kNames; i++)
refs.push_back(pool.add(TwineSpec::Suffix{prefix, stringf("$%d", i)}));
auto t1 = std::chrono::steady_clock::now();
for (int i = 0; i < kNames; i++)
ASSERT_EQ(pool.find(pool.str(refs[i])), refs[i]) << "at " << i;
auto t2 = std::chrono::steady_clock::now();
auto ms = [](auto a, auto b) {
return std::chrono::duration_cast<std::chrono::microseconds>(b - a).count() / 1000.0;
};
RecordProperty("intern_ms", std::to_string(ms(t0, t1)));
RecordProperty("find_ms", std::to_string(ms(t1, t2)));
std::cerr << "[ BENCH ] intern " << ms(t0, t1) << " ms, "
<< kNames << " finds " << ms(t1, t2) << " ms\n";
}
}
YOSYS_NAMESPACE_END
|