1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/CollectionsTest.java Thu Jan 24 18:04:44 2013 +0100
1.3 @@ -0,0 +1,106 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.compact.tck;
1.22 +
1.23 +import java.util.ArrayList;
1.24 +import java.util.Arrays;
1.25 +import java.util.Collection;
1.26 +import java.util.Comparator;
1.27 +import java.util.HashMap;
1.28 +import java.util.HashSet;
1.29 +import java.util.List;
1.30 +import java.util.Map;
1.31 +import org.apidesign.bck2brwsr.vmtest.Compare;
1.32 +import org.apidesign.bck2brwsr.vmtest.VMTest;
1.33 +import org.testng.annotations.Factory;
1.34 +
1.35 +/**
1.36 + *
1.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.38 + */
1.39 +public class CollectionsTest {
1.40 + @Compare public String sortStringsInArray() {
1.41 + List<String> list = new ArrayList<>();
1.42 + list.add("one");
1.43 + list.add("two");
1.44 + list.add("three");
1.45 + list.add("four");
1.46 + list.add("five");
1.47 + list.add("six");
1.48 + list.add("seven");
1.49 + list.add("eight");
1.50 + list.add("nine");
1.51 + list.add("ten");
1.52 +
1.53 + String[] arr = list.toArray(new String[list.size()]);
1.54 + Arrays.sort(arr);
1.55 +
1.56 + return Arrays.asList(arr).toString();
1.57 + }
1.58 +
1.59 + @Compare public String sortStringsInHashSet() {
1.60 + Collection<String> list = new HashSet<>();
1.61 + list.add("one");
1.62 + list.add("two");
1.63 + list.add("three");
1.64 + list.add("four");
1.65 + list.add("five");
1.66 + list.add("six");
1.67 + list.add("seven");
1.68 + list.add("eight");
1.69 + list.add("nine");
1.70 + list.add("ten");
1.71 +
1.72 + String[] arr = list.toArray(new String[0]);
1.73 + Arrays.sort(arr);
1.74 +
1.75 + return Arrays.asList(arr).toString();
1.76 + }
1.77 +
1.78 + @SuppressWarnings("unchecked")
1.79 + @Compare public String sortStringsInHashMapWithComparator() {
1.80 + class C implements Comparator<Map.Entry<String,Integer>> {
1.81 + public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
1.82 + return o1.getKey().compareTo(o2.getKey());
1.83 + }
1.84 + }
1.85 +
1.86 + Map<String,Integer> map = new HashMap<>();
1.87 + map.put("one", 1);
1.88 + map.put("two", 2);
1.89 + map.put("three", 3);
1.90 + map.put("four", 4);
1.91 + map.put("five", 5);
1.92 + map.put("six", 6);
1.93 + map.put("seven", 7);
1.94 + map.put("eight", 8);
1.95 + map.put("nine", 9);
1.96 + map.put("ten", 10);
1.97 +
1.98 + Map.Entry<String,Integer>[] arr = map.entrySet().toArray(new Map.Entry[map.size()]);
1.99 + Arrays.sort(arr, new C());
1.100 +
1.101 + return Arrays.asList(arr).toString();
1.102 + }
1.103 +
1.104 + @Factory
1.105 + public static Object[] create() {
1.106 + return VMTest.create(CollectionsTest.class);
1.107 + }
1.108 +
1.109 +}