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));
}
}
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));
}
}
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) {
}
}
}
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));
}
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));
}
}
Aggregations