Why are empty collections of different type equal?

后端 未结 3 2133
攒了一身酷
攒了一身酷 2021-02-11 12:29

What is mechanism below that makes equal different types?

import static org.testng.Assert.assertEquals;
@Test
public void whyThisIsEq         


        
3条回答
  •  忘掉有多难
    2021-02-11 12:58

    Testng calls through to a method implemented this way.

      public static void assertEquals(Collection actual, Collection expected, String message) {
        if (actual == expected) {
          return;
        }
    
        if (actual == null || expected == null) {
          if (message != null) {
            fail(message);
          } else {
            fail("Collections not equal: expected: " + expected + " and actual: " + actual);
          }
        }
    
        assertEquals(
            actual.size(),
            expected.size(),
            (message == null ? "" : message + ": ") + "lists don't have the same size");
    
        Iterator actIt = actual.iterator();
        Iterator expIt = expected.iterator();
        int i = -1;
        while (actIt.hasNext() && expIt.hasNext()) {
          i++;
          Object e = expIt.next();
          Object a = actIt.next();
          String explanation = "Lists differ at element [" + i + "]: " + e + " != " + a;
          String errorMessage = message == null ? explanation : message + ": " + explanation;
          assertEqualsImpl(a, e, errorMessage);
        }
      }
    

    This is trying to be helpful but is poor for a number of reasons.

    Two equals collections can appear to be different.

    Set a = new HashSet<>();
    a.add(82);
    a.add(100);
    System.err.println(a);
    Set b = new HashSet<>();
    for (int i = 82; i <= 100; i++)
        b.add(i);
    for (int i = 83; i <= 99; i++)
        b.remove(i);
    System.err.println(b);
    System.err.println("a.equals(b) && b.equals(a) is " + (a.equals(b) && b.equals(a)));
    assertEquals(a, b, "a <=> b");
    

    and

    Set a = new HashSet<>();
    a.add(100);
    a.add(82);
    System.err.println(a);
    Set b = new HashSet<>(32);
    b.add(100);
    b.add(82);
    System.err.println(b);
    System.err.println("a.equals(b) && b.equals(a) is " + (a.equals(b) && b.equals(a)));
    assertEquals(a, b, "a <=> b");
    

    prints

    [82, 100]
    [100, 82]
    a.equals(b) && b.equals(a) is true
    Exception in thread "main" java.lang.AssertionError: a <=> b: Lists differ at element [0]: 100 != 82
        at ....
    

    Two collections can be the same or different depending on how they are compared.

    assertEquals(a, (Iterable) b); // passes
    
    assertEquals(a, (Object) b); // passes
    
    assertEquals(Arrays.asList(a), Arrays.asList(b)); // passes
    

提交回复
热议问题