auto_test.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "auto_test.h"
  5. #include "rtconfig.h"
  6. #define MAX_TESTS 100
  7. typedef int (*TestFunc)(void);
  8. typedef struct {
  9. const char* name;
  10. TestFunc func;
  11. rt_err_t passed;
  12. } TestCase;
  13. TestCase test_cases[MAX_TESTS];
  14. int test_count = 0;
  15. // 注册测试用例
  16. void register_test(const char* name, TestFunc func) {
  17. if (test_count < MAX_TESTS) {
  18. test_cases[test_count].name = name;
  19. test_cases[test_count].func = func;
  20. test_cases[test_count].passed = 0;
  21. test_count++;
  22. } else {
  23. rt_kprintf("Exceeded the maximum number of test cases(%d)\n", MAX_TESTS);
  24. }
  25. }
  26. // 运行所有测试
  27. void run_all_tests() {
  28. int passed_count = 0;
  29. rt_kprintf("Run tests...\n");
  30. for (int i = 0; i < test_count; i++) {
  31. rt_err_t result = test_cases[i].func();
  32. test_cases[i].passed = result;
  33. if (result == RT_EOK) {
  34. rt_kprintf("[PASS] %s\n", test_cases[i].name);
  35. passed_count++;
  36. } else {
  37. rt_kprintf("[FAIL] %s %d\n", test_cases[i].name, result);
  38. }
  39. }
  40. #if defined(TARGET_ARMV8_AARCH64)
  41. rt_kprintf("\n%s aarch64 test results: \n", BOARD_NAME);
  42. #else
  43. rt_kprintf("\n%s aarch32 test results: \n", BOARD_NAME);
  44. #endif
  45. rt_kprintf("PASS: %d / %d\n", passed_count, test_count);
  46. if (passed_count < test_count)
  47. {
  48. rt_kprintf("[test_failure] example:\n");
  49. for (int i = 0; i < test_count; i++) {
  50. if (test_cases[i].passed != RT_EOK) {
  51. rt_kprintf(" - %s\n", test_cases[i].name);
  52. }
  53. }
  54. }
  55. else
  56. {
  57. rt_kprintf("[test_success]\n");
  58. }
  59. rt_kprintf("[rtthread_test_end]\n");
  60. }
  61. int auto_test() {
  62. #if defined BSP_USING_CAN
  63. register_test("can_loopback_sample", can_loopback_sample);
  64. #endif
  65. #if defined BSP_USING_SPI
  66. register_test("spi_sample", fspim_test_sample);
  67. #endif
  68. #if defined BSP_USING_GPIO
  69. register_test("gpio_sample", gpio_toggle_sample);
  70. #endif
  71. #if defined BSP_USING_I2C
  72. #if defined (PD2408_TEST_A_BOARD) || defined (PD2408_TEST_B_BOARD)
  73. register_test("i2c_msg_sample", i2c_msg_sample);
  74. #else
  75. register_test("i2c_sample", i2c_sample);
  76. #endif
  77. #endif
  78. #if defined BSP_USING_QSPI
  79. #if !defined(TARGET_PD2408)
  80. register_test("qspi_sample", qspi_sample);
  81. #endif
  82. #endif
  83. // 运行测试
  84. run_all_tests();
  85. return 0;
  86. }