test.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2013 Google, Inc.
  4. */
  5. #ifndef __TEST_TEST_H
  6. #define __TEST_TEST_H
  7. #include <malloc.h>
  8. #include <linux/bitops.h>
  9. /*
  10. * struct unit_test_state - Entire state of test system
  11. *
  12. * @fail_count: Number of tests that failed
  13. * @start: Store the starting mallinfo when doing leak test
  14. * @of_live: true to use livetree if available, false to use flattree
  15. * @of_root: Record of the livetree root node (used for setting up tests)
  16. * @root: Root device
  17. * @testdev: Test device
  18. * @force_fail_alloc: Force all memory allocs to fail
  19. * @skip_post_probe: Skip uclass post-probe processing
  20. * @expect_str: Temporary string used to hold expected string value
  21. * @actual_str: Temporary string used to hold actual string value
  22. */
  23. struct unit_test_state {
  24. int fail_count;
  25. struct mallinfo start;
  26. struct device_node *of_root;
  27. bool of_live;
  28. struct udevice *root;
  29. struct udevice *testdev;
  30. int force_fail_alloc;
  31. int skip_post_probe;
  32. char expect_str[512];
  33. char actual_str[512];
  34. };
  35. /* Test flags for each test */
  36. enum {
  37. UT_TESTF_SCAN_PDATA = BIT(0), /* test needs platform data */
  38. UT_TESTF_PROBE_TEST = BIT(1), /* probe test uclass */
  39. UT_TESTF_SCAN_FDT = BIT(2), /* scan device tree */
  40. UT_TESTF_FLAT_TREE = BIT(3), /* test needs flat DT */
  41. UT_TESTF_LIVE_TREE = BIT(4), /* needs live device tree */
  42. UT_TESTF_CONSOLE_REC = BIT(5), /* needs console recording */
  43. /* do extra driver model init and uninit */
  44. UT_TESTF_DM = BIT(6),
  45. };
  46. /**
  47. * struct unit_test - Information about a unit test
  48. *
  49. * @name: Name of test
  50. * @func: Function to call to perform test
  51. * @flags: Flags indicated pre-conditions for test
  52. */
  53. struct unit_test {
  54. const char *file;
  55. const char *name;
  56. int (*func)(struct unit_test_state *state);
  57. int flags;
  58. };
  59. /**
  60. * UNIT_TEST() - create linker generated list entry for unit a unit test
  61. *
  62. * The macro UNIT_TEST() is used to create a linker generated list entry. These
  63. * list entries are enumerate tests that can be execute using the ut command.
  64. * The list entries are used both by the implementation of the ut command as
  65. * well as in a related Python test.
  66. *
  67. * For Python testing the subtests are collected in Python function
  68. * generate_ut_subtest() by applying a regular expression to the lines of file
  69. * u-boot.sym. The list entries have to follow strict naming conventions to be
  70. * matched by the expression.
  71. *
  72. * Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in test suite
  73. * foo that can be executed via command 'ut foo bar' and is implemented in
  74. * function foo_test_bar().
  75. *
  76. * @_name: concatenation of name of the test suite, "_test_", and the name
  77. * of the test
  78. * @_flags: an integer field that can be evaluated by the test suite
  79. * implementation
  80. * @_suite: name of the test suite concatenated with "_test"
  81. */
  82. #define UNIT_TEST(_name, _flags, _suite) \
  83. ll_entry_declare(struct unit_test, _name, ut_ ## _suite) = { \
  84. .file = __FILE__, \
  85. .name = #_name, \
  86. .flags = _flags, \
  87. .func = _name, \
  88. }
  89. /* Get the start of a list of unit tests for a particular suite */
  90. #define UNIT_TEST_SUITE_START(_suite) \
  91. ll_entry_start(struct unit_test, ut_ ## _suite)
  92. #define UNIT_TEST_SUITE_COUNT(_suite) \
  93. ll_entry_count(struct unit_test, ut_ ## _suite)
  94. /* Use ! and ~ so that all tests will be sorted between these two values */
  95. #define UNIT_TEST_ALL_START() ll_entry_start(struct unit_test, ut_!)
  96. #define UNIT_TEST_ALL_END() ll_entry_start(struct unit_test, ut_~)
  97. #define UNIT_TEST_ALL_COUNT() (UNIT_TEST_ALL_END() - UNIT_TEST_ALL_START())
  98. /* Sizes for devres tests */
  99. enum {
  100. TEST_DEVRES_SIZE = 100,
  101. TEST_DEVRES_COUNT = 10,
  102. TEST_DEVRES_TOTAL = TEST_DEVRES_SIZE * TEST_DEVRES_COUNT,
  103. /* A few different sizes */
  104. TEST_DEVRES_SIZE2 = 15,
  105. TEST_DEVRES_SIZE3 = 37,
  106. };
  107. /**
  108. * testbus_get_clear_removed() - Test function to obtain removed device
  109. *
  110. * This is used in testbus to find out which device was removed. Calling this
  111. * function returns a pointer to the device and then clears it back to NULL, so
  112. * that a future test can check it.
  113. */
  114. struct udevice *testbus_get_clear_removed(void);
  115. static inline void arch_reset_for_test(void)
  116. {
  117. #ifdef CONFIG_SANDBOX
  118. #include <asm/state.h>
  119. state_reset_for_test(state_get_current());
  120. #endif
  121. }
  122. #endif /* __TEST_TEST_H */