package org.multiverse.stms.gamma.transactionalobjects.txnlong;
import org.junit.Before;
import org.junit.Test;
import org.multiverse.api.LockMode;
import org.multiverse.stms.gamma.GammaStm;
import org.multiverse.stms.gamma.transactionalobjects.GammaTxnLong;
import org.multiverse.stms.gamma.transactionalobjects.Tranlocal;
import org.multiverse.stms.gamma.transactions.GammaTxn;
import static org.junit.Assert.assertEquals;
import static org.multiverse.stms.gamma.GammaTestUtils.assertRefHasLockMode;
import static org.multiverse.stms.gamma.GammaTestUtils.assertVersionAndValue;
public class GammaTxnLong_getAndSetAndLock3Test {
private GammaStm stm;
@Before
public void setUp() {
stm = new GammaStm();
}
@Test
public void whenLockFree() {
whenLockFree(LockMode.None);
whenLockFree(LockMode.Read);
whenLockFree(LockMode.Write);
whenLockFree(LockMode.Exclusive);
}
public void whenLockFree(LockMode lockMode) {
long initialValue = 10;
GammaTxnLong ref = new GammaTxnLong(stm, initialValue);
long initialVersion = ref.getVersion();
GammaTxn tx = stm.newDefaultTxn();
long newValue = 20;
long result = ref.getAndSetAndLock(tx, newValue, lockMode);
Tranlocal tranlocal = tx.locate(ref);
assertEquals(initialValue, result);
assertEquals(newValue,tranlocal.long_value);
assertVersionAndValue(ref, initialVersion, initialValue);
assertRefHasLockMode(ref, tx, lockMode.asInt());
}
}