Unit tests be uniting (and testing).
Recently i got into rust again. Partially for fun, partially to learn, partially because i always liked rust and ideas behind it, partially because it will soon be of use to me. I've been writing some code in it, learning to fight the borrow checker and the whole data-oriented mindset.
Today i was bored, so i decided to write some simple unit tests for my project, using the built-in features of cargo test and #[cfg(test)] etc.
Turns out i had a MAJOR bug in my program logic which caused my data structures to lose anywhere between 1 entry to 99% of stored data after an insertion operation. It somehow went unnoticed in all prior development, despite the fact that i used the library in some example executables.
The bug couldn't have been found by the compiler, since it was a program logic bug - no memory safety was harmed, all memory operations were perfectly safe. They just happened to drop references to a bunch of data nodes LMAO.
I was very unlikely to find the bug on my own, since - as i've said - i was sure the logic was working. All the programs i wrote seemed to return correct results.
What really struck me is that everyone talks about testing, and i have tried unit testing my projects in the past, be they in C, C++ or whatever, but i never got into the habit of doing it since it usually felt boring and never really provided me with much benefit - a minor bug there, an off by one here, a missing if statement somewhere else.
This, however, was the first time where a bug happened that i was very much unlikely to catch without unit tests, and said bug was actually a major one that would completely nullify usefulness of my project! I have never in the past experienced such direct and significant benefit from writing a few test cases.
It also felt funny in a way, since that's specifically what testing is for. Hell, i work in software testing (sort-of). Despite that, i usually let the message of TDD preachers fall on deaf ears. I guess i will not be doing that anymore lmao.
I felt very glad that i wrote the tests. In fact, fixing that bug also made me rethink the code i was using in that particular place, and realised it was VERY inefficient (i wrote it this way since borrow checker was being mean to me and i was just glad once the code seemed to work lmao). I learned a new standard library function that allowed me to write not only correct, but also more efficient code!
Afterwards i installed llvm-cov cargo addon to check my coverage, and noticed that some of the more complex branches of code were missing from it. This in turn made me write more test cases.
In the end i was so hyped up (by testing, of all things, lmao) that i added CI to my repo and can now enjoy a green checkmark next to my commits and log that says "all tests passing" :)
One reason i'm making this post i guess is that i just feel good after doing some new things (unit testing rust, CI on github, learning new std:: functions) but also because i never expected to save this much potential future headaches by just writing some simple tests. And that is a good message to share - test your damn code people!












