rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Oct 2013 18:16:15 +0100
changeset 1394 66b5956ee49d
parent 772 d382dacfd73f
child 1701 0e061b0f80eb
permissions -rw-r--r--
Wasn't the goal to actually sort the array?
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.compact.tck;
    19 
    20 import java.util.ArrayList;
    21 import java.util.Arrays;
    22 import java.util.Collection;
    23 import java.util.Collections;
    24 import java.util.Comparator;
    25 import java.util.HashMap;
    26 import java.util.HashSet;
    27 import java.util.List;
    28 import java.util.Map;
    29 import java.util.Map.Entry;
    30 import java.util.Vector;
    31 import org.apidesign.bck2brwsr.vmtest.Compare;
    32 import org.apidesign.bck2brwsr.vmtest.VMTest;
    33 import org.testng.annotations.Factory;
    34 
    35 /**
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public class CollectionsTest {
    40     @Compare public String sortStringsInArray() {
    41         List<String> list = new ArrayList<>();
    42         list.add("one");
    43         list.add("two");
    44         list.add("three");
    45         list.add("four");
    46         list.add("five");
    47         list.add("six");
    48         list.add("seven");
    49         list.add("eight");
    50         list.add("nine");
    51         list.add("ten");
    52         
    53         String[] arr = list.toArray(new String[list.size()]);
    54         Arrays.sort(arr);
    55         
    56         return Arrays.asList(arr).toString();
    57     }
    58     
    59     @Compare public String sortStringsInHashSet() {
    60         Collection<String> list = new HashSet<>();
    61         list.add("one");
    62         list.add("two");
    63         list.add("three");
    64         list.add("four");
    65         list.add("five");
    66         list.add("six");
    67         list.add("seven");
    68         list.add("eight");
    69         list.add("nine");
    70         list.add("ten");
    71         
    72         String[] arr = list.toArray(new String[0]);
    73         Arrays.sort(arr);
    74         
    75         return Arrays.asList(arr).toString();
    76     }
    77 
    78     @SuppressWarnings("unchecked")
    79     @Compare public String sortStringsInHashMapWithComparator() {
    80         class C implements Comparator<Map.Entry<String,Integer>> {
    81             public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
    82                 return o1.getKey().compareTo(o2.getKey());
    83             }
    84         }
    85         
    86         Map<String,Integer> map = new HashMap<>();
    87         map.put("one", 1);
    88         map.put("two", 2);
    89         map.put("three", 3);
    90         map.put("four", 4);
    91         map.put("five", 5);
    92         map.put("six", 6);
    93         map.put("seven", 7);
    94         map.put("eight", 8);
    95         map.put("nine", 9);
    96         map.put("ten", 10);
    97         
    98         List<Entry<String,Integer>> arr = new Vector<>();
    99         arr.addAll(map.entrySet());
   100         Collections.sort(arr, new C());
   101         return arr.toString();
   102     }
   103     
   104     @Factory
   105     public static Object[] create() {
   106         return VMTest.create(CollectionsTest.class);
   107     }
   108     
   109 }