Add NaturalComparator and PathNaturalComparator

This commit is contained in:
seven332 committed 2018-12-12 21:30:42 +08:00
1 parent 700ebdfee3
commit 618ca14eca
4 files changed
+351

No files matched your search

@@ -0,0 +1,68 @@
/*
* Copyright 2018 Hippo Seven
*
* Licensed under the Apache 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.apache.org/licenses/LICENSE-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 com.hippo.util;
import static org.junit.Assert.assertTrue;
import org.junit.Assert;
import org.junit.Test;
public class NaturalComparatorTest {
@Test
public void testOrder() {
assertOrder("1", "2");
assertOrder("2", "11");
assertOrder("2", "00011");
assertOrder("a2", "a11");
assertOrder("a2a", "a11a");
}
@Test
public void testEquals() {
assertEquals("2", "2");
assertEquals("11", "11");
assertEquals("a11", "a11");
assertEquals("a2", "a2");
assertEquals("a2a", "a2a");
}
@Test
public void testEmpty() {
assertOrder("", "1");
assertEquals("", "");
}
@Test
public void testNull() {
assertEquals(null, null);
assertOrder(null, "1");
}
private void assertOrder(String s1, String s2) {
NaturalComparator comparator = new NaturalComparator();
assertTrue(comparator.compare(s1, s2) < 0);
assertTrue(comparator.compare(s2, s1) > 0);
}
private void assertEquals(String s1, String s2) {
NaturalComparator comparator = new NaturalComparator();
Assert.assertEquals(0, comparator.compare(s1, s2));
Assert.assertEquals(0, comparator.compare(s2, s1));
}
}