Home > Enterprise >  Using test case JUnit 5 to compare 2 2D arrays
Using test case JUnit 5 to compare 2 2D arrays

Time:10-16

I'm using test cases (JUnit5) to compare the 2D array that is generated via a series of methods with a 2D array that I made which is supposed to be identical.

@Test
void testReadCorpus() {
    UserInterface UI = new UserInterface();
    Translator translator = new Translator();
    
    //For English to French translation
    translator.setCorpus("English-French.txt");
    translator.readCorpus();
    translator.printCorpus();
    String[][] corpusA = translator.getCorpus();
    String[][] corpusB = {
            {"the","le"},
            {"the","la"},
            {"jellyfish","meduse"},
            {"boy","garcon"},
            {"dances","danse"},
            {"with","avec"},
            {"lazy","paresseux"},
            {"s melly","puante"},
            {"dog","chien"},
            {"family","famille"},
            {"house","maison"},
            {"brother","frere"},
            {"sister","soeur"},
            {"food","aliments"},
            {"flower","fleur"},
            {"in","dans"},
    };
    assertEquals(corpusA, corpusB);

The printCorpus method prints out the 2D array (corpusA), each row separated by going to a new line and each column separated by a comma. The output is the following:

the,le
the,la
jellyfish,meduse
boy,garcon
dances,danse
with,avec
lazy,paresseux
smelly,puante
dog,chien
family,famille
house,maison
brother,free
sister,soeur
food,aliments
flower,fleur
in,dans

To my knowledge, these two arrays are identical but I get a failure when using the assertEqual to running the test case. Any solutions or ideas?

More Context: The project I'm working on is a literal translator. It translates a sentence word by word. A corpus is a file that has a word in one language and its translation in another separated by a comma. Pretty much the output that you saw above. I just used a method, readCorpus() to read the text file and store its content in a 2D array.

CodePudding user response:

You should use assertArrayEquals instead of assertEquals to assert for deep arrays equality.

@Test
void testReadCorpus() {
    UserInterface UI = new UserInterface();
    Translator translator = new Translator();
    
    //For English to French translation
    translator.setCorpus("English-French.txt");
    translator.readCorpus();
    translator.printCorpus();
    String[][] corpusA = translator.getCorpus();
    String[][] corpusB = {
            {"the","le"},
            {"the","la"},
            {"jellyfish","meduse"},
            {"boy","garcon"},
            {"dances","danse"},
            {"with","avec"},
            {"lazy","paresseux"},
            {"s melly","puante"},
            {"dog","chien"},
            {"family","famille"},
            {"house","maison"},
            {"brother","frere"},
            {"sister","soeur"},
            {"food","aliments"},
            {"flower","fleur"},
            {"in","dans"},
    };
    assertArrayEquals(corpusA, corpusB);

}

Deep equality semantics are dictated by the java.util.Arrays#deepEquals contract:

Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal.

CodePudding user response:

I was supposed to compare the arrays using this method

assertTrue(Arrays.deepEquals(corpusA, corpusB));

thanks, @tmarwern for the help

  • Related