Skip to content

Add BubbleSort unit tests #3274

@aitorfi

Description

@aitorfi

Describe the solution you'd like
Remove the assert statements from BubbleSort class and implement unit tests.

These are the assert statements to be removed and the same test cases can be used with the assertArrayEquals statement.

public static void main(String[] args) {
  Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
  BubbleSort bubbleSort = new BubbleSort();
  bubbleSort.sort(integers);
  
  for (int i = 0; i < integers.length - 1; ++i) {
    assert integers[i] <= integers[i + 1];
  }
  print(integers);
  /* output: [1, 4, 6, 9, 12, 23, 54, 78, 231] */
  
  String[] strings = {"c", "a", "e", "b", "d"};
  bubbleSort.sort(strings);
  for (int i = 0; i < strings.length - 1; i++) {
    assert strings[i].compareTo(strings[i + 1]) <= 0;
  }
  print(bubbleSort.sort(strings));
  /* output: [a, b, c, d, e] */
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions