Created
January 24, 2011 15:43
-
-
Save taichi/793399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
import org.junit.Assert; | |
import org.junit.Test; | |
public class Under66536 { | |
@Test | |
public void under65536() { | |
assertUnder65536("0"); | |
assertUnder65536("065"); | |
assertUnder65536("004"); | |
assertUnder65536("0004"); | |
assertUnder65536("00004"); | |
assertUnder65536("-1"); | |
assertUnder65536("650000"); | |
assertUnder65536("6550123"); | |
assertUnder65536("601237"); | |
assertUnder65536(""); | |
assertUnder65536(null); | |
for (int i = 0; i < 700000; i++) { | |
assertUnder65536(String.valueOf(i)); | |
} | |
} | |
protected void assertUnder65536(String data) { | |
Assert.assertEquals(under65536(data), under65536ByRegex(data)); | |
} | |
protected boolean under65536ByRegex(String s) { | |
String regex = "\\b0*([1-5]?\\d{1,4}|6553[0-5]|6(55[012]|(5[0-4]|[0-4]\\d)\\d)\\d)\\b"; | |
return (s != null) && Pattern.matches(regex, s); | |
} | |
protected boolean under65536(String s) { | |
try { | |
int i = Integer.parseInt(s); | |
return -1 < i && i < 65536; | |
} catch (NumberFormatException e) { | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment