Skip to content

Commit

Permalink
refactor compareRight added tests for Compare method
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyMontoya committed Oct 22, 2015
1 parent 0150451 commit a3addb9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
32 changes: 4 additions & 28 deletions library/src/main/java/com/orm/util/NumberComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,10 @@ private static char charAt(String s, int i) {
}

private int compareRight(String a, String b) {
int bias = 0;
int ia = 0;
int ib = 0;
while (true) {
char ca = charAt(a, ia);
char cb = charAt(b, ib);

if ((!Character.isDigit(ca)) && (!Character.isDigit(cb))) {
return bias;
}
if (!Character.isDigit(ca)) {
return -1;
}
if (!Character.isDigit(cb)) {
return 1;
}
if (ca < cb) {
if (bias == 0) {
bias = -1;
}
} else if (ca > cb) {
if (bias == 0)
bias = 1;
} else if ((ca == 0) && (cb == 0))
return bias;
ia++;
ib++;
}
int result = a.compareTo(b);
if(result < 0) return -1;
else if(result > 0) return 1;
else return 0;
}

public int compare(Object o1, Object o2) {
Expand Down
37 changes: 37 additions & 0 deletions library/src/test/java/com/orm/util/NamingHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.orm.util;

import com.orm.util.NamingHelper;

import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class NamingHelperTest {
@Test
public void testToSQLNameCaseConversion() throws Exception {
assertToSqlNameEquals("TESTLOWERCASE", "testlowercase");
assertToSqlNameEquals("TESTUPPERCASE", "TESTUPPERCASE");
}

@Test
public void testToSQLNameUnderscore() {
assertToSqlNameEquals("TEST_UNDERSCORE", "testUnderscore");
assertToSqlNameEquals("AB_CD", "AbCd");
assertToSqlNameEquals("AB_CD", "ABCd");
assertToSqlNameEquals("AB_CD", "AbCD");
assertToSqlNameEquals("SOME_DETAILS_OBJECT", "SomeDetailsObject");
assertToSqlNameEquals("H_OL_A","hOlA");
assertToSqlNameEquals("A","a");
}

/**
* Helper method that asserts a CamelCaseString is converted to UPPER_CASE_UNDER_SCORE.
*
* @param expected a CamelCaseString
* @param actual the expected UPPER_CASE_UNDER_SCORE string
*/
private static void assertToSqlNameEquals(String expected, String actual) {
assertEquals(expected, NamingHelper.toSQLNameDefault(actual));
}

}
34 changes: 34 additions & 0 deletions library/src/test/java/com/orm/util/NumberComparatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.orm.util;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;

public class NumberComparatorTest {
private NumberComparator numberComparator = new NumberComparator();

@Test
public void testNumberComparatorEquals() throws Exception {
String a = "a";
String b = "a";
int compare = numberComparator.compare(a, b);
assertEquals(0, compare);
}

@Test
public void testNumberComparatorGreater() throws Exception {
String a = "test";
String b = "foo";
int compare = numberComparator.compare(a, b);
assertEquals(1, compare);
}

@Test
public void testNumberComparatorLesser() throws Exception {
String a = "foo";
String b = "test";
int compare = numberComparator.compare(a, b);
assertEquals(-1, compare);
}


}

0 comments on commit a3addb9

Please sign in to comment.