// Written by retoor@molodetz.nl

// This program benchmarks different methods for outputting or printing text in C, using a custom benchmarking utility defined in rbench.h.

// The code uses rbench.h and rprint.h for benchmarking and custom printing functions beyond standard C functions.

// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
// the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.

#include "rprint.h"
#include "rbench.h"

void test_putc(void *arg) {
    char *str = (char *)arg;
    while (*str) {
        putc('\r', stdout);
        str++;
    }
}

void test_putchar(void *arg) {
    char *str = (char *)arg;
    while (*str) {
        putchar('\r');
        str++;
    }
}

void test_fwrite(void *arg) {
    int length;
    if (rbf->first) {
        length = strlen((char *)arg);
        rbf->data = (void *)&length;
    } else {
        length = (intptr_t)&rbf->data;
    }
    fwrite((char *)arg, 1, length, stdout);
}

void test_printf(void *arg) { printf("%s", (char *)arg); }

void test_rprint(void *arg) { rprint("%s", (char *)arg); }

void test_rprintr(void *arg) { rprintr("%s", (char *)arg); }

int main() {
    rbench_t *r = rbench_new();
    r->stdout = false;
    r->silent = true;
    long times = 100000;
    r->add_function(r, "putc", "chrloop", test_putc);
    r->add_function(r, "putchar", "chrloop", test_putchar);
    r->add_function(r, "fwrite", "fileio", test_fwrite);
    r->add_function(r, "printf", "default", test_printf);
    r->add_function(r, "rprint", "custom", test_rprint);
    r->add_function(r, "rprintr", "color", test_rprintr);
    r->execute1(r, times, "                              \r");
    r->execute1(r, times, "\\c\\T\\l\\L                  \r");
    return rtest_end("");
}