aspose file tools*
The moose likes Testing and the fly likes how to test a shuffling method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "how to test a shuffling method" Watch "how to test a shuffling method" New topic
Author

how to test a shuffling method

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mattias Arthursson
Ranch Hand

Joined: Jul 26, 2004
Posts: 90
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.


Admit nothing. Blame everyone. Be bitter.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30929
    
158

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

Joined: Jul 26, 2004
Posts: 90
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
Personally, I'd do away with setting the flag and test that the order of the collection changes when you shuffle.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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

Joined: May 26, 2003
Posts: 30929
    
158

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

Joined: Jan 17, 2006
Posts: 1296
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to test a shuffling method