View Javadoc

    1   /**
    2    * Copyright (c) 2004-2011 QOS.ch
    3    * All rights reserved.
    4    *
    5    * Permission is hereby granted, free  of charge, to any person obtaining
    6    * a  copy  of this  software  and  associated  documentation files  (the
    7    * "Software"), to  deal in  the Software without  restriction, including
    8    * without limitation  the rights to  use, copy, modify,  merge, publish,
    9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    10   * permit persons to whom the Software  is furnished to do so, subject to
    11   * the following conditions:
    12   *
    13   * The  above  copyright  notice  and  this permission  notice  shall  be
    14   * included in all copies or substantial portions of the Software.
    15   *
    16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23   *
    24   */
    25  package org.slf4j.helpers;
    26
    27  import static org.junit.Assert.assertEquals;
    28  import static org.junit.Assert.assertTrue;
    29
    30  import java.util.Arrays;
    31  import java.util.Random;
    32
    33  import org.junit.Test;
    34
    35  /**
    36   * Test that our BubbleSort algorithm is correctly implemented.
    37   *
    38   * @author Ceki
    39   *
    40   */
    41  public class BubbleSortTest {
    42
    43      @Test
    44      public void testSmoke() {
    45          int[] a = new int[] { 5, 3, 2, 7 };
    46          BubbleSort.sort(a);
    47          int i = 0;
    48          assertEquals(2, a[i++]);
    49          assertEquals(3, a[i++]);
    50          assertEquals(5, a[i++]);
    51          assertEquals(7, a[i++]);
    52      }
    53
    54      @Test
    55      public void testEmpty() {
    56          int[] a = new int[] {};
    57          BubbleSort.sort(a);
    58      }
    59
    60      @Test
    61      public void testSorted() {
    62          int[] a = new int[] { 3, 30, 300, 3000 };
    63          BubbleSort.sort(a);
    64          int i = 0;
    65          assertEquals(3, a[i++]);
    66          assertEquals(30, a[i++]);
    67          assertEquals(300, a[i++]);
    68          assertEquals(3000, a[i++]);
    69      }
    70
    71      @Test
    72      public void testInverted() {
    73          int[] a = new int[] { 3000, 300, 30, 3 };
    74          BubbleSort.sort(a);
    75          int i = 0;
    76          assertEquals(3, a[i++]);
    77          assertEquals(30, a[i++]);
    78          assertEquals(300, a[i++]);
    79          assertEquals(3000, a[i++]);
    80      }
    81
    82      @Test
    83      public void testWithSameEntry() {
    84          int[] a = new int[] { 10, 20, 10, 20 };
    85          BubbleSort.sort(a);
    86          int i = 0;
    87          assertEquals(10, a[i++]);
    88          assertEquals(10, a[i++]);
    89          assertEquals(20, a[i++]);
    90          assertEquals(20, a[i++]);
    91      }
    92
    93      @Test
    94      public void testRandom() {
    95          int len = 100;
    96          Random random = new Random(156);
    97          int[] a = new int[len];
    98          int[] witness = new int[len];
    99          for (int i = 0; i < len; i++) {
    100             int r = random.nextInt();
    101             a[i] = r;
    102             witness[i] = r;
    103         }
    104         BubbleSort.sort(a);
    105         Arrays.sort(witness);
    106         assertTrue(Arrays.equals(witness, a));
    107     }
    108
    109 }