-1

I'm running a test to see if the values I've inputed into a file are the same values I generate from my API. So I have actual values which are generated from my API and I have another list of expected values. The problem I have is that I am not able to make apple to apple comparisons.

Example:

Actual = {red, bleu, yellow, purple}
expected = {bleu, red, purple, yellow}

failure: red != bleu, bleu != red, yellow != purple, purple != yellow 

I'm not sure how else to better describe what I'm saying other than showing you my code.

Here is my code:

TreeSet<String> hashSet = (TreeSet<String>) calcGraph.getInputs();
boolean success = true;
String error="";

for(String xpath : hashSet) {

    String actual = someApi(response, expression, xpath);

    for ( String values : data.getDataOutputs().keySet() ) {
        String expected = data.getDataOutputs().get(expectedXpath);

        if ( !expected.equals(actual)) {
            error+= "\nExpected : " + expected +"\nActual: " +actual+"\n";
            success = false;
        } if ( !success ) Assert.fail(error);

    }
}

How can I compare these lists within 1 foreach loop or equivalent? Any help or assistance would be appreciated.

Edit:

            Iterator<String> expectation = expectedList.iterator();
            Iterator<String> actuation = actualList.iterator();

            while((expectation.hasNext()) && (actuation.hasNext())) {

                String exp = expectation.next();
                String act = actuation.next();

                logger.info("Expected: "+exp);
                logger.info("Actual: "+act);

                // Validation check
                if ( !exp.equals(act)) {
                    error+= "\nExpected : " + exp +"\nActual: " +act+"\n";
                    success = false;
                } if ( !success ) Assert.fail(error);
            }

Order matters, so this will fail...

mosawi
  • 1,283
  • 5
  • 25
  • 48
  • for-each is not the guy for this job. You need an ordinary for loop. – Mohammed Aouf Zouag Nov 08 '15 at 07:38
  • 1
    I don't see any List here. I see a `hashSet` variable (which implies a `HashSet`) and a `Map` (`data.getExplanationOutputs()`). Which lists are you trying to compare? – Eran Nov 08 '15 at 07:41
  • You want to use assertj; it has assertions for cases like these (`assertThat(list1).containsExactlyElementsOf(list2)`). – fge Nov 08 '15 at 07:44
  • Arraylist and a for-loop with counter might be what you need! – ParkerHalo Nov 08 '15 at 07:44
  • 3
    If you just want to know if two sets have the same elements, just use `set1.equals(set2)`. – Eran Nov 08 '15 at 07:48
  • @Eran however that will not take the iteration order into account... – fge Nov 08 '15 at 07:49
  • 2
    @fge We don't know which Map implementation the OP is using, but if it's not ordered, the iteration order over its keySet is already meaningless. – Eran Nov 08 '15 at 07:50
  • @Eran as I see a `TreeSet` in the code, my guess is that iteration order matters here; I may be wrong, of course – fge Nov 08 '15 at 07:52
  • @Eran I'd like to structure of my comparisons the same, I'd just like to know a mechanism in which I can have both expected and actual. Currently actual is within a foreach loop where it loops through an entire list before iteratering through the next element in the outter foreach loop. – mosawi Nov 08 '15 at 07:53
  • @fge yes the order matters, that's the problem I'm having – mosawi Nov 08 '15 at 07:54
  • Then don't bother and just use assertj. – fge Nov 08 '15 at 07:54
  • where would i put an assertj assertion? I would be having the same problem regardless how I do my assertion where it's a pretty library or plain old java – mosawi Nov 08 '15 at 07:57
  • @Eran that won't work, the problem I have is order...that's why I was thinking we find a way to do assertions within the foreach loop – mosawi Nov 08 '15 at 07:59

2 Answers2

0

The question is just so weird. In the title you said HashSet vs HashSet comparison, and in the content you are using TreeSet.

From the question, it seems that you have a Set of actual results, and you want to compare against a Set of expected result, regardless of the iteration order, am I right?

Using contains is surely wrong, as well as using iterator to do comparison.

The solution is in fact straight-forward. From javadoc of Set:

boolean equals(Object o)

Compares the specified object with this set for equality. Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set). This definition ensures that the equals method works properly across different implementations of the set interface.

What you need to do is simply

expectedResultSet.equals(actaulResultSet)
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
-2

You can use

Set.contains(value)

to check if an actual value is in expected value, you only need one for loop to achieve this

See this

http://docs.oracle.com/javase/7/docs/api/java/util/Set.html#contains(java.lang.Object)

user207421
  • 305,947
  • 44
  • 307
  • 483
Francisco Hernandez
  • 2,378
  • 14
  • 18