Skip to content

Commit

Permalink
Issue oskopek#1: Added unit tests for CharacterRecognizer. Changed ne…
Browse files Browse the repository at this point in the history
…sted classes

in CharacterRecognizer to be static to make them easier to test.
  • Loading branch information
FridaTveit committed Dec 19, 2016
1 parent 8f12182 commit 5b15d69
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class CharacterRecognizer {

public abstract RecognizedChar recognize(Char chr);

public class RecognizedChar {
public static class RecognizedChar {
private Vector<RecognizedPattern> patterns;
private boolean isSorted;

Expand Down Expand Up @@ -117,7 +117,7 @@ public BufferedImage render() {
return histogram;
}

public final class RecognizedPattern {
public static final class RecognizedPattern {
private char chr;
private float cost;

Expand All @@ -135,7 +135,7 @@ public float getCost() {
}
}

public class PatternComparer implements Comparator<Object> {
public static class PatternComparer implements Comparator<Object> {

private int direction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public RecognizedChar recognize(Char chr) {
RecognizedChar recognized = new RecognizedChar();
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));
}
recognized.sort(0);
return recognized;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public RecognizedChar recognize(Char imgChar) {
Vector<Double> output = this.network.test(imgChar.extractFeatures());
RecognizedChar recognized = new RecognizedChar();
for (int i = 0; i < output.size(); i++) {
recognized.addPattern(recognized.new RecognizedPattern(ALPHABET[i], output.elementAt(i).floatValue()));
recognized.addPattern(new RecognizedChar.RecognizedPattern(ALPHABET[i], output.elementAt(i).floatValue()));
}
recognized.render();
recognized.sort(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2013 JavaANPR contributors
* Copyright 2006 Ondrej Martinsky
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.osedu.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package net.sf.javaanpr.recognizer;

import org.junit.Test;

import java.util.Vector;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class CharacterRecognizerTest {

private CharacterRecognizer.RecognizedChar getRecognizedCharWithThreePatterns() {
CharacterRecognizer.RecognizedChar recognizedChar = new CharacterRecognizer.RecognizedChar();
recognizedChar.addPattern(new CharacterRecognizer.RecognizedChar.RecognizedPattern('A', 3.0f));
recognizedChar.addPattern(new CharacterRecognizer.RecognizedChar.RecognizedPattern('B', 1.0f));
recognizedChar.addPattern(new CharacterRecognizer.RecognizedChar.RecognizedPattern('C', 4.0f));
return recognizedChar;
}

@Test
public void testPatternsCorrectlySortedAscending() {
CharacterRecognizer.RecognizedChar recognizedChar = getRecognizedCharWithThreePatterns();
assertFalse(recognizedChar.isSorted());
recognizedChar.sort(0);
assertTrue(recognizedChar.isSorted());
Vector<CharacterRecognizer.RecognizedChar.RecognizedPattern> patterns = recognizedChar.getPatterns();
assertThat(patterns.get(0).getCost(), is(1.0f));
assertThat(patterns.get(1).getCost(), is(3.0f));
assertThat(patterns.get(2).getCost(), is(4.0f));
}

@Test
public void testPatternsCorrectlySortedDescending() {
CharacterRecognizer.RecognizedChar recognizedChar = getRecognizedCharWithThreePatterns();
assertFalse(recognizedChar.isSorted());
recognizedChar.sort(1);
assertTrue(recognizedChar.isSorted());
Vector<CharacterRecognizer.RecognizedChar.RecognizedPattern> patterns = recognizedChar.getPatterns();
assertThat(patterns.get(0).getCost(), is(4.0f));
assertThat(patterns.get(1).getCost(), is(3.0f));
assertThat(patterns.get(2).getCost(), is(1.0f));
}

@Test
public void testGetPatternReturnsCorrectPatternWhenPatternsSorted() {
CharacterRecognizer.RecognizedChar recognizedChar = getRecognizedCharWithThreePatterns();
recognizedChar.sort(0);
assertThat(recognizedChar.getPattern(2).getCost(), is(4.0f));
}

@Test
public void testGetPatternReturnsNullWhenPatternsNotSorted() {
CharacterRecognizer.RecognizedChar recognizedChar = getRecognizedCharWithThreePatterns();
assertNull(recognizedChar.getPattern(2));
}
}

0 comments on commit 5b15d69

Please sign in to comment.