Skip to content

Conversation

FridaTveit
Copy link
Contributor

Changed nested classes in CharacterRecognizer to be static to make them easier to test.

…sted classes

in CharacterRecognizer to be static to make them easier to test.
@coveralls
Copy link

coveralls commented Dec 19, 2016

Coverage Status

Coverage increased (+1.2%) to 13.138% when pulling 5b15d69 on FridaTveit:AddUnitTestsForCharacterRecognizer into 8f12182 on oskopek:master.

Copy link
Owner

@oskopek oskopek left a 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.*;
Copy link
Owner

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.

Copy link
Contributor Author

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));
Copy link
Owner

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.

Copy link
Contributor Author

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() {
Copy link
Owner

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()
    }

Copy link
Contributor Author

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));
Copy link
Owner

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.

Copy link
Contributor Author

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.
@coveralls
Copy link

coveralls commented Dec 20, 2016

Coverage Status

Coverage increased (+1.5%) to 13.469% when pulling 93ce0ae on FridaTveit:AddUnitTestsForCharacterRecognizer into 8f12182 on oskopek:master.

Copy link
Owner

@oskopek oskopek left a 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> {
Copy link
Owner

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).

Copy link
Contributor Author

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) {
Copy link
Owner

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?

Copy link
Contributor Author

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) {
Copy link
Owner

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?

Copy link
Contributor Author

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);
Copy link
Owner

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.

Copy link
Contributor Author

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.
@coveralls
Copy link

coveralls commented Dec 21, 2016

Coverage Status

Coverage increased (+1.5%) to 13.462% when pulling dc3804a on FridaTveit:AddUnitTestsForCharacterRecognizer into 8f12182 on oskopek:master.

@oskopek oskopek merged commit b80b810 into oskopek:master Dec 21, 2016
@oskopek
Copy link
Owner

oskopek commented Dec 21, 2016

Wonderful, thanks a lot @FridaTveit!

@FridaTveit FridaTveit deleted the AddUnitTestsForCharacterRecognizer branch December 21, 2016 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants