public void API_TryWrappedInvoke() { var atomicInt = new LockFreeInt32(); var target = new AtomicDelegate <Func <int, int> >(); Assert.AreEqual((false, default(int)), target.TryWrappedInvoke(f => f(10))); target.Set(i => atomicInt.Add(i).CurrentValue); Assert.AreEqual((true, 10), target.TryWrappedInvoke(f => f(10))); Assert.AreEqual(10, atomicInt.Value); Assert.AreEqual(true, target.TryWrappedInvoke((Action <Func <int, int> >)(f => f(10)))); Assert.AreEqual(20, atomicInt.Value); target.Set(null); Assert.AreEqual(false, target.TryWrappedInvoke((Action <Func <int, int> >)(f => f(10)))); Assert.AreEqual(20, atomicInt.Value); // Context-consuming variants atomicInt.Set(0); target.Set(null); Assert.AreEqual((false, default(int)), target.TryWrappedInvoke((f, ctx) => f(ctx), 10)); target.Set(i => atomicInt.Add(i).CurrentValue); Assert.AreEqual((true, 10), target.TryWrappedInvoke((f, ctx) => f(ctx), 10)); Assert.AreEqual(10, atomicInt.Value); Assert.AreEqual(true, target.TryWrappedInvoke((Action <Func <int, int>, int>)((f, ctx) => f(ctx)), 10)); Assert.AreEqual(20, atomicInt.Value); target.Set(null); Assert.AreEqual(false, target.TryWrappedInvoke((Action <Func <int, int>, int>)((f, ctx) => f(ctx)), 10)); Assert.AreEqual(20, atomicInt.Value); }
public void API_TryDynamicInvoke() { var atomicInt = new LockFreeInt32(); var targetA = new AtomicDelegate <Action <int> >(); Assert.AreEqual((false, (object)null), targetA.TryDynamicInvoke(10)); targetA.Set(i => atomicInt.Add(i)); Assert.AreEqual((true, (object)null), targetA.TryDynamicInvoke(300)); Assert.Throws <TargetParameterCountException>(() => targetA.TryDynamicInvoke()); Assert.Throws <ArgumentException>(() => targetA.TryDynamicInvoke("")); Assert.Throws <TargetParameterCountException>(() => targetA.TryDynamicInvoke(3, 3)); var targetB = new AtomicDelegate <Func <int, int> >(); Assert.AreEqual((false, (object)null), targetB.TryDynamicInvoke(10)); targetB.Set(i => atomicInt.Add(i).CurrentValue); var invokeRes = targetB.TryDynamicInvoke(300); Assert.AreEqual(true, invokeRes.DelegateWasInvoked); Assert.AreEqual(600, (int)invokeRes.Result); Assert.Throws <TargetParameterCountException>(() => targetB.TryDynamicInvoke()); Assert.Throws <ArgumentException>(() => targetB.TryDynamicInvoke("")); Assert.Throws <TargetParameterCountException>(() => targetB.TryDynamicInvoke(3, 3)); }