File size: 9,626 Bytes
f61bac1 | 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | // SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include "boost/json/object.hpp"
#include "boost/json/parse.hpp"
#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();
}
// βββ TileGenerator::snapAt tests βββββββββββββββββββββββββββββββββββββββββββββ
class SnapTest : public tst::Nangate45Fixture
{
protected:
void SetUp() override
{
block_->setDieArea(odb::Rect(0, 0, 100000, 100000));
block_->setCoreArea(odb::Rect(0, 0, 100000, 100000));
}
void makeTileGen()
{
tile_gen_ = std::make_unique<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::unique_ptr<TileGenerator> tile_gen_;
};
TEST_F(SnapTest, NoBlockReturnsNotFound)
{
// Create a generator with a valid db but then clear the block's die area
// to create an empty design.
makeTileGen();
TileVisibility vis;
std::set<std::string> layers{"metal1"};
auto result
= tile_gen_->snapAt(50000, 50000, 1000, 10, true, true, vis, layers);
// With an empty design (no instances, no routing), snap should not find
// anything except possibly the die area edges.
// We just verify it doesn't crash and returns a valid result.
EXPECT_TRUE(result.found || !result.found); // no crash
}
TEST_F(SnapTest, SnapNearInstanceEdge)
{
// Place an instance at (0,0). BUF_X16 has a known size in Nangate45.
placeInst("BUF_X16", "buf1", 0, 0);
makeTileGen();
TileVisibility vis;
std::set<std::string> layers;
// Query near the origin where the instance is placed.
// The die area edge is at x=0 and y=0 which should be findable.
auto result = tile_gen_->snapAt(100, 100, 5000, 50, true, true, vis, layers);
// Should find at least the die area boundary.
EXPECT_TRUE(result.found);
EXPECT_LT(result.distance, 5000);
}
TEST_F(SnapTest, SnapWithHorizontalConstraint)
{
placeInst("BUF_X16", "buf1", 0, 0);
makeTileGen();
TileVisibility vis;
std::set<std::string> layers;
// horizontal=true, vertical=false β only horizontal edges (top/bottom)
auto result
= tile_gen_->snapAt(50000, 100, 5000, 50, true, false, vis, layers);
if (result.found) {
// A horizontal-only snap should return an edge where y1 == y2
EXPECT_EQ(result.edge.first.y(), result.edge.second.y());
}
}
TEST_F(SnapTest, SnapWithVerticalConstraint)
{
placeInst("BUF_X16", "buf1", 0, 0);
makeTileGen();
TileVisibility vis;
std::set<std::string> layers;
// horizontal=false, vertical=true β only vertical edges (left/right)
auto result
= tile_gen_->snapAt(100, 50000, 5000, 50, false, true, vis, layers);
if (result.found) {
// A vertical-only snap should return an edge where x1 == x2
EXPECT_EQ(result.edge.first.x(), result.edge.second.x());
}
}
TEST_F(SnapTest, SnapAtCenterReportsFarDistance)
{
makeTileGen();
TileVisibility vis;
std::set<std::string> layers;
// Die area (0,0)-(100000,100000). At center the nearest edge is 50000 away.
// Snap always finds the die area edge, but the distance should be large.
auto result = tile_gen_->snapAt(50000, 50000, 10, 5, true, true, vis, layers);
EXPECT_TRUE(result.found);
EXPECT_GE(result.distance, 49000);
}
TEST_F(SnapTest, DieAreaEdgeSnap)
{
makeTileGen();
TileVisibility vis;
std::set<std::string> layers;
// Die area is (0,0)β(100000,100000). Bottom edge is at y=0.
// Query just above the bottom edge.
auto result = tile_gen_->snapAt(50000, 50, 1000, 10, true, true, vis, layers);
EXPECT_TRUE(result.found);
// The bottom edge is horizontal at y=0.
EXPECT_EQ(result.edge.first.y(), 0);
EXPECT_EQ(result.edge.second.y(), 0);
}
TEST_F(SnapTest, VisibilityFilterHidesStdcells)
{
placeInst("BUF_X16", "buf1", 0, 0);
makeTileGen();
TileVisibility vis;
vis.stdcells = true;
std::set<std::string> layers;
// With stdcells visible, should find instance edges near origin.
auto result_visible
= tile_gen_->snapAt(100, 100, 5000, 50, true, true, vis, layers);
vis.stdcells = false;
auto result_hidden
= tile_gen_->snapAt(100, 100, 5000, 50, true, true, vis, layers);
// Both may find die area edges, but the instance edges should be different.
// At minimum, both calls should not crash.
EXPECT_TRUE(result_visible.found);
EXPECT_TRUE(result_hidden.found);
}
// βββ handleSnap request handler tests ββββββββββββββββββββββββββββββββββββββββ
class SnapHandlerTest : public tst::Nangate45Fixture
{
protected:
void SetUp() override
{
block_->setDieArea(odb::Rect(0, 0, 100000, 100000));
block_->setCoreArea(odb::Rect(0, 0, 100000, 100000));
placeInst("BUF_X16", "buf1", 0, 0);
gen_ = std::make_shared<TileGenerator>(
getDb(), /*sta=*/nullptr, getLogger());
tcl_eval_ = std::make_shared<TclEvaluator>(/*interp=*/nullptr, getLogger());
handler_ = std::make_unique<SelectHandler>(gen_, tcl_eval_);
}
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_;
std::shared_ptr<TclEvaluator> tcl_eval_;
std::unique_ptr<SelectHandler> handler_;
};
TEST_F(SnapHandlerTest, SnapReturnsJson)
{
WebSocketRequest req;
req.id = 100;
req.type = WebSocketRequest::kSnap;
req.json = parseObj(
R"({"dbu_x":50000,"dbu_y":50,"radius":1000,"point_threshold":10,"horizontal":true,"vertical":true,"visible_layers":[]})");
auto resp = handler_->handleSnap(req);
EXPECT_EQ(resp.id, 100u);
EXPECT_EQ(resp.type, WebSocketResponse::kJson); // JSON
std::string json = payloadStr(resp);
EXPECT_NE(json.find("\"found\""), std::string::npos);
}
TEST_F(SnapHandlerTest, SnapFoundContainsEdge)
{
WebSocketRequest req;
req.id = 101;
req.type = WebSocketRequest::kSnap;
// Query near die area bottom edge at y=0.
req.json = parseObj(
R"({"dbu_x":50000,"dbu_y":50,"radius":1000,"point_threshold":10,"horizontal":true,"vertical":true,"visible_layers":[]})");
auto resp = handler_->handleSnap(req);
EXPECT_EQ(resp.type, WebSocketResponse::kJson);
std::string json = payloadStr(resp);
// Should find the die area bottom edge.
EXPECT_NE(json.find("\"found\":true"), std::string::npos);
EXPECT_NE(json.find("\"edge\""), std::string::npos);
EXPECT_NE(json.find("\"is_point\""), std::string::npos);
}
TEST_F(SnapHandlerTest, SnapAlwaysFindsEdgeDueToChipBoundary)
{
WebSocketRequest req;
req.id = 102;
req.type = WebSocketRequest::kSnap;
// Center of design -- die area edges are always checked so snap always
// finds something (the closest die boundary).
req.json = parseObj(
R"({"dbu_x":50000,"dbu_y":50000,"radius":10,"point_threshold":5,"horizontal":true,"vertical":true,"visible_layers":[]})");
auto resp = handler_->handleSnap(req);
EXPECT_EQ(resp.type, WebSocketResponse::kJson);
std::string json = payloadStr(resp);
EXPECT_NE(json.find("\"found\":true"), std::string::npos);
EXPECT_NE(json.find("\"edge\""), std::string::npos);
}
TEST_F(SnapHandlerTest, SnapDispatchesCorrectly)
{
// Verify that dispatch_request routes SNAP type (even though SNAP goes
// through SelectHandler, not dispatch_request -- just verify SNAP type
// is recognized and doesn't error out through the general path).
WebSocketRequest req;
req.id = 103;
req.type = WebSocketRequest::kSnap;
req.json = parseObj(
R"({"dbu_x":100,"dbu_y":100,"radius":5000,"point_threshold":50,"horizontal":true,"vertical":true,"visible_layers":[]})");
auto resp = handler_->handleSnap(req);
EXPECT_EQ(resp.id, 103u);
EXPECT_NE(resp.type, WebSocketResponse::kError); // not error
}
TEST_F(SnapHandlerTest, TechResponseIncludesDbuPerMicron)
{
// The TECH response should include dbu_per_micron which is needed by the
// ruler for distance calculations.
TileHandler tile_handler(gen_);
SessionState state;
WebSocketRequest req;
req.id = 104;
req.type = WebSocketRequest::kTech;
auto resp = tile_handler.handleTile(req, state);
EXPECT_EQ(resp.type, WebSocketResponse::kJson);
std::string json = payloadStr(resp);
EXPECT_NE(json.find("\"dbu_per_micron\""), std::string::npos);
}
} // namespace
} // namespace web
|