test_utils.h 8.0 KB

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