Coverage for tests/util/unit_formatter_test.py: 97%

35 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-31 20:14 +0000

1# 

2# -------------------------------------------------------------------------------- 

3# SPDX-FileCopyrightText: 2024-2025 Martin Jan Köhler and Harald Pretl 

4# Johannes Kepler University, Institute for Integrated Circuits. 

5# 

6# This file is part of KPEX  

7# (see https://github.com/iic-jku/klayout-pex). 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21# SPDX-License-Identifier: GPL-3.0-or-later 

22# -------------------------------------------------------------------------------- 

23# 

24 

25from __future__ import annotations 

26 

27import allure 

28import unittest 

29 

30from klayout_pex.util.unit_formatter import format_spice_number 

31 

32 

33@allure.parent_suite("Unit Tests") 

34@allure.tag("Enum", "Help", "Util") 

35class UnitFormatterTest(unittest.TestCase): 

36 def test_auto_prefix(self): 

37 self.assertEqual(format_spice_number(1.2e-15), '1.2f') 

38 self.assertEqual(format_spice_number(3.4e-12), '3.4p') 

39 self.assertEqual(format_spice_number(4.7e-9), '4.7n') 

40 self.assertEqual(format_spice_number(1e-6), '1u') 

41 self.assertEqual(format_spice_number(2.2e-6), '2.2u') 

42 self.assertEqual(format_spice_number(0.0), '0') 

43 self.assertEqual(format_spice_number(-3.3e-12), '-3.3p') 

44 self.assertEqual(format_spice_number(5e-18), '5a') 

45 

46 def test_boundary_cases(self): 

47 self.assertEqual(format_spice_number(1e-12), '1p') 

48 self.assertEqual(format_spice_number(1e-15), '1f') 

49 self.assertEqual(format_spice_number(1e-18), '1a') 

50 self.assertEqual(format_spice_number(1e-9), '1n') # 1000p → 1n 

51 self.assertEqual(format_spice_number(1e-12), '1p') # exactly 1p 

52 

53 def test_forced_prefix(self): 

54 self.assertEqual(format_spice_number(1e-9, 'f'), '1000000f') 

55 self.assertEqual(format_spice_number(1e-9, 'p'), '1000p') # pF 

56 self.assertEqual(format_spice_number(1e-9, 'n'), '1n') # nF 

57 self.assertEqual(format_spice_number(2.2e-6, 'n'), '2200n') 

58 self.assertEqual(format_spice_number(4.7e-9, 'u'), '0.0047u') 

59 self.assertEqual(format_spice_number(1.0, 'K'), '0.001K') 

60 self.assertEqual(format_spice_number(5e-18, 'a'), '5a') 

61 

62 def test_invalid_prefix(self): 

63 with self.assertRaises(ValueError): 

64 format_spice_number(1e-6, 'x') # invalid prefix 

65 

66 

67if __name__ == '__main__': 

68 unittest.main()