Skip to content

Commit

Permalink
Add Unit Tests for shared_utils.py (riscv#309)
Browse files Browse the repository at this point in the history
* Added test cases for shared_utils

Signed-off-by: Aditya Mohan <[email protected]>

* Added definition for logging an error shared_utils.py

Signed-off-by: Jay Dev Jha <[email protected]>

* Pre-commit fixes for shared_utils.py

Signed-off-by: Jay Dev Jha <[email protected]>

* pyright fixes for test.py

Signed-off-by: Jay Dev Jha <[email protected]>

* Minor changes to shared_utils.py

Signed-off-by: Jay Dev Jha <[email protected]>

* Updated test.py

Signed-off-by: Jay Dev Jha <[email protected]>

---------

Signed-off-by: Aditya Mohan <[email protected]>
Signed-off-by: Jay Dev Jha <[email protected]>
Co-authored-by: Jay Dev Jha <[email protected]>
  • Loading branch information
2 people authored and Myrausman committed Nov 14, 2024
1 parent b7efe19 commit 65944d0
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,33 @@ def setUp(self):
def tearDown(self):
self.arg_lut_patcher.stop()

@patch("shared_utils.fixed_ranges")
@patch("shared_utils.single_fixed")
def test_process_enc_line(self, mock_single_fixed: Mock, mock_fixed_ranges: Mock):
"""Test processing of encoding lines"""
# Setup mock return values
mock_fixed_ranges.findall.return_value = [(6, 2, "0x0D")]
mock_fixed_ranges.sub.return_value = "rd imm20"
mock_single_fixed.findall.return_value = []
mock_single_fixed.sub.return_value = "rd imm20"

# Create a mock for split() that returns the expected list
mock_split = Mock(return_value=["rd", "imm20"])
mock_single_fixed.sub.return_value = Mock(split=mock_split)

name, data = process_enc_line("lui rd imm20 6..2=0x0D", "rv_i")

self.logger = logging.getLogger()
self.logger.disabled = True
# Create a patch for arg_lut
self.arg_lut_patcher = patch.dict(
"shared_utils.arg_lut", {"rd": (11, 7), "imm20": (31, 12)}
)
self.arg_lut_patcher.start()

def tearDown(self):
self.arg_lut_patcher.stop()

@patch("shared_utils.fixed_ranges")
@patch("shared_utils.single_fixed")
def test_process_enc_line(self, mock_single_fixed: Mock, mock_fixed_ranges: Mock):
Expand Down Expand Up @@ -226,7 +253,61 @@ def test_find_extension_file(self, mock_logging: Mock, mock_exists: Mock):
with self.assertRaises(SystemExit):
find_extension_file("rv32i", "/path/to/opcodes")
mock_logging.assert_called_with("Extension rv32i not found.")
self.assertIn("rd", data["variable_fields"])
self.assertIn("imm20", data["variable_fields"])

@patch("os.path.exists")
@patch("shared_utils.logging.error")
def test_find_extension_file(self, mock_logging: Mock, mock_exists: Mock):
"""Test extension file finding"""
# Test successful case - file exists in main directory
mock_exists.side_effect = [True, False]
result = find_extension_file("rv32i", "/path/to/opcodes")
self.assertEqual(result, "/path/to/opcodes/rv32i")

# Test successful case - file exists in unratified directory
mock_exists.side_effect = [False, True]
result = find_extension_file("rv32i", "/path/to/opcodes")
self.assertEqual(result, "/path/to/opcodes/unratified/rv32i")

# Test failure case - file doesn't exist anywhere
mock_exists.side_effect = [False, False]
with self.assertRaises(SystemExit):
find_extension_file("rv32i", "/path/to/opcodes")
mock_logging.assert_called_with("Extension rv32i not found.")

def test_process_standard_instructions(self):
"""Test processing of standard instructions"""
lines = [
"add rd rs1 rs2 31..25=0 14..12=0 6..2=0x0C 1..0=3",
"sub rd rs1 rs2 31..25=0x20 14..12=0 6..2=0x0C 1..0=3",
"$pseudo add_pseudo rd rs1 rs2", # Should be skipped
"$import rv32i::mul", # Should be skipped
]

instr_dict: InstrDict = {}
file_name = "rv32i"

with patch("shared_utils.process_enc_line") as mock_process_enc:
# Setup mock return values
mock_process_enc.side_effect = [
("add", {"extension": ["rv32i"], "encoding": "encoding1"}),
("sub", {"extension": ["rv32i"], "encoding": "encoding2"}),
]

process_standard_instructions(lines, instr_dict, file_name)

# Verify process_enc_line was called twice (skipping pseudo and import)
self.assertEqual(mock_process_enc.call_count, 2)

# Verify the instruction dictionary was updated correctly
self.assertEqual(len(instr_dict), 2)
self.assertIn("add", instr_dict)
self.assertIn("sub", instr_dict)


if __name__ == "__main__":
unittest.main()
def test_process_standard_instructions(self):
"""Test processing of standard instructions"""
lines = [
Expand Down

0 comments on commit 65944d0

Please sign in to comment.