File size: 18,690 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023-2026, The OpenROAD Authors

#include <string>

#include "Test3DBloxCheckerFixture.h"
#include "gtest/gtest.h"
#include "odb/3dblox.h"
#include "odb/db.h"
#include "odb/dbObject.h"
#include "odb/geom.h"

namespace odb {
namespace {

TEST_F(CheckerFixture, test_no_violations)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));  // Stacked on top
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");

  auto* conn1 = dbChipConn::create("c1", top_chip_, {inst2}, ri2, {inst1}, ri1);
  conn1->setThickness(0);

  check();
  EXPECT_TRUE(getMarkers(floating_chips_category).empty());
  EXPECT_TRUE(getMarkers(overlapping_chips_category).empty());
  EXPECT_TRUE(getMarkers(connected_regions_category).empty());
}

TEST_F(CheckerFixture, test_overlapping_chips)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(500, 500, 0));  // Overlaps in 3D
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  auto markers = getMarkers(overlapping_chips_category);
  EXPECT_EQ(markers.size(), 1);

  if (!markers.empty()) {
    auto sources = markers[0]->getSources();
    EXPECT_EQ(sources.size(), 2);
  }
}

TEST_F(CheckerFixture, test_single_floating_chip)
{
  // inst1 and inst2 connected, inst3 floating
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst3 = dbChipInst::create(top_chip_, chip1_, "inst3");
  inst3->setLoc(Point3D(5000, 5000, 0));  // Floating
  inst3->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");
  auto* conn1 = dbChipConn::create("c1", top_chip_, {inst2}, ri2, {inst1}, ri1);
  conn1->setThickness(0);

  check();
  auto markers = getMarkers(floating_chips_category);
  EXPECT_EQ(markers.size(), 1);

  if (!markers.empty()) {
    auto sources = markers[0]->getSources();
    EXPECT_EQ(sources.size(), 1);
  }
}

TEST_F(CheckerFixture, test_multiple_floating_groups)
{
  // Group 1: inst1 -> inst2 (Connected)
  // Group 2: inst3 (Floating)
  // Group 3: inst4 (Floating)

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst3 = dbChipInst::create(top_chip_, chip1_, "inst3");
  inst3->setLoc(Point3D(5000, 5000, 0));
  inst3->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst4 = dbChipInst::create(top_chip_, chip2_, "inst4");
  inst4->setLoc(Point3D(7000, 7000, 0));
  inst4->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");
  auto* conn1 = dbChipConn::create("c1", top_chip_, {inst2}, ri2, {inst1}, ri1);
  conn1->setThickness(0);

  check();
  auto markers = getMarkers(floating_chips_category);
  EXPECT_EQ(markers.size(), 2);
}

TEST_F(CheckerFixture, test_connectivity_gap)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");
  auto* conn = dbChipConn::create("c1", top_chip_, {inst2}, ri2, {inst1}, ri1);
  conn->setThickness(100);  // Expect gap of 100

  // Case 1: Valid connection
  // inst1 top is at 500. inst2 bot should be at 500 + 100 = 600
  inst2->setLoc(Point3D(0, 0, 600));
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  EXPECT_TRUE(getMarkers(floating_chips_category).empty());

  // Case 2: Broken connection (gap too large)
  // Move inst2 to 800
  inst2->setLoc(Point3D(0, 0, 800));
  check();
  EXPECT_EQ(getMarkers(floating_chips_category).size(), 1);
}

TEST_F(CheckerFixture, test_abutment_no_overlap)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip1_, "inst2");
  inst2->setLoc(Point3D(2000, 0, 0));  // Abuts at x=2000 (since width is 2000)
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  EXPECT_TRUE(getMarkers(overlapping_chips_category).empty());
}

TEST_F(CheckerFixture, test_close_proximity_no_overlap)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip1_, "inst2");
  inst2->setLoc(Point3D(2001, 0, 0));  // 1 unit gap
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  EXPECT_TRUE(getMarkers(overlapping_chips_category).empty());
}

TEST_F(CheckerFixture, test_z_separation_no_overlap)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip1_, "inst2");
  inst2->setLoc(Point3D(0, 0, 600));  // gap in Z (thick=500, so gap=100)
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  EXPECT_TRUE(getMarkers(overlapping_chips_category).empty());
}

TEST_F(CheckerFixture, test_overlap_despite_valid_connection)
{
  // Create INTERNAL_EXT regions for masking
  auto r1_int = dbChipRegion::create(
      chip1_, "r1_int", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r1_int->setBox(Rect(0, 0, 2000, 2000));

  auto r2_int = dbChipRegion::create(
      chip2_, "r2_int", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r2_int->setBox(Rect(0, 0, 1500, 1500));

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(100, 100, 0));  // Geometric overlap
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  // Connect them
  auto* ri1 = inst1->findChipRegionInst("r1_int");
  auto* ri2 = inst2->findChipRegionInst("r2_int");
  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  EXPECT_EQ(getMarkers(overlapping_chips_category).size(), 1);
}

TEST_F(CheckerFixture, test_overlap_partially_covered_by_connection)
{
  // Create small INTERNAL_EXT regions that don't cover the full overlap
  auto r1_int = dbChipRegion::create(
      chip1_, "r1_int_small", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r1_int->setBox(Rect(0, 0, 50, 50));

  auto r2_int = dbChipRegion::create(
      chip2_, "r2_int_small", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r2_int->setBox(Rect(0, 0, 50, 50));

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 0));  // Full overlap of chip2 inside chip1
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  // Connect them with small regions
  auto* ri1 = inst1->findChipRegionInst("r1_int_small");
  auto* ri2 = inst2->findChipRegionInst("r2_int_small");
  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  EXPECT_EQ(getMarkers(overlapping_chips_category).size(), 1);
}

TEST_F(CheckerFixture, test_overlap_with_invalid_connection)
{
  // Use FRONT/BACK regions - these should NOT mask overlaps
  // because masking requires INTERNAL regions
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 0));  // Overlap
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");
  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  EXPECT_EQ(getMarkers(overlapping_chips_category).size(), 1);
}

TEST_F(CheckerFixture, test_multiple_chips_complex_overlap)
{
  // inst1: 0..2000
  // inst2: 100..1600 (chip2 w=1500) overlaps inst1
  // inst3: 3000..5000 (chip1 w=2000) no overlap
  // inst4: 3100..5100 (chip1 w=2000) overlaps inst3

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(100, 0, 0));
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst3 = dbChipInst::create(top_chip_, chip1_, "inst3");
  inst3->setLoc(Point3D(3000, 0, 0));
  inst3->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst4 = dbChipInst::create(top_chip_, chip1_, "inst4");
  inst4->setLoc(Point3D(3100, 0, 0));
  inst4->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  auto markers = getMarkers(overlapping_chips_category);
  EXPECT_EQ(markers.size(), 2);
}

TEST_F(CheckerFixture, test_unused_internal_ext)
{
  // internal_ext region on chip1. Not connected.
  auto r1_int = dbChipRegion::create(
      chip1_, "r1_int", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r1_int->setBox(Rect(0, 0, 2000, 2000));

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  check();
  auto markers = getMarkers(unused_internal_ext_category);
  EXPECT_EQ(markers.size(), 1);
  if (!markers.empty()) {
    auto sources = markers[0]->getSources();
    EXPECT_EQ(sources.size(), 1);
    // Source should be the region instance
    if (!sources.empty()) {
      auto* source = *sources.begin();
      EXPECT_EQ(source->getObjectType(), dbChipRegionInstObj);
      auto* region_inst = static_cast<dbChipRegionInst*>(source);
      EXPECT_NE(region_inst, nullptr);
      if (region_inst) {
        EXPECT_EQ(region_inst->getChipRegion()->getName(), "r1_int");
      }
    }
  }
}

TEST_F(CheckerFixture, test_used_internal_ext)
{
  // internal_ext region on chip1
  auto r1_int = dbChipRegion::create(
      chip1_, "r1_int", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r1_int->setBox(Rect(0, 0, 2000, 2000));

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  // Connect them
  auto* ri1 = inst1->findChipRegionInst("r1_int");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");
  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  EXPECT_TRUE(getMarkers(unused_internal_ext_category).empty());
}

TEST_F(CheckerFixture, test_connection_invalid_xy)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(3000, 3000, 500));  // No XY overlap
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");

  auto* conn = dbChipConn::create("c1", top_chip_, {inst2}, ri2, {inst1}, ri1);
  conn->setThickness(0);

  check();
  auto markers = getMarkers(connected_regions_category);
  ASSERT_EQ(markers.size(), 1);
  EXPECT_NE(markers[0]->getComment().find("do not overlap in the XY plane"),
            std::string::npos);
}

TEST_F(CheckerFixture, test_connection_invalid_sides)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  // Both regions are on the FRONT side, but facing each other requires one to
  // be BACK
  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_fr");

  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  auto markers = getMarkers(connected_regions_category);
  ASSERT_EQ(markers.size(), 1);
  EXPECT_NE(markers[0]->getComment().find("Ill-defined stacking direction"),
            std::string::npos);
}

TEST_F(CheckerFixture, test_connection_swapped_top_bottom)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));  // Stacked on top
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");

  // The regions mate physically, but the connection declares the lower die's
  // region as top and the upper die's region as bottom.
  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  auto markers = getMarkers(connected_regions_category);
  ASSERT_EQ(markers.size(), 1);
  EXPECT_NE(markers[0]->getComment().find("Ill-defined stacking direction"),
            std::string::npos);
}

TEST_F(CheckerFixture, test_connection_in_z_mirrored_assembly)
{
  // Assembly with chip2 stacked on chip1 and a properly declared connection:
  // top is the upper die's down-facing region.
  auto* assembly
      = dbChip::create(db_.get(), nullptr, "Assembly", dbChip::ChipType::HIER);

  auto inst1 = dbChipInst::create(assembly, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(assembly, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 500));  // Stacked on top
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");
  auto* conn = dbChipConn::create("c1", assembly, {inst2}, ri2, {inst1}, ri1);
  conn->setThickness(0);

  // Mirror the whole assembly in Z at the top level. In world coordinates
  // r1_fr now faces down from above and r2_bk faces up from below.
  auto asm_inst = dbChipInst::create(top_chip_, assembly, "asm_inst");
  asm_inst->setOrient(dbOrientType3D(dbOrientType::R0, true));
  asm_inst->setLoc(Point3D(0, 0, 0));

  check();
  EXPECT_TRUE(getMarkers(connected_regions_category).empty());

  // The unfolded connection reflects world orientation: the declared roles
  // swap under the mirror.
  auto conns = db_->getUnfoldedChipConns();
  ASSERT_EQ(conns.size(), 1);
  dbUnfoldedChipConn* uf_conn = *conns.begin();
  EXPECT_EQ(
      uf_conn->getTopRegion()->getChipRegionInst()->getChipRegion()->getName(),
      "r1_fr");
  EXPECT_EQ(uf_conn->getBottomRegion()
                ->getChipRegionInst()
                ->getChipRegion()
                ->getName(),
            "r2_bk");
}

TEST_F(CheckerFixture, test_connection_thickness_mismatch)
{
  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 600));  // Distance is 100
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_fr");
  auto* ri2 = inst2->findChipRegionInst("r2_bk");

  auto* conn = dbChipConn::create("c1", top_chip_, {inst2}, ri2, {inst1}, ri1);
  conn->setThickness(50);  // Mismatch (100 != 50)

  check();
  auto markers = getMarkers(connected_regions_category);
  ASSERT_EQ(markers.size(), 1);
  EXPECT_NE(
      markers[0]->getComment().find("does not match connection thickness"),
      std::string::npos);
}

TEST_F(CheckerFixture, test_connection_internal_ext)
{
  // internal_ext regions overlap in Z
  auto r1_int = dbChipRegion::create(
      chip1_, "r1_int", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r1_int->setBox(Rect(0, 0, 2000, 2000));

  auto r2_int = dbChipRegion::create(
      chip2_, "r2_int", dbChipRegion::Side::INTERNAL_EXT, nullptr);
  r2_int->setBox(Rect(0, 0, 1500, 1500));

  auto inst1 = dbChipInst::create(top_chip_, chip1_, "inst1");
  inst1->setLoc(Point3D(0, 0, 0));
  inst1->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto inst2 = dbChipInst::create(top_chip_, chip2_, "inst2");
  inst2->setLoc(Point3D(0, 0, 100));  // Z overlap since chip1 thickness is 500
  inst2->setOrient(dbOrientType3D(dbOrientType::R0, false));

  auto* ri1 = inst1->findChipRegionInst("r1_int");
  auto* ri2 = inst2->findChipRegionInst("r2_int");

  auto* conn = dbChipConn::create("c1", top_chip_, {inst1}, ri1, {inst2}, ri2);
  conn->setThickness(0);

  check();
  EXPECT_TRUE(getMarkers(connected_regions_category).empty());

  // Test failure: move inst2 outside Z-range of inst1
  inst2->setLoc(Point3D(0, 0, 600));
  check();
  auto markers = getMarkers(connected_regions_category);
  ASSERT_EQ(markers.size(), 1);
  EXPECT_NE(markers[0]->getComment().find("do not overlap in Z"),
            std::string::npos);
}

}  // namespace
}  // namespace odb