测试列表<List<T>> 包含任何顺序的相同项目
Test List<List<T>> contains the same items in any order
我想以任意顺序比较包含相同元素的两个 List<List<String>>
。他们不相等。
// expectedResult
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
另一个是
// result
[
["eat", "tea", "ate"],
["bat"],
["tan", "nat"]
]
我应该使用什么测试方法(来自哪些库)来比较 **elements 中的元素而不进行排序?**
我试过了,没有成功。
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;
assertThat(expectedResult, containsInAnyOrder(result));
你可以尝试使用Apache Commons
public static boolean isEqualCollection(Collection a, Collection b)
Returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities.
That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.
Parameters:
- a - the first collection, must not be null
- b - the second collection, must not be null
Returns: true iff the collections contain the same elements with the same cardinalities.
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class TestSO {
@Test
public void testLists() {
List<List<String>> list = Arrays.asList(Arrays.asList("ate", "eat", "tea"), Arrays.asList("nat", "tan"), Arrays.asList("bat"));
List<List<String>> sameList = Arrays.asList(Arrays.asList("ate", "eat", "tea"), Arrays.asList("bat"), Arrays.asList("nat", "tan"));
List<List<String>> differentList = Arrays.asList(Arrays.asList("ate", "eat"), Arrays.asList("bat"), Arrays.asList("nat", "tan"));
Assert.assertTrue(CollectionUtils.isEqualCollection(list, sameList));
Assert.assertFalse(CollectionUtils.isEqualCollection(list, differentList));
}
}
方法 .containsAll()
适用于集合,不适用于集合的集合。
基本上我最终比较了第一个集合的每个元素,然后对嵌套集合使用 .containsAll()
。
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
@Test
public void groupAnagramsTest() {
List<List<String>> expectedResult = Arrays.asList(Arrays.asList("bat"), Arrays.asList("nat", "tan"), Arrays.asList("ate", "eat", "tea"));
List<List<String>> result = Arrays.asList(Arrays.asList("bat"), Arrays.asList("tan", "nat"), Arrays.asList("tea", "eat", "ate"));
// to test the equality without order,
// assert that all elements are contained in the compared list and vice versa
for (int i = 0; i < result.size(); i++) {
Assert.assertTrue(expectedResult.get(i).containsAll(result.get(i)));
Assert.assertTrue(result.get(i).containsAll(expectedResult.get(i)));
}
}
注意:两者outer List的顺序是一样的。不然不行。
我想以任意顺序比较包含相同元素的两个 List<List<String>>
。他们不相等。
// expectedResult
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
另一个是
// result
[
["eat", "tea", "ate"],
["bat"],
["tan", "nat"]
]
我应该使用什么测试方法(来自哪些库)来比较 **elements 中的元素而不进行排序?**
我试过了,没有成功。
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;
assertThat(expectedResult, containsInAnyOrder(result));
你可以尝试使用Apache Commons
public static boolean isEqualCollection(Collection a, Collection b)
Returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities.
That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.
Parameters:
- a - the first collection, must not be null
- b - the second collection, must not be null
Returns: true iff the collections contain the same elements with the same cardinalities.
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class TestSO {
@Test
public void testLists() {
List<List<String>> list = Arrays.asList(Arrays.asList("ate", "eat", "tea"), Arrays.asList("nat", "tan"), Arrays.asList("bat"));
List<List<String>> sameList = Arrays.asList(Arrays.asList("ate", "eat", "tea"), Arrays.asList("bat"), Arrays.asList("nat", "tan"));
List<List<String>> differentList = Arrays.asList(Arrays.asList("ate", "eat"), Arrays.asList("bat"), Arrays.asList("nat", "tan"));
Assert.assertTrue(CollectionUtils.isEqualCollection(list, sameList));
Assert.assertFalse(CollectionUtils.isEqualCollection(list, differentList));
}
}
方法 .containsAll()
适用于集合,不适用于集合的集合。
基本上我最终比较了第一个集合的每个元素,然后对嵌套集合使用 .containsAll()
。
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
@Test
public void groupAnagramsTest() {
List<List<String>> expectedResult = Arrays.asList(Arrays.asList("bat"), Arrays.asList("nat", "tan"), Arrays.asList("ate", "eat", "tea"));
List<List<String>> result = Arrays.asList(Arrays.asList("bat"), Arrays.asList("tan", "nat"), Arrays.asList("tea", "eat", "ate"));
// to test the equality without order,
// assert that all elements are contained in the compared list and vice versa
for (int i = 0; i < result.size(); i++) {
Assert.assertTrue(expectedResult.get(i).containsAll(result.get(i)));
Assert.assertTrue(result.get(i).containsAll(expectedResult.get(i)));
}
}
注意:两者outer List的顺序是一样的。不然不行。