hamcrest array is subset of another

160 views
Skip to first unread message

Kiran Mohan

unread,
Nov 24, 2014, 2:57:56 AM11/24/14
to hamcre...@googlegroups.com

Given that:

int[] a = {1, 2, 3, 4};
int[] b = {1, 2, 3, 4, 5};

How to asser that "a" is a subset of "b" using hamcrest matchers?

The following works

assertThat(Arrays.asList(b), hasItems(a));

But since I am creating "a" from "b", I would prefer to apply the asserts on "a" as the value. Something like

assertThat(a, isSubsetOf(b));

Additionally it is preferable to avoid converting the array to a list.

Nat Pryce

unread,
Nov 27, 2014, 10:30:49 AM11/27/14
to hamcre...@googlegroups.com
An isSubsetOf and isSupersetOf matcher for sets would be a good addition to the library.

As for arrays, don't worry about converting them to lists with Arrays.asList. That backs the list with the array, so there is no data copying and one small object is constructed to implement the List interface.  It may even be allocated on the stack if the JVM can work out that it doesn't escape the function.

--Nat


--
You received this message because you are subscribed to the Google Groups "Hamcrest Java Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hamcrest-jav...@googlegroups.com.
To post to this group, send email to hamcre...@googlegroups.com.
Visit this group at http://groups.google.com/group/hamcrest-java.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages