Skip to content

Commit

Permalink
On same name, creating a RDB category would return the original one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koefferlein committed Nov 13, 2024
1 parent fecb8a8 commit 0f438cd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/rdb/rdb/rdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,13 @@ Categories::category_by_name (const char *path)
return 0;
}

Category *
Categories::category_by_raw_name (const std::string &name)
{
auto c = m_categories_by_name.find (name);
return c == m_categories_by_name.end () ? 0 : c->second;
}

void
Categories::import_category (Category *category)
{
Expand Down Expand Up @@ -1353,6 +1360,11 @@ Database::create_category (const std::string &name)
Category *
Database::create_category (Categories *container, const std::string &name)
{
Category *existing = container->category_by_raw_name (name);
if (existing) {
return existing;
}

set_modified ();

Category *cat = new Category (name);
Expand Down
2 changes: 1 addition & 1 deletion src/rdb/rdb/rdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ class RDB_PUBLIC Categories
}

void add_category (Category *cath);

void set_database (Database *database);
Category *category_by_raw_name (const std::string &name);
};

/**
Expand Down
20 changes: 20 additions & 0 deletions testdata/python/rdbTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,26 @@ def test_13(self):
self.assertEqual(item1._is_const_object(), False)
self.assertEqual(item1.has_tag(17), True)

def test_14(self):

# same names do not generate a new category
rdb = pya.ReportDatabase("")

_cell = rdb.create_cell("CELL")

_cat = rdb.create_category("cat")
_cat_same = rdb.create_category("cat")
self.assertEqual(_cat.rdb_id(), _cat_same.rdb_id())

_subcat = rdb.create_category(_cat, "subcat")
_subcat_same = rdb.create_category(_cat_same, "subcat")
self.assertEqual(_subcat.rdb_id(), _subcat_same.rdb_id())

# testing whether decrementing the reference count would do harm
_cat = None
_cat_same = None
self.assertEqual(_subcat.rdb_id(), _subcat_same.rdb_id())

# run unit tests
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(RDB_TestClass)
Expand Down
17 changes: 17 additions & 0 deletions testdata/ruby/rdbTest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,23 @@ def test_13

end

def test_14

# same names do not generate a new category
rdb = RBA::ReportDatabase::new("")

_cell = rdb.create_cell("CELL")

_cat = rdb.create_category("cat")
_cat_same = rdb.create_category("cat")
assert_equal(_cat.rdb_id, _cat_same.rdb_id)

_subcat = rdb.create_category(_cat, "subcat")
_subcat_same = rdb.create_category(_cat_same, "subcat")
assert_equal(_subcat.rdb_id, _subcat_same.rdb_id)

end

end

load("test_epilogue.rb")

0 comments on commit 0f438cd

Please sign in to comment.