CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
unity.c2627 linesDownload Raw Back to unity
1/* =========================================================================2    Unity - A Test Framework for C3    ThrowTheSwitch.org4    Copyright (c) 2007-25 Mike Karlesky, Mark VanderVoord, & Greg Williams5    SPDX-License-Identifier: MIT6========================================================================= */7 8#include "unity.h"9 10#ifndef UNITY_PROGMEM11#define UNITY_PROGMEM12#endif13 14/* If omitted from header, declare overrideable prototypes here so they're ready for use */15#ifdef UNITY_OMIT_OUTPUT_CHAR_HEADER_DECLARATION16void UNITY_OUTPUT_CHAR(int);17#endif18 19/* Helpful macros for us to use here in Assert functions */20#define UNITY_FAIL_AND_BAIL         do { Unity.CurrentTestFailed  = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } while (0)21#define UNITY_IGNORE_AND_BAIL       do { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } while (0)22#define RETURN_IF_FAIL_OR_IGNORE    do { if (Unity.CurrentTestFailed || Unity.CurrentTestIgnored) { TEST_ABORT(); } } while (0)23 24struct UNITY_STORAGE_T Unity;25 26#ifdef UNITY_OUTPUT_COLOR27const char UNITY_PROGMEM UnityStrOk[]                            = "\033[42mOK\033[0m";28const char UNITY_PROGMEM UnityStrPass[]                          = "\033[42mPASS\033[0m";29const char UNITY_PROGMEM UnityStrFail[]                          = "\033[41mFAIL\033[0m";30const char UNITY_PROGMEM UnityStrIgnore[]                        = "\033[43mIGNORE\033[0m";31#else32const char UNITY_PROGMEM UnityStrOk[]                            = "OK";33const char UNITY_PROGMEM UnityStrPass[]                          = "PASS";34const char UNITY_PROGMEM UnityStrFail[]                          = "FAIL";35const char UNITY_PROGMEM UnityStrIgnore[]                        = "IGNORE";36#endif37static const char UNITY_PROGMEM UnityStrNull[]                   = "NULL";38static const char UNITY_PROGMEM UnityStrSpacer[]                 = ". ";39static const char UNITY_PROGMEM UnityStrExpected[]               = " Expected ";40static const char UNITY_PROGMEM UnityStrWas[]                    = " Was ";41static const char UNITY_PROGMEM UnityStrGt[]                     = " to be greater than ";42static const char UNITY_PROGMEM UnityStrLt[]                     = " to be less than ";43static const char UNITY_PROGMEM UnityStrOrEqual[]                = "or equal to ";44static const char UNITY_PROGMEM UnityStrNotEqual[]               = " to be not equal to ";45static const char UNITY_PROGMEM UnityStrElement[]                = " Element ";46static const char UNITY_PROGMEM UnityStrByte[]                   = " Byte ";47static const char UNITY_PROGMEM UnityStrMemory[]                 = " Memory Mismatch.";48static const char UNITY_PROGMEM UnityStrDelta[]                  = " Values Not Within Delta ";49static const char UNITY_PROGMEM UnityStrPointless[]              = " You Asked Me To Compare Nothing, Which Was Pointless.";50static const char UNITY_PROGMEM UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";51static const char UNITY_PROGMEM UnityStrNullPointerForActual[]   = " Actual pointer was NULL";52#ifndef UNITY_EXCLUDE_FLOAT53static const char UNITY_PROGMEM UnityStrNot[]                    = "Not ";54static const char UNITY_PROGMEM UnityStrInf[]                    = "Infinity";55static const char UNITY_PROGMEM UnityStrNegInf[]                 = "Negative Infinity";56static const char UNITY_PROGMEM UnityStrNaN[]                    = "NaN";57static const char UNITY_PROGMEM UnityStrDet[]                    = "Determinate";58static const char UNITY_PROGMEM UnityStrInvalidFloatTrait[]      = "Invalid Float Trait";59#endif60const char UNITY_PROGMEM UnityStrErrShorthand[]                  = "Unity Shorthand Support Disabled";61const char UNITY_PROGMEM UnityStrErrFloat[]                      = "Unity Floating Point Disabled";62const char UNITY_PROGMEM UnityStrErrDouble[]                     = "Unity Double Precision Disabled";63const char UNITY_PROGMEM UnityStrErr64[]                         = "Unity 64-bit Support Disabled";64const char UNITY_PROGMEM UnityStrErrDetailStack[]                = "Unity Detail Stack Support Disabled";65static const char UNITY_PROGMEM UnityStrBreaker[]                = "-----------------------";66static const char UNITY_PROGMEM UnityStrResultsTests[]           = " Tests ";67static const char UNITY_PROGMEM UnityStrResultsFailures[]        = " Failures ";68static const char UNITY_PROGMEM UnityStrResultsIgnored[]         = " Ignored ";69#ifndef UNITY_EXCLUDE_DETAILS70#ifdef UNITY_DETAIL_STACK_SIZE71static const char* UNITY_PROGMEM UnityStrDetailLabels[] = UNITY_DETAIL_LABEL_NAMES;72static const UNITY_COUNTER_TYPE UNITY_PROGMEM UnityStrDetailLabelsCount = sizeof(UnityStrDetailLabels) / sizeof(const char*);73static const char UNITY_PROGMEM UnityStrErrDetailStackEmpty[]           = " Detail Stack Empty";74static const char UNITY_PROGMEM UnityStrErrDetailStackFull[]            = " Detail Stack Full";75static const char UNITY_PROGMEM UnityStrErrDetailStackLabel[]           = " Detail Label Outside Of UNITY_DETAIL_LABEL_NAMES: ";76static const char UNITY_PROGMEM UnityStrErrDetailStackPop[]             = " Detail Pop With Unexpected Arguments";77#else78static const char UNITY_PROGMEM UnityStrDetail1Name[]            = UNITY_DETAIL1_NAME " ";79static const char UNITY_PROGMEM UnityStrDetail2Name[]            = " " UNITY_DETAIL2_NAME " ";80#endif81#endif82/*-----------------------------------------------83 * Pretty Printers & Test Result Output Handlers84 *-----------------------------------------------*/85 86/*-----------------------------------------------*/87/* Local helper function to print characters. */88static void UnityPrintChar(const char* pch)89{90    /* printable characters plus CR & LF are printed */91    if ((*pch <= 126) && (*pch >= 32))92    {93        UNITY_OUTPUT_CHAR(*pch);94    }95    /* write escaped carriage returns */96    else if (*pch == 13)97    {98        UNITY_OUTPUT_CHAR('\\');99        UNITY_OUTPUT_CHAR('r');100    }101    /* write escaped line feeds */102    else if (*pch == 10)103    {104        UNITY_OUTPUT_CHAR('\\');105        UNITY_OUTPUT_CHAR('n');106    }107    /* unprintable characters are shown as codes */108    else109    {110        UNITY_OUTPUT_CHAR('\\');111        UNITY_OUTPUT_CHAR('x');112        UnityPrintNumberHex((UNITY_UINT)*pch, 2);113    }114}115 116/*-----------------------------------------------*/117/* Local helper function to print ANSI escape strings e.g. "\033[42m". */118#ifdef UNITY_OUTPUT_COLOR119static UNITY_UINT UnityPrintAnsiEscapeString(const char* string)120{121    const char* pch = string;122    UNITY_UINT count = 0;123 124    while (*pch && (*pch != 'm'))125    {126        UNITY_OUTPUT_CHAR(*pch);127        pch++;128        count++;129    }130    UNITY_OUTPUT_CHAR('m');131    count++;132 133    return count;134}135#endif136 137/*-----------------------------------------------*/138void UnityPrint(const char* string)139{140    const char* pch = string;141 142    if (pch != NULL)143    {144        while (*pch)145        {146#ifdef UNITY_OUTPUT_COLOR147            /* print ANSI escape code */148            if ((*pch == 27) && (*(pch + 1) == '['))149            {150                pch += UnityPrintAnsiEscapeString(pch);151                continue;152            }153#endif154            UnityPrintChar(pch);155            pch++;156        }157    }158}159/*-----------------------------------------------*/160void UnityPrintLen(const char* string, const UNITY_UINT32 length)161{162    const char* pch = string;163 164    if (pch != NULL)165    {166        while (*pch && ((UNITY_UINT32)(pch - string) < length))167        {168            /* printable characters plus CR & LF are printed */169            if ((*pch <= 126) && (*pch >= 32))170            {171                UNITY_OUTPUT_CHAR(*pch);172            }173            /* write escaped carriage returns */174            else if (*pch == 13)175            {176                UNITY_OUTPUT_CHAR('\\');177                UNITY_OUTPUT_CHAR('r');178            }179            /* write escaped line feeds */180            else if (*pch == 10)181            {182                UNITY_OUTPUT_CHAR('\\');183                UNITY_OUTPUT_CHAR('n');184            }185            /* unprintable characters are shown as codes */186            else187            {188                UNITY_OUTPUT_CHAR('\\');189                UNITY_OUTPUT_CHAR('x');190                UnityPrintNumberHex((UNITY_UINT)*pch, 2);191            }192            pch++;193        }194    }195}196 197/*-----------------------------------------------*/198void UnityPrintIntNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)199{200    if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)201    {202        if (style == UNITY_DISPLAY_STYLE_CHAR)203        {204            /* printable characters plus CR & LF are printed */205            UNITY_OUTPUT_CHAR('\'');206            if ((number <= 126) && (number >= 32))207            {208                UNITY_OUTPUT_CHAR((int)number);209            }210            /* write escaped carriage returns */211            else if (number == 13)212            {213                UNITY_OUTPUT_CHAR('\\');214                UNITY_OUTPUT_CHAR('r');215            }216            /* write escaped line feeds */217            else if (number == 10)218            {219                UNITY_OUTPUT_CHAR('\\');220                UNITY_OUTPUT_CHAR('n');221            }222            /* unprintable characters are shown as codes */223            else224            {225                UNITY_OUTPUT_CHAR('\\');226                UNITY_OUTPUT_CHAR('x');227                UnityPrintNumberHex((UNITY_UINT)number, 2);228            }229            UNITY_OUTPUT_CHAR('\'');230        }231        else232        {233            UnityPrintNumber(number);234        }235    }236}237 238void UnityPrintUintNumberByStyle(const UNITY_UINT number, const UNITY_DISPLAY_STYLE_T style)239{240    if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)241    {242        UnityPrintNumberUnsigned(number);243    }244    else245    {246        UNITY_OUTPUT_CHAR('0');247        UNITY_OUTPUT_CHAR('x');248        UnityPrintNumberHex((UNITY_UINT)number, (char)((style & 0xF) * 2));249    }250}251 252/*-----------------------------------------------*/253void UnityPrintNumber(const UNITY_INT number_to_print)254{255    UNITY_UINT number = (UNITY_UINT)number_to_print;256 257    if (number_to_print < 0)258    {259        /* A negative number, including MIN negative */260        UNITY_OUTPUT_CHAR('-');261        number = (~number) + 1;262    }263    UnityPrintNumberUnsigned(number);264}265 266/*-----------------------------------------------267 * basically do an itoa using as little ram as possible */268void UnityPrintNumberUnsigned(const UNITY_UINT number)269{270    UNITY_UINT divisor = 1;271 272    /* figure out initial divisor */273    while (number / divisor > 9)274    {275        divisor *= 10;276    }277 278    /* now mod and print, then divide divisor */279    do280    {281        UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));282        divisor /= 10;283    } while (divisor > 0);284}285 286/*-----------------------------------------------*/287void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print)288{289    int nibble;290    char nibbles = nibbles_to_print;291 292    if ((unsigned)nibbles > UNITY_MAX_NIBBLES)293    {294        nibbles = UNITY_MAX_NIBBLES;295    }296 297    while (nibbles > 0)298    {299        nibbles--;300        nibble = (int)(number >> (nibbles * 4)) & 0x0F;301        if (nibble <= 9)302        {303            UNITY_OUTPUT_CHAR((char)('0' + nibble));304        }305        else306        {307            UNITY_OUTPUT_CHAR((char)('A' - 10 + nibble));308        }309    }310}311 312/*-----------------------------------------------*/313void UnityPrintMask(const UNITY_UINT mask, const UNITY_UINT number)314{315    UNITY_UINT current_bit = (UNITY_UINT)1 << (UNITY_INT_WIDTH - 1);316    UNITY_INT32 i;317 318    for (i = 0; i < UNITY_INT_WIDTH; i++)319    {320        if (current_bit & mask)321        {322            if (current_bit & number)323            {324                UNITY_OUTPUT_CHAR('1');325            }326            else327            {328                UNITY_OUTPUT_CHAR('0');329            }330        }331        else332        {333            UNITY_OUTPUT_CHAR('X');334        }335        current_bit = current_bit >> 1;336    }337}338 339/*-----------------------------------------------*/340#ifndef UNITY_EXCLUDE_FLOAT_PRINT341/*342 * This function prints a floating-point value in a format similar to343 * printf("%.7g") on a single-precision machine or printf("%.9g") on a344 * double-precision machine.  The 7th digit won't always be totally correct345 * in single-precision operation (for that level of accuracy, a more346 * complicated algorithm would be needed).347 */348void UnityPrintFloat(const UNITY_DOUBLE input_number)349{350#ifdef UNITY_INCLUDE_DOUBLE351    static const int sig_digits = 9;352    static const UNITY_INT32 min_scaled = 100000000;353    static const UNITY_INT32 max_scaled = 1000000000;354#else355    static const int sig_digits = 7;356    static const UNITY_INT32 min_scaled = 1000000;357    static const UNITY_INT32 max_scaled = 10000000;358#endif359 360    UNITY_DOUBLE number = input_number;361 362    /* print minus sign (does not handle negative zero) */363    if (number < 0.0f)364    {365        UNITY_OUTPUT_CHAR('-');366        number = -number;367    }368 369    /* handle zero, NaN, and +/- infinity */370    if (number == 0.0f)371    {372        UnityPrint("0");373    }374    else if (UNITY_IS_NAN(number))375    {376        UnityPrint("nan");377    }378    else if (UNITY_IS_INF(number))379    {380        UnityPrint("inf");381    }382    else383    {384        UNITY_INT32 n_int = 0;385        UNITY_INT32 n;386        int         exponent = 0;387        int         decimals;388        int         digits;389        char        buf[16] = {0};390 391        /*392         * Scale up or down by powers of 10.  To minimize rounding error,393         * start with a factor/divisor of 10^10, which is the largest394         * power of 10 that can be represented exactly.  Finally, compute395         * (exactly) the remaining power of 10 and perform one more396         * multiplication or division.397         */398        if (number < 1.0f)399        {400            UNITY_DOUBLE factor = 1.0f;401 402            while (number < (UNITY_DOUBLE)max_scaled / 1e10f)  { number *= 1e10f; exponent -= 10; }403            while (number * factor < (UNITY_DOUBLE)min_scaled) { factor *= 10.0f; exponent--; }404 405            number *= factor;406        }407        else if (number > (UNITY_DOUBLE)max_scaled)408        {409            UNITY_DOUBLE divisor = 1.0f;410 411            while (number > (UNITY_DOUBLE)min_scaled * 1e10f)   { number  /= 1e10f; exponent += 10; }412            while (number / divisor > (UNITY_DOUBLE)max_scaled) { divisor *= 10.0f; exponent++; }413 414            number /= divisor;415        }416        else417        {418            /*419             * In this range, we can split off the integer part before420             * doing any multiplications.  This reduces rounding error by421             * freeing up significant bits in the fractional part.422             */423            UNITY_DOUBLE factor = 1.0f;424            n_int = (UNITY_INT32)number;425            number -= (UNITY_DOUBLE)n_int;426 427            while (n_int < min_scaled) { n_int *= 10; factor *= 10.0f; exponent--; }428 429            number *= factor;430        }431 432        /* round to nearest integer */433        n = ((UNITY_INT32)(number + number) + 1) / 2;434 435#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO436        /* round to even if exactly between two integers */437        if ((n & 1) && (((UNITY_DOUBLE)n - number) == 0.5f))438            n--;439#endif440 441        n += n_int;442 443        if (n >= max_scaled)444        {445            n = min_scaled;446            exponent++;447        }448 449        /* determine where to place decimal point */450        decimals = ((exponent <= 0) && (exponent >= -(sig_digits + 3))) ? (-exponent) : (sig_digits - 1);451        exponent += decimals;452 453        /* truncate trailing zeroes after decimal point */454        while ((decimals > 0) && ((n % 10) == 0))455        {456            n /= 10;457            decimals--;458        }459 460        /* build up buffer in reverse order */461        digits = 0;462        while ((n != 0) || (digits <= decimals))463        {464            buf[digits++] = (char)('0' + n % 10);465            n /= 10;466        }467 468        /* print out buffer (backwards) */469        while (digits > 0)470        {471            if (digits == decimals)472            {473                UNITY_OUTPUT_CHAR('.');474            }475            UNITY_OUTPUT_CHAR(buf[--digits]);476        }477 478        /* print exponent if needed */479        if (exponent != 0)480        {481            UNITY_OUTPUT_CHAR('e');482 483            if (exponent < 0)484            {485                UNITY_OUTPUT_CHAR('-');486                exponent = -exponent;487            }488            else489            {490                UNITY_OUTPUT_CHAR('+');491            }492 493            digits = 0;494            while ((exponent != 0) || (digits < 2))495            {496                buf[digits++] = (char)('0' + exponent % 10);497                exponent /= 10;498            }499            while (digits > 0)500            {501                UNITY_OUTPUT_CHAR(buf[--digits]);502            }503        }504    }505}506#endif /* ! UNITY_EXCLUDE_FLOAT_PRINT */507 508/*-----------------------------------------------*/509static void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)510{511#ifdef UNITY_OUTPUT_FOR_ECLIPSE512    UNITY_OUTPUT_CHAR('(');513    UnityPrint(file);514    UNITY_OUTPUT_CHAR(':');515    UnityPrintNumber((UNITY_INT)line);516    UNITY_OUTPUT_CHAR(')');517    UNITY_OUTPUT_CHAR(' ');518    UnityPrint(Unity.CurrentTestName);519    UNITY_OUTPUT_CHAR(':');520#else521#ifdef UNITY_OUTPUT_FOR_IAR_WORKBENCH522    UnityPrint("<SRCREF line=");523    UnityPrintNumber((UNITY_INT)line);524    UnityPrint(" file=\"");525    UnityPrint(file);526    UNITY_OUTPUT_CHAR('"');527    UNITY_OUTPUT_CHAR('>');528    UnityPrint(Unity.CurrentTestName);529    UnityPrint("</SRCREF> ");530#else531#ifdef UNITY_OUTPUT_FOR_QT_CREATOR532    UnityPrint("file://");533    UnityPrint(file);534    UNITY_OUTPUT_CHAR(':');535    UnityPrintNumber((UNITY_INT)line);536    UNITY_OUTPUT_CHAR(' ');537    UnityPrint(Unity.CurrentTestName);538    UNITY_OUTPUT_CHAR(':');539#else540    UnityPrint(file);541    UNITY_OUTPUT_CHAR(':');542    UnityPrintNumber((UNITY_INT)line);543    UNITY_OUTPUT_CHAR(':');544    UnityPrint(Unity.CurrentTestName);545    UNITY_OUTPUT_CHAR(':');546#endif547#endif548#endif549}550 551/*-----------------------------------------------*/552static void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)553{554    UnityTestResultsBegin(Unity.TestFile, line);555    UnityPrint(UnityStrFail);556    UNITY_OUTPUT_CHAR(':');557}558 559/*-----------------------------------------------*/560void UnityConcludeTest(void)561{562    if (Unity.CurrentTestIgnored)563    {564        Unity.TestIgnores++;565    }566    else if (!Unity.CurrentTestFailed)567    {568        UnityTestResultsBegin(Unity.TestFile, Unity.CurrentTestLineNumber);569        UnityPrint(UnityStrPass);570    }571    else572    {573        Unity.TestFailures++;574    }575 576    Unity.CurrentTestFailed = 0;577    Unity.CurrentTestIgnored = 0;578    UNITY_PRINT_EXEC_TIME();579    UNITY_PRINT_EOL();580    UNITY_FLUSH_CALL();581}582 583/*-----------------------------------------------*/584static void UnityAddMsgIfSpecified(const char* msg)585{586#ifdef UNITY_PRINT_TEST_CONTEXT587    UnityPrint(UnityStrSpacer);588    UNITY_PRINT_TEST_CONTEXT();589#endif590#ifndef UNITY_EXCLUDE_DETAILS591#ifdef UNITY_DETAIL_STACK_SIZE592    {593        UNITY_COUNTER_TYPE c;594        for (c = 0; (c < Unity.CurrentDetailStackSize) && (c < UNITY_DETAIL_STACK_SIZE); c++) {595            const char* label;596            if ((Unity.CurrentDetailStackLabels[c] == UNITY_DETAIL_NONE) || (Unity.CurrentDetailStackLabels[c] > UnityStrDetailLabelsCount)) {597                break;598            }599            label = UnityStrDetailLabels[Unity.CurrentDetailStackLabels[c]];600            UnityPrint(UnityStrSpacer);601            if ((label[0] == '#') && (label[1] != 0)) {602                UnityPrint(label + 2);603                UNITY_OUTPUT_CHAR(' ');604                if ((label[1] & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) {605                    UnityPrintIntNumberByStyle((UNITY_INT)Unity.CurrentDetailStackValues[c], label[1]);606                } else {607                    UnityPrintUintNumberByStyle((UNITY_UINT)Unity.CurrentDetailStackValues[c], label[1]);608                }609            } else if (Unity.CurrentDetailStackValues[c] != 0){610                UnityPrint(label);611                UNITY_OUTPUT_CHAR(' ');612                UnityPrint((const char*)Unity.CurrentDetailStackValues[c]);613            }614        }615    }616#else617    if (Unity.CurrentDetail1)618    {619        UnityPrint(UnityStrSpacer);620        UnityPrint(UnityStrDetail1Name);621        UnityPrint(Unity.CurrentDetail1);622        if (Unity.CurrentDetail2)623        {624            UnityPrint(UnityStrDetail2Name);625            UnityPrint(Unity.CurrentDetail2);626        }627    }628#endif629#endif630    if (msg)631    {632        UnityPrint(UnityStrSpacer);633        UnityPrint(msg);634    }635}636 637/*-----------------------------------------------*/638static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)639{640    UnityPrint(UnityStrExpected);641    if (expected != NULL)642    {643        UNITY_OUTPUT_CHAR('\'');644        UnityPrint(expected);645        UNITY_OUTPUT_CHAR('\'');646    }647    else648    {649        UnityPrint(UnityStrNull);650    }651    UnityPrint(UnityStrWas);652    if (actual != NULL)653    {654        UNITY_OUTPUT_CHAR('\'');655        UnityPrint(actual);656        UNITY_OUTPUT_CHAR('\'');657    }658    else659    {660        UnityPrint(UnityStrNull);661    }662}663 664/*-----------------------------------------------*/665static void UnityPrintExpectedAndActualStringsLen(const char* expected,666                                                  const char* actual,667                                                  const UNITY_UINT32 length)668{669    UnityPrint(UnityStrExpected);670    if (expected != NULL)671    {672        UNITY_OUTPUT_CHAR('\'');673        UnityPrintLen(expected, length);674        UNITY_OUTPUT_CHAR('\'');675    }676    else677    {678        UnityPrint(UnityStrNull);679    }680    UnityPrint(UnityStrWas);681    if (actual != NULL)682    {683        UNITY_OUTPUT_CHAR('\'');684        UnityPrintLen(actual, length);685        UNITY_OUTPUT_CHAR('\'');686    }687    else688    {689        UnityPrint(UnityStrNull);690    }691}692 693/*-----------------------------------------------694 * Assertion & Control Helpers695 *-----------------------------------------------*/696 697/*-----------------------------------------------*/698static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected,699                               UNITY_INTERNAL_PTR actual,700                               const UNITY_LINE_TYPE lineNumber,701                               const char* msg)702{703    /* Both are NULL or same pointer */704    if (expected == actual) { return 0; }705 706    /* print and return true if just expected is NULL */707    if (expected == NULL)708    {709        UnityTestResultsFailBegin(lineNumber);710        UnityPrint(UnityStrNullPointerForExpected);711        UnityAddMsgIfSpecified(msg);712        return 1;713    }714 715    /* print and return true if just actual is NULL */716    if (actual == NULL)717    {718        UnityTestResultsFailBegin(lineNumber);719        UnityPrint(UnityStrNullPointerForActual);720        UnityAddMsgIfSpecified(msg);721        return 1;722    }723 724    return 0; /* return false if neither is NULL */725}726 727/*-----------------------------------------------728 * Assertion Functions729 *-----------------------------------------------*/730 731/*-----------------------------------------------*/732void UnityAssertBits(const UNITY_INT mask,733                     const UNITY_INT expected,734                     const UNITY_INT actual,735                     const char* msg,736                     const UNITY_LINE_TYPE lineNumber)737{738    RETURN_IF_FAIL_OR_IGNORE;739 740    if ((mask & expected) != (mask & actual))741    {742        UnityTestResultsFailBegin(lineNumber);743        UnityPrint(UnityStrExpected);744        UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)expected);745        UnityPrint(UnityStrWas);746        UnityPrintMask((UNITY_UINT)mask, (UNITY_UINT)actual);747        UnityAddMsgIfSpecified(msg);748        UNITY_FAIL_AND_BAIL;749    }750}751 752/*-----------------------------------------------*/753void UnityAssertEqualIntNumber(const UNITY_INT expected,754                               const UNITY_INT actual,755                               const char* msg,756                               const UNITY_LINE_TYPE lineNumber,757                               const UNITY_DISPLAY_STYLE_T style)758{759    RETURN_IF_FAIL_OR_IGNORE;760 761    if (expected != actual)762    {763        UnityTestResultsFailBegin(lineNumber);764        UnityPrint(UnityStrExpected);765        UnityPrintIntNumberByStyle(expected, style);766        UnityPrint(UnityStrWas);767        UnityPrintIntNumberByStyle(actual, style);768        UnityAddMsgIfSpecified(msg);769        UNITY_FAIL_AND_BAIL;770    }771}772 773void UnityAssertEqualUintNumber(const UNITY_UINT expected,774                                const UNITY_UINT actual,775                                const char* msg,776                                const UNITY_LINE_TYPE lineNumber,777                                const UNITY_DISPLAY_STYLE_T style)778{779    RETURN_IF_FAIL_OR_IGNORE;780 781    if (expected != actual)782    {783        UnityTestResultsFailBegin(lineNumber);784        UnityPrint(UnityStrExpected);785        UnityPrintUintNumberByStyle(expected, style);786        UnityPrint(UnityStrWas);787        UnityPrintUintNumberByStyle(actual, style);788        UnityAddMsgIfSpecified(msg);789        UNITY_FAIL_AND_BAIL;790    }791}792/*-----------------------------------------------*/793void UnityAssertIntGreaterOrLessOrEqualNumber(const UNITY_INT threshold,794                                              const UNITY_INT actual,795                                              const UNITY_COMPARISON_T compare,796                                              const char *msg,797                                              const UNITY_LINE_TYPE lineNumber,798                                              const UNITY_DISPLAY_STYLE_T style)799{800    int failed = 0;801    RETURN_IF_FAIL_OR_IGNORE;802 803    if ((threshold == actual) && !(compare & UNITY_EQUAL_TO)) { failed = 1; }804 805    if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }806    if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }807 808    if (failed)809    {810        UnityTestResultsFailBegin(lineNumber);811        UnityPrint(UnityStrExpected);812        UnityPrintIntNumberByStyle(actual, style);813        if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt);       }814        if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt);       }815        if (compare & UNITY_EQUAL_TO)     { UnityPrint(UnityStrOrEqual);  }816        if (compare == UNITY_NOT_EQUAL)   { UnityPrint(UnityStrNotEqual); }817        UnityPrintIntNumberByStyle(threshold, style);818        UnityAddMsgIfSpecified(msg);819        UNITY_FAIL_AND_BAIL;820    }821}822 823void UnityAssertUintGreaterOrLessOrEqualNumber(const UNITY_UINT threshold,824                                               const UNITY_UINT actual,825                                               const UNITY_COMPARISON_T compare,826                                               const char *msg,827                                               const UNITY_LINE_TYPE lineNumber,828                                               const UNITY_DISPLAY_STYLE_T style)829{830    int failed = 0;831    RETURN_IF_FAIL_OR_IGNORE;832 833    if ((threshold == actual) && !(compare & UNITY_EQUAL_TO)) { failed = 1; }834 835    /* UINT or HEX */836    if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }837    if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }838 839    if (failed)840    {841        UnityTestResultsFailBegin(lineNumber);842        UnityPrint(UnityStrExpected);843        UnityPrintUintNumberByStyle(actual, style);844        if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt);       }845        if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt);       }846        if (compare & UNITY_EQUAL_TO)     { UnityPrint(UnityStrOrEqual);  }847        if (compare == UNITY_NOT_EQUAL)   { UnityPrint(UnityStrNotEqual); }848        UnityPrintUintNumberByStyle(threshold, style);849        UnityAddMsgIfSpecified(msg);850        UNITY_FAIL_AND_BAIL;851    }852}853 854#define UnityPrintPointlessAndBail()       \855do {                                       \856    UnityTestResultsFailBegin(lineNumber); \857    UnityPrint(UnityStrPointless);         \858    UnityAddMsgIfSpecified(msg);           \859    UNITY_FAIL_AND_BAIL;                   \860} while (0)861 862/*-----------------------------------------------*/863void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,864                              UNITY_INTERNAL_PTR actual,865                              const UNITY_UINT32 num_elements,866                              const char* msg,867                              const UNITY_LINE_TYPE lineNumber,868                              const UNITY_DISPLAY_STYLE_T style,869                              const UNITY_FLAGS_T flags)870{871    UNITY_UINT32 elements  = num_elements;872    unsigned int length    = style & 0xF;873    unsigned int increment = 0;874 875    RETURN_IF_FAIL_OR_IGNORE;876 877    if (num_elements == 0)878    {879#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY880        UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg);881#else882        UnityPrintPointlessAndBail();883#endif884    }885 886    if (expected == actual)887    {888        return; /* Both are NULL or same pointer */889    }890 891    if (UnityIsOneArrayNull(expected, actual, lineNumber, msg))892    {893        UNITY_FAIL_AND_BAIL;894    }895 896    while ((elements > 0) && (elements--))897    {898        UNITY_INT expect_val;899        UNITY_INT actual_val;900 901        switch (length)902        {903            case 1:904                expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)expected;905                actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)actual;906                if (style & (UNITY_DISPLAY_RANGE_UINT | UNITY_DISPLAY_RANGE_HEX))907                {908                    expect_val &= 0x000000FF;909                    actual_val &= 0x000000FF;910                }911                increment  = sizeof(UNITY_INT8);912                break;913 914            case 2:915                expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)expected;916                actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)actual;917                if (style & (UNITY_DISPLAY_RANGE_UINT | UNITY_DISPLAY_RANGE_HEX))918                {919                    expect_val &= 0x0000FFFF;920                    actual_val &= 0x0000FFFF;921                }922                increment  = sizeof(UNITY_INT16);923                break;924 925#ifdef UNITY_SUPPORT_64926            case 8:927                expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)expected;928                actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)actual;929                increment  = sizeof(UNITY_INT64);930                break;931#endif932 933            default: /* default is length 4 bytes */934            case 4:935                expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)expected;936                actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)actual;937#ifdef UNITY_SUPPORT_64938                if (style & (UNITY_DISPLAY_RANGE_UINT | UNITY_DISPLAY_RANGE_HEX))939                {940                    expect_val &= 0x00000000FFFFFFFF;941                    actual_val &= 0x00000000FFFFFFFF;942                }943#endif944                increment  = sizeof(UNITY_INT32);945                length = 4;946                break;947        }948 949        if (expect_val != actual_val)950        {951            if ((style & UNITY_DISPLAY_RANGE_UINT) && (length < (UNITY_INT_WIDTH / 8)))952            {   /* For UINT, remove sign extension (padding 1's) from signed type casts above */953                UNITY_INT mask = 1;954                mask = (mask << 8 * length) - 1;955                expect_val &= mask;956                actual_val &= mask;957            }958            UnityTestResultsFailBegin(lineNumber);959            UnityPrint(UnityStrElement);960            UnityPrintNumberUnsigned(num_elements - elements - 1);961            UnityPrint(UnityStrExpected);962            UnityPrintIntNumberByStyle(expect_val, style);963            UnityPrint(UnityStrWas);964            UnityPrintIntNumberByStyle(actual_val, style);965            UnityAddMsgIfSpecified(msg);966            UNITY_FAIL_AND_BAIL;967        }968        /* Walk through array by incrementing the pointers */969        if (flags == UNITY_ARRAY_TO_ARRAY)970        {971            expected = (UNITY_INTERNAL_PTR)((const char*)expected + increment);972        }973        actual = (UNITY_INTERNAL_PTR)((const char*)actual + increment);974    }975}976 977/*-----------------------------------------------*/978#ifndef UNITY_EXCLUDE_FLOAT979/* Wrap this define in a function with variable types as float or double */980#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff)                           \981    if (UNITY_IS_INF(expected) && UNITY_IS_INF(actual) && (((expected) < 0) == ((actual) < 0))) return 1;   \982    if (UNITY_NAN_CHECK) return 1;                                                            \983    (diff) = (actual) - (expected);                                                           \984    if ((diff) < 0) (diff) = -(diff);                                                         \985    if ((delta) < 0) (delta) = -(delta);                                                      \986    return !(UNITY_IS_NAN(diff) || UNITY_IS_INF(diff) || ((diff) > (delta)))987    /* This first part of this condition will catch any NaN or Infinite values */988#ifndef UNITY_NAN_NOT_EQUAL_NAN989  #define UNITY_NAN_CHECK UNITY_IS_NAN(expected) && UNITY_IS_NAN(actual)990#else991  #define UNITY_NAN_CHECK 0992#endif993 994#ifndef UNITY_EXCLUDE_FLOAT_PRINT995  #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \996  do {                                                            \997    UnityPrint(UnityStrExpected);                                 \998    UnityPrintFloat(expected);                                    \999    UnityPrint(UnityStrWas);                                      \1000    UnityPrintFloat(actual);                                      \1001  } while (0)1002#else1003  #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \1004    UnityPrint(UnityStrDelta)1005#endif /* UNITY_EXCLUDE_FLOAT_PRINT */1006 1007/*-----------------------------------------------*/1008static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)1009{1010    UNITY_FLOAT diff;1011    UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);1012}1013 1014/*-----------------------------------------------*/1015void UnityAssertWithinFloatArray(const UNITY_FLOAT delta,1016                                 UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,1017                                 UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,1018                                 const UNITY_UINT32 num_elements,1019                                 const char* msg,1020                                 const UNITY_LINE_TYPE lineNumber,1021                                 const UNITY_FLAGS_T flags)1022{1023    UNITY_UINT32 elements = num_elements;1024    UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_expected = expected;1025    UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_actual = actual;1026    UNITY_FLOAT in_delta = delta;1027    UNITY_FLOAT current_element_delta = delta;1028 1029    RETURN_IF_FAIL_OR_IGNORE;1030 1031    if (elements == 0)1032    {1033#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY1034        UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg);1035#else1036        UnityPrintPointlessAndBail();1037#endif1038    }1039 1040    if (UNITY_IS_INF(in_delta))1041    {1042        return; /* Arrays will be force equal with infinite delta */1043    }1044 1045    if (UNITY_IS_NAN(in_delta))1046    {1047        /* Delta must be correct number */1048        UnityPrintPointlessAndBail();1049    }1050 1051    if (expected == actual)1052    {1053        return; /* Both are NULL or same pointer */1054    }1055 1056    if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg))1057    {1058        UNITY_FAIL_AND_BAIL;1059    }1060 1061    /* fix delta sign if need */1062    if (in_delta < 0)1063    {1064        in_delta = -in_delta;1065    }1066 1067    while (elements--)1068    {1069        current_element_delta = *ptr_expected * UNITY_FLOAT_PRECISION;1070 1071        if (current_element_delta < 0)1072        {1073            /* fix delta sign for correct calculations */1074            current_element_delta = -current_element_delta;1075        }1076 1077        if (!UnityFloatsWithin(in_delta + current_element_delta, *ptr_expected, *ptr_actual))1078        {1079            UnityTestResultsFailBegin(lineNumber);1080            UnityPrint(UnityStrElement);1081            UnityPrintNumberUnsigned(num_elements - elements - 1);1082            UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)*ptr_expected, (UNITY_DOUBLE)*ptr_actual);1083            UnityAddMsgIfSpecified(msg);1084            UNITY_FAIL_AND_BAIL;1085        }1086        if (flags == UNITY_ARRAY_TO_ARRAY)1087        {1088            ptr_expected++;1089        }1090        ptr_actual++;1091    }1092}1093 1094/*-----------------------------------------------*/1095void UnityAssertFloatsWithin(const UNITY_FLOAT delta,1096                             const UNITY_FLOAT expected,1097                             const UNITY_FLOAT actual,1098                             const char* msg,1099                             const UNITY_LINE_TYPE lineNumber)1100{1101    RETURN_IF_FAIL_OR_IGNORE;1102 1103 1104    if (!UnityFloatsWithin(delta, expected, actual))1105    {1106        UnityTestResultsFailBegin(lineNumber);1107        UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual);1108        UnityAddMsgIfSpecified(msg);1109        UNITY_FAIL_AND_BAIL;1110    }1111}1112 1113#ifndef   UNITY_EXCLUDE_FLOAT_PRINT1114/*-----------------------------------------------*/1115void UnityAssertFloatsNotWithin(const UNITY_FLOAT delta,1116                                const UNITY_FLOAT expected,1117                                const UNITY_FLOAT actual,1118                                const char* msg,1119                                const UNITY_LINE_TYPE lineNumber)1120{1121    RETURN_IF_FAIL_OR_IGNORE;1122 1123    if (UnityFloatsWithin(delta, expected, actual))1124    {1125        UnityTestResultsFailBegin(lineNumber);1126        UnityPrint(UnityStrExpected);1127        UnityPrintFloat((UNITY_DOUBLE)expected);1128        UnityPrint(UnityStrNotEqual);1129        UnityPrintFloat((UNITY_DOUBLE)actual);1130        UnityAddMsgIfSpecified(msg);1131        UNITY_FAIL_AND_BAIL;1132    }1133}1134 1135/*-----------------------------------------------*/1136void UnityAssertGreaterOrLessFloat(const UNITY_FLOAT threshold,1137                                   const UNITY_FLOAT actual,1138                                   const UNITY_COMPARISON_T compare,1139                                   const char* msg,1140                                   const UNITY_LINE_TYPE lineNumber)1141{1142    int failed;1143 1144    RETURN_IF_FAIL_OR_IGNORE;1145 1146    failed = 0;1147 1148    /* Checking for "not success" rather than failure to get the right result for NaN */1149    if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }1150    if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }1151 1152    if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_FLOAT_PRECISION, threshold, actual)) { failed = 0; }1153 1154    if (failed)1155    {1156        UnityTestResultsFailBegin(lineNumber);1157        UnityPrint(UnityStrExpected);1158        UnityPrintFloat(actual);1159        if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }1160        if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }1161        if (compare & UNITY_EQUAL_TO)     { UnityPrint(UnityStrOrEqual);  }1162        UnityPrintFloat(threshold);1163        UnityAddMsgIfSpecified(msg);1164        UNITY_FAIL_AND_BAIL;1165    }1166}1167#endif /* ! UNITY_EXCLUDE_FLOAT_PRINT */1168 1169/*-----------------------------------------------*/1170void UnityAssertFloatSpecial(const UNITY_FLOAT actual,1171                             const char* msg,1172                             const UNITY_LINE_TYPE lineNumber,1173                             const UNITY_FLOAT_TRAIT_T style)1174{1175    const char* trait_names[] = {UnityStrInf, UnityStrNegInf, UnityStrNaN, UnityStrDet};1176    UNITY_INT should_be_trait = ((UNITY_INT)style & 1);1177    UNITY_INT is_trait        = !should_be_trait;1178    UNITY_INT trait_index     = (UNITY_INT)(style >> 1);1179 1180    RETURN_IF_FAIL_OR_IGNORE;1181 1182    switch (style)1183    {1184        case UNITY_FLOAT_IS_INF:1185        case UNITY_FLOAT_IS_NOT_INF:1186            is_trait = UNITY_IS_INF(actual) && (actual > 0);1187            break;1188        case UNITY_FLOAT_IS_NEG_INF:1189        case UNITY_FLOAT_IS_NOT_NEG_INF:1190            is_trait = UNITY_IS_INF(actual) && (actual < 0);1191            break;1192 1193        case UNITY_FLOAT_IS_NAN:1194        case UNITY_FLOAT_IS_NOT_NAN:1195            is_trait = UNITY_IS_NAN(actual) ? 1 : 0;1196            break;1197 1198        case UNITY_FLOAT_IS_DET: /* A determinate number is non infinite and not NaN. */1199        case UNITY_FLOAT_IS_NOT_DET:1200            is_trait = !UNITY_IS_INF(actual) && !UNITY_IS_NAN(actual);

Showing the first 1,200 of 2627 lines. Download the file for the rest.