I’ve started working on a little ruby project that will have sample implementations of a number of different data structures and algorithms. Right now it’s just for me to refresh on stuff I haven’t done for a while, but I’m hoping to have it set up kind of like Ruby Koans, with a bunch of unit tests written for the data structures but the implementations empty (with full implementations in another branch). It could then be used as a nice learning tool or code kata.
However, I’m having trouble coming up with a good way to write the tests. I can’t just test the public behavior as that won’t necessarily tell me about the implementation, and that’s kind of important here. For example, the public interfaces of a normal BST and a Red-Black tree would be the same, but the RB Tree has very specific data organization requirements. How would I test that?
Not sure what you are up to, but if you want to provide common interfaces for various data structures which can then be implemented and/or specialized further in various ways, I don’t see how you could write tests for any concrete implementation which does not yet exist.
You can write tests for the common interface to ensure that all implementations fulfill the contract specified by that interface. E.g. all implementations of a sorted tree should order their elements properly etc. As a side note, this wouldn’t actually be a unit test but rather functionality / acceptance test.
For red-black trees, you could write a suite of additional (optional) tests though, to verify that the tree is reordered properly after inserts & deletes. This can and should be tested via the public interface nevertheless. E.g. add a series of elements to the tree in a way that would make the tree unbalanced without reordering, then check the tree structure to ensure that it got reordered properly.