/*
    PIP - Platform Independent Primitives
    System tests program
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "piconfig.h"
#include "pisystemtests.h"
#include "pisystemtime.h"
#ifdef CC_GCC
#  include <unistd.h>
#endif

int main(int argc, char * argv[]) {
#if defined(WINDOWS) || defined(MAC_OS)
	piCout << "This program is useless for Windows";
	return 0;
#else
	if (getuid() != 0) {
		piCout << "You should run this program as root!";
		return 0;
	}
	PIConfig conf(
#  ifndef WINDOWS
		"/etc/pip.conf"
#  else
		"pip.conf"
#  endif
	);
	PITimeMeasurer timer, tm;
	timespec ts;
	long stc = 0;
	double st;
	llong sts = 0;
	clock_getres(CLOCK_REALTIME, &ts);
	stc = long(ts.tv_sec) * 1000000000l + long(ts.tv_nsec);
	conf.setValue("time_resolution_ns", stc);
	piCout << "Timer resolution is " << stc << " ns";

	piCout << "\"PITimer.elapsed_*\" test ... ";
	stc        = 0;
	ts.tv_sec  = 0;
	ts.tv_nsec = 1000;
	PIVector<double> times;
	times.resize(8192);
	tm.reset();
	PISystemTests::time_elapsed_ns = 0;
	while (tm.elapsed_s() < 3.) {
		for (int i = 0; i < times.size_s(); ++i) {
			timer.reset();
			times[i] = timer.elapsed_m();
			times[i] = timer.elapsed_s();
			times[i] = timer.elapsed_u();
		}
		st = 0;
		for (int i = 0; i < times.size_s(); ++i)
			st += times[i];
		// cout << times[0] << endl;
		// cout << st / times.size_s() / 3. * 1000. << endl;
		sts += piRoundd(st / times.size_s() / 3. * 1000.);
		// cout << sts << endl;
		stc++;
	}
	sts /= stc;
	conf.setValue("time_elapsed_ns", long(sts));
	piCout << "ok, cost" << sts << "ns, average in" << stc << "series (" << (stc * 3 * times.size_s()) << "executes)";

	piCout << "\"usleep\" offset test ... ";
	PISystemTests::time_elapsed_ns = sts;
	tm.reset();
	stc = 0;
	sts = 0;
	times.resize(128);
	while (tm.elapsed_s() < 3.) {
		for (int i = 0; i < times.size_s(); ++i) {
			timer.reset();
			usleep(1000);
			times[i] = timer.elapsed_u();
		}
		st = 0;
		for (int i = 0; i < times.size_s(); ++i)
			st += times[i] - 1000;
		// cout << times[0] << endl;
		// cout << st / times.size_s() / 3. * 1000. << endl;
		sts += piRoundd(st / times.size_s());
		// cout << sts << endl;
		stc++;
	}
	sts /= stc;
	conf.setValue("usleep_offset_us", long(sts));
	piCout << "ok," << sts << "us, average in" << stc << "series (" << (stc * times.size_s()) << "executes)";
	return 0;
#endif
};
