135375f98Sopenharmony_ci/* ==========================================
235375f98Sopenharmony_ci    Unity Project - A Test Framework for C
335375f98Sopenharmony_ci    Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
435375f98Sopenharmony_ci    [Released under MIT License. Please refer to license.txt for details]
535375f98Sopenharmony_ci========================================== */
635375f98Sopenharmony_ci
735375f98Sopenharmony_ci#include <setjmp.h>
835375f98Sopenharmony_ci#include <stdio.h>
935375f98Sopenharmony_ci#include "unity.h"
1035375f98Sopenharmony_ci#include "types_for_test.h"
1135375f98Sopenharmony_ci
1235375f98Sopenharmony_ci/* Include Passthroughs for Linking Tests */
1335375f98Sopenharmony_civoid putcharSpy(int c) { (void)putchar(c);}
1435375f98Sopenharmony_civoid flushSpy(void) {}
1535375f98Sopenharmony_ci
1635375f98Sopenharmony_ci#define EXPECT_ABORT_BEGIN \
1735375f98Sopenharmony_ci    if (TEST_PROTECT())    \
1835375f98Sopenharmony_ci    {
1935375f98Sopenharmony_ci
2035375f98Sopenharmony_ci#define VERIFY_FAILS_END                                                       \
2135375f98Sopenharmony_ci    }                                                                          \
2235375f98Sopenharmony_ci    Unity.CurrentTestFailed = (Unity.CurrentTestFailed != 0) ? 0 : 1;          \
2335375f98Sopenharmony_ci    if (Unity.CurrentTestFailed == 1) {                                        \
2435375f98Sopenharmony_ci      SetToOneMeanWeAlreadyCheckedThisGuy = 1;                                 \
2535375f98Sopenharmony_ci      UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber);                   \
2635375f98Sopenharmony_ci      UNITY_OUTPUT_CHAR(':');                                                  \
2735375f98Sopenharmony_ci      UnityPrint(Unity.CurrentTestName);                                       \
2835375f98Sopenharmony_ci      UnityPrint(":FAIL: [[[[ Test Should Have Failed But Did Not ]]]]");      \
2935375f98Sopenharmony_ci      UNITY_OUTPUT_CHAR('\n');                                                 \
3035375f98Sopenharmony_ci    }
3135375f98Sopenharmony_ci
3235375f98Sopenharmony_ci#define VERIFY_IGNORES_END                                                     \
3335375f98Sopenharmony_ci    }                                                                          \
3435375f98Sopenharmony_ci    Unity.CurrentTestFailed = (Unity.CurrentTestIgnored != 0) ? 0 : 1;         \
3535375f98Sopenharmony_ci    Unity.CurrentTestIgnored = 0;                                              \
3635375f98Sopenharmony_ci    if (Unity.CurrentTestFailed == 1) {                                        \
3735375f98Sopenharmony_ci      SetToOneMeanWeAlreadyCheckedThisGuy = 1;                                 \
3835375f98Sopenharmony_ci      UnityPrintNumberUnsigned(Unity.CurrentTestLineNumber);                   \
3935375f98Sopenharmony_ci      UNITY_OUTPUT_CHAR(':');                                                  \
4035375f98Sopenharmony_ci      UnityPrint(Unity.CurrentTestName);                                       \
4135375f98Sopenharmony_ci      UnityPrint(":FAIL: [[[[ Test Should Have Ignored But Did Not ]]]]");     \
4235375f98Sopenharmony_ci      UNITY_OUTPUT_CHAR('\n');                                                 \
4335375f98Sopenharmony_ci    }
4435375f98Sopenharmony_ci
4535375f98Sopenharmony_cistatic int SetToOneToFailInTearDown;
4635375f98Sopenharmony_cistatic int SetToOneMeanWeAlreadyCheckedThisGuy;
4735375f98Sopenharmony_cistatic unsigned NextExpectedStringIndex;
4835375f98Sopenharmony_cistatic unsigned NextExpectedCharIndex;
4935375f98Sopenharmony_cistatic unsigned NextExpectedSpaceIndex;
5035375f98Sopenharmony_ci
5135375f98Sopenharmony_civoid suiteSetUp(void)
5235375f98Sopenharmony_ci{
5335375f98Sopenharmony_ci    NextExpectedStringIndex = 0;
5435375f98Sopenharmony_ci    NextExpectedCharIndex = 0;
5535375f98Sopenharmony_ci    NextExpectedSpaceIndex = 0;
5635375f98Sopenharmony_ci}
5735375f98Sopenharmony_ci
5835375f98Sopenharmony_civoid setUp(void)
5935375f98Sopenharmony_ci{
6035375f98Sopenharmony_ci  SetToOneToFailInTearDown = 0;
6135375f98Sopenharmony_ci  SetToOneMeanWeAlreadyCheckedThisGuy = 0;
6235375f98Sopenharmony_ci}
6335375f98Sopenharmony_ci
6435375f98Sopenharmony_civoid tearDown(void)
6535375f98Sopenharmony_ci{
6635375f98Sopenharmony_ci  if (SetToOneToFailInTearDown == 1)
6735375f98Sopenharmony_ci    TEST_FAIL_MESSAGE("<= Failed in tearDown");
6835375f98Sopenharmony_ci  if ((SetToOneMeanWeAlreadyCheckedThisGuy == 0) && (Unity.CurrentTestFailed > 0))
6935375f98Sopenharmony_ci  {
7035375f98Sopenharmony_ci    UnityPrint(": [[[[ Test Should Have Passed But Did Not ]]]]");
7135375f98Sopenharmony_ci    UNITY_OUTPUT_CHAR('\n');
7235375f98Sopenharmony_ci  }
7335375f98Sopenharmony_ci}
7435375f98Sopenharmony_ci
7535375f98Sopenharmony_ciTEST_CASE(0)
7635375f98Sopenharmony_ciTEST_CASE(44)
7735375f98Sopenharmony_ciTEST_CASE((90)+9)
7835375f98Sopenharmony_civoid test_TheseShouldAllPass(int Num)
7935375f98Sopenharmony_ci{
8035375f98Sopenharmony_ci    TEST_ASSERT_TRUE(Num < 100);
8135375f98Sopenharmony_ci}
8235375f98Sopenharmony_ci
8335375f98Sopenharmony_ciTEST_CASE(3)
8435375f98Sopenharmony_ciTEST_CASE(77)
8535375f98Sopenharmony_ciTEST_CASE( (99) + 1 - (1))
8635375f98Sopenharmony_civoid test_TheseShouldAllFail(int Num)
8735375f98Sopenharmony_ci{
8835375f98Sopenharmony_ci    EXPECT_ABORT_BEGIN
8935375f98Sopenharmony_ci    TEST_ASSERT_TRUE(Num > 100);
9035375f98Sopenharmony_ci    VERIFY_FAILS_END
9135375f98Sopenharmony_ci}
9235375f98Sopenharmony_ci
9335375f98Sopenharmony_ciTEST_CASE(1)
9435375f98Sopenharmony_ciTEST_CASE(44)
9535375f98Sopenharmony_ciTEST_CASE(99)
9635375f98Sopenharmony_ciTEST_CASE(98)
9735375f98Sopenharmony_civoid test_TheseAreEveryOther(int Num)
9835375f98Sopenharmony_ci{
9935375f98Sopenharmony_ci    if (Num & 1)
10035375f98Sopenharmony_ci    {
10135375f98Sopenharmony_ci        EXPECT_ABORT_BEGIN
10235375f98Sopenharmony_ci        TEST_ASSERT_TRUE(Num > 100);
10335375f98Sopenharmony_ci        VERIFY_FAILS_END
10435375f98Sopenharmony_ci    }
10535375f98Sopenharmony_ci    else
10635375f98Sopenharmony_ci    {
10735375f98Sopenharmony_ci        TEST_ASSERT_TRUE(Num < 100);
10835375f98Sopenharmony_ci    }
10935375f98Sopenharmony_ci}
11035375f98Sopenharmony_ci
11135375f98Sopenharmony_civoid test_NormalPassesStillWork(void)
11235375f98Sopenharmony_ci{
11335375f98Sopenharmony_ci    TEST_ASSERT_TRUE(1);
11435375f98Sopenharmony_ci}
11535375f98Sopenharmony_ci
11635375f98Sopenharmony_civoid test_NormalFailsStillWork(void)
11735375f98Sopenharmony_ci{
11835375f98Sopenharmony_ci    EXPECT_ABORT_BEGIN
11935375f98Sopenharmony_ci    TEST_ASSERT_TRUE(0);
12035375f98Sopenharmony_ci    VERIFY_FAILS_END
12135375f98Sopenharmony_ci}
12235375f98Sopenharmony_ci
12335375f98Sopenharmony_ciTEST_CASE(0, "abc")
12435375f98Sopenharmony_ciTEST_CASE(1, "{")
12535375f98Sopenharmony_ciTEST_CASE(2, "}")
12635375f98Sopenharmony_ciTEST_CASE(3, ";")
12735375f98Sopenharmony_ciTEST_CASE(4, "\"quoted\"")
12835375f98Sopenharmony_civoid test_StringsArePreserved(unsigned index, const char * str)
12935375f98Sopenharmony_ci{
13035375f98Sopenharmony_ci    static const char * const expected[] =
13135375f98Sopenharmony_ci    {
13235375f98Sopenharmony_ci        "abc",
13335375f98Sopenharmony_ci        "{",
13435375f98Sopenharmony_ci        "}",
13535375f98Sopenharmony_ci        ";",
13635375f98Sopenharmony_ci        "\"quoted\""
13735375f98Sopenharmony_ci    };
13835375f98Sopenharmony_ci
13935375f98Sopenharmony_ci    /* Ensure that no test cases are skipped by tracking the next expected index */
14035375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_UINT32(NextExpectedStringIndex, index);
14135375f98Sopenharmony_ci    TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index);
14235375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_STRING(expected[index], str);
14335375f98Sopenharmony_ci
14435375f98Sopenharmony_ci    NextExpectedStringIndex++;
14535375f98Sopenharmony_ci}
14635375f98Sopenharmony_ci
14735375f98Sopenharmony_ciTEST_CASE(0, 'x')
14835375f98Sopenharmony_ciTEST_CASE(1, '{')
14935375f98Sopenharmony_ciTEST_CASE(2, '}')
15035375f98Sopenharmony_ciTEST_CASE(3, ';')
15135375f98Sopenharmony_ciTEST_CASE(4, '\'')
15235375f98Sopenharmony_ciTEST_CASE(5, '"')
15335375f98Sopenharmony_civoid test_CharsArePreserved(unsigned index, char c)
15435375f98Sopenharmony_ci{
15535375f98Sopenharmony_ci    static const char expected[] =
15635375f98Sopenharmony_ci    {
15735375f98Sopenharmony_ci        'x',
15835375f98Sopenharmony_ci        '{',
15935375f98Sopenharmony_ci        '}',
16035375f98Sopenharmony_ci        ';',
16135375f98Sopenharmony_ci        '\'',
16235375f98Sopenharmony_ci        '"'
16335375f98Sopenharmony_ci    };
16435375f98Sopenharmony_ci
16535375f98Sopenharmony_ci    /* Ensure that no test cases are skipped by tracking the next expected index */
16635375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_UINT32(NextExpectedCharIndex, index);
16735375f98Sopenharmony_ci    TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index);
16835375f98Sopenharmony_ci    TEST_ASSERT_EQUAL(expected[index], c);
16935375f98Sopenharmony_ci
17035375f98Sopenharmony_ci    NextExpectedCharIndex++;
17135375f98Sopenharmony_ci}
17235375f98Sopenharmony_ci
17335375f98Sopenharmony_ciTEST_RANGE([0, 10, 2])
17435375f98Sopenharmony_civoid test_SingleRange(unsigned value)
17535375f98Sopenharmony_ci{
17635375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, value % 2);
17735375f98Sopenharmony_ci  TEST_ASSERT_LESS_OR_EQUAL(10, value);
17835375f98Sopenharmony_ci}
17935375f98Sopenharmony_ci
18035375f98Sopenharmony_ciTEST_RANGE([1, 2, 1], [2, 1, -1])
18135375f98Sopenharmony_civoid test_TwoRanges(unsigned first, unsigned second)
18235375f98Sopenharmony_ci{
18335375f98Sopenharmony_ci  TEST_ASSERT_LESS_OR_EQUAL(4, first * second);
18435375f98Sopenharmony_ci}
18535375f98Sopenharmony_ci
18635375f98Sopenharmony_ciTEST_RANGE(<0, 10, 2>)
18735375f98Sopenharmony_civoid test_SingleExclusiveRange(unsigned value)
18835375f98Sopenharmony_ci{
18935375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, value % 2);
19035375f98Sopenharmony_ci  TEST_ASSERT_LESS_THAN(10, value);
19135375f98Sopenharmony_ci}
19235375f98Sopenharmony_ci
19335375f98Sopenharmony_ciTEST_RANGE([2, 4, 1], <1, 2, 1>)
19435375f98Sopenharmony_civoid test_BothInclusiveAndExclusiveRange(unsigned first, unsigned second)
19535375f98Sopenharmony_ci{
19635375f98Sopenharmony_ci  TEST_ASSERT_LESS_THAN(first, second);
19735375f98Sopenharmony_ci}
19835375f98Sopenharmony_ci
19935375f98Sopenharmony_ciTEST_CASE(0,
20035375f98Sopenharmony_ci
20135375f98Sopenharmony_ci          1)
20235375f98Sopenharmony_ciTEST_CASE(1,
20335375f98Sopenharmony_ci
20435375f98Sopenharmony_ci          2
20535375f98Sopenharmony_ci
20635375f98Sopenharmony_ci  )
20735375f98Sopenharmony_ciTEST_RANGE([2,
20835375f98Sopenharmony_ci            5  ,
20935375f98Sopenharmony_ci            1], [6, 6, 1])
21035375f98Sopenharmony_ciTEST_CASE(
21135375f98Sopenharmony_ci
21235375f98Sopenharmony_ci  6 , 7)
21335375f98Sopenharmony_ciTEST_MATRIX([7,
21435375f98Sopenharmony_ci             8   ,
21535375f98Sopenharmony_ci
21635375f98Sopenharmony_ci             9, 10],
21735375f98Sopenharmony_ci    [
21835375f98Sopenharmony_ci      11]
21935375f98Sopenharmony_ci
22035375f98Sopenharmony_ci      )
22135375f98Sopenharmony_civoid test_SpaceInTestCase(unsigned index, unsigned bigger)
22235375f98Sopenharmony_ci{
22335375f98Sopenharmony_ci  TEST_ASSERT_EQUAL_UINT32(NextExpectedSpaceIndex, index);
22435375f98Sopenharmony_ci  TEST_ASSERT_LESS_THAN(bigger, index);
22535375f98Sopenharmony_ci
22635375f98Sopenharmony_ci  NextExpectedSpaceIndex++;
22735375f98Sopenharmony_ci}
22835375f98Sopenharmony_ci
22935375f98Sopenharmony_ciTEST_MATRIX([1, 5, (2*2)+1, 4])
23035375f98Sopenharmony_civoid test_SingleMatix(unsigned value)
23135375f98Sopenharmony_ci{
23235375f98Sopenharmony_ci  TEST_ASSERT_LESS_OR_EQUAL(10, value);
23335375f98Sopenharmony_ci}
23435375f98Sopenharmony_ci
23535375f98Sopenharmony_ciTEST_MATRIX([2, 5l, 4u+3, 4ul], [-2, 3])
23635375f98Sopenharmony_civoid test_TwoMatrices(unsigned first, signed second)
23735375f98Sopenharmony_ci{
23835375f98Sopenharmony_ci  static unsigned idx = 0;
23935375f98Sopenharmony_ci  static const unsigned expected[] =
24035375f98Sopenharmony_ci  {
24135375f98Sopenharmony_ci  // -2   3
24235375f98Sopenharmony_ci     -4,  6, // 2
24335375f98Sopenharmony_ci    -10, 15, // 5
24435375f98Sopenharmony_ci    -14, 21, // 7
24535375f98Sopenharmony_ci     -8, 12, // 4
24635375f98Sopenharmony_ci  };
24735375f98Sopenharmony_ci  TEST_ASSERT_EQUAL_INT(expected[idx++], first * second);
24835375f98Sopenharmony_ci}
24935375f98Sopenharmony_ci
25035375f98Sopenharmony_ciTEST_MATRIX(["String1", "String,2", "Stri" "ng3", "String[4]", "String\"5\""], [-5, 12.5f])
25135375f98Sopenharmony_civoid test_StringsAndNumbersMatrices(const char* str, float number)
25235375f98Sopenharmony_ci{
25335375f98Sopenharmony_ci  static unsigned idx = 0;
25435375f98Sopenharmony_ci  static const char* expected[] =
25535375f98Sopenharmony_ci  {
25635375f98Sopenharmony_ci    "String1_-05.00",
25735375f98Sopenharmony_ci    "String1_+12.50",
25835375f98Sopenharmony_ci    "String,2_-05.00",
25935375f98Sopenharmony_ci    "String,2_+12.50",
26035375f98Sopenharmony_ci    "String3_-05.00",
26135375f98Sopenharmony_ci    "String3_+12.50",
26235375f98Sopenharmony_ci    "String[4]_-05.00",
26335375f98Sopenharmony_ci    "String[4]_+12.50",
26435375f98Sopenharmony_ci    "String\"5\"_-05.00",
26535375f98Sopenharmony_ci    "String\"5\"_+12.50",
26635375f98Sopenharmony_ci  };
26735375f98Sopenharmony_ci  char buf[200] = {0};
26835375f98Sopenharmony_ci  snprintf(buf, sizeof(buf), "%s_%+06.2f", str, number);
26935375f98Sopenharmony_ci  TEST_ASSERT_EQUAL_STRING(expected[idx++], buf);
27035375f98Sopenharmony_ci}
27135375f98Sopenharmony_ci
27235375f98Sopenharmony_ciTEST_MATRIX(
27335375f98Sopenharmony_ci  [ENUM_A, ENUM_4, ENUM_C],
27435375f98Sopenharmony_ci  [test_arr[0], 7.8f, test_arr[2]],
27535375f98Sopenharmony_ci  ['a', 'f', '[', ']', '\'', '"'],
27635375f98Sopenharmony_ci)
27735375f98Sopenharmony_civoid test_EnumCharAndArrayMatrices(test_enum_t e, float n, char c)
27835375f98Sopenharmony_ci{
27935375f98Sopenharmony_ci  static unsigned enum_idx = 0;
28035375f98Sopenharmony_ci  static const test_enum_t exp_enum[3] = {
28135375f98Sopenharmony_ci    ENUM_A, ENUM_4, ENUM_C,
28235375f98Sopenharmony_ci  };
28335375f98Sopenharmony_ci
28435375f98Sopenharmony_ci  static unsigned float_idx = 0;
28535375f98Sopenharmony_ci  float exp_float[3] = {0};
28635375f98Sopenharmony_ci  exp_float[0] = test_arr[0];
28735375f98Sopenharmony_ci  exp_float[1] = 7.8f;
28835375f98Sopenharmony_ci  exp_float[2] = test_arr[2];
28935375f98Sopenharmony_ci
29035375f98Sopenharmony_ci  static unsigned char_idx = 0;
29135375f98Sopenharmony_ci  static const test_enum_t exp_char[] = {
29235375f98Sopenharmony_ci    'a', 'f', '[', ']', '\'', '"'
29335375f98Sopenharmony_ci  };
29435375f98Sopenharmony_ci
29535375f98Sopenharmony_ci  TEST_ASSERT_EQUAL_INT(exp_enum[enum_idx], e);
29635375f98Sopenharmony_ci  TEST_ASSERT_EQUAL_FLOAT(exp_float[float_idx], n);
29735375f98Sopenharmony_ci  TEST_ASSERT_EQUAL_CHAR(exp_char[char_idx], c);
29835375f98Sopenharmony_ci
29935375f98Sopenharmony_ci  char_idx = (char_idx + 1) % 6;
30035375f98Sopenharmony_ci  if (char_idx == 0.0f)
30135375f98Sopenharmony_ci  {
30235375f98Sopenharmony_ci    float_idx = (float_idx + 1) % 3;
30335375f98Sopenharmony_ci    if (float_idx == 0.0f)
30435375f98Sopenharmony_ci    {
30535375f98Sopenharmony_ci      enum_idx = (enum_idx + 1) % 3;
30635375f98Sopenharmony_ci    }
30735375f98Sopenharmony_ci  }
30835375f98Sopenharmony_ci}
309