LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/subsys/testsuite/ztest/src - ztest.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 60.8 % 383 233
Test Date: 2026-11-06 14:43:19 Functions: 57.9 % 38 22
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 44.1 % 170 75

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2016 Intel Corporation
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : #include <stdlib.h>
       8                 :             : #include <zephyr/ztest.h>
       9                 :             : 
      10                 :             : #include <zephyr/app_memory/app_memdomain.h>
      11                 :             : #ifdef CONFIG_USERSPACE
      12                 :             : #include <zephyr/sys/libc-hooks.h>
      13                 :             : #endif
      14                 :             : #include <zephyr/logging/log_ctrl.h>
      15                 :             : #include <zephyr/sys/reboot.h>
      16                 :             : 
      17                 :             : #include <zephyr/llext/symbol.h>
      18                 :             : 
      19                 :             : #include <zephyr/sys/barrier.h>
      20                 :             : 
      21                 :             : #ifndef ZTEST_UNITTEST
      22                 :             : static struct k_thread ztest_thread;
      23                 :             : #endif
      24                 :             : static bool failed_expectation;
      25                 :             : 
      26                 :             : #ifdef CONFIG_ZTEST_SHELL
      27                 :             : #include <zephyr/shell/shell.h>
      28                 :             : #endif
      29                 :             : 
      30                 :             : #ifdef CONFIG_ZTEST_SHUFFLE
      31                 :             : #include <time.h>
      32                 :             : #include <zephyr/random/random.h>
      33                 :             : #endif /* CONFIG_ZTEST_SHUFFLE */
      34                 :             : 
      35                 :             : #ifdef CONFIG_ZTEST_REPEAT
      36                 :             : #define NUM_ITER_PER_SUITE CONFIG_ZTEST_SUITE_REPEAT_COUNT
      37                 :             : #define NUM_ITER_PER_TEST  CONFIG_ZTEST_TEST_REPEAT_COUNT
      38                 :             : #else
      39                 :             : #define NUM_ITER_PER_SUITE 1
      40                 :             : #define NUM_ITER_PER_TEST  1
      41                 :             : #endif
      42                 :             : 
      43                 :             : #ifdef CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS
      44                 :             : #include <coverage.h>
      45                 :             : #endif
      46                 :             : 
      47                 :             : /* ZTEST_DMEM and ZTEST_BMEM are used for the application shared memory test  */
      48                 :             : 
      49                 :             : /**
      50                 :             :  * @brief The current status of the test binary
      51                 :             :  */
      52                 :             : enum ztest_status {
      53                 :             :         ZTEST_STATUS_OK,
      54                 :             :         ZTEST_STATUS_HAS_FAILURE,
      55                 :             :         ZTEST_STATUS_CRITICAL_ERROR
      56                 :             : };
      57                 :             : 
      58                 :             : /**
      59                 :             :  * @brief Tracks the current phase that ztest is operating in.
      60                 :             :  */
      61                 :             : static ZTEST_DMEM enum ztest_phase cur_phase = TEST_PHASE_FRAMEWORK;
      62                 :             : 
      63                 :             : static ZTEST_BMEM enum ztest_status test_status = ZTEST_STATUS_OK;
      64                 :             : 
      65                 :             : extern ZTEST_DMEM const struct ztest_arch_api ztest_api;
      66                 :             : 
      67                 :             : /*
      68                 :             :  * Per-invocation state for value-parameterized tests (ZTEST_P).
      69                 :             :  * Set by z_ztest_run_test_dispatch() before each per-value call; cleared
      70                 :             :  * immediately after.  Never affects the fixture pointer passed via data.
      71                 :             :  */
      72                 :             : struct z_ztest_param_ctx {
      73                 :             :         const void *value;
      74                 :             :         size_t index;
      75                 :             :         size_t elem_size;
      76                 :             :         bool active;
      77                 :             :         /* Formatted display name used by TC_START / Z_TC_END_RESULT */
      78                 :             :         char display_name[128];
      79                 :             :         /*
      80                 :             :          * Aligned scratch buffer for range-generated values.
      81                 :             :          * The value_at_cb writes here; 'value' is then pointed at this storage.
      82                 :             :          * Two uint64_t words (16 bytes) cover every scalar type up to __int128.
      83                 :             :          */
      84                 :             :         uint64_t value_buf[2];
      85                 :             : };
      86                 :             : 
      87                 :             : static ZTEST_BMEM struct z_ztest_param_ctx z_ztest_param_state;
      88                 :             : 
      89                 :             : static void __ztest_show_suite_summary(void);
      90                 :             : 
      91                 :           1 : static void end_report(void)
      92                 :             : {
      93                 :           1 :         __ztest_show_suite_summary();
      94         [ -  + ]:           1 :         if (test_status != ZTEST_STATUS_OK) {
      95                 :           0 :                 TC_END_REPORT(TC_FAIL);
      96                 :             :         } else {
      97                 :           1 :                 TC_END_REPORT(TC_PASS);
      98                 :             :         }
      99                 :             : }
     100                 :             : 
     101                 :          50 : static int cleanup_test(struct ztest_unit_test *test)
     102                 :             : {
     103                 :          50 :         int ret = TC_PASS;
     104                 :          50 :         int mock_status;
     105                 :             : 
     106                 :          50 :         mock_status = z_cleanup_mock();
     107                 :             : 
     108                 :             : #ifndef ZTEST_UNITTEST
     109                 :             :         /* we need to remove the ztest_thread information from the timeout_q.
     110                 :             :          * Because we reuse the same k_thread structure this would
     111                 :             :          * causes some problems.
     112                 :             :          */
     113                 :          50 :         if (IS_ENABLED(CONFIG_MULTITHREADING)) {
     114                 :          50 :                 k_thread_abort(&ztest_thread);
     115                 :             :         }
     116                 :             : #endif
     117                 :             : 
     118                 :          50 :         if (!ret && mock_status == 1) {
     119                 :             :                 PRINT_DATA("Test %s failed: Unused mock parameter values\n", test->name);
     120                 :             :                 ret = TC_FAIL;
     121                 :          50 :         } else if (!ret && mock_status == 2) {
     122                 :             :                 PRINT_DATA("Test %s failed: Unused mock return values\n", test->name);
     123                 :             :                 ret = TC_FAIL;
     124                 :             :         } else {
     125                 :             :                 /* No mock failures to report. */
     126                 :          50 :         }
     127                 :             : 
     128                 :          50 :         return ret;
     129                 :             : }
     130                 :             : 
     131                 :             : #ifndef ZTEST_UNITTEST
     132                 :             : 
     133                 :             : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
     134                 :             : #define MAX_NUM_CPUHOLD  (CONFIG_MP_MAX_NUM_CPUS - 1)
     135                 :             : #define CPUHOLD_STACK_SZ (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
     136                 :             : 
     137                 :             : struct cpuhold_pool_item {
     138                 :             :         struct k_thread thread;
     139                 :             :         bool used;
     140                 :             : };
     141                 :             : 
     142                 :             : static struct cpuhold_pool_item cpuhold_pool_items[MAX_NUM_CPUHOLD + 1];
     143                 :             : 
     144                 :             : K_KERNEL_STACK_ARRAY_DEFINE(cpuhold_stacks, MAX_NUM_CPUHOLD + 1, CPUHOLD_STACK_SZ);
     145                 :             : 
     146                 :             : static struct k_sem cpuhold_sem;
     147                 :             : 
     148                 :             : volatile int cpuhold_active;
     149                 :             : volatile bool cpuhold_spawned;
     150                 :             : 
     151                 :             : static int find_unused_thread(void)
     152                 :             : {
     153                 :             :         for (unsigned int i = 0; i <= MAX_NUM_CPUHOLD; i++) {
     154                 :             :                 if (!cpuhold_pool_items[i].used) {
     155                 :             :                         return i;
     156                 :             :                 }
     157                 :             :         }
     158                 :             : 
     159                 :             :         return -1;
     160                 :             : }
     161                 :             : 
     162                 :             : static void mark_thread_unused(struct k_thread *thread)
     163                 :             : {
     164                 :             :         for (unsigned int i = 0; i <= MAX_NUM_CPUHOLD; i++) {
     165                 :             :                 if (&cpuhold_pool_items[i].thread == thread) {
     166                 :             :                         cpuhold_pool_items[i].used = false;
     167                 :             :                 }
     168                 :             :         }
     169                 :             : }
     170                 :             : 
     171                 :             : static inline void wait_for_thread_to_switch_out(struct k_thread *thread)
     172                 :             : {
     173                 :             :         unsigned int key = arch_irq_lock();
     174                 :             :         volatile void **shp = (void *)&thread->switch_handle;
     175                 :             : 
     176                 :             :         while (*shp == NULL) {
     177                 :             :                 arch_spin_relax();
     178                 :             :         }
     179                 :             :         /* Read barrier: don't allow any subsequent loads in the
     180                 :             :          * calling code to reorder before we saw switch_handle go
     181                 :             :          * non-null.
     182                 :             :          */
     183                 :             :         barrier_dmem_fence_full();
     184                 :             : 
     185                 :             :         arch_irq_unlock(key);
     186                 :             : }
     187                 :             : 
     188                 :             : /* "Holds" a CPU for use with the "1cpu" test cases.  Note that we
     189                 :             :  * can't use tools like the cpumask feature because we have tests that
     190                 :             :  * may need to control that configuration themselves.  We do this at
     191                 :             :  * the lowest level, but locking interrupts directly and spinning.
     192                 :             :  */
     193                 :             : static void cpu_hold(void *arg1, void *arg2, void *arg3)
     194                 :             : {
     195                 :             :         struct k_thread *thread = arg1;
     196                 :             :         unsigned int idx = (unsigned int)(uintptr_t)arg2;
     197                 :             :         char tname[CONFIG_THREAD_MAX_NAME_LEN];
     198                 :             : 
     199                 :             :         ARG_UNUSED(arg3);
     200                 :             : 
     201                 :             :         if (arch_proc_id() == 0) {
     202                 :             :                 int i;
     203                 :             : 
     204                 :             :                 i = find_unused_thread();
     205                 :             : 
     206                 :             :                 __ASSERT_NO_MSG(i != -1);
     207                 :             : 
     208                 :             :                 cpuhold_spawned = false;
     209                 :             : 
     210                 :             :                 cpuhold_pool_items[i].used = true;
     211                 :             :                 k_thread_create(&cpuhold_pool_items[i].thread, cpuhold_stacks[i], CPUHOLD_STACK_SZ,
     212                 :             :                                 cpu_hold, k_current_get(), (void *)(uintptr_t)idx, NULL,
     213                 :             :                                 K_HIGHEST_THREAD_PRIO, 0, K_NO_WAIT);
     214                 :             : 
     215                 :             :                 /*
     216                 :             :                  * Busy-wait until we know the spawned thread is running to
     217                 :             :                  * ensure it does not spawn on CPU0.
     218                 :             :                  */
     219                 :             : 
     220                 :             :                 while (!cpuhold_spawned) {
     221                 :             :                         k_busy_wait(1000);
     222                 :             :                 }
     223                 :             : 
     224                 :             :                 return;
     225                 :             :         }
     226                 :             : 
     227                 :             :         if (thread != NULL) {
     228                 :             :                 cpuhold_spawned = true;
     229                 :             : 
     230                 :             :                 /* Busywait until a new thread is scheduled in on CPU0 */
     231                 :             : 
     232                 :             :                 wait_for_thread_to_switch_out(thread);
     233                 :             : 
     234                 :             :                 mark_thread_unused(thread);
     235                 :             :         }
     236                 :             : 
     237                 :             :         if (IS_ENABLED(CONFIG_THREAD_NAME)) {
     238                 :             :                 snprintk(tname, CONFIG_THREAD_MAX_NAME_LEN, "cpuhold%02d", idx);
     239                 :             :                 k_thread_name_set(k_current_get(), tname);
     240                 :             :         }
     241                 :             : 
     242                 :             :         uint32_t dt, start_ms = k_uptime_get_32();
     243                 :             :         unsigned int key = arch_irq_lock();
     244                 :             : 
     245                 :             :         k_sem_give(&cpuhold_sem);
     246                 :             : 
     247                 :             : #if (defined(CONFIG_ARM64) || defined(CONFIG_RISCV)) && defined(CONFIG_FPU_SHARING)
     248                 :             :         /*
     249                 :             :          * We'll be spinning with IRQs disabled. The flush-your-FPU request
     250                 :             :          * IPI will never be serviced during that time. Therefore we flush
     251                 :             :          * the FPU preemptively here to prevent any other CPU waiting after
     252                 :             :          * this CPU forever and deadlock the system.
     253                 :             :          */
     254                 :             :         k_float_disable(_current_cpu->arch.fpu_owner);
     255                 :             : #endif
     256                 :             : 
     257                 :             :         while (cpuhold_active) {
     258                 :             :                 k_busy_wait(1000);
     259                 :             :         }
     260                 :             : 
     261                 :             :         /* Holding the CPU via spinning is expensive, and abusing this
     262                 :             :          * for long-running test cases tends to overload the CI system
     263                 :             :          * (qemu runs separate CPUs in different threads, but the CI
     264                 :             :          * logic views it as one "job") and cause other test failures.
     265                 :             :          */
     266                 :             :         dt = k_uptime_get_32() - start_ms;
     267                 :             :         zassert_true(dt < CONFIG_ZTEST_CPU_HOLD_TIME_MS, "1cpu test took too long (%d ms)", dt);
     268                 :             :         arch_irq_unlock(key);
     269                 :             : }
     270                 :             : #endif /* CONFIG_SMP && (CONFIG_MP_MAX_NUM_CPUS > 1) */
     271                 :             : 
     272                 :           0 : void z_impl_z_test_1cpu_start(void)
     273                 :             : {
     274                 :             : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
     275                 :             :         unsigned int num_cpus = arch_num_cpus();
     276                 :             :         int j;
     277                 :             : 
     278                 :             :         cpuhold_active = 1;
     279                 :             : 
     280                 :             :         k_sem_init(&cpuhold_sem, 0, 999);
     281                 :             : 
     282                 :             :         /* Spawn N-1 threads to "hold" the other CPUs, waiting for
     283                 :             :          * each to signal us that it's locked and spinning.
     284                 :             :          */
     285                 :             :         for (int i = 0; i < num_cpus - 1; i++) {
     286                 :             :                 j = find_unused_thread();
     287                 :             : 
     288                 :             :                 __ASSERT_NO_MSG(j != -1);
     289                 :             : 
     290                 :             :                 cpuhold_pool_items[j].used = true;
     291                 :             :                 k_thread_create(&cpuhold_pool_items[j].thread, cpuhold_stacks[j], CPUHOLD_STACK_SZ,
     292                 :             :                                 cpu_hold, NULL, (void *)(uintptr_t)i, NULL, K_HIGHEST_THREAD_PRIO,
     293                 :             :                                 0, K_NO_WAIT);
     294                 :             :                 k_sem_take(&cpuhold_sem, K_FOREVER);
     295                 :             :         }
     296                 :             : #endif
     297                 :           0 : }
     298                 :             : 
     299                 :           0 : void z_impl_z_test_1cpu_stop(void)
     300                 :             : {
     301                 :             : #if defined(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
     302                 :             :         cpuhold_active = 0;
     303                 :             : 
     304                 :             :         for (int i = 0; i <= MAX_NUM_CPUHOLD; i++) {
     305                 :             :                 if (cpuhold_pool_items[i].used) {
     306                 :             :                         /* Must join the cpuhold threads before z_test_1cpu_start()
     307                 :             :                          * reuses the thread objects.
     308                 :             :                          */
     309                 :             :                         k_thread_join(&cpuhold_pool_items[i].thread, K_FOREVER);
     310                 :             :                         cpuhold_pool_items[i].used = false;
     311                 :             :                 }
     312                 :             :         }
     313                 :             : #endif
     314                 :           0 : }
     315                 :             : 
     316                 :             : #ifdef CONFIG_USERSPACE
     317                 :             : void z_vrfy_z_test_1cpu_start(void)
     318                 :             : {
     319                 :             :         z_impl_z_test_1cpu_start();
     320                 :             : }
     321                 :             : #include <zephyr/syscalls/z_test_1cpu_start_mrsh.c>
     322                 :             : 
     323                 :             : void z_vrfy_z_test_1cpu_stop(void)
     324                 :             : {
     325                 :             :         z_impl_z_test_1cpu_stop();
     326                 :             : }
     327                 :             : #include <zephyr/syscalls/z_test_1cpu_stop_mrsh.c>
     328                 :             : #endif /* CONFIG_USERSPACE */
     329                 :             : #endif
     330                 :             : 
     331                 :         100 : __maybe_unused static void run_test_rules(bool is_before, struct ztest_unit_test *test, void *data)
     332                 :             : {
     333                 :         100 :         for (struct ztest_test_rule *rule = _ztest_test_rule_list_start;
     334         [ +  + ]:         200 :              rule < _ztest_test_rule_list_end; ++rule) {
     335   [ +  +  -  + ]:         100 :                 if (is_before && rule->before_each) {
     336                 :           0 :                         rule->before_each(test, data);
     337         [ +  - ]:          50 :                 } else if (!is_before && rule->after_each) {
     338                 :          50 :                         rule->after_each(test, data);
     339                 :             :                 } else {
     340                 :             :                         /* No matching rule callback for this phase. */
     341                 :         100 :                 }
     342                 :             :         }
     343                 :         100 : }
     344                 :             : 
     345                 :          50 : static void run_test_functions(struct ztest_suite_node *suite, struct ztest_unit_test *test,
     346                 :             :                                void *data)
     347                 :             : {
     348                 :          50 :         ARG_UNUSED(suite);
     349                 :          50 :         __ztest_set_test_phase(TEST_PHASE_TEST);
     350                 :          50 :         test->test(data);
     351                 :          50 : }
     352                 :             : 
     353                 :             : COND_CODE_1(ZTEST_UNITTEST, (), (ZTEST_BMEM)) static enum ztest_result test_result;
     354                 :             : 
     355                 :          50 : static int get_final_test_result(const struct ztest_unit_test *test, int ret)
     356                 :             : {
     357                 :          50 :         enum ztest_expected_result expected_result = -1;
     358                 :             : 
     359                 :          50 :         for (struct ztest_expected_result_entry *expectation =
     360                 :             :                      _ztest_expected_result_entry_list_start;
     361         [ -  + ]:          50 :              expectation < _ztest_expected_result_entry_list_end; ++expectation) {
     362         [ #  # ]:           0 :                 if (strcmp(expectation->test_name, test->name) == 0 &&
     363         [ #  # ]:           0 :                     strcmp(expectation->test_suite_name, test->test_suite_name) == 0) {
     364                 :           0 :                         expected_result = expectation->expected_result;
     365                 :           0 :                         break;
     366                 :             :                 }
     367                 :             :         }
     368                 :             : 
     369         [ -  + ]:          50 :         if (expected_result == ZTEST_EXPECTED_RESULT_FAIL) {
     370                 :             :                 /* Expected a failure:
     371                 :             :                  * - If we got a failure, return TC_PASS
     372                 :             :                  * - Otherwise force a failure
     373                 :             :                  */
     374                 :           0 :                 return (ret == TC_FAIL) ? TC_PASS : TC_FAIL;
     375                 :             :         }
     376         [ -  + ]:          50 :         if (expected_result == ZTEST_EXPECTED_RESULT_SKIP) {
     377                 :             :                 /* Expected a skip:
     378                 :             :                  * - If we got a skip, return TC_PASS
     379                 :             :                  * - Otherwise force a failure
     380                 :             :                  */
     381                 :           0 :                 return (ret == TC_SKIP) ? TC_PASS : TC_FAIL;
     382                 :             :         }
     383                 :             :         /* No expectation was made, no change is needed. */
     384                 :             :         return ret;
     385                 :             : }
     386                 :             : 
     387                 :             : /**
     388                 :             :  * @brief Get a friendly name string for a given test phrase.
     389                 :             :  *
     390                 :             :  * @param phase an enum ztest_phase value describing the desired test phase
     391                 :             :  * @returns a string name for `phase`
     392                 :             :  */
     393                 :           0 : static inline const char *get_friendly_phase_name(enum ztest_phase phase)
     394                 :             : {
     395         [ #  # ]:           0 :         switch (phase) {
     396                 :             :         case TEST_PHASE_SETUP:
     397                 :             :                 return "setup";
     398                 :             :         case TEST_PHASE_BEFORE:
     399                 :             :                 return "before";
     400                 :             :         case TEST_PHASE_TEST:
     401                 :             :                 return "test";
     402                 :             :         case TEST_PHASE_AFTER:
     403                 :             :                 return "after";
     404                 :             :         case TEST_PHASE_TEARDOWN:
     405                 :             :                 return "teardown";
     406                 :             :         case TEST_PHASE_FRAMEWORK:
     407                 :             :                 return "framework";
     408                 :             :         default:
     409                 :             :                 return "(unknown)";
     410                 :             :         }
     411                 :             : }
     412                 :             : 
     413                 :             : static bool current_test_failed_assumption;
     414                 :           0 : void ztest_skip_failed_assumption(void)
     415                 :             : {
     416                 :           0 :         if (IS_ENABLED(CONFIG_ZTEST_FAIL_ON_ASSUME)) {
     417                 :             :                 current_test_failed_assumption = true;
     418                 :             :                 ztest_test_fail();
     419                 :             :         } else {
     420                 :           0 :                 ztest_test_skip();
     421                 :             :         }
     422                 :           0 : }
     423                 :             : 
     424                 :             : #ifdef ZTEST_UNITTEST
     425                 :             : 
     426                 :             : /* Static code analysis tool can raise a violation that the standard header
     427                 :             :  * <setjmp.h> shall not be used.
     428                 :             :  *
     429                 :             :  * setjmp is using in a test code, not in a runtime code, it is acceptable.
     430                 :             :  * It is a deliberate deviation.
     431                 :             :  */
     432                 :             : #include <setjmp.h> /* parasoft-suppress MISRAC2012-RULE_21_4-a MISRAC2012-RULE_21_4-b*/
     433                 :             : #include <signal.h>
     434                 :             : #include <string.h>
     435                 :             : 
     436                 :             : #define FAIL_FAST 0
     437                 :             : 
     438                 :             : static jmp_buf test_fail;
     439                 :             : static jmp_buf test_pass;
     440                 :             : static jmp_buf test_skip;
     441                 :             : static jmp_buf stack_fail;
     442                 :             : static jmp_buf test_suite_fail;
     443                 :             : 
     444                 :             : void ztest_test_fail(void)
     445                 :             : {
     446                 :             :         switch (cur_phase) {
     447                 :             :         case TEST_PHASE_SETUP:
     448                 :             :                 PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
     449                 :             :                 longjmp(test_suite_fail, 1);
     450                 :             :         case TEST_PHASE_BEFORE:
     451                 :             :         case TEST_PHASE_TEST:
     452                 :             :                 PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
     453                 :             :                 longjmp(test_fail, 1);
     454                 :             :         case TEST_PHASE_AFTER:
     455                 :             :         case TEST_PHASE_TEARDOWN:
     456                 :             :         case TEST_PHASE_FRAMEWORK:
     457                 :             :                 PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
     458                 :             :                            get_friendly_phase_name(cur_phase));
     459                 :             :                 longjmp(stack_fail, 1);
     460                 :             :         }
     461                 :             : }
     462                 :             : EXPORT_SYMBOL(ztest_test_fail);
     463                 :             : 
     464                 :             : void ztest_test_pass(void)
     465                 :             : {
     466                 :             :         if (cur_phase == TEST_PHASE_TEST) {
     467                 :             :                 longjmp(test_pass, 1);
     468                 :             :         }
     469                 :             :         PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
     470                 :             :                    get_friendly_phase_name(cur_phase));
     471                 :             :         longjmp(stack_fail, 1);
     472                 :             : }
     473                 :             : EXPORT_SYMBOL(ztest_test_pass);
     474                 :             : 
     475                 :             : void ztest_test_skip(void)
     476                 :             : {
     477                 :             :         switch (cur_phase) {
     478                 :             :         case TEST_PHASE_SETUP:
     479                 :             :         case TEST_PHASE_BEFORE:
     480                 :             :         case TEST_PHASE_TEST:
     481                 :             :                 longjmp(test_skip, 1);
     482                 :             :         default:
     483                 :             :                 PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
     484                 :             :                            get_friendly_phase_name(cur_phase));
     485                 :             :                 longjmp(stack_fail, 1);
     486                 :             :         }
     487                 :             : }
     488                 :             : EXPORT_SYMBOL(ztest_test_skip);
     489                 :             : 
     490                 :             : void ztest_test_expect_fail(void)
     491                 :             : {
     492                 :             :         failed_expectation = true;
     493                 :             : 
     494                 :             :         switch (cur_phase) {
     495                 :             :         case TEST_PHASE_SETUP:
     496                 :             :                 PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
     497                 :             :                 break;
     498                 :             :         case TEST_PHASE_BEFORE:
     499                 :             :         case TEST_PHASE_TEST:
     500                 :             :                 PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
     501                 :             :                 break;
     502                 :             :         case TEST_PHASE_AFTER:
     503                 :             :         case TEST_PHASE_TEARDOWN:
     504                 :             :         case TEST_PHASE_FRAMEWORK:
     505                 :             :                 PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
     506                 :             :                            get_friendly_phase_name(cur_phase));
     507                 :             :                 longjmp(stack_fail, 1);
     508                 :             :         }
     509                 :             : }
     510                 :             : 
     511                 :             : static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test, void *data)
     512                 :             : {
     513                 :             :         int ret = TC_PASS;
     514                 :             :         const char *tc_name = (z_ztest_param_state.active &&
     515                 :             :                                z_ztest_param_state.display_name[0] != '\0')
     516                 :             :                               ? z_ztest_param_state.display_name
     517                 :             :                               : test->name;
     518                 :             : 
     519                 :             :         TC_START(tc_name);
     520                 :             :         __ztest_set_test_phase(TEST_PHASE_BEFORE);
     521                 :             : 
     522                 :             :         if (test_result == ZTEST_RESULT_SUITE_FAIL) {
     523                 :             :                 ret = TC_FAIL;
     524                 :             :                 goto out;
     525                 :             :         }
     526                 :             : 
     527                 :             :         if (setjmp(test_fail)) {
     528                 :             :                 ret = TC_FAIL;
     529                 :             :                 goto out;
     530                 :             :         }
     531                 :             : 
     532                 :             :         if (setjmp(test_pass)) {
     533                 :             :                 ret = TC_PASS;
     534                 :             :                 goto out;
     535                 :             :         }
     536                 :             : 
     537                 :             :         if (setjmp(test_skip)) {
     538                 :             :                 ret = TC_SKIP;
     539                 :             :                 goto out;
     540                 :             :         }
     541                 :             : 
     542                 :             :         run_test_rules(/*is_before=*/true, test, data);
     543                 :             :         if (suite->before) {
     544                 :             :                 suite->before(data);
     545                 :             :         }
     546                 :             :         run_test_functions(suite, test, data);
     547                 :             : out:
     548                 :             :         if (failed_expectation) {
     549                 :             :                 failed_expectation = false;
     550                 :             :                 ret = TC_FAIL;
     551                 :             :         }
     552                 :             : 
     553                 :             :         __ztest_set_test_phase(TEST_PHASE_AFTER);
     554                 :             :         if (test_result != ZTEST_RESULT_SUITE_FAIL) {
     555                 :             :                 if (suite->after != NULL) {
     556                 :             :                         suite->after(data);
     557                 :             :                 }
     558                 :             :                 run_test_rules(/*is_before=*/false, test, data);
     559                 :             :         }
     560                 :             :         __ztest_set_test_phase(TEST_PHASE_FRAMEWORK);
     561                 :             :         ret |= cleanup_test(test);
     562                 :             : 
     563                 :             :         ret = get_final_test_result(test, ret);
     564                 :             :         Z_TC_END_RESULT(ret, tc_name);
     565                 :             :         if (ret == TC_SKIP && current_test_failed_assumption) {
     566                 :             :                 test_status = ZTEST_STATUS_HAS_FAILURE;
     567                 :             :         }
     568                 :             : 
     569                 :             :         return ret;
     570                 :             : }
     571                 :             : 
     572                 :             : #else /* !ZTEST_UNITTEST */
     573                 :             : 
     574                 :             : /* Zephyr's probably going to cause all tests to fail if one test fails, so
     575                 :             :  * skip the rest of tests if one of them fails
     576                 :             :  */
     577                 :             : #ifdef CONFIG_ZTEST_FAIL_FAST
     578                 :             : #define FAIL_FAST 1
     579                 :             : #else
     580                 :             : #define FAIL_FAST 0
     581                 :             : #endif
     582                 :             : 
     583                 :             : /*
     584                 :             :  * Declared with external linkage (see ztest_test.h) so that tests inspecting
     585                 :             :  * the runner thread's stack (e.g. the userspace memory-protection tests) can
     586                 :             :  * reference it.
     587                 :             :  */
     588                 :             : K_THREAD_STACK_DEFINE(ztest_thread_stack, CONFIG_ZTEST_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE);
     589                 :             : 
     590                 :           0 : static void test_finalize(void)
     591                 :             : {
     592                 :           0 :         if (IS_ENABLED(CONFIG_MULTITHREADING)) {
     593                 :           0 :                 k_thread_abort(&ztest_thread);
     594         [ #  # ]:           0 :                 if (k_is_in_isr()) {
     595                 :           0 :                         return;
     596                 :             :                 }
     597                 :             : 
     598                 :           0 :                 k_thread_abort(k_current_get());
     599                 :           0 :                 CODE_UNREACHABLE;
     600                 :             :         }
     601                 :             : }
     602                 :             : 
     603                 :           0 : void ztest_test_fail(void)
     604                 :             : {
     605      [ #  #  # ]:           0 :         switch (cur_phase) {
     606                 :           0 :         case TEST_PHASE_SETUP:
     607                 :           0 :                 __ztest_set_test_result(ZTEST_RESULT_SUITE_FAIL);
     608                 :           0 :                 break;
     609                 :           0 :         case TEST_PHASE_BEFORE:
     610                 :             :         case TEST_PHASE_TEST:
     611                 :           0 :                 __ztest_set_test_result(ZTEST_RESULT_FAIL);
     612                 :           0 :                 test_finalize();
     613                 :           0 :                 break;
     614                 :           0 :         default:
     615                 :           0 :                 PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
     616                 :             :                            get_friendly_phase_name(cur_phase));
     617                 :           0 :                 test_status = ZTEST_STATUS_CRITICAL_ERROR;
     618                 :           0 :                 break;
     619                 :             :         }
     620                 :           0 : }
     621                 :             : EXPORT_SYMBOL(ztest_test_fail);
     622                 :             : 
     623                 :           0 : void ztest_test_pass(void)
     624                 :             : {
     625         [ #  # ]:           0 :         switch (cur_phase) {
     626                 :           0 :         case TEST_PHASE_TEST:
     627                 :           0 :                 __ztest_set_test_result(ZTEST_RESULT_PASS);
     628                 :           0 :                 test_finalize();
     629                 :           0 :                 break;
     630                 :           0 :         default:
     631                 :           0 :                 PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
     632                 :             :                            get_friendly_phase_name(cur_phase));
     633                 :           0 :                 test_status = ZTEST_STATUS_CRITICAL_ERROR;
     634         [ #  # ]:           0 :                 if (cur_phase == TEST_PHASE_BEFORE) {
     635                 :           0 :                         test_finalize();
     636                 :             :                 }
     637                 :             :                 break;
     638                 :             :         }
     639                 :           0 : }
     640                 :             : EXPORT_SYMBOL(ztest_test_pass);
     641                 :             : 
     642                 :           0 : void ztest_test_skip(void)
     643                 :             : {
     644      [ #  #  # ]:           0 :         switch (cur_phase) {
     645                 :           0 :         case TEST_PHASE_SETUP:
     646                 :           0 :                 __ztest_set_test_result(ZTEST_RESULT_SUITE_SKIP);
     647                 :           0 :                 break;
     648                 :           0 :         case TEST_PHASE_BEFORE:
     649                 :             :         case TEST_PHASE_TEST:
     650                 :           0 :                 __ztest_set_test_result(ZTEST_RESULT_SKIP);
     651                 :           0 :                 test_finalize();
     652                 :           0 :                 break;
     653                 :           0 :         default:
     654                 :           0 :                 PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
     655                 :             :                            get_friendly_phase_name(cur_phase));
     656                 :           0 :                 test_status = ZTEST_STATUS_CRITICAL_ERROR;
     657                 :           0 :                 break;
     658                 :             :         }
     659                 :           0 : }
     660                 :             : EXPORT_SYMBOL(ztest_test_skip);
     661                 :             : 
     662                 :           0 : void ztest_test_expect_fail(void)
     663                 :             : {
     664                 :           0 :         failed_expectation = true;
     665                 :           0 : }
     666                 :             : 
     667                 :           0 : void ztest_simple_1cpu_before(void *data)
     668                 :             : {
     669                 :           0 :         ARG_UNUSED(data);
     670                 :           0 :         z_test_1cpu_start();
     671                 :           0 : }
     672                 :             : 
     673                 :           0 : void ztest_simple_1cpu_after(void *data)
     674                 :             : {
     675                 :           0 :         ARG_UNUSED(data);
     676                 :           0 :         z_test_1cpu_stop();
     677                 :           0 : }
     678                 :             : 
     679                 :          50 : static void test_cb(void *a, void *b, void *c)
     680                 :             : {
     681                 :          50 :         struct ztest_suite_node *suite = a;
     682                 :          50 :         struct ztest_unit_test *test = b;
     683                 :          50 :         const bool config_user_mode = FIELD_GET(K_USER, test->thread_options) != 0;
     684                 :             : 
     685         [ +  - ]:          50 :         if (!k_is_user_context()) {
     686                 :          50 :                 __ztest_set_test_result(ZTEST_RESULT_PENDING);
     687                 :          50 :                 run_test_rules(/*is_before=*/true, test, /*data=*/c);
     688         [ -  + ]:          50 :                 if (suite->before) {
     689                 :           0 :                         suite->before(/*data=*/c);
     690                 :             :                 }
     691                 :             :                 if (IS_ENABLED(CONFIG_USERSPACE) && config_user_mode) {
     692                 :             :                         k_thread_user_mode_enter(test_cb, a, b, c);
     693                 :             :                 }
     694                 :             :         }
     695                 :          50 :         run_test_functions(suite, test, c);
     696                 :          50 :         __ztest_set_test_result(ZTEST_RESULT_PASS);
     697                 :          50 : }
     698                 :             : 
     699                 :          50 : static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test, void *data)
     700                 :             : {
     701                 :          50 :         int ret = TC_PASS;
     702                 :         100 :         const char *tc_name = (z_ztest_param_state.active &&
     703         [ #  # ]:           0 :                                z_ztest_param_state.display_name[0] != '\0')
     704                 :             :                               ? z_ztest_param_state.display_name
     705         [ -  + ]:          50 :                               : test->name;
     706                 :             : 
     707                 :             : #if CONFIG_ZTEST_TEST_DELAY_MS > 0
     708                 :             :         k_busy_wait(CONFIG_ZTEST_TEST_DELAY_MS * USEC_PER_MSEC);
     709                 :             : #endif
     710                 :          50 :         TC_START(tc_name);
     711                 :             : 
     712                 :          50 :         __ztest_set_test_phase(TEST_PHASE_BEFORE);
     713                 :             : 
     714                 :             :         /* If the suite's setup function marked us as skipped, don't bother
     715                 :             :          * running the tests.
     716                 :             :          */
     717                 :          50 :         if (IS_ENABLED(CONFIG_MULTITHREADING)) {
     718                 :          50 :                 get_start_time_cyc();
     719                 :          50 :                 (void)k_thread_create(&ztest_thread, ztest_thread_stack,
     720                 :             :                                 K_THREAD_STACK_SIZEOF(ztest_thread_stack), test_cb, suite, test,
     721                 :          50 :                                 data, CONFIG_ZTEST_THREAD_PRIORITY, K_INHERIT_PERMS, K_FOREVER);
     722                 :             : 
     723                 :          50 :                 k_thread_access_grant(&ztest_thread, suite, test, suite->stats);
     724         [ +  - ]:          50 :                 if (test->name != NULL) {
     725                 :          50 :                         (void)k_thread_name_set(&ztest_thread, test->name);
     726                 :             :                 }
     727                 :             :                 /* Only start the thread if we're not skipping the suite */
     728         [ +  - ]:          50 :                 if (test_result != ZTEST_RESULT_SUITE_SKIP &&
     729                 :             :                     test_result != ZTEST_RESULT_SUITE_FAIL) {
     730                 :          50 :                         k_thread_start(&ztest_thread);
     731                 :          50 :                         (void)k_thread_join(&ztest_thread, K_FOREVER);
     732                 :             :                 }
     733                 :             :         } else if (test_result != ZTEST_RESULT_SUITE_SKIP &&
     734                 :             :                    test_result != ZTEST_RESULT_SUITE_FAIL) {
     735                 :             :                 __ztest_set_test_result(ZTEST_RESULT_PENDING);
     736                 :             :                 get_start_time_cyc();
     737                 :             :                 run_test_rules(/*is_before=*/true, test, data);
     738                 :             :                 if (suite->before) {
     739                 :             :                         suite->before(data);
     740                 :             :                 }
     741                 :             :                 run_test_functions(suite, test, data);
     742                 :             :         } else {
     743                 :             :                 /* Suite was skipped or failed; no tests to run. */
     744                 :          50 :         }
     745                 :             : 
     746                 :          50 :         __ztest_set_test_phase(TEST_PHASE_AFTER);
     747         [ -  + ]:          50 :         if (suite->after != NULL) {
     748                 :           0 :                 suite->after(data);
     749                 :             :         }
     750                 :          50 :         run_test_rules(/*is_before=*/false, test, data);
     751                 :             : 
     752                 :          50 :         get_test_duration_ms();
     753         [ -  + ]:          50 :         if (tc_spend_time > test->stats->duration_worst_ms) {
     754                 :           0 :                 test->stats->duration_worst_ms = tc_spend_time;
     755                 :             :         }
     756                 :             : 
     757                 :          50 :         __ztest_set_test_phase(TEST_PHASE_FRAMEWORK);
     758                 :             : 
     759                 :             :         /* Flush all logs in case deferred mode and default logging thread are used. */
     760                 :          50 :         while (IS_ENABLED(CONFIG_TEST_LOGGING_FLUSH_AFTER_TEST) &&
     761                 :             :                IS_ENABLED(CONFIG_LOG_PROCESS_THREAD) && log_data_pending()) {
     762                 :             :                 (void)k_msleep(100);
     763                 :             :         }
     764                 :             : 
     765   [ +  -  -  + ]:          50 :         if (test_result == ZTEST_RESULT_FAIL || test_result == ZTEST_RESULT_SUITE_FAIL ||
     766                 :             :             failed_expectation) {
     767                 :           0 :                 ret = TC_FAIL;
     768                 :           0 :                 failed_expectation = false;
     769         [ -  + ]:          50 :         } else if (test_result == ZTEST_RESULT_SKIP || test_result == ZTEST_RESULT_SUITE_SKIP) {
     770                 :           0 :                 ret = TC_SKIP;
     771                 :             :         } else {
     772                 :             :                 /* Result already reflected in ret. */
     773                 :          50 :         }
     774                 :             : 
     775                 :          50 :         if (test_result == ZTEST_RESULT_PASS || !FAIL_FAST) {
     776                 :          50 :                 ret |= cleanup_test(test);
     777                 :             :         }
     778                 :             : 
     779                 :          50 :         ret = get_final_test_result(test, ret);
     780                 :          50 :         Z_TC_END_RESULT(ret, tc_name);
     781   [ -  +  -  - ]:          50 :         if (ret == TC_SKIP && current_test_failed_assumption) {
     782                 :           0 :                 test_status = ZTEST_STATUS_HAS_FAILURE;
     783                 :             :         }
     784                 :             : 
     785                 :          50 :         return ret;
     786                 :             : }
     787                 :             : 
     788                 :             : #endif /* ZTEST_UNITTEST */
     789                 :             : 
     790                 :          50 : static struct ztest_suite_node *ztest_find_test_suite(const char *name)
     791                 :             : {
     792                 :          50 :         struct ztest_suite_node *node;
     793                 :             : 
     794         [ +  - ]:          50 :         for (node = _ztest_suite_node_list_start; node < _ztest_suite_node_list_end; ++node) {
     795         [ +  - ]:          50 :                 if (strcmp(name, node->name) == 0) {
     796                 :          50 :                         return node;
     797                 :             :                 }
     798                 :             :         }
     799                 :             : 
     800                 :             :         return NULL;
     801                 :             : }
     802                 :             : 
     803                 :         153 : struct ztest_unit_test *z_ztest_get_next_test(const char *suite, struct ztest_unit_test *prev)
     804                 :             : {
     805         [ +  + ]:         153 :         struct ztest_unit_test *test = (prev == NULL) ? _ztest_unit_test_list_start : prev + 1;
     806                 :             : 
     807         [ +  + ]:         153 :         for (; test < _ztest_unit_test_list_end; ++test) {
     808         [ +  - ]:         150 :                 if (strcmp(suite, test->test_suite_name) == 0) {
     809                 :         150 :                         return test;
     810                 :             :                 }
     811                 :             :         }
     812                 :             :         return NULL;
     813                 :             : }
     814                 :             : 
     815                 :             : /*
     816                 :             :  * Run a single unit test, dispatching once per parameter value for each
     817                 :             :  * registered ztest_param_inst that matches (suite, test_name).  Supports
     818                 :             :  * multiple instantiations of the same ZTEST_P function (one per instance
     819                 :             :  * name), each with an independent parameter set.  Falls back to a single
     820                 :             :  * non-parameterized run when no matching instantiation exists.
     821                 :             :  *
     822                 :             :  * Returns the number of failures accumulated across all invocations.
     823                 :             :  */
     824                 :          50 : static int z_ztest_run_test_dispatch(struct ztest_suite_node *suite,
     825                 :             :                                      struct ztest_unit_test *test, void *data)
     826                 :             : {
     827                 :          50 :         const uint8_t *base;
     828                 :          50 :         int tc_result;
     829                 :          50 :         int fail = 0;
     830                 :          50 :         bool any_inst = false;
     831                 :             : 
     832   [ -  +  -  + ]:          50 :         STRUCT_SECTION_FOREACH(ztest_param_inst, inst) {
     833         [ #  # ]:           0 :                 if (strcmp(inst->test_suite_name, suite->name) != 0 ||
     834         [ #  # ]:           0 :                     strcmp(inst->test_name, test->name) != 0) {
     835                 :           0 :                         continue;
     836                 :             :                 }
     837                 :             : 
     838                 :           0 :                 any_inst = true;
     839                 :             : 
     840                 :             :                 /* Parameterized: run once per registered value. */
     841         [ #  # ]:           0 :                 if (inst->values->setup_cb != NULL) {
     842                 :           0 :                         inst->values->setup_cb();
     843                 :             :                 }
     844                 :           0 :                 base = (const uint8_t *)inst->values->values;
     845         [ #  # ]:           0 :                 for (size_t i = 0U; i < inst->values->count; i++) {
     846         [ #  # ]:           0 :                         if (inst->values->value_at_cb != NULL) {
     847                 :             :                                 /* Range-generated: compute value into aligned scratch
     848                 :             :                                  * buffer and point state.value at it.
     849                 :             :                                  */
     850                 :           0 :                                 inst->values->value_at_cb(i, z_ztest_param_state.value_buf);
     851                 :           0 :                                 z_ztest_param_state.value =
     852                 :             :                                         (const void *)z_ztest_param_state.value_buf;
     853                 :             :                         } else {
     854                 :             :                                 /* Array-backed: direct pointer into values array. */
     855                 :           0 :                                 z_ztest_param_state.value = base + i * inst->values->elem_size;
     856                 :             :                         }
     857                 :           0 :                         z_ztest_param_state.index = i;
     858                 :           0 :                         z_ztest_param_state.elem_size = inst->values->elem_size;
     859                 :           0 :                         z_ztest_param_state.active = true;
     860                 :             : 
     861                 :             :                         /* Build a unique display name: test_name[instance/N] or
     862                 :             :                          * test_name[instance/custom] when name_cb is provided.
     863                 :             :                          * This mirrors the Google Test naming convention and makes
     864                 :             :                          * each parameter invocation distinguishable in the output.
     865                 :             :                          */
     866         [ #  # ]:           0 :                         if (inst->values->name_cb != NULL) {
     867                 :           0 :                                 const char *param_name =
     868                 :           0 :                                         inst->values->name_cb(i, z_ztest_param_state.value);
     869                 :             : 
     870                 :           0 :                                 snprintk(z_ztest_param_state.display_name,
     871                 :             :                                          sizeof(z_ztest_param_state.display_name),
     872                 :             :                                          "%s[%s/%s]", test->name, inst->instance_name,
     873                 :             :                                          param_name);
     874                 :             :                         } else {
     875                 :           0 :                                 snprintk(z_ztest_param_state.display_name,
     876                 :             :                                          sizeof(z_ztest_param_state.display_name),
     877                 :             :                                          "%s[%s/%zu]", test->name, inst->instance_name, i);
     878                 :             :                         }
     879                 :             : 
     880                 :           0 :                         test->stats->run_count++;
     881                 :           0 :                         tc_result = run_test(suite, test, data);
     882         [ #  # ]:           0 :                         if (tc_result == TC_PASS) {
     883                 :           0 :                                 test->stats->pass_count++;
     884         [ #  # ]:           0 :                         } else if (tc_result == TC_SKIP) {
     885                 :           0 :                                 test->stats->skip_count++;
     886         [ #  # ]:           0 :                         } else if (tc_result == TC_FAIL) {
     887                 :           0 :                                 test->stats->fail_count++;
     888                 :           0 :                                 fail++;
     889                 :             :                         }
     890         [ #  # ]:           0 :                         if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) {
     891                 :           0 :                                 goto done;
     892                 :             :                         }
     893                 :             :                 }
     894                 :             :         }
     895                 :             : 
     896                 :          50 : done:
     897                 :          50 :         z_ztest_param_state.active = false;
     898                 :          50 :         z_ztest_param_state.value = NULL;
     899                 :          50 :         z_ztest_param_state.display_name[0] = '\0';
     900                 :             : 
     901         [ -  + ]:          50 :         if (!any_inst) {
     902                 :             :                 /* Non-parameterized: clear context and run once. */
     903                 :          50 :                 z_ztest_param_state.index = 0U;
     904                 :          50 :                 z_ztest_param_state.elem_size = 0U;
     905                 :          50 :                 test->stats->run_count++;
     906                 :          50 :                 tc_result = run_test(suite, test, data);
     907         [ +  - ]:          50 :                 if (tc_result == TC_PASS) {
     908                 :          50 :                         test->stats->pass_count++;
     909         [ #  # ]:           0 :                 } else if (tc_result == TC_SKIP) {
     910                 :           0 :                         test->stats->skip_count++;
     911         [ #  # ]:           0 :                 } else if (tc_result == TC_FAIL) {
     912                 :           0 :                         test->stats->fail_count++;
     913                 :           0 :                         fail++;
     914                 :             :                 }
     915                 :             :         }
     916                 :             : 
     917                 :          50 :         return fail;
     918                 :             : }
     919                 :             : 
     920                 :             : #if CONFIG_ZTEST_SHUFFLE
     921                 :             : static void z_ztest_shuffle(bool shuffle, void *dest[], intptr_t start, size_t num_items,
     922                 :             :                             size_t element_size)
     923                 :             : {
     924                 :             :         /* Initialize dest array */
     925                 :             :         for (size_t i = 0; i < num_items; ++i) {
     926                 :             :                 dest[i] = (void *)(start + (i * element_size));
     927                 :             :         }
     928                 :             :         void *tmp;
     929                 :             : 
     930                 :             :         /* Shuffle dest array */
     931                 :             :         if (shuffle) {
     932                 :             :                 for (size_t i = num_items - 1; i > 0; i--) {
     933                 :             :                         int j = sys_rand32_get() % (i + 1);
     934                 :             : 
     935                 :             :                         if (i != j) {
     936                 :             :                                 tmp = dest[j];
     937                 :             :                                 dest[j] = dest[i];
     938                 :             :                                 dest[i] = tmp;
     939                 :             :                         }
     940                 :             :                 }
     941                 :             :         }
     942                 :             : }
     943                 :             : #endif
     944                 :             : 
     945                 :           1 : static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite, bool shuffle, int suite_iter,
     946                 :             :                                       int case_iter, void *param)
     947                 :             : {
     948                 :           1 :         struct ztest_unit_test *test = NULL;
     949                 :           1 :         void *data = NULL;
     950                 :           1 :         int fail = 0;
     951                 :             : 
     952                 :             :         /*
     953                 :             :          * 'param' formerly let callers overwrite the fixture pointer from
     954                 :             :          * setup().  That behaviour was incorrect: fixture and parameter are
     955                 :             :          * independent channels.  The argument is kept for ABI compatibility.
     956                 :             :          */
     957                 :           1 :         ARG_UNUSED(param);
     958                 :             : 
     959                 :             : #ifndef CONFIG_ZTEST_SHUFFLE
     960                 :           1 :         ARG_UNUSED(shuffle);
     961                 :           1 :         ARG_UNUSED(suite_iter);
     962                 :             : #endif
     963                 :             : 
     964                 :           1 :         if (FAIL_FAST && test_status != ZTEST_STATUS_OK) {
     965                 :             :                 return test_status;
     966                 :             :         }
     967                 :             : 
     968         [ -  + ]:           1 :         if (suite == NULL) {
     969                 :           0 :                 test_status = ZTEST_STATUS_CRITICAL_ERROR;
     970                 :           0 :                 return -1;
     971                 :             :         }
     972                 :             : 
     973                 :             : #ifdef ZTEST_UNITTEST
     974                 :             :         if (setjmp(stack_fail)) {
     975                 :             :                 PRINT_DATA("TESTSUITE crashed.\n");
     976                 :             :                 test_status = ZTEST_STATUS_CRITICAL_ERROR;
     977                 :             :                 end_report();
     978                 :             :                 exit(1);
     979                 :             :         }
     980                 :             : #else
     981                 :           1 :         k_object_access_all_grant(&ztest_thread);
     982                 :             : #endif
     983                 :             : 
     984                 :           1 :         TC_SUITE_START(suite->name);
     985                 :           1 :         current_test_failed_assumption = false;
     986                 :           1 :         __ztest_set_test_result(ZTEST_RESULT_PENDING);
     987                 :           1 :         __ztest_set_test_phase(TEST_PHASE_SETUP);
     988                 :             : #ifdef ZTEST_UNITTEST
     989                 :             :         if (setjmp(test_suite_fail)) {
     990                 :             :                 __ztest_set_test_result(ZTEST_RESULT_SUITE_FAIL);
     991                 :             :         }
     992                 :             : #endif
     993   [ +  -  -  + ]:           1 :         if (test_result != ZTEST_RESULT_SUITE_FAIL && suite->setup != NULL) {
     994                 :           0 :                 data = suite->setup();
     995                 :             :         }
     996                 :             : 
     997         [ +  + ]:           2 :         for (int i = 0; i < case_iter; i++) {
     998                 :             : #ifdef CONFIG_ZTEST_SHUFFLE
     999                 :             :                 struct ztest_unit_test *tests_to_run[ZTEST_TEST_COUNT];
    1000                 :             : 
    1001                 :             :                 memset(tests_to_run, 0, ZTEST_TEST_COUNT * sizeof(struct ztest_unit_test *));
    1002                 :             :                 z_ztest_shuffle(shuffle, (void **)tests_to_run,
    1003                 :             :                                 (intptr_t)_ztest_unit_test_list_start, ZTEST_TEST_COUNT,
    1004                 :             :                                 sizeof(struct ztest_unit_test));
    1005                 :             :                 for (size_t j = 0; j < ZTEST_TEST_COUNT; ++j) {
    1006                 :             :                         test = tests_to_run[j];
    1007                 :             :                         /* Make sure that the test belongs to this suite */
    1008                 :             :                         if (strcmp(suite->name, test->test_suite_name) != 0) {
    1009                 :             :                                 continue;
    1010                 :             :                         }
    1011                 :             :                         if (ztest_api.should_test_run(suite->name, test->name)) {
    1012                 :             :                                 fail += z_ztest_run_test_dispatch(suite, test, data);
    1013                 :             :                         }
    1014                 :             : 
    1015                 :             :                         if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) {
    1016                 :             :                                 break;
    1017                 :             :                         }
    1018                 :             :                 }
    1019                 :             : #else
    1020         [ +  + ]:          51 :                 for (test = z_ztest_get_next_test(suite->name, NULL); test != NULL;
    1021                 :          50 :                      test = z_ztest_get_next_test(suite->name, test)) {
    1022         [ +  - ]:          50 :                         if (ztest_api.should_test_run(suite->name, test->name)) {
    1023                 :          50 :                                 fail += z_ztest_run_test_dispatch(suite, test, data);
    1024                 :             :                         }
    1025                 :             : 
    1026         [ +  - ]:          50 :                         if ((fail && FAIL_FAST) || test_status == ZTEST_STATUS_CRITICAL_ERROR) {
    1027                 :             :                                 break;
    1028                 :             :                         }
    1029                 :             :                 }
    1030                 :             : #endif
    1031   [ +  -  -  + ]:           1 :                 if (test_status == ZTEST_STATUS_OK && fail != 0) {
    1032                 :           0 :                         test_status = ZTEST_STATUS_HAS_FAILURE;
    1033                 :             :                 }
    1034                 :             :         }
    1035                 :             : 
    1036         [ +  - ]:           1 :         TC_SUITE_END(suite->name, (fail > 0 ? TC_FAIL : TC_PASS));
    1037                 :           1 :         __ztest_set_test_phase(TEST_PHASE_TEARDOWN);
    1038         [ -  + ]:           1 :         if (suite->teardown != NULL) {
    1039                 :           0 :                 suite->teardown(data);
    1040                 :             :         }
    1041                 :             : 
    1042                 :             :         return fail;
    1043                 :             : }
    1044                 :             : 
    1045                 :           0 : int z_ztest_run_test_suite(const char *name, bool shuffle,
    1046                 :             :         int suite_iter, int case_iter, void *param)
    1047                 :             : {
    1048                 :           0 :         return z_ztest_run_test_suite_ptr(ztest_find_test_suite(name), shuffle, suite_iter,
    1049                 :             :                                           case_iter, param);
    1050                 :             : }
    1051                 :             : 
    1052                 :             : #ifdef CONFIG_USERSPACE
    1053                 :             : K_APPMEM_PARTITION_DEFINE(ztest_mem_partition);
    1054                 :             : #endif
    1055                 :             : 
    1056                 :             : /* Show one line summary for a test suite.
    1057                 :             :  */
    1058                 :           1 : static void __ztest_show_suite_summary_oneline(struct ztest_suite_node *suite)
    1059                 :             : {
    1060                 :           1 :         int distinct_pass = 0, distinct_fail = 0, distinct_skip = 0, distinct_total = 0;
    1061                 :           1 :         int effective_total = 0;
    1062                 :           1 :         int expanded_pass = 0, expanded_passrate = 0;
    1063                 :           1 :         int passrate_major = 0, passrate_minor = 0, passrate_tail = 0;
    1064                 :           1 :         int suite_result = TC_PASS;
    1065                 :             : 
    1066                 :           1 :         struct ztest_unit_test *test = NULL;
    1067                 :           1 :         unsigned int suite_duration_worst_ms = 0;
    1068                 :             : 
    1069                 :             :         /** summary of distinct run  */
    1070         [ +  + ]:          51 :         for (test = z_ztest_get_next_test(suite->name, NULL); test != NULL;
    1071                 :          50 :              test = z_ztest_get_next_test(suite->name, test)) {
    1072                 :          50 :                 distinct_total++;
    1073                 :          50 :                 suite_duration_worst_ms += test->stats->duration_worst_ms;
    1074         [ -  + ]:          50 :                 if (test->stats->skip_count == test->stats->run_count) {
    1075                 :           0 :                         distinct_skip++;
    1076         [ +  - ]:          50 :                 } else if (test->stats->pass_count + test->stats->skip_count ==
    1077                 :             :                            test->stats->run_count) {
    1078                 :             :                         /* all non-skipped invocations passed */
    1079                 :          50 :                         distinct_pass++;
    1080                 :             :                 } else {
    1081                 :           0 :                         distinct_fail++;
    1082                 :             :                 }
    1083                 :             :         }
    1084                 :             : 
    1085         [ +  - ]:           1 :         if (distinct_skip == distinct_total) {
    1086                 :             :                 suite_result = TC_SKIP;
    1087                 :             :                 passrate_minor = 0;
    1088                 :             :                 passrate_major = 0;
    1089                 :             :         } else {
    1090                 :           1 :                 suite_result = (distinct_fail > 0) ? TC_FAIL : TC_PASS;
    1091                 :           1 :                 effective_total = distinct_total - distinct_skip;
    1092                 :           1 :                 expanded_pass = distinct_pass * 100000;
    1093                 :           1 :                 expanded_passrate = expanded_pass / effective_total;
    1094                 :           1 :                 passrate_major = expanded_passrate / 1000;
    1095                 :           1 :                 passrate_minor = (expanded_passrate - passrate_major * 1000) / 10;
    1096                 :           1 :                 passrate_tail = expanded_passrate - passrate_major * 1000 - passrate_minor * 10;
    1097         [ -  + ]:           1 :                 if (passrate_tail >= 5) { /* rounding */
    1098                 :           0 :                         passrate_minor++;
    1099                 :             :                 }
    1100                 :             :         }
    1101                 :             : 
    1102                 :           1 :         TC_SUMMARY_PRINT("SUITE %s - %3d.%02d%% [%s]: pass = %d, fail = %d, "
    1103                 :             :                          "skip = %d, total = %d duration = %u.%03u seconds\n",
    1104                 :             :                          TC_RESULT_TO_STR(suite_result), passrate_major, passrate_minor,
    1105                 :             :                          suite->name, distinct_pass, distinct_fail, distinct_skip, distinct_total,
    1106                 :             :                          suite_duration_worst_ms / 1000, suite_duration_worst_ms % 1000);
    1107                 :           1 :         log_flush();
    1108                 :           1 : }
    1109                 :             : 
    1110                 :           1 : static void __ztest_show_suite_summary_verbose(struct ztest_suite_node *suite)
    1111                 :             : {
    1112                 :           1 :         struct ztest_unit_test *test = NULL;
    1113                 :           1 :         int tc_result = TC_PASS;
    1114                 :           1 :         int flush_frequency = 0;
    1115                 :             : 
    1116                 :           1 :         if (IS_ENABLED(CONFIG_ZTEST_VERBOSE_SUMMARY) == 0) {
    1117                 :             :                 return;
    1118                 :             :         }
    1119                 :             : 
    1120         [ +  + ]:          51 :         for (test = z_ztest_get_next_test(suite->name, NULL); test != NULL;
    1121                 :          50 :              test = z_ztest_get_next_test(suite->name, test)) {
    1122         [ +  - ]:          50 :                 if (test->stats->skip_count == test->stats->run_count) {
    1123                 :             :                         tc_result = TC_SKIP;
    1124         [ -  + ]:          50 :                 } else if (test->stats->pass_count + test->stats->skip_count ==
    1125                 :             :                            test->stats->run_count) {
    1126                 :             :                         /* all non-skipped invocations passed */
    1127                 :             :                         tc_result = TC_PASS;
    1128         [ #  # ]:           0 :                 } else if (test->stats->pass_count == 0) {
    1129                 :             :                         tc_result = TC_FAIL;
    1130                 :             :                 } else {
    1131                 :           0 :                         tc_result = TC_FLAKY;
    1132                 :             :                 }
    1133                 :             : 
    1134                 :           0 :                 if (tc_result == TC_FLAKY) {
    1135                 :           0 :                         TC_SUMMARY_PRINT(
    1136                 :             :                                 " - %s - [%s.%s] - (Failed %d of %d attempts)"
    1137                 :             :                                 " - duration = %u.%03u seconds\n",
    1138                 :             :                                 TC_RESULT_TO_STR(tc_result), test->test_suite_name, test->name,
    1139                 :             :                                 test->stats->run_count - test->stats->pass_count,
    1140                 :             :                                 test->stats->run_count, test->stats->duration_worst_ms / 1000,
    1141                 :             :                                 test->stats->duration_worst_ms % 1000);
    1142                 :             :                 } else {
    1143                 :          50 :                         TC_SUMMARY_PRINT(" - %s - [%s.%s] duration = %u.%03u seconds\n",
    1144                 :             :                                          TC_RESULT_TO_STR(tc_result), test->test_suite_name,
    1145                 :             :                                          test->name, test->stats->duration_worst_ms / 1000,
    1146                 :             :                                          test->stats->duration_worst_ms % 1000);
    1147                 :             :                 }
    1148                 :             : 
    1149                 :          50 :                 if (flush_frequency % 3 == 0) {
    1150                 :             :                         /** Reduce the flush frequency a bit to speed up the output */
    1151                 :             :                         log_flush();
    1152                 :             :                 }
    1153                 :          50 :                 flush_frequency++;
    1154                 :             :         }
    1155                 :           1 :         TC_SUMMARY_PRINT("\n");
    1156                 :           1 :         log_flush();
    1157                 :             : }
    1158                 :             : 
    1159                 :           1 : static void __ztest_show_suite_summary(void)
    1160                 :             : {
    1161                 :           1 :         if (IS_ENABLED(CONFIG_ZTEST_SUMMARY) == 0) {
    1162                 :             :                 return;
    1163                 :             :         }
    1164                 :             :         /* Flush the log a lot to ensure that no summary content
    1165                 :             :          * is dropped if it goes through the logging subsystem.
    1166                 :             :          */
    1167                 :           1 :         log_flush();
    1168                 :           1 :         TC_SUMMARY_PRINT("\n------ TESTSUITE SUMMARY START ------\n\n");
    1169                 :           1 :         log_flush();
    1170                 :           1 :         for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
    1171         [ +  + ]:           2 :              ptr < _ztest_suite_node_list_end; ++ptr) {
    1172                 :             : 
    1173                 :           1 :                 __ztest_show_suite_summary_oneline(ptr);
    1174                 :           1 :                 __ztest_show_suite_summary_verbose(ptr);
    1175                 :             :         }
    1176                 :           1 :         TC_SUMMARY_PRINT("------ TESTSUITE SUMMARY END ------\n\n");
    1177                 :           1 :         log_flush();
    1178                 :             : }
    1179                 :             : 
    1180                 :           1 : static int __ztest_run_test_suite(struct ztest_suite_node *ptr, const void *state, bool shuffle,
    1181                 :             :                                   int suite_iter, int case_iter, void *param)
    1182                 :             : {
    1183                 :           1 :         struct ztest_suite_stats *stats = ptr->stats;
    1184                 :           1 :         int count = 0;
    1185                 :             : 
    1186         [ +  + ]:           2 :         for (int i = 0; i < suite_iter; i++) {
    1187         [ +  - ]:           1 :                 if (ztest_api.should_suite_run(state, ptr)) {
    1188                 :           1 :                         int fail = z_ztest_run_test_suite_ptr(ptr, shuffle,
    1189                 :             :                                                         suite_iter, case_iter, param);
    1190                 :             : 
    1191                 :           1 :                         count++;
    1192                 :           1 :                         stats->run_count++;
    1193         [ +  - ]:           2 :                         stats->fail_count += (fail != 0) ? 1 : 0;
    1194                 :             :                 } else {
    1195                 :           0 :                         stats->skip_count++;
    1196                 :             :                 }
    1197                 :             :         }
    1198                 :             : 
    1199                 :           1 :         return count;
    1200                 :             : }
    1201                 :             : 
    1202                 :           1 : int z_impl_ztest_run_test_suites(const void *state, bool shuffle, int suite_iter, int case_iter)
    1203                 :             : {
    1204                 :           1 :         int count = 0;
    1205                 :           1 :         void *param = NULL;
    1206         [ +  - ]:           1 :         if (test_status == ZTEST_STATUS_CRITICAL_ERROR) {
    1207                 :             :                 return count;
    1208                 :             :         }
    1209                 :             : 
    1210                 :             : #ifdef CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS
    1211                 :             :         gcov_reset_all_counts();
    1212                 :             : #endif
    1213                 :             : 
    1214                 :             : #ifdef CONFIG_ZTEST_SHUFFLE
    1215                 :             :         struct ztest_suite_node *suites_to_run[ZTEST_SUITE_COUNT];
    1216                 :             : 
    1217                 :             :         memset(suites_to_run, 0, ZTEST_SUITE_COUNT * sizeof(struct ztest_suite_node *));
    1218                 :             :         z_ztest_shuffle(shuffle, (void **)suites_to_run, (intptr_t)_ztest_suite_node_list_start,
    1219                 :             :                         ZTEST_SUITE_COUNT, sizeof(struct ztest_suite_node));
    1220                 :             :         for (size_t i = 0; i < ZTEST_SUITE_COUNT; ++i) {
    1221                 :             :                 count += __ztest_run_test_suite(suites_to_run[i], state, shuffle, suite_iter,
    1222                 :             :                                                 case_iter, param);
    1223                 :             :                 /* Stop running tests if we have a critical error or if we have a failure and
    1224                 :             :                  * FAIL_FAST was set
    1225                 :             :                  */
    1226                 :             :                 if (test_status == ZTEST_STATUS_CRITICAL_ERROR ||
    1227                 :             :                     (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) {
    1228                 :             :                         break;
    1229                 :             :                 }
    1230                 :             :         }
    1231                 :             : #else
    1232                 :             :         for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
    1233         [ +  + ]:           2 :              ptr < _ztest_suite_node_list_end; ++ptr) {
    1234                 :           1 :                 count += __ztest_run_test_suite(ptr, state, shuffle, suite_iter, case_iter, param);
    1235                 :             :                 /* Stop running tests if we have a critical error or if we have a failure and
    1236                 :             :                  * FAIL_FAST was set
    1237                 :             :                  */
    1238         [ +  - ]:           1 :                 if (test_status == ZTEST_STATUS_CRITICAL_ERROR ||
    1239                 :             :                     (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) {
    1240                 :             :                         break;
    1241                 :             :                 }
    1242                 :             :         }
    1243                 :             : #endif
    1244                 :             : 
    1245                 :             :         return count;
    1246                 :             : }
    1247                 :             : 
    1248                 :         101 : void z_impl___ztest_set_test_result(enum ztest_result new_result)
    1249                 :             : {
    1250                 :         101 :         test_result = new_result;
    1251                 :         101 : }
    1252                 :             : 
    1253                 :         202 : void z_impl___ztest_set_test_phase(enum ztest_phase new_phase)
    1254                 :             : {
    1255                 :         202 :         cur_phase = new_phase;
    1256                 :         202 : }
    1257                 :             : 
    1258                 :           0 : const void *ztest_get_current_param(void)
    1259                 :             : {
    1260                 :           0 :         return z_ztest_param_state.value;
    1261                 :             : }
    1262                 :             : 
    1263                 :           0 : size_t ztest_get_current_param_index(void)
    1264                 :             : {
    1265                 :           0 :         return z_ztest_param_state.index;
    1266                 :             : }
    1267                 :             : 
    1268                 :           0 : size_t ztest_get_current_param_size(void)
    1269                 :             : {
    1270                 :           0 :         return z_ztest_param_state.elem_size;
    1271                 :             : }
    1272                 :             : 
    1273                 :           0 : bool ztest_has_current_param(void)
    1274                 :             : {
    1275                 :           0 :         return z_ztest_param_state.active;
    1276                 :             : }
    1277                 :             : 
    1278                 :             : #ifdef CONFIG_USERSPACE
    1279                 :             : void z_vrfy___ztest_set_test_result(enum ztest_result new_result)
    1280                 :             : {
    1281                 :             :         z_impl___ztest_set_test_result(new_result);
    1282                 :             : }
    1283                 :             : #include <zephyr/syscalls/__ztest_set_test_result_mrsh.c>
    1284                 :             : 
    1285                 :             : void z_vrfy___ztest_set_test_phase(enum ztest_phase new_phase)
    1286                 :             : {
    1287                 :             :         z_impl___ztest_set_test_phase(new_phase);
    1288                 :             : }
    1289                 :             : #include <zephyr/syscalls/__ztest_set_test_phase_mrsh.c>
    1290                 :             : #endif /* CONFIG_USERSPACE */
    1291                 :             : 
    1292                 :           1 : void ztest_verify_all_test_suites_ran(void)
    1293                 :             : {
    1294                 :           1 :         bool all_tests_run = true;
    1295                 :           1 :         struct ztest_suite_node *suite;
    1296                 :           1 :         struct ztest_unit_test *test;
    1297                 :             : 
    1298                 :           1 :         if (IS_ENABLED(CONFIG_ZTEST_VERIFY_RUN_ALL)) {
    1299         [ +  + ]:           2 :                 for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end;
    1300                 :           1 :                      ++suite) {
    1301         [ -  + ]:           1 :                         if (suite->stats->run_count < 1) {
    1302                 :           0 :                                 PRINT_DATA("ERROR: Test suite '%s' did not run.\n", suite->name);
    1303                 :           0 :                                 all_tests_run = false;
    1304                 :             :                         }
    1305                 :             :                 }
    1306                 :             : 
    1307         [ +  + ]:          51 :                 for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
    1308                 :          50 :                         suite = ztest_find_test_suite(test->test_suite_name);
    1309         [ -  + ]:          50 :                         if (suite == NULL) {
    1310                 :           0 :                                 PRINT_DATA("ERROR: Test '%s' assigned to test suite '%s' which "
    1311                 :             :                                            "doesn't "
    1312                 :             :                                            "exist\n",
    1313                 :             :                                            test->name, test->test_suite_name);
    1314                 :           0 :                                 all_tests_run = false;
    1315                 :             :                         }
    1316                 :             :                 }
    1317                 :             : 
    1318         [ -  + ]:           1 :                 if (!all_tests_run) {
    1319                 :           0 :                         test_status = ZTEST_STATUS_HAS_FAILURE;
    1320                 :             :                 }
    1321                 :             :         }
    1322                 :             : 
    1323         [ +  + ]:          51 :         for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
    1324                 :          50 :                 if (test->stats->fail_count + test->stats->pass_count + test->stats->skip_count !=
    1325         [ -  + ]:          50 :                     test->stats->run_count) {
    1326                 :           0 :                         PRINT_DATA("Bad stats for %s.%s\n", test->test_suite_name, test->name);
    1327                 :           0 :                         test_status = ZTEST_STATUS_HAS_FAILURE;
    1328                 :             :                 }
    1329                 :             :         }
    1330                 :           1 : }
    1331                 :             : 
    1332                 :           1 : void ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter)
    1333                 :             : {
    1334                 :           1 :         ztest_api.run_all(state, shuffle, suite_iter, case_iter);
    1335                 :           1 : }
    1336                 :             : 
    1337                 :           1 : void __weak test_main(void)
    1338                 :             : {
    1339                 :             : #if CONFIG_ZTEST_SHUFFLE
    1340                 :             :         ztest_run_all(NULL, true, NUM_ITER_PER_SUITE, NUM_ITER_PER_TEST);
    1341                 :             : #else
    1342                 :           1 :         ztest_run_all(NULL, false, NUM_ITER_PER_SUITE, NUM_ITER_PER_TEST);
    1343                 :             : #endif
    1344                 :           1 :         ztest_verify_all_test_suites_ran();
    1345                 :           1 : }
    1346                 :             : 
    1347                 :             : #ifdef ZTEST_UNITTEST
    1348                 :             : int main(void)
    1349                 :             : {
    1350                 :             :         TC_PRINT_RUNID_START;
    1351                 :             :         z_init_mock();
    1352                 :             :         test_main();
    1353                 :             :         end_report();
    1354                 :             : #ifdef CONFIG_ZTEST_NO_YIELD
    1355                 :             :         /*
    1356                 :             :          * Rather than yielding to idle thread, keep the part awake so debugger can
    1357                 :             :          * still access it, since some SOCs cannot be debugged in low power states.
    1358                 :             :          */
    1359                 :             :         uint32_t key = irq_lock();
    1360                 :             : 
    1361                 :             :         while (1) {
    1362                 :             :                 ; /* Spin */
    1363                 :             :         }
    1364                 :             :         irq_unlock(key);
    1365                 :             : #endif
    1366                 :             :         return test_status;
    1367                 :             : }
    1368                 :             : #else
    1369                 :             : 
    1370                 :             : /* Shell */
    1371                 :             : 
    1372                 :             : #ifdef CONFIG_ZTEST_SHELL
    1373                 :             : static int cmd_list_suites(const struct shell *sh, size_t argc, char **argv)
    1374                 :             : {
    1375                 :             :         struct ztest_suite_node *suite;
    1376                 :             : 
    1377                 :             :         for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end; ++suite) {
    1378                 :             :                 shell_print(sh, "%s", suite->name);
    1379                 :             :         }
    1380                 :             :         return 0;
    1381                 :             : }
    1382                 :             : 
    1383                 :             : static int cmd_list_cases(const struct shell *sh, size_t argc, char **argv)
    1384                 :             : {
    1385                 :             :         struct ztest_suite_node *ptr;
    1386                 :             :         struct ztest_unit_test *test = NULL;
    1387                 :             :         int test_count = 0;
    1388                 :             : 
    1389                 :             :         for (ptr = _ztest_suite_node_list_start; ptr < _ztest_suite_node_list_end; ++ptr) {
    1390                 :             :                 for (test = z_ztest_get_next_test(ptr->name, NULL); test != NULL;
    1391                 :             :                      test = z_ztest_get_next_test(ptr->name, test)) {
    1392                 :             :                         shell_print(sh, "%s::%s", test->test_suite_name, test->name);
    1393                 :             :                         test_count++;
    1394                 :             :                 }
    1395                 :             :         }
    1396                 :             :         return 0;
    1397                 :             : }
    1398                 :             : extern void ztest_set_test_args(char *argv);
    1399                 :             : extern void ztest_reset_test_args(void);
    1400                 :             : 
    1401                 :             : static int cmd_runall(const struct shell *sh, size_t argc, char **argv)
    1402                 :             : {
    1403                 :             :         ztest_reset_test_args();
    1404                 :             :         ztest_run_all(NULL, false, 1, 1);
    1405                 :             :         end_report();
    1406                 :             :         return 0;
    1407                 :             : }
    1408                 :             : 
    1409                 :             : #ifdef CONFIG_ZTEST_SHUFFLE
    1410                 :             : static int cmd_shuffle(const struct shell *sh, size_t argc, char **argv)
    1411                 :             : {
    1412                 :             : 
    1413                 :             :         struct sys_getopt_state *state;
    1414                 :             :         int opt;
    1415                 :             :         static struct sys_getopt_option long_options[] = {
    1416                 :             :                 {"suite_iter", sys_getopt_required_argument, 0, 's'},
    1417                 :             :                 {"case_iter", sys_getopt_required_argument, 0, 'c'},
    1418                 :             :                 {0, 0, 0, 0}};
    1419                 :             :         int opt_index = 0;
    1420                 :             :         int val;
    1421                 :             :         int opt_num = 0;
    1422                 :             : 
    1423                 :             :         int suite_iter = 1;
    1424                 :             :         int case_iter = 1;
    1425                 :             : 
    1426                 :             :         while ((opt = sys_getopt_long(argc, argv, "s:c:", long_options, &opt_index)) != -1) {
    1427                 :             :                 state = sys_getopt_state_get();
    1428                 :             :                 switch (opt) {
    1429                 :             :                 case 's':
    1430                 :             :                         val = atoi(state->optarg);
    1431                 :             :                         if (val < 1) {
    1432                 :             :                                 shell_error(sh, "Invalid number of suite iterations");
    1433                 :             :                                 return -ENOEXEC;
    1434                 :             :                         }
    1435                 :             :                         suite_iter = val;
    1436                 :             :                         opt_num++;
    1437                 :             :                         break;
    1438                 :             :                 case 'c':
    1439                 :             :                         val = atoi(state->optarg);
    1440                 :             :                         if (val < 1) {
    1441                 :             :                                 shell_error(sh, "Invalid number of case iterations");
    1442                 :             :                                 return -ENOEXEC;
    1443                 :             :                         }
    1444                 :             :                         case_iter = val;
    1445                 :             :                         opt_num++;
    1446                 :             :                         break;
    1447                 :             :                 default:
    1448                 :             :                         shell_error(sh, "Invalid option or option usage: %s",
    1449                 :             :                                     argv[opt_index + 1]);
    1450                 :             :                         return -ENOEXEC;
    1451                 :             :                 }
    1452                 :             :         }
    1453                 :             :         ztest_reset_test_args();
    1454                 :             :         ztest_run_all(NULL, true, suite_iter, case_iter);
    1455                 :             :         end_report();
    1456                 :             :         return 0;
    1457                 :             : }
    1458                 :             : #endif
    1459                 :             : 
    1460                 :             : static int cmd_run_suite(const struct shell *sh, size_t argc, char **argv)
    1461                 :             : {
    1462                 :             :         struct sys_getopt_state *state;
    1463                 :             :         int opt;
    1464                 :             :         static struct sys_getopt_option long_options[] = {
    1465                 :             :                 {"repeat_iter", sys_getopt_required_argument, NULL, 'r'},
    1466                 :             :                 {NULL, 0, NULL, 0}};
    1467                 :             :         int opt_index = 0;
    1468                 :             :         int val;
    1469                 :             :         int opt_num = 0;
    1470                 :             :         void *param = NULL;
    1471                 :             :         int repeat_iter = 1;
    1472                 :             : 
    1473                 :             :         while ((opt = sys_getopt_long(argc, argv, "r:p:", long_options, &opt_index)) != -1) {
    1474                 :             :                 state = sys_getopt_state_get();
    1475                 :             :                 switch (opt) {
    1476                 :             :                 case 'r':
    1477                 :             :                         val = atoi(state->optarg);
    1478                 :             :                         if (val < 1) {
    1479                 :             :                                 shell_fprintf(sh, SHELL_ERROR,
    1480                 :             :                                         "Invalid number of suite iterations\n");
    1481                 :             :                                 return -ENOEXEC;
    1482                 :             :                         }
    1483                 :             :                         repeat_iter = val;
    1484                 :             :                         opt_num++;
    1485                 :             :                         break;
    1486                 :             :                 case 'p':
    1487                 :             :                         param = state->optarg;
    1488                 :             :                         opt_num++;
    1489                 :             :                         break;
    1490                 :             :                 default:
    1491                 :             :                         shell_fprintf(sh, SHELL_ERROR,
    1492                 :             :                                 "Invalid option or option usage: %s\n", argv[opt_index + 1]);
    1493                 :             :                         return -ENOEXEC;
    1494                 :             :                 }
    1495                 :             :         }
    1496                 :             :         int count = 0;
    1497                 :             :         bool shuffle = false;
    1498                 :             :         const char *shell_command = argv[0];
    1499                 :             : 
    1500                 :             :         /*
    1501                 :             :          * This if statement determines which argv contains the test name.
    1502                 :             :          * If the optional argument is used, the test name is in the third
    1503                 :             :          * argv instead of the first.
    1504                 :             :          */
    1505                 :             :         if (opt_num == 1) {
    1506                 :             :                 ztest_set_test_args(argv[3]);
    1507                 :             :         } else {
    1508                 :             :                 ztest_set_test_args(argv[1]);
    1509                 :             :         }
    1510                 :             : 
    1511                 :             :         for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
    1512                 :             :              ptr < _ztest_suite_node_list_end; ++ptr) {
    1513                 :             :                 if (strcmp(shell_command, "run-testcase") == 0) {
    1514                 :             :                         count += __ztest_run_test_suite(ptr, NULL, shuffle, 1, repeat_iter, param);
    1515                 :             :                 } else if (strcmp(shell_command, "run-testsuite") == 0) {
    1516                 :             :                         count += __ztest_run_test_suite(ptr, NULL, shuffle, repeat_iter, 1, NULL);
    1517                 :             :                 }
    1518                 :             :                 if (test_status == ZTEST_STATUS_CRITICAL_ERROR ||
    1519                 :             :                     (test_status == ZTEST_STATUS_HAS_FAILURE && FAIL_FAST)) {
    1520                 :             :                         break;
    1521                 :             :                 }
    1522                 :             :         }
    1523                 :             :         return 0;
    1524                 :             : }
    1525                 :             : 
    1526                 :             : static void testsuite_list_get(size_t idx, struct shell_static_entry *entry);
    1527                 :             : 
    1528                 :             : SHELL_DYNAMIC_CMD_CREATE(testsuite_names, testsuite_list_get);
    1529                 :             : 
    1530                 :             : static size_t testsuite_get_all_static(struct ztest_suite_node const **suites)
    1531                 :             : {
    1532                 :             :         *suites = _ztest_suite_node_list_start;
    1533                 :             :         return _ztest_suite_node_list_end - _ztest_suite_node_list_start;
    1534                 :             : }
    1535                 :             : 
    1536                 :             : static const struct ztest_suite_node *suite_lookup(size_t idx, const char *prefix)
    1537                 :             : {
    1538                 :             :         size_t match_idx = 0;
    1539                 :             :         const struct ztest_suite_node *suite;
    1540                 :             :         size_t len = testsuite_get_all_static(&suite);
    1541                 :             :         const struct ztest_suite_node *suite_end = suite + len;
    1542                 :             : 
    1543                 :             :         while (suite < suite_end) {
    1544                 :             :                 if ((suite->name != NULL) && (strlen(suite->name) != 0) &&
    1545                 :             :                     ((prefix == NULL) || (strncmp(prefix, suite->name, strlen(prefix)) == 0))) {
    1546                 :             :                         if (match_idx == idx) {
    1547                 :             :                                 return suite;
    1548                 :             :                         }
    1549                 :             :                         ++match_idx;
    1550                 :             :                 }
    1551                 :             :                 ++suite;
    1552                 :             :         }
    1553                 :             : 
    1554                 :             :         return NULL;
    1555                 :             : }
    1556                 :             : 
    1557                 :             : static void testsuite_list_get(size_t idx, struct shell_static_entry *entry)
    1558                 :             : {
    1559                 :             :         const struct ztest_suite_node *suite = suite_lookup(idx, "");
    1560                 :             : 
    1561                 :             :         entry->syntax = (suite != NULL) ? suite->name : NULL;
    1562                 :             :         entry->handler = NULL;
    1563                 :             :         entry->help = NULL;
    1564                 :             :         entry->subcmd = NULL;
    1565                 :             : }
    1566                 :             : 
    1567                 :             : /* clang-format off */
    1568                 :             :         SHELL_STATIC_SUBCMD_SET_CREATE(
    1569                 :             :                 sub_ztest_cmds,
    1570                 :             :                 SHELL_CMD_ARG(run-all, NULL, "Run all tests", cmd_runall, 0, 0),
    1571                 :             : #ifdef CONFIG_ZTEST_SHUFFLE
    1572                 :             :                 SHELL_COND_CMD_ARG(CONFIG_ZTEST_SHUFFLE, shuffle, NULL,
    1573                 :             :                         "Shuffle tests", cmd_shuffle, 0, 2),
    1574                 :             : #endif
    1575                 :             :                 SHELL_CMD_ARG(list-testsuites, NULL,
    1576                 :             :                         "List all test suites", cmd_list_suites, 0, 0),
    1577                 :             :                 SHELL_CMD_ARG(list-testcases, NULL,
    1578                 :             :                         "List all test cases", cmd_list_cases, 0, 0),
    1579                 :             :                 SHELL_CMD_ARG(run-testsuite, &testsuite_names,
    1580                 :             :                         "Run test suite", cmd_run_suite, 2, 2),
    1581                 :             :                 SHELL_CMD_ARG(run-testcase, NULL, "Run testcase", cmd_run_suite, 2, 2),
    1582                 :             :                 SHELL_SUBCMD_SET_END /* Array terminated. */
    1583                 :             :         );
    1584                 :             : /* clang-format on */
    1585                 :             : 
    1586                 :             : SHELL_CMD_REGISTER(ztest, &sub_ztest_cmds, "Ztest commands", NULL);
    1587                 :             : #endif /* CONFIG_ZTEST_SHELL */
    1588                 :             : 
    1589                 :           1 : int main(void)
    1590                 :             : {
    1591                 :             : #ifdef CONFIG_USERSPACE
    1592                 :             :         /* Partition containing globals tagged with ZTEST_DMEM and ZTEST_BMEM
    1593                 :             :          * macros. Any variables that user code may reference need to be
    1594                 :             :          * placed in this partition if no other memory domain configuration
    1595                 :             :          * is made.
    1596                 :             :          */
    1597                 :             :         k_mem_domain_add_partition(&k_mem_domain_default, &ztest_mem_partition);
    1598                 :             : #ifdef Z_MALLOC_PARTITION_EXISTS
    1599                 :             :         /* Allow access to malloc() memory */
    1600                 :             :         if (z_malloc_partition.size != 0U) {
    1601                 :             :                 k_mem_domain_add_partition(&k_mem_domain_default, &z_malloc_partition);
    1602                 :             :         }
    1603                 :             : #endif
    1604                 :             : #endif /* CONFIG_USERSPACE */
    1605                 :             : 
    1606                 :           1 :         TC_PRINT_RUNID_START;
    1607                 :           1 :         z_init_mock();
    1608                 :             : #ifndef CONFIG_ZTEST_SHELL
    1609                 :           1 :         test_main();
    1610                 :             : #ifdef CONFIG_ZTEST_BENCHMARK
    1611                 :             :         benchmark_main();
    1612                 :             : #endif
    1613                 :           1 :         end_report();
    1614                 :             :         log_flush();
    1615                 :             :         LOG_PANIC();
    1616                 :             :         if (IS_ENABLED(CONFIG_ZTEST_RETEST_IF_PASSED)) {
    1617                 :             :                 static __noinit struct {
    1618                 :             :                         uint32_t magic;
    1619                 :             :                         uint32_t boots;
    1620                 :             :                 } state;
    1621                 :             :                 const uint32_t magic = 0x152ac523;
    1622                 :             : 
    1623                 :             :                 if (state.magic != magic) {
    1624                 :             :                         state.magic = magic;
    1625                 :             :                         state.boots = 0;
    1626                 :             :                 }
    1627                 :             :                 state.boots += 1;
    1628                 :             :                 if (test_status == 0) {
    1629                 :             :                         PRINT_DATA("Reset board #%u to test again\n", state.boots);
    1630                 :             :                         (void)k_msleep(10);
    1631                 :             :                         sys_reboot(SYS_REBOOT_COLD);
    1632                 :             :                 } else {
    1633                 :             :                         PRINT_DATA("Failed after %u attempts\n", state.boots);
    1634                 :             :                         state.boots = 0;
    1635                 :             :                 }
    1636                 :             :         }
    1637                 :             : #ifdef CONFIG_ZTEST_NO_YIELD
    1638                 :             :         /*
    1639                 :             :          * Rather than yielding to idle thread, keep the part awake so debugger can
    1640                 :             :          * still access it, since some SOCs cannot be debugged in low power states.
    1641                 :             :          */
    1642                 :             :         uint32_t key = irq_lock();
    1643                 :             : 
    1644                 :             :         while (1) {
    1645                 :             :                 ; /* Spin */
    1646                 :             :         }
    1647                 :             :         irq_unlock(key);
    1648                 :             : #endif /* CONFIG_ZTEST_NO_YIELD */
    1649                 :             : #endif /* CONFIG_ZTEST_SHELL */
    1650                 :             :         return 0;
    1651                 :             : }
    1652                 :             : #endif
        

Generated by: LCOV version 2.0-1