#include <iostream>
#include <iomanip>
#include "data.hpp"
#include "toolbox.hpp"

#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60

void printProgress(const float64_t& percentage) noexcept {
	const uint64_t val = static_cast<uint64_t>(percentage * 100);
	const int32_t lpad = static_cast<int32_t>(percentage * PBWIDTH);
	const int32_t rpad = PBWIDTH - lpad;
	fprintf(stderr, "%3lu%% [%.*s%*s]\r", val, lpad, PBSTR, rpad, "");
	fflush(stderr);
}

void clearProgress(void) noexcept {
	// Progress bar width + space before + num space + space after
	fprintf(stderr, "%*c\r", PBWIDTH + 1 + 3 + 3, ' ');
}

template<typename T>
void test(const uint64_t& N) noexcept {
#if __DEBUG
	printf("DETERMINISTIC for N=%s of %s sized %s\n", thousand_sep(N).c_str(), typeid(T).name(), format_byte_size(sizeof(T)).c_str());
	printf("Estimating memory footprint at : %s\n", format_byte_size(3 * N * sizeof(T)).c_str());
#endif

	T *a = new T[N], *b = new T[N], *c = new T[N];

	T mean = static_cast<T>(0.0);
	const size_t percent = N / 100;
	for(size_t i = 0; i < N; ++i){
		if (i % percent == 0) printProgress(static_cast<float64_t>(i) / N);
		a[i] = static_cast<T>(i < N>>1 ? 0.1 : 1.0);
		b[i] = static_cast<T>(1.0);
		c[i] = a[i] * b[i];
		mean += c[i];
	}
	mean /= static_cast<T>(N);

	clearProgress();
	std::cout << mean << std::endl;

	delete[] a, delete[] b, delete[] c;
}

void test_float(void) noexcept {
	std::cout << std::setprecision(1<<8);
	const uint64_t N = static_cast<uint64_t>(1)<<28;
	test<float128_t>(N);
	test<float64_t>(N);
	test<float32_t>(N);

	//printf("%.128af\n", static_cast<float64_t>(1) / 3);
	//std::cout << static_cast<float64_t>(1) / 3 << std::endl;
	//std::cout << std::hexfloat << static_cast<float64_t>(1) / 3 << std::endl;

	//printf("%.128Lf\n", static_cast<long float64_t>(1) / 3);
	//printf("%.128lf\n", static_cast<float64_t>(1) / 3);
	//printf("%.128f\n", static_cast<float>(1) / 3);
}