package be.nikiroo.utils.test;
+import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
addTest(new TestCase("Justifying") {
@Override
public void test() throws Exception {
- for (String data : new String[] { "test",
- "let's test some words", "" }) {
- int total = 0;
- for (String word : data.split((" "))) {
- total += word.replace("-", "").replace(" ", "")
- .length();
- }
- List<String> result = StringUtils.justifyText(data, 5,
- StringUtils.Alignment.LEFT);
-
- System.out.println("["+data+"] -> [");
-
- int totalResult = 0;
- for (String resultLine : result) {
- System.out.println(resultLine);
- for (String word : resultLine.split((" "))) {
- totalResult += word.replace("-", "")
- .replace(" ", "").length();
- }
- }
- System.out.println("]");
+ Map<String, Map<Integer, Entry<Alignment, List<String>>>> source = new HashMap<String, Map<Integer, Entry<Alignment, List<String>>>>();
+ addValue(source, Alignment.LEFT, "testy", -1, "testy");
+ addValue(source, Alignment.RIGHT, "testy", -1, "testy");
+ addValue(source, Alignment.CENTER, "testy", -1, "testy");
+ addValue(source, Alignment.JUSTIFY, "testy", -1, "testy");
+ addValue(source, Alignment.LEFT, "testy", 5, "testy");
+ addValue(source, Alignment.LEFT, "testy", 3, "te-", "sty");
+ addValue(source, Alignment.LEFT,
+ "Un petit texte qui se mettra sur plusieurs lignes",
+ 10, "Un petit", "texte qui", "se mettra", "sur",
+ "plusieurs", "lignes");
+ addValue(source, Alignment.LEFT,
+ "Un petit texte qui se mettra sur plusieurs lignes", 7,
+ "Un", "petit", "texte", "qui se", "mettra", "sur",
+ "plusie-", "urs", "lignes");
+ addValue(source, Alignment.RIGHT,
+ "Un petit texte qui se mettra sur plusieurs lignes", 7,
+ " Un", " petit", " texte", " qui se", " mettra",
+ " sur", "plusie-", " urs", " lignes");
+ addValue(source, Alignment.CENTER,
+ "Un petit texte qui se mettra sur plusieurs lignes", 7,
+ " Un ", " petit ", " texte ", "qui se ", "mettra ",
+ " sur ", "plusie-", " urs ", "lignes ");
+ addValue(source, Alignment.JUSTIFY,
+ "Un petit texte qui se mettra sur plusieurs lignes", 7,
+ "Un pet-", "it tex-", "te qui", "se met-", "tra sur",
+ "plusie-", "urs li-", "gnes");
+ addValue(source, Alignment.JUSTIFY,
+ "Un petit texte qui se mettra sur plusieurs lignes",
+ 14, "Un petit", "texte qui se",
+ "mettra sur", "plusieurs lig-", "nes");
+ addValue(source, Alignment.JUSTIFY, "le dash-test", 9,
+ "le dash-", "test");
+
+ for (String data : source.keySet()) {
+ for (int size : source.get(data).keySet()) {
+ Alignment align = source.get(data).get(size).getKey();
+ List<String> values = source.get(data).get(size)
+ .getValue();
+
+ List<String> result = StringUtils.justifyText(data,
+ size, align);
- assertEquals(
- "The number of letters ('-' not included) should be identical before and after",
- total, totalResult);
+ // System.out.println("[" + data + " (" + size + ")" +
+ // "] -> [");
+ // for (int i = 0; i < result.size(); i++) {
+ // String resultLine = result.get(i);
+ // System.out.println(i + ": " + resultLine);
+ // }
+ // System.out.println("]");
+
+ assertEquals(values, result);
+ }
}
}
});
@Override
public void test() throws Exception {
String orig = "test";
- String zipped = StringUtils.zip64(orig);
- String unzipped = StringUtils.unzip64(zipped);
+ String zipped = StringUtils.base64(orig, true);
+ String unzipped = StringUtils.unbase64s(zipped, true);
assertEquals(orig, unzipped);
}
});
+
+ addTest(new TestCase("format/toNumber simple") {
+ @Override
+ public void test() throws Exception {
+ assertEquals(263l, StringUtils.toNumber("263"));
+ assertEquals(21200l, StringUtils.toNumber("21200"));
+ assertEquals(0l, StringUtils.toNumber("0"));
+ assertEquals("263", StringUtils.formatNumber(263l));
+ assertEquals("21k", StringUtils.formatNumber(21000l));
+ assertEquals("0", StringUtils.formatNumber(0l));
+ }
+ });
+
+ addTest(new TestCase("format/toNumber not 000") {
+ @Override
+ public void test() throws Exception {
+ assertEquals(263200l, StringUtils.toNumber("263.2k"));
+ assertEquals(42000l, StringUtils.toNumber("42.0k"));
+ assertEquals(12000000l, StringUtils.toNumber("12M"));
+ assertEquals(2000000000l, StringUtils.toNumber("2G"));
+ assertEquals("263k", StringUtils.formatNumber(263012l));
+ assertEquals("42k", StringUtils.formatNumber(42012l));
+ assertEquals("12M", StringUtils.formatNumber(12012121l));
+ assertEquals("7G", StringUtils.formatNumber(7364635928l));
+ }
+ });
+
+ addTest(new TestCase("format/toNumber decimals") {
+ @Override
+ public void test() throws Exception {
+ assertEquals(263200l, StringUtils.toNumber("263.2k"));
+ assertEquals(1200l, StringUtils.toNumber("1.2k"));
+ assertEquals(42700000l, StringUtils.toNumber("42.7M"));
+ assertEquals(1220l, StringUtils.toNumber("1.22k"));
+ assertEquals(1432l, StringUtils.toNumber("1.432k"));
+ assertEquals(6938l, StringUtils.toNumber("6.938k"));
+ assertEquals("1.3k", StringUtils.formatNumber(1300l, 1));
+ assertEquals("263.2020k", StringUtils.formatNumber(263202l, 4));
+ assertEquals("1.26k", StringUtils.formatNumber(1267l, 2));
+ assertEquals("42.7M", StringUtils.formatNumber(42712121l, 1));
+ assertEquals("5.09G", StringUtils.formatNumber(5094837485l, 2));
+ }
+ });
+ }
+
+ static private void addValue(
+ Map<String, Map<Integer, Entry<Alignment, List<String>>>> source,
+ final Alignment align, String input, int size,
+ final String... result) {
+ if (!source.containsKey(input)) {
+ source.put(input,
+ new HashMap<Integer, Entry<Alignment, List<String>>>());
+ }
+
+ source.get(input).put(size, new Entry<Alignment, List<String>>() {
+ @Override
+ public Alignment getKey() {
+ return align;
+ }
+
+ @Override
+ public List<String> getValue() {
+ return Arrays.asList(result);
+ }
+
+ @Override
+ public List<String> setValue(List<String> value) {
+ return null;
+ }
+ });
}
}