• 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

TDD kata (Finding anagrams pairs)

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to do practice TDD using small kata. Kata is to find the anagram pairs in the given file input. I tried to do
the same and write some test cases. Any idea?

Anagrams: An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new
word or phrase, using all the original letters exactly once.

Input is file in which each line has a single word. Each word is going to compare another
words present in the file to find whether word is anagram or not.

A word is anagram when its having following scenarios so it makes my test cases as well:

1) should be of same length
2) should contains only alphabets
3) should contains characters exactly same and be used only once
4) should not match null or spaces
5) Should print all the anagram pairs present in the input
6) Should remove already matched words to avoid check again and improve performance
 
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
I'm not sure what you are asking here.How to start? You'd just pick the easiest case and write a test for it. For example, if you have an empty file, what does your method return?
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written some test cases and code, wanted to know opinion for them.

Test code:





Production code:




Update: I have updated code as i saw some problem in it. Sorry if some one read it in between.
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Above test cases are for below story points

1) should be of same length
2) should contains only alphabets
3) should contains characters exactly same
4) should not match null


Also, there is issue in "isCharactersMatchesBetweenInput" method in production code. So updated one is as below

 
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 great. One minor improvement:


Can be rewritten as:



The later doesn't have the negation and has less to parse.

 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne, i made correction. It looks better now.

One more question, i have doubt on my another story point:

should used characters only once with respect to original one. i.e. The frequency of the character should match.

I have written some test cases and production code for it. Its working fine but can it be better and optimized also i thought i have written more code then
tests. Please suggest:

Test code:


Production code:



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic