Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I assert two collections' elements are identical?

How should I assert that two collections contain the same elements id order does NOT matter?

This means that the number of each element in the two collections are the same. Here are some examples:

Equal:
1,2,3,4 == 1,2,3,4
1,2,3,4 == 4,2,3,1
2,1,2 == 2,2,1
1,2,2 == 2,2,1

Not Equal:
1 != 1,1
1,1,2 != 1,2,2

Is there some canned function that will do what I want? I assume this would be in Microsoft.VisualStudio.QualityTools.UnitTestFramework.Assert or in LINQ. Assert would be preferable, since it would presumably give more information about how they are different.

like image 222
Patrick M Avatar asked Jan 21 '13 19:01

Patrick M


People also ask

How do you assert two lists are equal?

Using Counter() , we usually are able to get frequency of each element in list, checking for it, for both the list, we can check if two lists are identical or not.

How can you tell if two Arraylists have the same element?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.

How do you check if two lists are equal in JUnit?

Using JUnit We can use the logic below to compare the equality of two lists using the assertTrue and assertFalse methods. In this first test, the size of both lists is compared before we check if the elements in both lists are the same. As both of these conditions return true, our test will pass.

How do you check if two elements in a list are the same Java?

The equals() method of List interface compares the specified object with this collection for equality. It returns a Boolean value true if both the lists have same elements and are of the same size.


1 Answers

You can use CollectionAssert.AreEquivalent.

like image 157
devdigital Avatar answered Sep 19 '22 22:09

devdigital