2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertTrue;
26 import org.testng.annotations.Test;
28 public class MemorySettingTest
31 @Test(groups = "Functional")
32 public void testGetMemorySetting()
39 /* some of these tests assume a host machine with RAM somewhere between 1GB and 1TB */
41 // should return 100% of physical memory available (or 1TB whichever is
43 long mem1 = MemorySetting.getMemorySetting("1T", "100");
44 long fullmem = mem1 + 512 * MB; // mem1 gets 512MB removed for the OS
45 long mem1b = MemorySetting.getMemorySetting("1t", "100");
46 assertTrue(mem1 > 1 * GB);
47 assertEquals(mem1, mem1b);
49 // test 10% memory. Note 512MB is set as minimum, so adjust to 50% if less
64 long mem1c = MemorySetting.getMemorySetting("1T", pc);
65 assertTrue(mem1c > (pcf - 0.01) * fullmem
66 && mem1c < (pcf + 0.01) * fullmem); // allowing for floating point
69 // should return 1GB (assuming host machine has more than 1GB RAM)
70 long mem2 = MemorySetting.getMemorySetting("1G", "100");
71 long mem2b = MemorySetting.getMemorySetting("1g", "100");
72 assertEquals(mem2, 1 * GB);
73 assertEquals(mem2, mem2b);
75 long mem3 = MemorySetting.getMemorySetting("1024M", "100");
76 long mem3b = MemorySetting.getMemorySetting("1024m", "100");
77 assertEquals(mem3, 1024 * MB);
78 assertEquals(mem3, mem3b);
80 long mem4 = MemorySetting.getMemorySetting("1048576K", "100");
81 long mem4b = MemorySetting.getMemorySetting("1048576k", "100");
82 assertEquals(mem4, 1048576 * KB);
83 assertEquals(mem4, mem4b);
85 long mem5 = MemorySetting.getMemorySetting("1073741824B", "100");
86 long mem5b = MemorySetting.getMemorySetting("1073741824b", "100");
87 long mem5c = MemorySetting.getMemorySetting("1073741824", "100");
88 assertEquals(mem5, 1073741824L);
89 assertEquals(mem5, mem5b);
90 assertEquals(mem5, mem5c);
92 // check g, m, k, b, "" acting as they should
93 assertEquals(mem2, mem3);
94 assertEquals(mem2, mem4);
95 assertEquals(mem2, mem5);
97 // default should not be more than 90% memory or 32GB
98 long mem6 = MemorySetting.getMemorySetting();
99 assertTrue(mem6 <= (long) (0.905 * fullmem));
100 assertTrue(mem6 <= 32 * GB);
102 // ensure enough memory for application
103 long mem7 = MemorySetting.getMemorySetting("1B", "0.000000001");
104 assertEquals(mem7, 512 * MB);
106 // ensure enough memory for OS
107 long mem8 = MemorySetting.getMemorySetting("2T", "100"); // this should be
109 long mem8b = MemorySetting.getMemorySetting("2T", "50");
111 long diff = mem8b * 2 - mem8;
112 assertTrue(512 * MB - 10 * KB < diff && diff < 512 * MB + 10 * KB);
113 // assertEquals(mem8b * 2 - mem8, 512 * MB);