$darkmode
DENOPTIM
GenUtilsTest.java
Go to the documentation of this file.
1package denoptim.utils;
2
3/*
4 * DENOPTIM
5 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
6 * and Marco Foscato <marco.foscato@uib.no>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22import static org.junit.jupiter.api.Assertions.assertEquals;
23
24import java.util.ArrayList;
25import java.util.Arrays;
26import java.util.HashSet;
27import java.util.List;
28import java.util.Set;
29
30import org.junit.jupiter.api.Test;
31
38public class GenUtilsTest
39{
40//------------------------------------------------------------------------------
41
42 @Test
43 public void testUnionOfSets() throws Exception
44 {
45 List<Set<Integer>> list = new ArrayList<Set<Integer>>();
46 list.add(new HashSet<Integer>(Arrays.asList(1,2,3)));
47 list.add(new HashSet<Integer>(Arrays.asList(7,9)));
48 list.add(new HashSet<Integer>(Arrays.asList(8,10)));
49 list.add(new HashSet<Integer>(Arrays.asList(2,3,4)));
50 list.add(new HashSet<Integer>(Arrays.asList(4,5,6)));
51 list.add(new HashSet<Integer>(Arrays.asList(7,8)));
52 list.add(new HashSet<Integer>(Arrays.asList(1)));
53 list.add(new HashSet<Integer>(Arrays.asList(10)));
54 list.add(new HashSet<Integer>(Arrays.asList(11)));
55 list.add(new HashSet<Integer>(Arrays.asList(11)));
56 list.add(new HashSet<Integer>(Arrays.asList(7,8)));
57 list.add(new HashSet<Integer>(Arrays.asList(11)));
58
59 Set<Integer> expectedOne = new HashSet<Integer>(Arrays.asList(
60 1,2,3,4,5,6));
61 Set<Integer> expectedTwo = new HashSet<Integer>(Arrays.asList(
62 7,8,9,10));
63 Set<Integer> expectedThree = new HashSet<Integer>(Arrays.asList(
64 11));
65
67 assertEquals(3,list.size(),"Number of united sets");
68 assertEquals(expectedOne,list.get(0),"Elements in first set");
69 assertEquals(expectedTwo,list.get(1),"Elements in second set");
70 assertEquals(expectedThree,list.get(2),"Elements in third set");
71 }
72
73//------------------------------------------------------------------------------
74
75}
static< T > void unionOfIntersectingSets(List< Set< T > > list)
Takes the union of any two sets in this list that intersect.