Search in sources :

Example 66 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testGetSet.

/**
 * get returns the last value set at index
 */
public void testGetSet() {
    AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(1, aa.get(i));
        aa.set(i, 2);
        assertEquals(2, aa.get(i));
        aa.set(i, -3);
        assertEquals(-3, aa.get(i));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 67 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testGetAndSet.

/**
 * getAndSet returns previous value and sets to given value at given index
 */
public void testGetAndSet() {
    AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(1, aa.getAndSet(i, 0));
        assertEquals(0, aa.getAndSet(i, -10));
        assertEquals(-10, aa.getAndSet(i, 1));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 68 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testIndexing.

/**
 * get and set for out of bound indices throw IndexOutOfBoundsException
 */
public void testIndexing() {
    AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
    for (int index : new int[] { -1, SIZE }) {
        try {
            aa.get(index);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
        try {
            aa.set(index, 1);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
        try {
            aa.lazySet(index, 1);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
        try {
            aa.compareAndSet(index, 1, 2);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
        try {
            aa.weakCompareAndSet(index, 1, 2);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
        try {
            aa.getAndAdd(index, 1);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
        try {
            aa.addAndGet(index, 1);
            shouldThrow();
        } catch (IndexOutOfBoundsException success) {
        }
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 69 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testCompareAndSetInMultipleThreads.

/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicIntegerArray a = new AtomicIntegerArray(1);
    a.set(0, 1);
    Thread t = new Thread(new CheckedRunnable() {

        public void realRun() {
            while (!a.compareAndSet(0, 2, 3)) Thread.yield();
        }
    });
    t.start();
    assertTrue(a.compareAndSet(0, 1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, a.get(0));
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 70 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testAddAndGet.

/**
 * addAndGet adds given value to current, and returns current value
 */
public void testAddAndGet() {
    AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertEquals(3, aa.addAndGet(i, 2));
        assertEquals(3, aa.get(i));
        assertEquals(-1, aa.addAndGet(i, -4));
        assertEquals(-1, aa.get(i));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Aggregations

AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)74 Test (org.junit.Test)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 List (java.util.List)5 AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 CacheException (javax.cache.CacheException)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 GridRandom (org.apache.ignite.internal.util.GridRandom)3 CAX (org.apache.ignite.internal.util.typedef.CAX)3 AtomicIntegerArrayAssertBaseTest (org.assertj.core.api.AtomicIntegerArrayAssertBaseTest)3 JSONArray (com.alibaba.fastjson.JSONArray)2