Data Structures and Algorithms in Java
GitHubAuthor
  • Preface
    • Syllabus
    • Schedule
  • 0. Getting Started
    • 0.1. Environment Setup
    • 0.2. Quiz
  • 1. Java Essentials
    • 1.1. Abstraction
    • 1.2. Implementation
    • 1.3. Unit Testing
    • 1.4. Quiz
  • 2. Priority Queues
    • 2.1. Simple Priority Queues
    • 2.2. Binary Heap
    • 2.3. Unit Testing
    • 2.4. Benchmarking
    • 2.5. Quiz
  • 3. Sorting Algorithms
    • 3.1. Abstraction
    • 3.2. Comparison-based Sort
    • 3.3. Divide & Conquer Sort
    • 3.4. Distribution-based Sort
    • 3.5. Quiz
    • 3.6. Homework
  • 4. Binary Search Trees
    • 4.1. Binary Search Trees
    • 4.2. Balanced BST
    • 4.2. AVL Trees
    • 4.3. Red-Black Trees
    • 4.4. Quiz
  • 5. Tries
    • 5.1. Concept
    • 5.2. Implementation
    • 5.3. Quiz
    • 5.4. Homework
  • 6. Disjoint Sets
    • 6.1. Concept
    • 6.2. Implementation
    • 6.3. Quiz
  • 7. Graphs
    • 7.1. Implementation
    • 7.2. Cycle Detection
    • 7.3. Topological Sorting
    • 7.4. Quiz
  • 8. Minimum Spanning Trees
    • 8.1. Abstraction
    • 8.2. Prim's Algorithm
    • 8.3. Kruskal’s Algorithm
    • 8.4. Edmonds' Algorithm
    • 8.5. Quiz
    • 8.6. Homework
  • 9. Network Flow
    • 9.1. Flow Network
    • 9.2. Ford-Fulkerson Algorithm
    • 9.3. Simplex Algorithm
    • 9.3. Quiz
  • 10. Dynamic Programming
    • 10.1. Fibonacci Sequence
    • 10.2. Tower of Hanoi
    • 10.3. Longest Common Subsequence
    • 10.4. Quiz
Powered by GitBook

©2023 Emory University - All rights reserved

On this page
  • Test: LongInteger()
  • Test: multiply()
  • Test: compareTo()

Was this helpful?

Export as PDF
  1. 1. Java Essentials

1.3. Unit Testing

LongInteger: unit tests.

Previous1.2. ImplementationNext1.4. Quiz

Last updated 2 years ago

Was this helpful?

Test: LongInteger()

Let us create a testing class called and make a for the constructors:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LongIntegerTest {
    @Test
    public void testConstructors() {
        // default constructor
        assertEquals("0", new LongInteger().toString());

        // constructor with a string parameter
        assertEquals("12", new LongInteger("12").toString());
        assertEquals("34", new LongInteger("+34").toString());
        assertEquals("-56", new LongInteger("-56").toString());
        assertEquals("-0", new LongInteger("-0").toString());

        // copy constructor
        assertEquals("12", new LongInteger(new LongInteger("12")).toString());
        assertEquals("-34", new LongInteger(new LongInteger("-34")).toString());
    }
}
  • L7: methods used for unit testing must be public.

  • L8: tests the default constructor

  • L12-15: tests the constructor with a string parameter.

  • L18-19: tests the copy constructor.

When should we use import static instead of import?

When you run this test, you see a prompt similar to the followings:

Tests passed: 1 of 1 test

Test: multiply()

Let us define another method for testing the multiply() method:

@Test
public void testMultiply() {
    LongInteger a = new LongInteger("123456789");

    a.multiply(new LongInteger("1"));
    assertEquals("123456789", a.toString());

    a.multiply(new LongInteger("-1"));
    assertEquals("-123456789", a.toString());

    a.multiply(new LongInteger("-1234567890123456789"));
    assertEquals("152415787517146788750190521", a.toString());

    a.multiply(new LongInteger("0"));
    assertEquals("0", a.toString());

    a.multiply(new LongInteger("-0"));
    assertEquals("-0", a.toString());
}
  • L11-12: a can hold the integer 152415787517146788750190521 (≈287\approx 2^{87}≈287), which is much larger than the maximum value of long that is 263−12^{63}-1263−1.

Test: compareTo()

Let us define a method for testing the compareTo() method:

@Test
public void testCompareTo() {
    assertTrue(0 < new LongInteger("0").compareTo(new LongInteger("-0")));
    assertTrue(0 > new LongInteger("-0").compareTo(new LongInteger("0")));

    assertTrue(0 < new LongInteger("12").compareTo(new LongInteger("-34")));
    assertTrue(0 > new LongInteger("-12").compareTo(new LongInteger("34")));

    assertTrue(0 > new LongInteger("-34").compareTo(new LongInteger("12")));
    assertTrue(0 < new LongInteger("34").compareTo(new LongInteger("-12")));
}

L6: the annotation indicates that this method is used for unit testing.

: compares two parameters where the left parameter is the expected value and the right parameter is the actual value.

: passes if the parameter returns true.

provides an effective way of ensuring the correctness of your program. Making a unit test for every method is standard practice, although this standard is often dismissed due to time pressure.

LongIntegerTest
unit test
@Test
assertEquals()
assertTrue()
Unit testing