package gov.nist.androidsuite;

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

public class Val2 {
	/**
	 * val2.java 2013-06-04 1:06PM Reference executable for validating
	 * application profiling tools. val2: two functions with a single invocation
	 * and no recursion.
	 * 
	 * The call graph is simply main->leaffn with nearly 100 % of time spent in
	 * leaffn.
	 * 
	 * 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 INNERLOOP: how many iterations in leaf functions.
	private long innerloop = 800000;

	public String Output = "";

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

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

	// Function to call and trace
	private void leaffn() {
		for (long looper = 0; looper < innerloop; ++looper) {
			adder += Math.PI * 3;
			accumulator = (long) ((accumulator * 3) + adder);
		}
	}

	// / 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, innerloop, MainActivity.BufferToTrace);
	}
}
