Jump to content
Sign in to follow this  
bjkrug

Unit Tests in Assembly Language

Recommended Posts

Does anyone here write unit tests for your Assembly Language projects?
What is your preferred coding format for doing so?

 

The below is my approach. ARYINS is the routine being tested. AEQ and ANEQ stand for "Assert Equal" and "Assert Not Equal", respectively. They are comparing the contents of R0 and R1.

*
* Insert an element at the middle of
*   an array.
* Grow array and move it.
*   No empty block follows the original.
INS5
* Arrange
       LI   R0,INS5Y
       MOV  R0,@BUFADR
       LI   R0,INS5Z
       MOV  R0,@BUFEND
* Act
       LI   R0,INS5X+2
       LI   R1,3
       BLWP @ARYINS
       MOV  R0,R9
       MOV  R1,R10
* Assert
       LI   R0,INS5X+2
       MOV  R9,R1
       BLWP @ANEQ
       TEXT 'Array should have moved.'
       BYTE 0
       EVEN
*
       MOV  R9,R0
       AI   R0,4+12
       MOV  R10,R1
       BLWP @AEQ
       TEXT 'Element address should be '
       TEXT 'directly after the third '
       TEXT 'element.'
       BYTE 0
       EVEN
.
.
.
       RT

 

  • Like 4

Share this post


Link to post
Share on other sites

I do once in a while make something like a unit test when I have an isolated function that's doing something quite complex and returns a simple result. But I delete the test code again instead of building a suite of tests the can be run again later. It's often difficult to isolate your test unit because global state is used, and mocking your dependencies would be quite difficult. Maybe it would be different if I was writing libraries instead of games? In games I often don't even have the option to print the results of asserts on the screen. Perhaps I don't even have an ASCII font? And maybe my function requires keyboard or joystick input?

 

One thing I have been dreaming of is to be able to tell an emulator to print out asserts or expressions to a log at given points in the code. You would place the asserts in you code, and the assembler would output them to a separate file that could be loaded into the emulator. I think that would be a great tool to complement breakpoint debugging, and it could also be used for unit tests.

  • Like 1

Share this post


Link to post
Share on other sites

Yeah, unit tests only make sense if you're writing libraries that are independently loaded, or using some kind of complex infrastructure that allows it.

 

My CRPG work has made me WISH for regression tests at times, just so changes I make don't break something I forgot about.

Share this post


Link to post
Share on other sites

Mocking data in assembly hasn't been a lot harder for me than mocking data in C#. Mocking routines could eventually get hairy; I've only had to do a little bit of that so far. If a mocked routine is supposed to return different values under different circumstances, then something needs to be done in the Arrange portion of the test to specify that. That can sacrifice legibility.

 

And, yeah, I'm writing tests for memory management and array algorithms. If I were writing a game, my test coverage would be way lower, if not zero. As far as I know, modularizing code enough for unit testing could impact game performance.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...