test_utils.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. // Utilities for esp-idf unit tests
  8. #include <stdint.h>
  9. #include <esp_partition.h>
  10. #include "sdkconfig.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "unity.h"
  14. #include "soc/soc_caps.h"
  15. /* include performance pass standards header file */
  16. #include "idf_performance.h"
  17. #include "idf_performance_target.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /* For performance check with unity test on IDF */
  22. /* These macros should only be used with ESP-IDF.
  23. * To use performance check, we need to first define pass standard in idf_performance.h.
  24. */
  25. //macros call this to expand an argument instead of directly converting into str
  26. #define PERFORMANCE_STR(s) #s
  27. //macros call this to contact strings after expanding them
  28. #define PERFORMANCE_CON(a, b) _PERFORMANCE_CON(a, b)
  29. #define _PERFORMANCE_CON(a, b) a##b
  30. #if !CONFIG_UNITY_IGNORE_PERFORMANCE_TESTS
  31. #define _TEST_PERFORMANCE_ASSERT TEST_ASSERT
  32. #else
  33. #define _TEST_PERFORMANCE_ASSERT(ARG) printf("Ignoring performance test [%s]\n", PERFORMANCE_STR(ARG))
  34. #endif
  35. #define TEST_PERFORMANCE_LESS_THAN(name, value_fmt, value, ...) do { \
  36. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__); \
  37. _TEST_PERFORMANCE_ASSERT(value < PERFORMANCE_CON(IDF_PERFORMANCE_MAX_, name)); \
  38. } while(0)
  39. #define TEST_PERFORMANCE_GREATER_THAN(name, value_fmt, value, ...) do { \
  40. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__); \
  41. _TEST_PERFORMANCE_ASSERT(value > PERFORMANCE_CON(IDF_PERFORMANCE_MIN_, name)); \
  42. } while(0)
  43. /* Macros to be used when performance is calculated using the cache compensated timer
  44. will not assert if ccomp not supported */
  45. #if SOC_CCOMP_TIMER_SUPPORTED
  46. #define TEST_PERFORMANCE_CCOMP_GREATER_THAN(name, value_fmt, value, ...) \
  47. TEST_PERFORMANCE_GREATER_THAN(name, value_fmt, value, ##__VA_ARGS__)
  48. #define TEST_PERFORMANCE_CCOMP_LESS_THAN(name, value_fmt, value, ...) \
  49. TEST_PERFORMANCE_LESS_THAN(name, value_fmt, value, ##__VA_ARGS__)
  50. #else
  51. #define TEST_PERFORMANCE_CCOMP_GREATER_THAN(name, value_fmt, value, ...) \
  52. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__)
  53. #define TEST_PERFORMANCE_CCOMP_LESS_THAN(name, value_fmt, value, ...) \
  54. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__)
  55. #endif //SOC_CCOMP_TIMER_SUPPORTED
  56. /* @brief macro to print IDF performance
  57. * @param item : performance item name. a string pointer.
  58. * @param value_fmt: print format and unit of the value, for example: "%02fms", "%dKB"
  59. * @param value : the performance value.
  60. */
  61. #define IDF_LOG_PERFORMANCE(item, value_fmt, value, ...) \
  62. printf("[Performance][%s]: " value_fmt "\n", item, value, ##__VA_ARGS__)
  63. /* Some definitions applicable to Unity running in FreeRTOS */
  64. #define UNITY_FREERTOS_PRIORITY CONFIG_UNITY_FREERTOS_PRIORITY
  65. #define UNITY_FREERTOS_CPU CONFIG_UNITY_FREERTOS_CPU
  66. #define UNITY_FREERTOS_STACK_SIZE CONFIG_UNITY_FREERTOS_STACK_SIZE
  67. /* Return the 'flash_test' custom data partition (type 0x55)
  68. defined in the custom partition table.
  69. */
  70. const esp_partition_t *get_test_data_partition(void);
  71. /**
  72. * @brief Initialize reference clock
  73. *
  74. * Reference clock provides timestamps at constant 1 MHz frequency, even when
  75. * the APB frequency is changing.
  76. */
  77. void ref_clock_init(void);
  78. /**
  79. * @brief Deinitialize reference clock
  80. */
  81. void ref_clock_deinit(void);
  82. /**
  83. * @brief Get reference clock timestamp
  84. * @return number of microseconds since the reference clock was initialized
  85. */
  86. uint64_t ref_clock_get(void);
  87. /**
  88. * @brief Entry point of the test application
  89. *
  90. * Starts Unity test runner in a separate task and returns.
  91. */
  92. void test_main(void);
  93. void unity_reset_leak_checks(void);
  94. /**
  95. * @brief Call this function from a test case which requires TCP/IP or
  96. * LWIP functionality.
  97. *
  98. * @note This should be the first function the test case calls, as it will
  99. * allocate memory on first use (and also reset the test case leak checker).
  100. */
  101. void test_case_uses_tcpip(void);
  102. /**
  103. * @brief wait for signals with parameters.
  104. *
  105. * for multiple devices test cases, DUT might need to wait for other DUTs before continue testing.
  106. * As all DUTs are independent, need user (or test script) interaction to make test synchronized.
  107. *
  108. * Here we provide signal functions for this.
  109. * For example, we're testing GPIO, DUT1 has one pin connect to with DUT2.
  110. * DUT2 will output high level and then DUT1 will read input.
  111. * DUT1 should call `unity_wait_for_signal("output high level");` before it reads input.
  112. * DUT2 should call `unity_send_signal("output high level");` after it finished setting output high level.
  113. * According to the console logs:
  114. *
  115. * DUT1 console:
  116. *
  117. * ```
  118. * Waiting for signal: [output high level]!
  119. * Please press "Enter" key to once any board send this signal.
  120. * ```
  121. *
  122. * DUT2 console:
  123. *
  124. * ```
  125. * Send signal: [output high level]!
  126. * ```
  127. *
  128. * Then we press Enter key on DUT1's console, DUT1 starts to read input and then test success.
  129. *
  130. * Another example, we have 2 DUTs in multiple devices test, and DUT1 need to get DUT2's mac address to perform BT connection.
  131. * DUT1 should call `unity_wait_for_signal_param("dut2 mac address", mac, 19);` to wait for DUT2's mac address.
  132. * DUT2 should call `unity_send_signal_param("dut2 mac address", "10:20:30:40:50:60");` to send to DUT1 its mac address.
  133. * According to the console logs:
  134. *
  135. * DUT1 console:
  136. *
  137. * ```
  138. * Waiting for signal: [dut2 mac address]!
  139. * Please input parameter value from any board send this signal and press "Enter" key.
  140. * ```
  141. *
  142. * DUT2 console:
  143. *
  144. * ```
  145. * Send signal: [dut2 mac address][10:20:30:40:50:60]!
  146. * ```
  147. *
  148. * @param signal_name signal name which DUT expected to wait before proceed testing
  149. * @param parameter_buf buffer to receive parameter
  150. * @param buf_len length of parameter_buf.
  151. * Currently we have a limitation that it will write 1 extra byte at the end of string.
  152. * We need to use a buffer with 2 bytes longer than actual string length.
  153. */
  154. void unity_wait_for_signal_param(const char* signal_name, char *parameter_buf, uint8_t buf_len);
  155. /**
  156. * @brief wait for signals.
  157. *
  158. * @param signal_name signal name which DUT expected to wait before proceed testing
  159. */
  160. static inline void unity_wait_for_signal(const char* signal_name)
  161. {
  162. unity_wait_for_signal_param(signal_name, NULL, 0);
  163. }
  164. /**
  165. * @brief DUT send signal and pass parameter to other devices.
  166. *
  167. * @param signal_name signal name which DUT send once it finished preparing.
  168. * @param parameter a string to let remote device to receive.
  169. */
  170. void unity_send_signal_param(const char* signal_name, const char *parameter);
  171. /**
  172. * @brief DUT send signal with parameter.
  173. *
  174. * @param signal_name signal name which DUT send once it finished preparing.
  175. */
  176. static inline void unity_send_signal(const char* signal_name)
  177. {
  178. unity_send_signal_param(signal_name, NULL);
  179. }
  180. /**
  181. * @brief convert mac string to mac address
  182. *
  183. * @param mac_str MAC address string with format "xx:xx:xx:xx:xx:xx"
  184. * @param[out] mac_addr store converted MAC address
  185. */
  186. bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr);
  187. typedef struct test_utils_exhaust_memory_record_s *test_utils_exhaust_memory_rec;
  188. /**
  189. * Limit the largest free block of memory with a particular capability set to
  190. * 'limit' bytes (meaning an allocation of 'limit' should succeed at least once,
  191. * but any allocation of more bytes will fail.)
  192. *
  193. * Returns a record pointer which needs to be passed back in to test_utils_free_exhausted_memory
  194. * before the test completes, to avoid a major memory leak.
  195. *
  196. * @param caps Capabilities of memory to exhause
  197. * @param limit The size to limit largest free block to
  198. * @return Record pointer to pass to test_utils_free_exhausted_memory() once done
  199. */
  200. test_utils_exhaust_memory_rec test_utils_exhaust_memory(uint32_t caps, size_t limit);
  201. /**
  202. * Call to free memory which was taken up by test_utils_exhaust_memory() call
  203. *
  204. * @param rec Result previously returned from test_utils_exhaust_memory()
  205. */
  206. void test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec);
  207. /**
  208. * @brief Delete task ensuring dynamic memory (for stack, tcb etc.) gets freed up immediately
  209. *
  210. * @param[in] thandle Handle of task to be deleted (should not be NULL or self handle)
  211. */
  212. void test_utils_task_delete(TaskHandle_t thandle);
  213. #ifdef __cplusplus
  214. }
  215. #endif