• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to test a shuffling method

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to ensure that after the shuffleDeck() method returns the deck of cards in a random order. Currently I'm just testing by checking the value of the isShuffled field.
But I can't think of a way to test whether the contract of deck of cards in random order is fufilled
[ August 05, 2006: Message edited by: Garrett Rowe ]
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the Collections.shuffle() method you should rely on that method being properly tested by its implementor; thus you really shouldn't need to test for randomness in your unit test. What you might want to make sure is that Collections.shuffle() is actually called. Then again, that's not all that easy to do, since static methods generally can't be mocked...

I'd probably be happy with testing that the flag is set correctly and then assuming that this is too simple to break.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add a bit more to the test to make sure it doesn't pass if the Collections.shuffle(deck) line is removed.

For example, shuffle the deck check the cards aren't in the original order. While it is possible for the shuffled deck to be in the same order, you can reduce this possibility even further by shuffling multiple times.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I can't think of a way to test whether the contract of deck of cards in random order is fufilled

Just want to clarify that I am talking about making sure the method is called, not making sure Sun met the contract of the shuffle method. In other words, that your method meets its contract assuming that Sun's method does.

If this was more complicated, you could create a wrapper for the Collections method. But here I think that is more trouble than it is worth.
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
For example, shuffle the deck check the cards aren't in the original order. While it is possible for the shuffled deck to be in the same order, you can reduce this possibility even further by shuffling multiple times.

Well, if you check that the cards aren't in the original order you verify that you did something to the collection, which might be considered better than nothing, but you're in no way checking that the correct method is called. IMO the benefit of that is very limited (as is - admittedly - just checking that the flag is set correctly).
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I'd do away with setting the flag and test that the order of the collection changes when you shuffle.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice all. I'm pretty new to unit testing. Here's what my testShuffle() method looks like now.

[ August 07, 2006: Message edited by: Garrett Rowe ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett,
Assuming your equals() method on Deck is implemented to check whether the contents of the deck are equal (as opposed to using object.equals()), this is fine.

There is a slight chance that a shuffle may result in the exact same order, but I think it is too small to be worth worrying about.

The last two lines in your test are redundant though. If you want to use the technique of shuffling twice to reduce the probablity of a shuffle being in the original order, you need to use some logic:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using the second test to test that the order of the deck after the shuffle() method was called was really random, as oppposed to to some predefined swapping of cards.

i.e. starting with two decks in the same order (a.equals(b)) and invoking the shuffle() method on both decks that they will (likely) now have a different ordering (!a.equals(b)).
[ August 09, 2006: Message edited by: Garrett Rowe ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic