1+ # SPDX-FileCopyrightText: 2025 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
2+ #
3+ # SPDX-License-Identifier: Apache-2.0
4+
5+ import os
6+ import unittest
7+
8+ from najaeda import netlist
9+ from najaeda import naja
10+ import logging
11+
12+ najaeda_source_test_path = os .environ .get ('NAJAEDA_SOURCE_TEST_PATH' )
13+
14+ class NajaNetlistCustomPrimitivesTest (unittest .TestCase ):
15+ def setUp (self ):
16+ logging .basicConfig (level = logging .DEBUG )
17+ universe = naja .NLUniverse .create ()
18+ db = naja .NLDB .create (universe )
19+ universe .setTopDB (db )
20+
21+ def tearDown (self ):
22+ if naja .NLUniverse .get ():
23+ naja .NLUniverse .get ().destroy ()
24+
25+ def test_loading (self ):
26+ universe = naja .NLUniverse .get ()
27+ self .assertIsNotNone (universe )
28+ db = universe .getTopDB ()
29+ self .assertIsNotNone (db )
30+ primitives_path = os .path .join (najaeda_source_test_path , 'test_najaeda_custom_primitives' , 'primitives.py' )
31+ netlist .load_primitives_from_file (primitives_path )
32+
33+ primitives = db .getLibrary ("custom_lib" )
34+ self .assertIsNotNone (primitives )
35+ nb_primitives = sum (1 for _ in primitives .getSNLDesigns ())
36+ self .assertEqual (nb_primitives , 2 )
37+ self .assertIsNotNone (primitives .getSNLDesign ("AND2" ))
38+ self .assertIsNotNone (primitives .getSNLDesign ("OR2" ))
39+ self .assertIsNotNone (primitives .getSNLDesign ("AND2" ).getScalarTerm ("I0" ))
40+ self .assertIsNotNone (primitives .getSNLDesign ("AND2" ).getScalarTerm ("I1" ))
41+ self .assertIsNotNone (primitives .getSNLDesign ("AND2" ).getScalarTerm ("O" ))
42+ self .assertIsNotNone (primitives .getSNLDesign ("OR2" ).getScalarTerm ("I0" ))
43+ self .assertIsNotNone (primitives .getSNLDesign ("OR2" ).getScalarTerm ("I1" ))
44+ self .assertIsNotNone (primitives .getSNLDesign ("OR2" ).getScalarTerm ("O" ))
45+
46+ #now instantiate in najaeda
47+ top = netlist .create_top ('Top' )
48+ self .assertIsNotNone (top )
49+ and2_ins0 = top .create_child_instance ('AND2' , 'and2_ins0' )
50+ self .assertIsNotNone (and2_ins0 )
51+ or2_ins0 = top .create_child_instance ('OR2' , 'or2_ins0' )
52+ self .assertIsNotNone (or2_ins0 )
53+ net = top .create_net ('net' )
54+ self .assertIsNotNone (net )
55+ and2_ins0 .get_term ("O" ).connect (net )
56+ or2_ins0 .get_term ("I0" ).connect (net )
57+ nb_connections = sum (1 for _ in net .get_inst_terms ())
58+ self .assertEqual (nb_connections , 2 )
59+
60+ def test_errors (self ):
61+ universe = naja .NLUniverse .get ()
62+ self .assertIsNotNone (universe )
63+ db = universe .getTopDB ()
64+ self .assertIsNotNone (db )
65+ self .assertRaises (Exception , netlist .load_primitives_from_file , "non_existent_file.py" )
66+ primitives_path = os .path .join (najaeda_source_test_path , 'test_najaeda_custom_primitives' , 'error_primitives.py' )
67+ self .assertRaises (Exception , netlist .load_primitives_from_file , primitives_path )
0 commit comments