-1

The function I want to test returns an int[][] aaiCluster1 with data

aaiCluster1[0] = int[] { 1, 2 }
aaiCluster1[1] = int[] { 0 }

so I test using

CollectionAssert.AreEqual (aaiCluster1[0], new int[] { 1, 2 });
CollectionAssert.AreEqual (aaiCluster1[1], new int[] { 0 });

But the order of the int[] is not important and may change, so I rather want to test

CollectionAssert.Contains (aaiCluster1, new int[] { 1, 2 });

but this fails.
Is this command not able to evaluate the content of the array?
Any ideas for a suitable workaround, that does not require additional helper functions?

edit
I have to clarify:
it's about testing whether a jagged array contains another simple array nested in it.
What else must I explain??

edit #2
2 answers suggesting CollectionAssert.AreEquivalent were given already and have been deleted again, because AreEquivalent is not what I'm looking for... it does not work here.
1 answer suggesting Sequence.whatever was given, and it's even more wrong.


edit #3
I reverted Igor's changes to my question, because I feel they too much alter the meaning. However, I think his words are a good alternative describtion, so here is his text:

So I test using

CollectionAssert.AreEqual(new int[] { 1, 2 }, aaiCluster1[0]);
CollectionAssert.AreEqual(new int[] { 0 }, aaiCluster1[1]);

But the order in aaiCluster1 (actual) is not known so the above tests could fail as soon as the returned order in aaiCluster1 changes.

Question

How can I call something similar to this

CollectionAssert.AreEqual(new int[] { 1, 2 }, aaiCluster1);
// CollectionAssert.AreEqual(ICollection<T> expected, ICollection<ICollection<T>> actual);

where all arrays in aaiCluster1 are evaluated against the expected parameter new int[] { 1, 2 } and the assert passes if there is at least one contained array that is found to be Equal to the expected?

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45

2 Answers2

0
aaiCluster1.ContainsArray(new int[] { 1, 2 });

static class Extensions
{
    public static bool ItemsEqual<TSource>(this TSource[] array1, TSource[] array2)
    {
        if (array1 == null && array2 == null)
            return true;
        if (array1 == null || array2 == null)
            return false;
        return array1.Count() == array2.Count() && !array1.Except(array2).Any();
    }

    public static bool ContainsArray<TSource>(this TSource[][] array1, TSource[] array2)
    {
        return array1.Any(x => x.ItemsEqual(array2));
    }
}
Dongdong
  • 2,208
  • 19
  • 28
  • I didn't downvote, and this looks pretty much like what I need. However, you just could have mentioned that I need a helper function. I was looking for a solution without one, but obviously there isn't any. – Tobias Knauss Jul 24 '18 at 15:41
  • if Microsoft implement everything, your company won't need you any more. Microsoft provided `extensions` to extend base class, you should fall in love with it, but not against it. you could add this extension to same namespace with CollectionAssert, then you will see: the extension look like a kind of official method.. – Dongdong Jul 24 '18 at 15:45
  • @TobiasKnauss, I'd say this is the most complete correct answer so far, the only addition which could be made would be to handle duplicates, but that can be done with a group by – johnny 5 Jul 24 '18 at 15:47
  • @Dongdong: true. I just thought there was a built-in function for such a case. But obviously my situation is too rare to be handled by Microsoft. – Tobias Knauss Jul 24 '18 at 15:48
  • they implement base only, but for real world project, we have to add thousands of extension, that's the real value of us. – Dongdong Jul 24 '18 at 15:49
-1

If you want to make sure the on set fully contains another you can just use linq, Assuming there are no duplicates you can do this:

Assert.IsTrue(new int[] { 1, 2 }.Except(aaiCluster1[0]).Count() == 0);
johnny 5
  • 19,893
  • 50
  • 121
  • 195