/* * Copyright (c) 2018, Red Hat, Inc. All rights reserved. * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * Tests for capacity after map is populated with a given number N of mappings. * Maps are populated using a copy constructor on a map with N mappings, * other constructors followed by N put() calls, and other constructors followed * by putAll() on a map with N mappings. * * String labels encode the test case for ease of diagnosis if one of the test cases fails. * For example, "plm2pn" is "populated LinkedHashMap, 2-arg constructor, followed by putN".
*/
// helper method for one populated capacity case, to provide target types for lambdas
Object[] pcc(String label, int size, int expectedCapacity,
Supplier<Map<String, String>> supplier,
Consumer<Map<String, String>> consumer) { returnnew Object[]{label, size, expectedCapacity, supplier, consumer};
}
// Test case "fwm0pa" is commented out because WeakHashMap uses a different allocation // policy from the other map implementations: it deliberately under-allocates in this case.
}
// numbers in this range are truncated by a float computation with 0.75f // but can get an exact result with a double computation with 0.75d
cases.addAll(genFakePopulatedCapacityCases(25165824, 33554432));
cases.addAll(genFakePopulatedCapacityCases(25165825, 67108864));
cases.addAll(genFakePopulatedCapacityCases(50331648, 67108864));
cases.addAll(genFakePopulatedCapacityCases(50331649, 134217728));
return cases.iterator();
}
@Test(dataProvider = "populatedCapacity") publicvoid populatedCapacity(String label, // unused, included for diagnostics int size, // unused, included for diagnostics int expectedCapacity,
Supplier<Map<String, String>> s,
Consumer<Map<String, String>> c) {
Map<String, String> map = s.get();
c.accept(map);
assertEquals(capacity(map), expectedCapacity);
}
/* * tests for requested size (static factory methods)
*/
// helper method for one requested size case, to provide target types for lambda
Object[] rsc(String label, int size, int expectedCapacity,
Supplier<Capacitiable> supplier) { returnnew Object[]{label, size, expectedCapacity, supplier};
}
List<Object[]> genRequestedSizeCases(int size, int cap) { return Arrays.asList(
rsc("rshm", size, cap, () -> new MapCapacitiable(HashMap.newHashMap(size))),
rsc("rslm", size, cap, () -> new MapCapacitiable(LinkedHashMap.newLinkedHashMap(size))),
rsc("rswm", size, cap, () -> new MapCapacitiable(WeakHashMap.newWeakHashMap(size))),
rsc("rshs", size, cap, () -> new SetCapacitiable(HashSet.newHashSet(size))),
rsc("rsls", size, cap, () -> new SetCapacitiable(LinkedHashSet.newLinkedHashSet(size)))
);
}
// numbers in this range are truncated by a float computation with 0.75f // but can get an exact result with a double computation with 0.75d
cases.addAll(genRequestedSizeCases(25165824, 33554432));
cases.addAll(genRequestedSizeCases(25165825, 67108864));
cases.addAll(genRequestedSizeCases(50331648, 67108864));
cases.addAll(genRequestedSizeCases(50331649, 134217728));
return cases.iterator();
}
@Test(dataProvider = "requestedSize") publicvoid requestedSize(String label, // unused, included for diagnostics int size, // unused, included for diagnostics int expectedCapacity,
Supplier<Capacitiable> s) {
Capacitiable capacitiable = s.get();
capacitiable.init();
assertEquals(capacitiable.capacity(), expectedCapacity);
}
interface Capacitiable {
void init();
int capacity();
}
class MapCapacitiable implements Capacitiable {
privatefinal Map<String, String> content;
public MapCapacitiable(Map<String, String> content) { this.content = content;
}
/** * Tests that the APIs that take {@code numMappings} or {@code numElements} as a parameter for * creating the collection instance (for example: {@link HashMap#newHashMap(int)}), throw * an {@code IllegalArgumentException} when a negative value is passed to them
*/
@Test(dataProvider = "negativeNumMappings") publicvoid testNegativeNumMappings(final IntFunction<?> method, final String methodName) {
assertThrows(IllegalArgumentException.class, () -> method.apply(-1));
}
}
¤ Dauer der Verarbeitung: 0.5 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.