subcmd-help.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "tests.h"
  3. #include <linux/compiler.h>
  4. #include <subcmd/help.h>
  5. static int test__load_cmdnames(struct test_suite *test __maybe_unused,
  6. int subtest __maybe_unused)
  7. {
  8. struct cmdnames cmds = {};
  9. add_cmdname(&cmds, "aaa", 3);
  10. add_cmdname(&cmds, "foo", 3);
  11. add_cmdname(&cmds, "xyz", 3);
  12. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds, "aaa") == 1);
  13. TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds, "bar") == 0);
  14. TEST_ASSERT_VAL("case sensitive", is_in_cmdlist(&cmds, "XYZ") == 0);
  15. clean_cmdnames(&cmds);
  16. return TEST_OK;
  17. }
  18. static int test__uniq_cmdnames(struct test_suite *test __maybe_unused,
  19. int subtest __maybe_unused)
  20. {
  21. struct cmdnames cmds = {};
  22. /* uniq() assumes it's sorted */
  23. add_cmdname(&cmds, "aaa", 3);
  24. add_cmdname(&cmds, "aaa", 3);
  25. add_cmdname(&cmds, "bbb", 3);
  26. TEST_ASSERT_VAL("invalid original size", cmds.cnt == 3);
  27. /* uniquify command names (to remove second 'aaa') */
  28. uniq(&cmds);
  29. TEST_ASSERT_VAL("invalid final size", cmds.cnt == 2);
  30. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds, "aaa") == 1);
  31. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds, "bbb") == 1);
  32. TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds, "ccc") == 0);
  33. clean_cmdnames(&cmds);
  34. return TEST_OK;
  35. }
  36. static int test__exclude_cmdnames(struct test_suite *test __maybe_unused,
  37. int subtest __maybe_unused)
  38. {
  39. struct cmdnames cmds1 = {};
  40. struct cmdnames cmds2 = {};
  41. add_cmdname(&cmds1, "aaa", 3);
  42. add_cmdname(&cmds1, "bbb", 3);
  43. add_cmdname(&cmds1, "ccc", 3);
  44. add_cmdname(&cmds1, "ddd", 3);
  45. add_cmdname(&cmds1, "eee", 3);
  46. add_cmdname(&cmds1, "fff", 3);
  47. add_cmdname(&cmds1, "ggg", 3);
  48. add_cmdname(&cmds1, "hhh", 3);
  49. add_cmdname(&cmds1, "iii", 3);
  50. add_cmdname(&cmds1, "jjj", 3);
  51. add_cmdname(&cmds2, "bbb", 3);
  52. add_cmdname(&cmds2, "eee", 3);
  53. add_cmdname(&cmds2, "jjj", 3);
  54. TEST_ASSERT_VAL("invalid original size", cmds1.cnt == 10);
  55. TEST_ASSERT_VAL("invalid original size", cmds2.cnt == 3);
  56. /* remove duplicate command names in cmds1 */
  57. exclude_cmds(&cmds1, &cmds2);
  58. TEST_ASSERT_VAL("invalid excluded size", cmds1.cnt == 7);
  59. TEST_ASSERT_VAL("invalid excluded size", cmds2.cnt == 3);
  60. /* excluded commands should not belong to cmds1 */
  61. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "aaa") == 1);
  62. TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "bbb") == 0);
  63. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "ccc") == 1);
  64. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "ddd") == 1);
  65. TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "eee") == 0);
  66. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "fff") == 1);
  67. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "ggg") == 1);
  68. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "hhh") == 1);
  69. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "iii") == 1);
  70. TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "jjj") == 0);
  71. /* they should be only in cmds2 */
  72. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds2, "bbb") == 1);
  73. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds2, "eee") == 1);
  74. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds2, "jjj") == 1);
  75. clean_cmdnames(&cmds1);
  76. clean_cmdnames(&cmds2);
  77. return TEST_OK;
  78. }
  79. static int test__exclude_cmdnames_no_overlap(struct test_suite *test __maybe_unused,
  80. int subtest __maybe_unused)
  81. {
  82. struct cmdnames cmds1 = {};
  83. struct cmdnames cmds2 = {};
  84. add_cmdname(&cmds1, "read-vdso32", 11);
  85. add_cmdname(&cmds2, "archive", 7);
  86. TEST_ASSERT_VAL("invalid original size", cmds1.cnt == 1);
  87. TEST_ASSERT_VAL("invalid original size", cmds2.cnt == 1);
  88. exclude_cmds(&cmds1, &cmds2);
  89. TEST_ASSERT_VAL("invalid excluded size", cmds1.cnt == 1);
  90. TEST_ASSERT_VAL("invalid excluded size", cmds2.cnt == 1);
  91. TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "read-vdso32") == 1);
  92. TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "archive") == 0);
  93. clean_cmdnames(&cmds1);
  94. clean_cmdnames(&cmds2);
  95. return TEST_OK;
  96. }
  97. static struct test_case tests__subcmd_help[] = {
  98. TEST_CASE("Load subcmd names", load_cmdnames),
  99. TEST_CASE("Uniquify subcmd names", uniq_cmdnames),
  100. TEST_CASE("Exclude duplicate subcmd names", exclude_cmdnames),
  101. TEST_CASE("Exclude disjoint subcmd names", exclude_cmdnames_no_overlap),
  102. { .name = NULL, }
  103. };
  104. struct test_suite suite__subcmd_help = {
  105. .desc = "libsubcmd help tests",
  106. .test_cases = tests__subcmd_help,
  107. };