package qub;

public class SpinMutexTests
{
    public static void test(final TestRunner runner)
    {
        runner.testGroup("SpinMutex", new Action0()
        {
            @Override
            public void run()
            {
                runner.test("constructor()", new Action1<Test>()
                {
                    @Override
                    public void run(Test test)
                    {
                        final SpinMutex mutex = new SpinMutex();
                        test.assertFalse(mutex.isAcquired());
                    }
                });
                
                runner.test("acquire() when not locked", new Action1<Test>()
                {
                    @Override
                    public void run(Test test)
                    {
                        final SpinMutex mutex = new SpinMutex();
                        mutex.acquire();
                        test.assertTrue(mutex.isAcquired());
                    }
                });

                runner.testGroup("tryAcquire()", new Action0()
                {
                    @Override
                    public void run()
                    {
                        runner.test("when not locked", new Action1<Test>()
                        {
                            @Override
                            public void run(Test test)
                            {
                                final SpinMutex mutex = new SpinMutex();
                                test.assertTrue(mutex.tryAcquire());
                                test.assertTrue(mutex.isAcquired());
                            }
                        });

                        runner.test("when locked", new Action1<Test>()
                        {
                            @Override
                            public void run(Test test)
                            {
                                final SpinMutex mutex = new SpinMutex();
                                mutex.acquire();
                                test.assertFalse(mutex.tryAcquire());
                                test.assertTrue(mutex.isAcquired());
                            }
                        });
                    }
                });

                runner.testGroup("release()", new Action0()
                {
                    @Override
                    public void run()
                    {
                        runner.test("when not locked", new Action1<Test>()
                        {
                            @Override
                            public void run(Test test)
                            {
                                final SpinMutex mutex = new SpinMutex();
                                test.assertFalse(mutex.release());
                                test.assertFalse(mutex.isAcquired());
                            }
                        });

                        runner.test("when locked", new Action1<Test>()
                        {
                            @Override
                            public void run(Test test)
                            {
                                final SpinMutex mutex = new SpinMutex();
                                mutex.acquire();
                                test.assertTrue(mutex.release());
                                test.assertFalse(mutex.isAcquired());
                            }
                        });
                    }
                });
            }
        });
    }
}

@@ -3473,32 +3473,3775 @@
              %7D);
+%0A%0A                runner.testGroup(%22criticalSection(Action0)%22, new Action0()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A                        runner.test(%22with null action%22, new Action1%3CTest%3E()%0A                        %7B%0A                            @Override%0A                            public void run(Test test)%0A                            %7B%0A                                final SpinMutex mutex = new SpinMutex();%0A                                mutex.criticalSection((Action0)null);%0A                                test.assertFalse(mutex.isAcquired());%0A                            %7D%0A                        %7D);%0A%0A                        runner.test(%22with non-null action%22, new Action1%3CTest%3E()%0A                        %7B%0A                            @Override%0A                            public void run(final Test test)%0A                            %7B%0A                                final SpinMutex mutex = new SpinMutex();%0A                                final Value%3CInteger%3E value = new Value%3C%3E();%0A                                mutex.criticalSection(new Action0()%0A                                %7B%0A                                    @Override%0A                                    public void run()%0A                                    %7B%0A                                        test.assertTrue(mutex.isAcquired());%0A                                        value.set(20);%0A                                    %7D%0A                                %7D);%0A                                test.assertFalse(mutex.isAcquired());%0A                                test.assertEqual(20, value.get());%0A                            %7D%0A                        %7D);%0A                    %7D%0A                %7D);%0A%0A                runner.testGroup(%22criticalSection(Function0%3CT%3E)%22, new Action0()%0A                %7B%0A                    @Override%0A                    public void run()%0A                    %7B%0A                        runner.test(%22with null function%22, new Action1%3CTest%3E()%0A                        %7B%0A                            @Override%0A                            public void run(Test test)%0A                            %7B%0A                                final SpinMutex mutex = new SpinMutex();%0A                                final Integer result = mutex.criticalSection((Function0%3CInteger%3E)null);%0A                                test.assertFalse(mutex.isAcquired());%0A                                test.assertNull(result);%0A                            %7D%0A                        %7D);%0A%0A                        runner.test(%22with non-null function%22, new Action1%3CTest%3E()%0A                        %7B%0A                            @Override%0A                            public void run(final Test test)%0A                            %7B%0A                                final SpinMutex mutex = new SpinMutex();%0A                                final Value%3CInteger%3E value = new Value%3C%3E();%0A                                final Boolean result = mutex.criticalSection(new Function0%3CBoolean%3E()%0A                                %7B%0A                                    @Override%0A                                    public Boolean run()%0A                                    %7B%0A                                        test.assertTrue(mutex.isAcquired());%0A                                        value.set(20);%0A                                        return true;%0A                                    %7D%0A                                %7D);%0A                                test.assertFalse(mutex.isAcquired());%0A                                test.assertEqual(20, value.get());%0A                                test.assertTrue(result);%0A                            %7D%0A                        %7D);%0A                    %7D%0A                %7D);
 %0A            %7D%0A 

Improve test coverage for SpinMutex