Skip to content
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

refactor compareRight added tests for Compare method #405

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 25 additions & 61 deletions library/src/main/java/com/orm/util/NumberComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,88 +13,52 @@ 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) {
String a = o1.toString();
String b = o2.toString();
public int compare(Object object1, Object object2) {
String a = object1.toString();
String b = object2.toString();

int ia = 0;
int ib = 0;
int nza = 0;
int nzb = 0;
while (true) {
nza = nzb = 0;
int indexA = 0, indexB = 0;

char ca = charAt(a, ia);
char cb = charAt(b, ib);
while (true) {
int nza = 0, nzb = 0;

while ((Character.isSpaceChar(ca)) || (ca == '0')) {
if (ca == '0') {
nza++;
} else {
nza = 0;
}
char charA = charAt(a, indexA);
char charB = charAt(b, indexB);

ca = charAt(a, ++ia);
while ((Character.isSpaceChar(charA)) || (charA == '0')) {
nza = (charA == 0) ? nza+1 : 0;
charA = charAt(a, ++indexA);
}

while ((Character.isSpaceChar(cb)) || (cb == '0')) {
if (cb == '0') {
nzb++;
} else {
nzb = 0;
}

cb = charAt(b, ++ib);
while ((Character.isSpaceChar(charB)) || (charB == '0')) {
nzb = (charB == 0) ? nzb+1 : 0;
charB = charAt(b, ++indexB);
}
int result;
if ((Character.isDigit(ca)) && (Character.isDigit(cb)) &&
((result = compareRight(a.substring(ia), b.substring(ib))) != 0)) {
if ((Character.isDigit(charA)) && (Character.isDigit(charB)) &&
((result = compareRight(a.substring(indexA), b.substring(indexB))) != 0)) {
return result;
}

if ((ca == 0) && (cb == 0)) {
if ((charA == 0) && (charB == 0)) {
return nza - nzb;
}

if (ca < cb) {
if (charA < charB) {
return -1;
}
if (ca > cb) {
if (charA > charB) {
return 1;
}

ia++;
ib++;
indexA++;
indexB++;
}
}

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


}