• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How would you test a data structure?

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to come up with a specific data structure to hold subscriptions for a messaging queue. I couldn't find pre made collections that fit the bill for a variety of reasons, so I decided to roll my own. I thought about it for a while and as a base structure it wouldn't be much different than a hierarchy tree with some special rules for inserting and removing. So my basic structure was going to be something like this:


And maybe other supporting methods. Basically, any Topic would look something like "/root/some/levels/then/a/node" with each portion (in this example delineated by "/") would be translated to a TopicLevel and could have a subscription for it (plus a few wildcards so you can subscribe to many topics at once).

My problem is, I have no idea how to write unit tests for it. I could write tests that put things into the tree (subscribe), then pulls them out (getSubscriptions) and assert that I get what I expect. But then I am testing both the put and get functions in a single test, and a failure could mean a failure in either unit, and just as bad, success could mean a failure in one being hidden by a failure in the other creating a false success. I also thought I might be able to separate the putting from the getting by first putting some things into the tree, then iterating over it, building a representation of what it is holding (a List, or Map or something) and comparing with expectations, but that requires knowledge of the inner-workings (i.e. I would have to get at the root, then iterate recursively through all the children map - the root, the inner class, and the map holding the children all what I would consider implementation details) and so would break if the implementation changes (not to mention I would have to be careful not to require some sort of dependency on returned order).

So how would you (or better, how should I) write tests for this? Or should I consider a different design?
 
Marshal
Posts: 5316
324
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be happy enough with a bunch of tests that do a combination of put and query operations to verify the expected behaviour of of interface. So having tests that call both 'subscribe' and 'getSubscriptions' then do some (one) assertion(s) I think is fine. The problem, as you mention, with doing anything more invasive is that you're now coupling your tests to the implementation.

As a general comment I would suggest that even the name TopicTree is too revealing of the internal structure of the class. Consider extracting an interface and naming it something more generic. Perhaps something like:
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, Thanks Tim. I think that is the approach I will take - pairing subscribe()s with getSubscriptions().
 
Legend has it that if you rub the right tiny ad, a genie comes out.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic