package org.deuce.test.parallel;

import junit.framework.Assert;

import org.deuce.Atomic;
import org.deuce.transaction.TransactionException;
import org.junit.Before;
import org.junit.Test;


public class StaticUpdate {

	volatile private static int intVar;
	private static long longVar;
	private static double doubleVar;
//	private static Object objectVar;
//	private static String stringVar;
	
	private static int[] intArrVar;
	private static long[] longArrVar;
	private static double[] doubleArrVar;
//	private static Object[] objectArrvar;
//	private static String[] stringArrvar;

	@Before 
    public void setUp() { 
    	intVar = 0;
    	longVar = 0;
    	doubleVar = 0;
//    	objectVar = null;
//    	stringVar = null;
    	
    	intArrVar = new int[1];
    	longArrVar = new long[1];
    	doubleArrVar = new double[1];
//    	objectArrvar = new Object[1];
//    	stringArrvar = new String[1];
	}
	
	@Test
	public void simpleAdd() throws InterruptedException {
		int threadsCount = 2;
		final int rounds = 10000;
		Thread[] threads = new Thread[threadsCount];
		
		for( int j=0 ; j<threadsCount ; ++j)
		{
			threads[j] = new Thread(){
				@Override
				public void run(){
					int[] results = new int[2];
					for( int i=0; i<rounds ;++i)
					{
						try{
							atomicSingleUpdate(results);
//							Assert.assertEquals(results[0]+1, results[1]);
						}
						catch( TransactionException e)
						{
							i--;
						}
					}
				}
			};
		}
		for( int j=0 ; j<threadsCount ; ++j)
		{
			threads[j].start();
		}
		
		for( int j=0 ; j<threadsCount ; ++j)
		{
			threads[j].join();
		}
		
		
		Assert.assertEquals( rounds*threadsCount, intVar);
//		Assert.assertEquals( rounds*threadsCount, longVar);
//		Assert.assertEquals( (double)rounds*threadsCount, doubleVar);
////		Assert.assertNotNull(objectVar);
////		Assert.assertEquals( "a", stringVar);
//		
//		Assert.assertEquals( rounds*threadsCount, intArrVar[0]);
//		Assert.assertEquals( rounds*threadsCount, longArrVar[0]);
//		Assert.assertEquals( (double)rounds*threadsCount, doubleArrVar[0]);
//		Assert.assertNotNull(objectArrvar[0]);
//		Assert.assertEquals( "a", stringArrvar[0]);
		
	}
	
	@Atomic
	private void atomicSingleUpdate(int[] results) {
//		results[0] = intVar;
		int i = intVar;
		int j = intVar;
		if( j != i)
		{
//			try {
//				Thread.sleep(10);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			int x = intVar;
			System.out.println( i + " @@@@@@@@@@@@@@@@@@ " + j + " " + x);
		}
		++intVar;
//		longVar++;
//		doubleVar++;
//		objectVar = new Object();
//		stringVar = new String("a");
		
//		intArrVar[0]++;
//    	longArrVar[0]++;
//    	doubleArrVar[0]++;
//    	objectArrvar[0] = new Object();
//    	stringArrvar[0] = new String("a");
//    	results[1] = intVar;
	}
	
//	@Test 
//	public void simpleAddAndDec() {
//		atomicSingleUpdateAndDec();
//		
//		Assert.assertEquals(intVar, 0);
//		Assert.assertEquals(longVar, 0);
//		Assert.assertEquals(doubleVar, 0.0);
//		Assert.assertNull(objectVar);
//		Assert.assertNull(stringVar);
//		
//		Assert.assertEquals(intArrVar[0], 0);
//		Assert.assertEquals(longArrVar[0], 0);
//		Assert.assertEquals(doubleArrVar[0], 0.0);
//		Assert.assertNull(objectArrvar[0]);
//		Assert.assertNull(stringArrvar[0]);
//		
//	}
//	
//	@Atomic
//	private void atomicSingleUpdateAndDec() {
//		intVar++;
//		intVar--;
//		
//		longVar++;
//		longVar--;
//		
//		doubleVar++;
//		doubleVar--;
//		
//		objectVar = new Object();
//		objectVar = null;
//		
//		stringVar = "a";
//		stringVar = null;
//		
//		intArrVar[0]++;
//		intArrVar[0]--;
//		
//    	longArrVar[0]++;
//    	longArrVar[0]--;
//    	
//    	doubleArrVar[0]++;
//    	doubleArrVar[0]--;
//    	
//    	objectArrvar[0] = new Object();
//    	objectArrvar[0] = null;
//    	
//    	stringArrvar[0] = new String("a");
//    	stringArrvar[0] = null;
//	}

}

@@ -1179,41 +1179,8 @@
 ()%7B%0A
-%09%09%09%09%09int%5B%5D results = new int%5B2%5D;%0A
 %09%09%09%09
@@ -1257,71 +1257,8 @@
 ate(
-results);%0A//%09%09%09%09%09%09%09Assert.assertEquals(results%5B0%5D+1, results%5B1%5D
 );%0A%09
@@ -2103,342 +2103,11 @@
 ate(
-int%5B%5D results) %7B%0A//%09%09results%5B0%5D = intVar;%0A%09%09int i = intVar;%0A%09%09int j = intVar;%0A%09%09if( j != i)%0A%09%09%7B%0A//%09%09%09try %7B%0A//%09%09%09%09Thread.sleep(10);%0A//%09%09%09%7D catch (InterruptedException e) %7B%0A//%09%09%09%09// TODO Auto-generated catch block%0A//%09%09%09%09e.printStackTrace();%0A//%09%09%09%7D%0A%09%09%09int x = intVar;%0A%09%09%09System.out.println( i + %22 @@@@@@@@@@@@@@@@@@ %22 + j + %22 %22 + x);%0A%09%09%7D
+) %7B
 %0A%09%09+
@@ -2318,24 +2318,24 @@
 w Object();%0A
+
 //    %09strin
@@ -2368,36 +2368,8 @@
 %22);%0A
-//    %09results%5B1%5D = intVar;%0A
 %09%7D%0A%09

Remove debug code