Add unit testing, refactoring, and various fixes along the way

Troy Curtis, Jr requested to merge unit-testing into main

This is probably more changes than ideal, but I needed to step through the code at least to get more familiar with things, and I wanted a more permanent description of the API interaction in the form of unit tests. I picked a simple, tried-and-true unit testing library, libcheck, and went module by module. Some places had fixes uncovered during testing which were obviously appropriate to fix. In other places, the same set of code was repeated multiple times, so I factored it out into common functions. There were a few places which ideally the API should be updated (namely with const where appropriate), but that isn't considered ABI compatible, so I refrained from those changes.

@timsoft I'm not sure whether you are much into the coding itself, definitely take a look if you are and have the time. I'll go through a "self-review" as well in the MR, just to get a different perspective on all the changes.

Merge request reports