emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 24 Jan 2013 18:04:44 +0100
branchemul
changeset 574 7cf961390eac
child 599 d0f57d3ea898
permissions -rw-r--r--
ArrayList, HashSet, HashMap and sorting algorithm on arrays seems to work
     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.Comparator;
    24 import java.util.HashMap;
    25 import java.util.HashSet;
    26 import java.util.List;
    27 import java.util.Map;
    28 import org.apidesign.bck2brwsr.vmtest.Compare;
    29 import org.apidesign.bck2brwsr.vmtest.VMTest;
    30 import org.testng.annotations.Factory;
    31 
    32 /**
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 public class CollectionsTest {
    37     @Compare public String sortStringsInArray() {
    38         List<String> list = new ArrayList<>();
    39         list.add("one");
    40         list.add("two");
    41         list.add("three");
    42         list.add("four");
    43         list.add("five");
    44         list.add("six");
    45         list.add("seven");
    46         list.add("eight");
    47         list.add("nine");
    48         list.add("ten");
    49         
    50         String[] arr = list.toArray(new String[list.size()]);
    51         Arrays.sort(arr);
    52         
    53         return Arrays.asList(arr).toString();
    54     }
    55     
    56     @Compare public String sortStringsInHashSet() {
    57         Collection<String> list = new HashSet<>();
    58         list.add("one");
    59         list.add("two");
    60         list.add("three");
    61         list.add("four");
    62         list.add("five");
    63         list.add("six");
    64         list.add("seven");
    65         list.add("eight");
    66         list.add("nine");
    67         list.add("ten");
    68         
    69         String[] arr = list.toArray(new String[0]);
    70         Arrays.sort(arr);
    71         
    72         return Arrays.asList(arr).toString();
    73     }
    74 
    75     @SuppressWarnings("unchecked")
    76     @Compare public String sortStringsInHashMapWithComparator() {
    77         class C implements Comparator<Map.Entry<String,Integer>> {
    78             public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
    79                 return o1.getKey().compareTo(o2.getKey());
    80             }
    81         }
    82         
    83         Map<String,Integer> map = new HashMap<>();
    84         map.put("one", 1);
    85         map.put("two", 2);
    86         map.put("three", 3);
    87         map.put("four", 4);
    88         map.put("five", 5);
    89         map.put("six", 6);
    90         map.put("seven", 7);
    91         map.put("eight", 8);
    92         map.put("nine", 9);
    93         map.put("ten", 10);
    94         
    95         Map.Entry<String,Integer>[] arr = map.entrySet().toArray(new Map.Entry[map.size()]);
    96         Arrays.sort(arr, new C());
    97         
    98         return Arrays.asList(arr).toString();
    99     }
   100     
   101     @Factory
   102     public static Object[] create() {
   103         return VMTest.create(CollectionsTest.class);
   104     }
   105     
   106 }