File size: 14,579 Bytes
784ee2c | 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 | #!/usr/bin/env python3
import unittest
from unittest.mock import MagicMock, patch, call
import sys
import os
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util"))
import def2stream
def make_mock_cell(name, cell_index=0, is_empty=False, parent_cells=1):
"""Create a mock cell object."""
cell = MagicMock()
cell.name = name
cell.cell_index.return_value = cell_index
cell.is_empty.return_value = is_empty
cell.parent_cells.return_value = parent_cells
return cell
def make_mock_pya(cells_before_read=None, cells_after_read=None, top_only_cells=None):
"""Create a mock pya module with configurable cell behavior.
Args:
cells_before_read: Cells in main_layout before reading DEF.
cells_after_read: Cells in main_layout after reading DEF/GDS.
top_only_cells: Cells in top_only_layout after copy_tree.
"""
pya_mod = MagicMock()
# Technology mock
tech = MagicMock()
pya_mod.Technology.return_value = tech
# Main layout mock
main_layout = MagicMock()
pya_mod.Layout.side_effect = [main_layout]
if cells_before_read is None:
cells_before_read = []
if cells_after_read is None:
cells_after_read = []
# each_cell returns different results before and after read
main_layout.each_cell.side_effect = [
iter(cells_before_read), # first call: before reading DEF
iter(cells_after_read), # second call: clearing non-top cells
]
# top_only_layout is the second Layout() call
top_only_layout = MagicMock()
if top_only_cells is None:
top_only_cells = []
# top_only each_cell called twice: missing cell check and orphan check
top_only_layout.each_cell.side_effect = [
iter(top_only_cells), # missing cell check
iter(top_only_cells), # orphan cell check
]
# Override Layout side_effect to return both layouts
pya_mod.Layout.side_effect = [main_layout, top_only_layout]
top_cell = MagicMock()
top_cell.name = "test_design"
top_only_layout.create_cell.return_value = top_cell
top_only_layout.top_cell.return_value = top_cell
top_only_layout.top_cells.return_value = []
return pya_mod, main_layout, top_only_layout, top_cell
class TestMergeGdsBasic(unittest.TestCase):
def test_no_errors_clean_design(self):
"""A clean design with no missing/orphan cells should return 0 errors."""
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, top_only_layout, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[],
)
main_layout.cell.return_value = top
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="/tmp/cells.gds",
seal_file="",
out_file="/tmp/out.gds",
)
self.assertEqual(errors, 0)
top_only_layout.write.assert_called_once_with("/tmp/out.gds")
def test_layer_map_applied(self):
"""When layer_map is non-empty, it should be set on layout options."""
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[],
)
main_layout.cell.return_value = top
tech = pya_mod.Technology.return_value
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="/tmp/layer.map",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
self.assertEqual(
tech.load_layout_options.lefdef_config.map_file, "/tmp/layer.map"
)
def test_empty_layer_map_not_applied(self):
"""When layer_map is empty, map_file should not be set."""
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[],
)
main_layout.cell.return_value = top
tech = pya_mod.Technology.return_value
original_map = tech.load_layout_options.lefdef_config.map_file
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
self.assertEqual(tech.load_layout_options.lefdef_config.map_file, original_map)
class TestCellClearing(unittest.TestCase):
def test_non_top_cells_cleared(self):
"""Non-top cells (not VIA_ or _DEF_FILL) should be cleared."""
top = make_mock_cell("test_design", cell_index=0)
filler = make_mock_cell("FILLER_cell", cell_index=1)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top, filler],
top_only_cells=[],
)
main_layout.cell.return_value = top
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
filler.clear.assert_called_once()
def test_via_cells_preserved(self):
"""Cells starting with VIA_ should NOT be cleared."""
top = make_mock_cell("test_design", cell_index=0)
via = make_mock_cell("VIA_M1M2", cell_index=1)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top, via],
top_only_cells=[],
)
main_layout.cell.return_value = top
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
via.clear.assert_not_called()
def test_def_fill_cells_preserved(self):
"""Cells ending with _DEF_FILL should NOT be cleared."""
top = make_mock_cell("test_design", cell_index=0)
fill = make_mock_cell("some_DEF_FILL", cell_index=2)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top, fill],
top_only_cells=[],
)
main_layout.cell.return_value = top
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
fill.clear.assert_not_called()
class TestMissingCells(unittest.TestCase):
def test_empty_cell_is_error(self):
"""An empty cell without GDS_ALLOW_EMPTY should count as an error."""
missing = make_mock_cell(
"missing_gds", cell_index=1, is_empty=True, parent_cells=1
)
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[missing],
)
main_layout.cell.return_value = top
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
self.assertEqual(errors, 1)
def test_allow_empty_regex_suppresses_error(self):
"""GDS_ALLOW_EMPTY regex should suppress errors for matching cells."""
missing = make_mock_cell(
"pad_io_cell", cell_index=1, is_empty=True, parent_cells=1
)
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[missing],
)
main_layout.cell.return_value = top
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
allow_empty="pad_.*",
)
self.assertEqual(errors, 0)
def test_allow_empty_regex_no_match_still_errors(self):
"""GDS_ALLOW_EMPTY regex should not suppress non-matching cells."""
missing = make_mock_cell(
"other_cell", cell_index=1, is_empty=True, parent_cells=1
)
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[missing],
)
main_layout.cell.return_value = top
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
allow_empty="pad_.*",
)
self.assertEqual(errors, 1)
class TestOrphanCells(unittest.TestCase):
def test_orphan_cell_is_error(self):
"""A cell with no parents (orphan) should count as an error."""
orphan = make_mock_cell(
"orphan_cell", cell_index=1, is_empty=False, parent_cells=0
)
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[orphan],
)
main_layout.cell.return_value = top
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
self.assertEqual(errors, 1)
def test_top_cell_not_orphan(self):
"""The top cell itself should not be counted as an orphan."""
top = make_mock_cell(
"test_design", cell_index=0, is_empty=False, parent_cells=0
)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[top],
)
main_layout.cell.return_value = top
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
self.assertEqual(errors, 0)
class TestSealFile(unittest.TestCase):
def test_seal_file_merged(self):
"""When seal_file is provided, seal cells should be merged."""
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, top_only_layout, top_cell = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[],
)
main_layout.cell.return_value = top
seal_cell = MagicMock()
seal_cell.name = "seal_ring"
seal_cell.cell_index.return_value = 5
# top_cells returns original top + seal after reading seal file
top_only_layout.top_cells.return_value = [top_cell, seal_cell]
errors = def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="/tmp/seal.gds",
out_file="/tmp/out.gds",
)
self.assertEqual(errors, 0)
top_only_layout.read.assert_called_once_with("/tmp/seal.gds")
pya_mod.CellInstArray.assert_called_once_with(5, pya_mod.Trans.return_value)
def test_no_seal_file(self):
"""When seal_file is empty, no seal merging should happen."""
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, top_only_layout, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[],
)
main_layout.cell.return_value = top
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="",
seal_file="",
out_file="/tmp/out.gds",
)
top_only_layout.read.assert_not_called()
class TestGdsFileMerging(unittest.TestCase):
def test_multiple_gds_files_read(self):
"""All space-separated GDS files should be read into main_layout."""
top = make_mock_cell("test_design", cell_index=0)
pya_mod, main_layout, _, _ = make_mock_pya(
cells_before_read=[],
cells_after_read=[top],
top_only_cells=[],
)
main_layout.cell.return_value = top
def2stream.merge_gds(
pya_mod=pya_mod,
tech_file="/tmp/test.lyt",
layer_map="",
in_def="/tmp/test.def",
design_name="test_design",
in_files="/tmp/a.gds /tmp/b.gds /tmp/c.gds",
seal_file="",
out_file="/tmp/out.gds",
)
# read is called for DEF + 3 GDS files = 4 total
read_calls = main_layout.read.call_args_list
self.assertEqual(len(read_calls), 4) # 1 DEF + 3 GDS
if __name__ == "__main__":
unittest.main()
|