Skip to content

Add support for Sail unit tests #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Timmmm
Copy link
Collaborator

@Timmmm Timmmm commented Feb 5, 2025

This adds basic support for Sail unit tests. These have some pros and cons over C tests:

Pros:

  • They don't require a RISC-V C compiler.
  • They can test internal code (hence why I've called them unit tests).
  • They can "cheat", e.g. to change privilege mode you can just magically cur_privilege = Supervisor. With C you have to implement a syscall, etc.

Cons:

  • They are tied to internal code which will make refactoring more tedious because it means you are likely to need to update the tests. We can minimise that by using "high level" functions like execute(), read_CSR(), etc.
  • They only run on the Sail model, so we can't e.g. verify them against SPIKE.

Given the sorry state of testing in this repo, and the fact that we don't have a way of writing C tests at all yet, I think this is probably a good medium term approach.

Currently the unit test executable reuses a load of code from the emulator, which is not ideal. When we have wrapped the model in a nice C++ library we can use that instead.

@jordancarlin
Copy link
Collaborator

Feels like these have the potential to be a lower barrier of entry for writing certain kinds of tests,(especially for tricky to reach edge cases of the system), so seems like a good idea to me. Once we get the C/assembly test situation sorted out we can think about what makes sense to run as a C test versus a unit test, but having both options seems like a net positive.

@jordancarlin
Copy link
Collaborator

jordancarlin commented Apr 3, 2025

@Timmmm Now that Sail 0.19 is released with unit test support, this is something that can be finished up if we still want it. Not sure that there are a whole lot of tests that should exist here instead of as first party assembly tests, but seems reasonable to include support for them.

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 39c5491 to 6598087 Compare May 13, 2025 09:52
@Timmmm Timmmm marked this pull request as ready for review May 13, 2025 09:53
@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 6598087 to e73b81d Compare May 13, 2025 10:31
Copy link

github-actions bot commented May 13, 2025

Test Results

402 tests  +2   402 ✅ +2   1m 25s ⏱️ -1s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit d4e8acd. ± Comparison against base commit cc3cf59.

♻️ This comment has been updated with latest results.

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch 3 times, most recently from 8a55801 to a1d9603 Compare May 23, 2025 10:00
Copy link
Collaborator

@jordancarlin jordancarlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a README to the test/unit_tests directory explaining that the tests themselves are in the model/unit_tests directory or directly inline with the model. Otherwise, LGTM. Great to get more testing in!

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from a1d9603 to d4e8acd Compare June 13, 2025 12:08
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jun 13, 2025

Rebased & README added.

@Timmmm Timmmm added will be merged Scheduled to be merged in a few days if nobody objects and removed will be merged Scheduled to be merged in a few days if nobody objects labels Jun 13, 2025
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jun 13, 2025

I'll see if I can sort out the Lean errors.

This adds basic support for Sail unit tests. These have some pros and cons over C tests:

Pros:

* They don't require a RISC-V C compiler.
* They can test internal code (hence why I've called them unit tests).
* They can "cheat", e.g. to change privilege mode you can just magically `cur_privilege = Supervisor`. With C you have to implement a syscall, etc.

Cons:

* They are tied to internal code which will make refactoring more tedious because it means you are likely to need to update the tests. We can minimise that by using "high level" functions like `execute()`, `read_CSR()`, etc.
* They only run on the Sail model, so we can't e.g. verify them against SPIKE.

Currently the unit test executable reuses a load of code from the emulator, which is not ideal. When we have wrapped the model in a nice C++ library we can use that instead.
@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from d4e8acd to 272ea4a Compare June 16, 2025 14:01
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jun 16, 2025

Rebased but it's broken now and awkward to fix due to using the Sail generated header. Will probably have to wait until runtime XLEN is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants