• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Junit testing

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im trying to test a class 'generator' which gives a 9x9 suduko puzzle. firstly a user puts in a number of how many blank squares it want. then it generates

this is the generator class


as it generates a random puzzle each time, how can i test it?



i hope this is enough to help
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jean,
A Sudoku puzzle has certain rules. In particular, no duplicate #s in the same row, column or box. You can use asserts (in loops) to confirm the randomly generated puzzle meets all this criteria. You can also call instance.createRandomPuzzle twice to ensure you get a different puzzle across calls. It is theoretically possible to get the same puzzle twice in a rows so I make multiple calls when I do this to decrease that chance to an acceptable level. (There's enough Sudoku puzzles that you are starting at an acceptable level though.)
 
Jean Stu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Jean,
A Sudoku puzzle has certain rules. In particular, no duplicate #s in the same row, column or box. You can use asserts (in loops) to confirm the randomly generated puzzle meets all this criteria. You can also call instance.createRandomPuzzle twice to ensure you get a different puzzle across calls. It is theoretically possible to get the same puzzle twice in a rows so I make multiple calls when I do this to decrease that chance to an acceptable level. (There's enough Sudoku puzzles that you are starting at an acceptable level though.)



thanks
so what im initailly trying is to test if numbers are unique ie no duplicates in row,


i get the following error, expected: <1,2,3,4,5,6,7,8,9> but was: </* this was the full suduko puzzle*/> how can i get it so it passes
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is looping through all the rows and columns.


You want to do this one row at a time and one column at a time separately. This handles one of them. Note the assert is inside the loop. You might consider using a Set rather than a List. Then you don't need the contains and can just add integers to your set.



Handling one column at a time is similar, but you change the index order.
 
Jean Stu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:



ive added the code as followed:


it now gives the error; expected:<[1,2,3,4,5,6,7,8,9]> but was: <[single number that keeps changing after each test run]> ie <[2]> or <[6]>
it also gives the same error if i do
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this is why one (me) should always test code. The code I had provided created a new list of size one and called assert which would never work. This one should go better.

 
Jean Stu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:And this is why one (me) should always test code. The code I had provided created a new list of size one and called assert which would never work. This one should go better.



when added it produces the error

expected: <[1,2,3,4,5,6,7,8,9]> but was: <[2,1,5,4,6,3,9,7,8]>

all it is, is in a different order
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jean Stu wrote:all it is, is in a different order


See my suggestion about using Set so your test isn't dependent on order. Or you can call Collections.sort() on the list before the assert.
 
Jean Stu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:
See my suggestion about using Set so your test isn't dependent on order. Or you can call Collections.sort() on the list before the assert.



ive used

but get this error
java.lang.Integer cannot be cast to java.lang.Character
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic