1   /*
2    * Copyright (c) 2004-2008 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 java.util.Arrays;
28  import java.util.Random;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * Test that our BubbleSort algorithm is correctly implemented.
34   * 
35   * @author Ceki
36   *
37   */
38  public class BubbleSortTest extends TestCase {
39    
40    public void testSmoke() {
41       int[] a = new int[] {5,3,2,7};
42       BubbleSort.sort(a);
43       int i = 0;
44       assertEquals(2, a[i++]);
45       assertEquals(3, a[i++]);
46       assertEquals(5, a[i++]);
47       assertEquals(7, a[i++]);
48    }
49    
50    public void testEmpty() {
51      int[] a = new int[] {};
52      BubbleSort.sort(a);
53    }
54    
55    public void testSorted() {
56      int[] a = new int[] {3,30,300,3000};
57      BubbleSort.sort(a);
58      int i = 0;
59      assertEquals(3, a[i++]);
60      assertEquals(30, a[i++]);
61      assertEquals(300, a[i++]);
62      assertEquals(3000, a[i++]);
63    }
64    
65    public void testInverted() {
66      int[] a = new int[] {3000,300,30,3};
67      BubbleSort.sort(a);
68      int i = 0;
69      assertEquals(3, a[i++]);
70      assertEquals(30, a[i++]);
71      assertEquals(300, a[i++]);
72      assertEquals(3000, a[i++]);
73    }
74  
75    public void testWithSameEntry() {
76      int[] a = new int[] {10,20,10,20};
77      BubbleSort.sort(a);
78      int i = 0;
79      assertEquals(10, a[i++]);
80      assertEquals(10, a[i++]);
81      assertEquals(20, a[i++]);
82      assertEquals(20, a[i++]);
83    }
84  
85    
86    public void testRandom() {
87      int len = 100;
88      Random random = new Random(156);
89      int[] a = new int[len];
90      int[] witness = new int[len];
91      for(int i = 0; i < len; i++) {
92        int r = random.nextInt();
93        a[i] = r;
94        witness[i] = r;
95      }
96     BubbleSort.sort(a);
97     Arrays.sort(witness);
98     assertTrue(Arrays.equals(witness, a));
99    }
100 
101 }