From notifications-return-5704-archive-asf-public=cust-asf.ponee.io@ignite.apache.org Mon Aug 5 09:36:12 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 1903518057A for ; Mon, 5 Aug 2019 11:36:12 +0200 (CEST) Received: (qmail 37355 invoked by uid 500); 5 Aug 2019 09:36:11 -0000 Mailing-List: contact notifications-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list notifications@ignite.apache.org Received: (qmail 37341 invoked by uid 99); 5 Aug 2019 09:36:11 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Aug 2019 09:36:11 +0000 From: GitBox To: notifications@ignite.apache.org Subject: [GitHub] [ignite] ingvard commented on a change in pull request #6536: IGNITE-6957 Boxing / Unboxing optimization - added IntSet. Message-ID: <156499777144.32429.13998824340692182974.gitbox@gitbox.apache.org> Date: Mon, 05 Aug 2019 09:36:11 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit ingvard commented on a change in pull request #6536: IGNITE-6957 Boxing / Unboxing optimization - added IntSet. URL: https://github.com/apache/ignite/pull/6536#discussion_r306368122 ########## File path: modules/core/src/test/java/org/apache/ignite/internal/util/collection/AbstractBaseIntMapTest.java ########## @@ -0,0 +1,232 @@ +package org.apache.ignite.internal.util.collection; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicLong; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Common map tests. + */ +public abstract class AbstractBaseIntMapTest { + /** + * @return Returns particular implementation of IntMap. + */ + protected abstract IntMap instantiateMap(); + + /** + * + */ + @Test + public void sizeOfMap() { + IntMap map = instantiateMap(); + + assertTrue(map.isEmpty()); + assertEquals(0, map.size()); + + for (int i = 1; i < 100_000; i++) { + map.put(i, value(i)); + assertFalse(map.isEmpty()); + assertEquals(i, map.size()); + } + + for (int i = 99_999; i > 0; i--) { + map.remove(i); + assertEquals(i-1, map.size()); + } + + assertTrue(map.isEmpty()); + } + + /** + * + */ + @Test + public void getEmpty() { + IntMap map = instantiateMap(); + + assertNull(map.get(0)); + } + + /** + * + */ + @Test + public void putAndGet() { + IntMap map = instantiateMap(); + + map.put(0, value(0)); + map.put(239, value(239)); + map.put(677, value(677)); + + assertEquals(value(0), map.get(0)); + assertEquals(value(239), map.get(239)); + assertEquals(value(677), map.get(677)); + } + + /** + * + */ + @Test + public void getAbsentKey() { + IntMap map = instantiateMap(); + + map.put(0, value(0)); + map.put(239, value(239)); + map.put(677, value(677)); + + assertNull(map.get(32)); + } + + /** + * + */ + @Test + public void putPresentKey() { + IntMap map = instantiateMap(); + + map.put(0, value(0)); + String oldVal = map.put(0, value(1)); + + assertEquals(value(0), oldVal); + assertEquals(value(1), map.get(0)); + assertEquals(1, map.size()); + } + + /** + * + */ + @Test + public void remove() { + IntMap map = instantiateMap(); + + map.put(0, value(0)); + + assertEquals(value(0), map.remove(0)); + assertEquals(0, map.size()); + } + + /** + * + */ + @Test + public void removeAbsentKey() { + IntMap map = instantiateMap(); + + assertNull(map.remove(0)); + } + + /** + * + */ + @Test + public void putIfAbsent() { + IntMap map = instantiateMap(); + + String newVal = map.putIfAbsent(1, value(1)); + + assertNull(newVal); + + assertEquals(value(1), map.get(1)); + + String retry = map.putIfAbsent(1, value(2)); + + assertEquals(value(1), retry); + } + + /** + * + */ + @Test + public void forEach() { + IntMap map = instantiateMap(); + + for (int i = 1; i < 100_000; i++) + map.put(i, value(i)); + + + final AtomicLong cntr = new AtomicLong(0); + + map.forEach((key, value) -> cntr.addAndGet(key)); + + assertEquals(99_999L * 100_000L / 2, cntr.get()); + } + + /** + * + */ + @Test + public void contains() { + IntMap map = instantiateMap(); + + for (int i = 1; i < 10_000; i++) + map.put(i, value(i)); + + for (int i = 1; i < 10_000; i++) { + assertTrue(map.containsKey(i)); + assertTrue(map.containsValue(value(i))); + } + + assertFalse(map.containsKey(0)); + assertFalse(map.containsValue(value(0))); + } + + /** + * + */ + @Test + public void compareWithReferenceImplementation() { + Map originMap = new HashMap<>(); + IntMap testable = instantiateMap(); + + ThreadLocalRandom randomGen = ThreadLocalRandom.current(); + + for (int i = 0; i < 10_000_000; i++) { + int nextKey = randomGen.nextInt(0, 1_000_000); + int actId = randomGen.nextInt(0, 4); + + if (actId == 0) { + String oPut = originMap.put(nextKey, value(nextKey)); + String ePut = testable.put(nextKey, value(nextKey)); + + assertEquals(oPut, ePut); + assertEquals(originMap.containsKey(nextKey), testable.containsKey(nextKey)); + } + else if (actId == 1) { + assertEquals(originMap.get(nextKey), testable.get(nextKey)); + assertEquals(originMap.containsKey(nextKey), testable.containsKey(nextKey)); + } + else if (actId == 2) { + String oPutAbs = originMap.putIfAbsent(nextKey, value(nextKey)); + String ePutAbs = testable.putIfAbsent(nextKey, value(nextKey)); + + assertEquals(oPutAbs, ePutAbs); + assertEquals(originMap.containsKey(nextKey), testable.containsKey(nextKey)); + } + else { + String oRmv = originMap.remove(nextKey); + String eRmv = testable.remove(nextKey); + + assertEquals(oRmv, eRmv); + assertEquals(originMap.get(nextKey), testable.get(nextKey)); + assertEquals(originMap.containsKey(nextKey), testable.containsKey(nextKey)); + } + + Assert.assertEquals(originMap.size(), testable.size()); Review comment: This test does it. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services