$darkmode
DENOPTIM
SizeControlledSetTest.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;
23import static org.junit.jupiter.api.Assertions.assertFalse;
24import static org.junit.jupiter.api.Assertions.assertTrue;
25
26import java.io.File;
27
28import org.junit.jupiter.api.Test;
29import org.junit.jupiter.api.io.TempDir;
30
31import denoptim.io.DenoptimIO;
32
40{
41 private final String SEP = System.getProperty("file.separator");
42
43 @TempDir
44 File tempDir;
45
46//------------------------------------------------------------------------------
47
48 @Test
49 public void test() throws Exception
50 {
51 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
52 String memoryFile = tempDir.getAbsolutePath() + SEP + "test_memOnDisk";
53 String allData = tempDir.getAbsolutePath() + SEP + "test_allData";
54
55 SizeControlledSet scs = new SizeControlledSet(10, memoryFile, allData);
56
57 int tot = 20;
58 String base = "entry";
59
60 for (int i=0; i<tot; i++)
61 {
62 String s = base+i;
63 assertTrue(scs.addNewUniqueEntry(s),"Adding "+s);
64 assertEquals(i+1,scs.size(),"Size of SCS after adding "+i);
65 }
66
67 for (int i=0; i<tot; i++)
68 {
69 String s = base+i;
70 assertTrue(scs.contains(s),"Contains "+s);
71 assertFalse(scs.addNewUniqueEntry(s),"OverLoading "+s);
72 }
73
74 int j=-1;
75 for (String line : DenoptimIO.readList(allData))
76 {
77 j++;
78 assertEquals(line,base+j,"Line saves in all data file");
79 }
80 }
81
82//------------------------------------------------------------------------------
83
84}
Utility methods for input/output.
static ArrayList< String > readList(String fileName)
Read list of data as text.
Class meant to collect unique strings without leading to memory overflow.
synchronized int size()
Returns the number of unique entries.
synchronized boolean contains(String entry)
Checks if an entry is contained in this collection.
synchronized boolean addNewUniqueEntry(String entry)
Checks if the given entry is already container in the set of known entries and, if not,...
Unit test for SizeControlledSet.