-
Notifications
You must be signed in to change notification settings - Fork 114
Issue #1: Added unit tests for CharacterRecognizer. #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #1: Added unit tests for CharacterRecognizer. #31
Conversation
…sted classes in CharacterRecognizer to be static to make them easier to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pull request @FridaTveit! I've have a few nitpicks I left in the comments for you. The ones marked optional do not necessarily have to be addressed.
If you don't feel like fixing them at all I will merge in a few days anyway, as the test is great! Thank you for your time and effort :)
import java.util.Vector; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.hamcrest.CoreMatchers.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better if we used raw junit for assertions, if it's not too much of a pain. As far as I can tell, all the assertThat(..) statements in this test can be substituted with assertEquals.
If in the future we find out that we need Hamcrest's matchers, lets use them and add them to our dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
for (int x = 0; x < this.learnVectors.size(); x++) { | ||
float fx = this.simplifiedEuclideanDistance(tested, this.learnVectors.elementAt(x)); | ||
recognized.addPattern(recognized.new RecognizedPattern(ALPHABET[x], fx)); | ||
recognized.addPattern(new RecognizedChar.RecognizedPattern(ALPHABET[x], fx)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: The way the RecognizedPattern
, RecognizedChar
and PatternComparer
classes are nested in CharacterRecognizer
is a pain. It's good that you made them static, but I would suggest going even further and pulling them out into separate files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
|
||
public class CharacterRecognizerTest { | ||
|
||
private CharacterRecognizer.RecognizedChar getRecognizedCharWithThreePatterns() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: This is an ok way to do create a RecognizedChar
for testing purposed, but the way this is usually done in Junit tests is adding a setup method and then using the variable directly in the tests.
public class CharacterRecognizerTest {
private RecognizedChar threePaternRecognizedChar;
@Before
private void setUp() {
// ... contents of current getRecognizedCharWithThreePatterns()
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
recognizedChar.sort(0); | ||
assertTrue(recognizedChar.isSorted()); | ||
Vector<CharacterRecognizer.RecognizedChar.RecognizedPattern> patterns = recognizedChar.getPatterns(); | ||
assertThat(patterns.get(0).getCost(), is(1.0f)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful with this, as floats tend to be "not equal" at the weirdest times... See https://stackoverflow.com/questions/31776127/how-to-test-for-equality-of-float-double-in-junit for more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, changed it now! :)
…edPattern. They are no longer nested inside CharacterRecognizer but have their own files.
…lasses in CharacterRecognizer have been extracted.
…ts as they were really testing RecognizedChar. CharacterRecognizer and RecognizedPattern don't really have anything to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a few more optional comments. Sorry I didn't find them before.
Feel free to ignore them, the PR is great as it is :) I'll merge it anyway if you don't get around to fixing them.
|
||
import java.util.Comparator; | ||
|
||
public class PatternComparer implements Comparator<Object> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: Lets make it an implementation of Comparator<RecognizedPattern>
, so that we can avoid the ugly casts on lines 30-31.
Optional: Rename the class to standard English, i.e. PatternComparator (also the test).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
public class PatternComparer implements Comparator<Object> { | ||
private int direction; | ||
|
||
public PatternComparer(int direction) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: It makes no sense to have a direction int
, if it only ever has two values. boolean
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
} | ||
|
||
@Override | ||
public int compare(Object o1, Object o2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: Also, if you're up for it, the actual implementation of the compare
method could be greatly simplified using a ternary operator + Integer.compareTo
.
Too bad we're still on Java 6 and don't have the static Integer.compare
(JDK7+). On a side note, what do you think about moving to Java 7/Java 8 with the project?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :) I think that sounds like a good idea!
public void testCompareAscendingFirstPatternSmallerReturnsMinusOne() { | ||
RecognizedPattern recognizedPattern1 = new RecognizedPattern('A', 1.0f); | ||
RecognizedPattern recognizedPattern2 = new RecognizedPattern('A', 2.0f); | ||
assertEquals(new PatternComparer(0).compare(recognizedPattern1, recognizedPattern2), -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: A more robust way of testing a comparator wold be to test for equality with 0
, < 0
and > 0
. See the definition of Comparable for more info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
…mparator<RecognizedPattern>. Changed PatternComparator to take a boolean shouldSortDescending instead of int direction.
…mparatorTest to check for < 0 and > 0 instead of = -1 and = 1.
Wonderful, thanks a lot @FridaTveit! |
Changed nested classes in CharacterRecognizer to be static to make them easier to test.