<commit_before>#ifdef HAVE_CONFIG_H
#include "../config.h"

#ifdef HAVE_CUNIT_CUNIT_H
//#ifdef HAVE_LIBCUNIT

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
#include "../config_parser.h"

typedef struct {
    ConfigErrorType error_type;
    char* error_filename;
    int error_line_number;
} ErrorInfo;


static bool config_error_handler(
    ConfigErrorType type, const char* filename, int line_number, void* data)
{
    ErrorInfo* p = (ErrorInfo*)data;
    p->error_type = type;
    p->error_filename = strdup(filename);
    p->error_line_number = line_number;
    return true;
}


static void test_parse_config()
{
    char test_input[] = "# comment\n"
                        "\r\n"
                        "idle:1:test.sh\n"
                        "wakeup:30:wakeup.sh\n";
    FILE* f = fmemopen(test_input, sizeof(test_input), "r");
    ErrorInfo error_info = { NO_ERROR, NULL, 0 };

    Config* p = parse_config(f, "test.conf", config_error_handler, &error_info);

    CU_ASSERT_EQUAL(error_info.error_type, NO_ERROR);
    CU_ASSERT_STRING_EQUAL(find_equals(p->idle_commands, 1)[0], "test.sh");
    CU_ASSERT_STRING_EQUAL(find_equals(p->wakeup_commands, 30)[0], "wakeup.sh");

    fclose(f);
    delete_config(p);
}


static void test_parse_config_too_short_minutes_error()
{
    char test_input[] = "idle:0:too_short_minute\n";
    FILE* f = fmemopen(test_input, sizeof(test_input), "r");
    ErrorInfo error_info = { NO_ERROR, NULL, 0 };

    Config* p = parse_config(f, "test.conf", config_error_handler, &error_info);

    CU_ASSERT_EQUAL(error_info.error_type, TOO_SHORT_MINUTES);

    fclose(f);
    delete_config(p);
}


static void test_parse_config_too_long_minutes_error()
{
    char test_input[1024];
    snprintf(test_input, sizeof(test_input),
        "idle:%lu:too_long_minutes\n", ULONG_MAX);
    FILE* f = fmemopen(test_input, sizeof(test_input), "r");
    ErrorInfo error_info = { NO_ERROR, NULL, 0 };

    Config* p = parse_config(f, "test.conf", config_error_handler, &error_info);

    CU_ASSERT_EQUAL(error_info.error_type, TOO_LONG_MINUTES);

    fclose(f);
    delete_config(p);
}

<commit_msg>fix config_parser_test to fail rarely<commit_after>#ifdef HAVE_CONFIG_H
#include "../config.h"

#ifdef HAVE_CUNIT_CUNIT_H
//#ifdef HAVE_LIBCUNIT

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
#include "../config_parser.h"

typedef struct {
    ConfigErrorType error_type;
    char* error_filename;
    int error_line_number;
} ErrorInfo;


static bool config_error_handler(
    ConfigErrorType type, const char* filename, int line_number, void* data)
{
    ErrorInfo* p = (ErrorInfo*)data;
    p->error_type = type;
    p->error_filename = strdup(filename);
    p->error_line_number = line_number;
    return true;
}


static void test_parse_config()
{
    char test_input[] = "# comment\n"
                        "\r\n"
                        "idle:1:test.sh\n"
                        "wakeup:30:wakeup.sh\n";
    FILE* f = fmemopen(test_input, sizeof(test_input), "r");
    ErrorInfo error_info = { NO_ERROR, NULL, 0 };

    Config* p = parse_config(f, "test.conf", config_error_handler, &error_info);

    CU_ASSERT_EQUAL(error_info.error_type, NO_ERROR);
    CU_ASSERT_STRING_EQUAL(find_equals(p->idle_commands, 1)[0], "test.sh");
    CU_ASSERT_STRING_EQUAL(find_equals(p->wakeup_commands, 30)[0], "wakeup.sh");

    fclose(f);
    delete_config(p);
}


static void test_parse_config_too_short_minutes_error()
{
    char test_input[] = "idle:0:too_short_minute\n";
    FILE* f = fmemopen(test_input, sizeof(test_input), "r");
    ErrorInfo error_info = { NO_ERROR, NULL, 0 };

    Config* p = parse_config(f, "test.conf", config_error_handler, &error_info);

    CU_ASSERT_EQUAL(error_info.error_type, TOO_SHORT_MINUTES);

    fclose(f);
    delete_config(p);
}


static void test_parse_config_too_long_minutes_error()
{
    char test_input[1024];
    snprintf(test_input, sizeof(test_input),
        "idle:%lu:too_long_minutes\n", ULONG_MAX);
    FILE* f = fmemopen(test_input, strlen(test_input), "r");
    ErrorInfo error_info = { NO_ERROR, NULL, 0 };

    Config* p = parse_config(f, "test.conf", config_error_handler, &error_info);

    CU_ASSERT_EQUAL(error_info.error_type, TOO_LONG_MINUTES);

    fclose(f);
    delete_config(p);
}


{'fraction_non_alphanumeric': 0.13276327632763277, 'fraction_numerical': 0.0072007200720072, 'mean_word_length': 3.911602209944751, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6412754', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1607, 'n_words': 292}