001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.utils.collection;
015
016import java.util.HashMap;
017import java.util.LinkedList;
018import java.util.Map;
019import java.util.Queue;
020
021import org.junit.jupiter.api.Test;
022
023import static org.junit.jupiter.api.Assertions.assertEquals;
024
025public class MapUtilsTest {
026
027  @Test
028  public void testMap() {
029    Map<String, Double> map = new HashMap<String, Double>(4);
030    map.put("A", 99.5);
031    map.put("B", 67.4);
032    map.put("C", 67.5);
033    map.put("D", 67.3);
034
035    Map<String, Double> mapSorted = MapUtils.sortByValue(map);
036
037    assertEquals(4, mapSorted.size());
038
039    Queue<String> expected = new LinkedList<String>();
040    expected.add("D");
041    expected.add("B");
042    expected.add("C");
043    expected.add("A");
044    for (String k : mapSorted.keySet()) {
045      assertEquals(expected.poll(), k);
046    }
047  }
048
049  @Test
050  public void testMap2() {
051    Map<Integer, String> map = new HashMap<Integer, String>(4);
052    map.put(5, "C");
053    map.put(4, "B");
054    map.put(99, "A");
055    map.put(3, "D");
056
057    Map<Integer, String> mapSorted = MapUtils.sortByValue(map);
058
059    assertEquals(4, mapSorted.size());
060
061    Queue<Integer> expected = new LinkedList<Integer>();
062    expected.add(99);
063    expected.add(4);
064    expected.add(5);
065    expected.add(3);
066    for (Integer k : mapSorted.keySet()) {
067      assertEquals(expected.poll(), k);
068    }
069  }
070}