/**
 * Copyright (c) 2016 - 2017 Syncleus, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aparapi.runtime;

import static org.junit.Assert.assertArrayEquals;

import com.aparapi.Kernel;
import com.aparapi.Range;
import com.aparapi.device.Device;
import com.aparapi.device.OpenCLDevice;
import com.aparapi.internal.kernel.KernelManager;
import static org.junit.Assume.*;
import org.junit.BeforeClass;
import org.junit.Test;

public class LocalArrayArgsIssue79Test {
    static OpenCLDevice openCLDevice = null;
    private static final int SIZE = 32;
    private int[] targetArray;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        Device device = KernelManager.instance().bestDevice();
        assumeTrue (device != null && device instanceof OpenCLDevice);
        openCLDevice = (OpenCLDevice) device;
    }

    @Test
    public void test() {
        final LocalArrayArgsKernel kernel = new LocalArrayArgsKernel();
        final Range range = openCLDevice.createRange(SIZE, SIZE);
        targetArray = new int[SIZE];
        kernel.setExplicit(false);
        kernel.setArray(targetArray);
        kernel.execute(range);
        validate();
    }
    
    @Test
    public void testExplicit() {
        final LocalArrayArgsKernel kernel = new LocalArrayArgsKernel();
        final Range range = openCLDevice.createRange(SIZE, SIZE);
        targetArray = new int[SIZE];
        kernel.setExplicit(true);
        kernel.setArray(targetArray);
        kernel.put(targetArray);
        kernel.execute(range);
        kernel.get(targetArray);
        validate();
    }

    void validate() {
        int[] expected = new int[SIZE];
        for (int threadId = 0; threadId < SIZE; threadId++) {
            for (int i = 0; i < SIZE; i++) {
                expected[threadId] += i + threadId;
            }
        	expected[threadId] *= threadId;
        }
        
        assertArrayEquals("destArray", expected, targetArray);
    }

    public static class LocalArrayArgsKernel extends Kernel {
        private int[] destArray;
        
        @Local
        private int[] myArray = new int[SIZE];

        public LocalArrayArgsKernel() {
        }
        
        @NoCL
        public void setArray(int[] target) {
            destArray = target;
        }

        private void doComputation1(@Local int[] arr, int id) {
            for (int i = 0; i < SIZE; i++) {
                arr[id] += i + id;
            }
        }

        private void doComputation2(int[] arr_$local$, int id) {
            arr_$local$[id] *= id;
        }

        @Override
        public void run() {
            int id = getLocalId();
            
            myArray[id] = destArray[id];

            doComputation1(myArray, id);
            doComputation2(myArray, id);
            
            destArray[id] = myArray[id];            
        }
    }
}

@@ -895,16 +895,17 @@
 sume.*;%0A
+%0A
 import o
@@ -919,21 +919,16 @@
 t.Before
-Class
 ;%0Aimport
@@ -1114,21 +1114,16 @@
  @Before
-Class
 %0A    pub
@@ -1126,23 +1126,16 @@
  public 
-static 
 void set
@@ -2437,19 +2437,21 @@
 Equals(%22
-des
+targe
 tArray%22,
@@ -2566,19 +2566,21 @@
 e int%5B%5D 
-d
+r
 es
+ul
 tArray;%0A
@@ -2649,16 +2649,114 @@
 t%5BSIZE%5D;
+%0A        %0A        @PrivateMemorySpace(SIZE)%0A        private int%5B%5D other_$private$ = new int%5BSIZE%5D;
 %0A%0A      
@@ -2875,27 +2875,29 @@
             
-d
+r
 es
+ul
 tArray = tar
@@ -2904,32 +2904,165 @@
 get;%0A        %7D%0A%0A
+        private void doInitialCopy(@Local int%5B%5D target, int%5B%5D source, int id) %7B%0A        %09target%5Bid%5D = source%5Bid%5D;%0A        %7D%0A        %0A
         private 
@@ -3315,32 +3315,183 @@
 *= id;%0A        %7D
+%0A        %0A        private void doComputation3(int%5B%5D arr_$local$, int%5B%5D arr_$private$, int id) %7B%0A        %09arr_$private$%5Bid%5D = arr_$local$%5Bid%5D;%0A        %7D
 %0A%0A        @Overr
@@ -3556,16 +3556,32 @@
 calId();
+                
 %0A       
@@ -3602,37 +3602,61 @@
     
-myArray%5Bid%5D = destArray%5Bid%5D;%0A
+%0A            doInitialCopy(myArray, resultArray, id);
 %0A   
@@ -3750,24 +3750,84 @@
     
-%0A            des
+doComputation3(myArray, other_$private$, id);%0A            %0A            resul
 tArr

Add support for Local arguments in kernel functions (refs #79) - improved unit test