File size: 6,206 Bytes
5cdf637 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | // SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors
#include <memory>
#include <mutex>
#include <string>
#include <string_view>
#include "boost/json/object.hpp"
#include "boost/json/parse.hpp"
#include "clock_tree_report.h"
#include "gtest/gtest.h"
#include "odb/db.h"
#include "request_handler.h"
#include "tile_generator.h"
#include "tst/nangate45_fixture.h"
namespace web {
namespace {
// Helper to extract payload as string.
std::string payloadStr(const WebSocketResponse& resp)
{
return std::string(resp.payload.begin(), resp.payload.end());
}
// Helper to parse a JSON literal into a boost::json::object for tests.
boost::json::object parseObj(std::string_view json)
{
return boost::json::parse(json).as_object();
}
//------------------------------------------------------------------------------
// ClockTreeNode::typeToString tests (no fixture needed)
//------------------------------------------------------------------------------
TEST(ClockTreeNodeTest, TypeToStringRoot)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kRoot), "root");
}
TEST(ClockTreeNodeTest, TypeToStringBuffer)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kBuffer), "buffer");
}
TEST(ClockTreeNodeTest, TypeToStringInverter)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kInverter),
"inverter");
}
TEST(ClockTreeNodeTest, TypeToStringClockGate)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kClockGate),
"clock_gate");
}
TEST(ClockTreeNodeTest, TypeToStringRegister)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kRegister),
"register");
}
TEST(ClockTreeNodeTest, TypeToStringMacro)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kMacro), "macro");
}
TEST(ClockTreeNodeTest, TypeToStringUnknown)
{
EXPECT_STREQ(ClockTreeNode::typeToString(ClockTreeNode::kUnknown), "unknown");
}
//------------------------------------------------------------------------------
// ClockTreeData default values
//------------------------------------------------------------------------------
TEST(ClockTreeDataTest, DefaultValues)
{
ClockTreeData data;
EXPECT_TRUE(data.clock_name.empty());
EXPECT_EQ(data.min_arrival, 0.0f);
EXPECT_EQ(data.max_arrival, 0.0f);
EXPECT_TRUE(data.time_unit.empty());
EXPECT_TRUE(data.nodes.empty());
}
//------------------------------------------------------------------------------
// ClockTreeNode default values
//------------------------------------------------------------------------------
TEST(ClockTreeNodeTest, DefaultValues)
{
ClockTreeNode node;
EXPECT_EQ(node.id, 0);
EXPECT_EQ(node.parent_id, -1);
EXPECT_TRUE(node.name.empty());
EXPECT_TRUE(node.pin_name.empty());
EXPECT_EQ(node.type, ClockTreeNode::kUnknown);
EXPECT_EQ(node.arrival, 0.0f);
EXPECT_EQ(node.delay, 0.0f);
EXPECT_EQ(node.fanout, 0);
EXPECT_EQ(node.level, 0);
EXPECT_EQ(node.dbu_x, 0);
EXPECT_EQ(node.dbu_y, 0);
}
//------------------------------------------------------------------------------
// ClockTreeHandler::handleClockTreeHighlight tests
//------------------------------------------------------------------------------
class ClockTreeHighlightTest : public tst::Nangate45Fixture
{
protected:
void SetUp() override
{
block_->setDieArea(odb::Rect(0, 0, 100000, 100000));
placeInst("BUF_X16", "clkbuf1", 5000, 5000);
gen_ = std::make_shared<TileGenerator>(
getDb(), /*sta=*/nullptr, getLogger());
}
odb::dbInst* placeInst(const char* master_name,
const char* inst_name,
int x,
int y)
{
odb::dbMaster* master = lib_->findMaster(master_name);
EXPECT_NE(master, nullptr);
odb::dbInst* inst = odb::dbInst::create(block_, master, inst_name);
inst->setLocation(x, y);
inst->setPlacementStatus(odb::dbPlacementStatus::PLACED);
return inst;
}
std::shared_ptr<TileGenerator> gen_;
SessionState state_;
};
TEST_F(ClockTreeHighlightTest, HighlightExistingInstance)
{
auto handler = std::make_unique<ClockTreeHandler>(gen_, nullptr, nullptr);
WebSocketRequest req;
req.id = 1;
req.type = WebSocketRequest::kClockTreeHighlight;
req.json = parseObj(R"({"inst_name":"clkbuf1"})");
auto resp = handler->handleClockTreeHighlight(req, state_);
EXPECT_EQ(resp.id, 1u);
EXPECT_EQ(resp.type, WebSocketResponse::kJson);
std::string json = payloadStr(resp);
EXPECT_NE(json.find("\"ok\""), std::string::npos);
// Should have populated highlight_rects with the instance bbox
std::lock_guard<std::mutex> lock(state_.selection_mutex);
EXPECT_EQ(state_.highlight_rects.size(), 1u);
}
TEST_F(ClockTreeHighlightTest, HighlightNonExistentInstance)
{
auto handler = std::make_unique<ClockTreeHandler>(gen_, nullptr, nullptr);
WebSocketRequest req;
req.id = 2;
req.type = WebSocketRequest::kClockTreeHighlight;
req.json = parseObj(R"({"inst_name":"does_not_exist"})");
auto resp = handler->handleClockTreeHighlight(req, state_);
EXPECT_EQ(resp.type, WebSocketResponse::kJson);
// No instance found → no highlight rects
std::lock_guard<std::mutex> lock(state_.selection_mutex);
EXPECT_TRUE(state_.highlight_rects.empty());
}
TEST_F(ClockTreeHighlightTest, EmptyNameClearsState)
{
// Pre-populate some state
{
std::lock_guard<std::mutex> lock(state_.selection_mutex);
state_.highlight_rects.emplace_back(0, 0, 100, 100);
state_.timing_rects.push_back(
{odb::Rect(0, 0, 1, 1), {.r = 255, .g = 0, .b = 0, .a = 255}, ""});
}
auto handler = std::make_unique<ClockTreeHandler>(gen_, nullptr, nullptr);
WebSocketRequest req;
req.id = 3;
req.type = WebSocketRequest::kClockTreeHighlight;
req.json = parseObj(R"({"inst_name":""})");
auto resp = handler->handleClockTreeHighlight(req, state_);
EXPECT_EQ(resp.type, WebSocketResponse::kJson);
// All state should be cleared
std::lock_guard<std::mutex> lock(state_.selection_mutex);
EXPECT_TRUE(state_.highlight_rects.empty());
EXPECT_TRUE(state_.timing_rects.empty());
EXPECT_TRUE(state_.timing_lines.empty());
}
} // namespace
} // namespace web
|