jdk/test/sample/mergesort/MergeSortTest.java
changeset 45342 1164ed6b9b8e
parent 45341 b7cc7f639ecf
parent 45031 cd369ee4d4bc
child 45343 b0b8f2d6584e
equal deleted inserted replaced
45341:b7cc7f639ecf 45342:1164ed6b9b8e
     1 /*
       
     2  * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 
       
    25 /* @test
       
    26  * @summary Test MergeSort
       
    27  *
       
    28  * @library /src/sample/share/forkjoin/mergesort
       
    29  * @build MergeSortTest MergeDemo MergeSort
       
    30  * @run testng MergeSortTest
       
    31  */
       
    32 
       
    33 import java.util.Arrays;
       
    34 import java.util.Random;
       
    35 
       
    36 import org.testng.annotations.Test;
       
    37 
       
    38 public class MergeSortTest {
       
    39     private Random random;
       
    40     private MergeSort target;
       
    41 
       
    42     public MergeSortTest(Random random, MergeSort target) {
       
    43         this.random = random;
       
    44         this.target = target;
       
    45     }
       
    46 
       
    47     @Test
       
    48     public static void doTest() {
       
    49         MergeSortTest test = new MergeSortTest(new Random(), new MergeSort(Runtime.getRuntime().availableProcessors() * 4));
       
    50         test.run();
       
    51     }
       
    52 
       
    53     private int[] generateArray(int elements) {
       
    54         int[] array = new int[elements];
       
    55         for (int i = 0; i < array.length; ++i) {
       
    56             array[i] = random.nextInt(10);
       
    57         }
       
    58         return array;
       
    59     }
       
    60 
       
    61     private void run() {
       
    62         testSort();
       
    63         testSortSingle();
       
    64         testSortEmpty();
       
    65         testLong();
       
    66     }
       
    67 
       
    68     public void testLong() {
       
    69         for (int i = 0; i < 1000; ++i) {
       
    70             int elements = 1 + i * 100;
       
    71 
       
    72             int[] array = generateArray(elements);
       
    73             int[] copy = Arrays.copyOf(array, array.length);
       
    74             Arrays.sort(copy);
       
    75             target.sort(array);
       
    76             assertEqual(copy, array);
       
    77         }
       
    78    }
       
    79 
       
    80     private void testSortEmpty() {
       
    81         int[] array = { };
       
    82         target.sort(array);
       
    83         assertEqual(new int[] { }, array);
       
    84     }
       
    85 
       
    86     private void testSortSingle() {
       
    87         int[] array = { 1 };
       
    88         target.sort(array);
       
    89         assertEqual(new int[] { 1 }, array);
       
    90     }
       
    91 
       
    92     private void testSort() {
       
    93         int[] array = { 7, 3, 9, 0, -6, 12, 54, 3, -6, 88, 1412};
       
    94         target.sort(array);
       
    95         assertEqual(new int[] { -6, -6, 0, 3, 3, 7, 9, 12, 54, 88, 1412 }, array);
       
    96     }
       
    97 
       
    98     private void assertEqual(int[] expected, int[] array) {
       
    99         if (!Arrays.equals(expected, array)) {
       
   100             throw new RuntimeException("Invalid sorted array!");
       
   101         }
       
   102     }
       
   103 
       
   104 
       
   105 }