<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 2013 KIWI.KI GmbH.
 */

#include <string.h> // strcmp

#ifndef TEST_ASSERTS_H
#define TEST_ASSERTS_H

#include "test_colors.h"
#include <setjmp.h>

extern jmp_buf test_runner_env;

#define TEST_FAIL() \
  longjmp(test_runner_env, 1)

#define TEST_TRUE(condition) \
do { \
  if (!(condition)) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected true, was false)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__ \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected true, was false)\n", \
        __LINE__ \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_FALSE(condition) \
do { \
  if (condition) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected false, was true)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__ \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected false, was true)\n", \
        __LINE__ \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_GT(a, b) \
do { \
  if (!((a) > (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " > " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " > " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_LT(a, b) \
do { \
  if (!((a) < (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " < " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " < " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_GE(a, b) \
do { \
  if (!((a) >= (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " >= " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " >= " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_LE(a, b) \
do { \
  if (!((a) <= (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " <= " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " <= " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_EQ(a, b) \
do { \
  if (!((a) == (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " == " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " == " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_NE(a, b) \
do { \
  if (!((a) != (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " != " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " != " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)a, (ptrdiff_t)b \
      ); \
    } \<commit_msg>Allow expressions in test asserts<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 2013 KIWI.KI GmbH.
 */

#include <string.h> // strcmp

#ifndef TEST_ASSERTS_H
#define TEST_ASSERTS_H

#include "test_colors.h"
#include <setjmp.h>

extern jmp_buf test_runner_env;

#define TEST_FAIL() \
  longjmp(test_runner_env, 1)

#define TEST_TRUE(condition) \
do { \
  if (!(condition)) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected true, was false)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__ \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected true, was false)\n", \
        __LINE__ \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_FALSE(condition) \
do { \
  if (condition) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected false, was true)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__ \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #condition \
        "' (expected false, was true)\n", \
        __LINE__ \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_GT(a, b) \
do { \
  if (!((a) > (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " > " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " > " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_LT(a, b) \
do { \
  if (!((a) < (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " < " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " < " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_GE(a, b) \
do { \
  if (!((a) >= (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " >= " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " >= " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_LE(a, b) \
do { \
  if (!((a) <= (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " <= " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " <= " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_EQ(a, b) \
do { \
  if (!((a) == (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " == " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " == " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    printf("%s", failure_message_buffer); \
    TEST_FAIL(); \
  } \
} while(0)

#define TEST_NE(a, b) \
do { \
  if (!((a) != (b))) \
  { \
    if (with_color) \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        "\r\e[1;%d;%dm" __FILE__ ":%d\nTest failed: '" #a " != " #b \
        "' (" #a "=%td, " #b "=%td)\e[0m\n", \
        FOREGROUND_WHITE, BACKGROUND_RED, \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
    else \
    { \
      snprintf( \
        failure_message_buffer, \
        sizeof(failure_message_buffer), \
        __FILE__ ":%d\nTest failed: '" #a " != " #b \
        "' (" #a "=%td, " #b "=%td)\n", \
        __LINE__, (ptrdiff_t)(a), (ptrdiff_t)(b) \
      ); \
    } \
{'fraction_non_alphanumeric': 0.19808108944599195, 'fraction_numerical': 0.004952027236149799, 'mean_word_length': 1.775391883186601, 'pattern_counts': {'":': 32, '<': 19, '<?xml version=': 0, '>': 19, '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': '6917819', 'n_tokens_mistral': 5104, 'n_tokens_neox': 4997, 'n_words': 830}