package gov.nist.androidsuite;

import android.os.Debug;
import android.os.Environment;

public class Val4 {
	/**
	 * val4.java 2013-06-04 2:36PM Reference executable for validating
	 * application profiling tools. val4: three functions with a single
	 * invocation and no recursion.
	 * 
	 * With default values of FN1LOOP and FN2LOOP, the expected distribution of
	 * time is approximately main total 100 % self 0 % fn2 total 100 % self 50 %
	 * fn1 total 50 % self 50 %
	 * 
	 * Old (2013-02-28 10:15) Spin was for Windows + Intel Composer XE 2013 +
	 * Microsoft Visual Studio 2012. Language c99.
	 * 
	 * New Spin for Android + DDMS + Eclipse. Language Java.
	 */

	// Configurable FN1LOOP: how many busy-work iterations in fn1.
	private long fn1loop = 400000;

	// Configurable FN2LOOP: how many busy-work iterations in fn2.
	private long fn2loop = 400000;

	public String Output = "";

	private long accumulator = 1;
	private double adder = 0;

	// / Method to start calling of functions
	public int Start(int repeat, long FN1LOOP, long FN2LOOP, int TraceBuffer) {
		if (FN1LOOP > 0) {
			fn1loop = FN1LOOP;
		}
		if (FN2LOOP > 0) {
			fn2loop = FN2LOOP;
		}
		Debug.startMethodTracing(Environment.getExternalStorageDirectory()+"/Traces/ValFour" + Integer.toString(repeat),
				TraceBuffer);
		fn2();
		Debug.stopMethodTracing();
		Output = "Accumulator: " + accumulator + "\n" + "Adder: " + adder;
		return 0;
	}

	// / Overload of the start function, if programmer just starts the gathering
	// process.
	public int Start() {
		// MainActivity.BufferToTrace is a variable being pass from the app
		// class,
		// if this class isn't available MainActivity.BufferToTrace default is
		// 8000000
		return Start(1, fn1loop, fn2loop, MainActivity.BufferToTrace);
	}

	// Function 1
	private void fn1() {
		for (long looper = 0; looper < fn1loop; ++looper) {
			adder += Math.PI * 3;
			accumulator = (long) ((accumulator * 3) + adder);
		}
	}

	// Function 2, calls function 1 at end of loop
	private void fn2() {
		for (long looper = 0; looper < fn2loop; ++looper) {
			adder += Math.PI * 3;
			accumulator = (long) ((accumulator * 3) + adder);
		}
		fn1();
	}
}
