001/** 002 * Copyright (c) 2004-2011 QOS.ch 003 * All rights reserved. 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining 006 * a copy of this software and associated documentation files (the 007 * "Software"), to deal in the Software without restriction, including 008 * without limitation the rights to use, copy, modify, merge, publish, 009 * distribute, sublicense, and/or sell copies of the Software, and to 010 * permit persons to whom the Software is furnished to do so, subject to 011 * the following conditions: 012 * 013 * The above copyright notice and this permission notice shall be 014 * included in all copies or substantial portions of the Software. 015 * 016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 017 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 021 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 023 * 024 */ 025package org.slf4j.helpers; 026 027import static org.junit.Assert.assertEquals; 028import static org.junit.Assert.assertTrue; 029 030import java.util.Arrays; 031import java.util.Random; 032 033import org.junit.Test; 034 035/** 036 * Test that our BubbleSort algorithm is correctly implemented. 037 * 038 * @author Ceki 039 * 040 */ 041public class BubbleSortTest { 042 043 @Test 044 public void testSmoke() { 045 int[] a = new int[] { 5, 3, 2, 7 }; 046 BubbleSort.sort(a); 047 int i = 0; 048 assertEquals(2, a[i++]); 049 assertEquals(3, a[i++]); 050 assertEquals(5, a[i++]); 051 assertEquals(7, a[i++]); 052 } 053 054 @Test 055 public void testEmpty() { 056 int[] a = new int[] {}; 057 BubbleSort.sort(a); 058 } 059 060 @Test 061 public void testSorted() { 062 int[] a = new int[] { 3, 30, 300, 3000 }; 063 BubbleSort.sort(a); 064 int i = 0; 065 assertEquals(3, a[i++]); 066 assertEquals(30, a[i++]); 067 assertEquals(300, a[i++]); 068 assertEquals(3000, a[i++]); 069 } 070 071 @Test 072 public void testInverted() { 073 int[] a = new int[] { 3000, 300, 30, 3 }; 074 BubbleSort.sort(a); 075 int i = 0; 076 assertEquals(3, a[i++]); 077 assertEquals(30, a[i++]); 078 assertEquals(300, a[i++]); 079 assertEquals(3000, a[i++]); 080 } 081 082 @Test 083 public void testWithSameEntry() { 084 int[] a = new int[] { 10, 20, 10, 20 }; 085 BubbleSort.sort(a); 086 int i = 0; 087 assertEquals(10, a[i++]); 088 assertEquals(10, a[i++]); 089 assertEquals(20, a[i++]); 090 assertEquals(20, a[i++]); 091 } 092 093 @Test 094 public void testRandom() { 095 int len = 100; 096 Random random = new Random(156); 097 int[] a = new int[len]; 098 int[] witness = new int[len]; 099 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}