/** @file utils.cpp
 *
 *  @author Cory Alexander Balaton (coryab)
 *  @author Janita Ovidie Sandtrøen Willumsen (janitaws)
 *
 *  @version 1.0
 *
 *  @brief Implementation of the utils
 *
 *  @bug No known bugs
 * */
#include "utils.hpp"
#include <cstdlib>
#include <cstring>
#include <string>
#include <bits/stdc++.h>

std::string scientific_format(double d, int width, int prec)
{
    std::stringstream ss;
    ss << std::setw(width) << std::setprecision(prec) << std::scientific << d;
    return ss.str();
}

std::string scientific_format(const std::vector<double>& v, int width, int prec)
{
    std::stringstream ss;
    for(double elem : v) {
        ss << scientific_format(elem, width, prec);
    }
    return ss.str();
}

static void print_message(const char* msg)
{
    if (strlen(msg) > 0) {
        std::cout << "message: " << msg << "\n\n";
    }
    else {
        std::cout << "\n";
    }
}

void m_assert(bool expr, 
              const char* expr_str, 
              const char* f, 
              const char* file, 
              int line,
              const char* msg)
{
    std::string new_assert(strlen(f) + (expr ? 4 : 6), '-');
    std::cout << "\x1B[36m" << new_assert << "\033[0m\n";
    std::cout << f << ": ";
    if (expr) {
        std::cout << "\x1B[32mOK\033[0m\n";
        print_message(msg);
    }
    else {
        std::cout << "\x1B[31mFAIL\033[0m\n";
        print_message(msg);
        std::cout << file <<  " " <<  line << ": Assertion \"" 
            << expr_str << "\" Failed\n\n";
        abort();
    }
}
