xref: /openbmc/u-boot/test/cmd_ut.c (revision cd1cc31f)
1 /*
2  * (C) Copyright 2015
3  * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4  *
5  * SPDX-License-Identifier:	GPL-2.0
6  */
7 
8 #include <common.h>
9 #include <command.h>
10 #include <test/suites.h>
11 #include <test/test.h>
12 
13 static int do_ut_all(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
14 
15 int cmd_ut_category(const char *name, struct unit_test *tests, int n_ents,
16 		    int argc, char * const argv[])
17 {
18 	struct unit_test_state uts = { .fail_count = 0 };
19 	struct unit_test *test;
20 
21 	if (argc == 1)
22 		printf("Running %d %s tests\n", n_ents, name);
23 
24 	for (test = tests; test < tests + n_ents; test++) {
25 		if (argc > 1 && strcmp(argv[1], test->name))
26 			continue;
27 		printf("Test: %s\n", test->name);
28 
29 		uts.start = mallinfo();
30 
31 		test->func(&uts);
32 	}
33 
34 	printf("Failures: %d\n", uts.fail_count);
35 
36 	return uts.fail_count ? CMD_RET_FAILURE : 0;
37 }
38 
39 static cmd_tbl_t cmd_ut_sub[] = {
40 	U_BOOT_CMD_MKENT(all, CONFIG_SYS_MAXARGS, 1, do_ut_all, "", ""),
41 #if defined(CONFIG_UT_DM)
42 	U_BOOT_CMD_MKENT(dm, CONFIG_SYS_MAXARGS, 1, do_ut_dm, "", ""),
43 #endif
44 #if defined(CONFIG_UT_ENV)
45 	U_BOOT_CMD_MKENT(env, CONFIG_SYS_MAXARGS, 1, do_ut_env, "", ""),
46 #endif
47 #ifdef CONFIG_UT_OVERLAY
48 	U_BOOT_CMD_MKENT(overlay, CONFIG_SYS_MAXARGS, 1, do_ut_overlay, "", ""),
49 #endif
50 #ifdef CONFIG_UT_TIME
51 	U_BOOT_CMD_MKENT(time, CONFIG_SYS_MAXARGS, 1, do_ut_time, "", ""),
52 #endif
53 #ifdef CONFIG_SANDBOX
54 	U_BOOT_CMD_MKENT(compression, CONFIG_SYS_MAXARGS, 1, do_ut_compression,
55 			 "", ""),
56 #endif
57 };
58 
59 static int do_ut_all(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
60 {
61 	int i;
62 	int retval;
63 	int any_fail = 0;
64 
65 	for (i = 1; i < ARRAY_SIZE(cmd_ut_sub); i++) {
66 		printf("----Running %s tests----\n", cmd_ut_sub[i].name);
67 		retval = cmd_ut_sub[i].cmd(cmdtp, flag, 1, &cmd_ut_sub[i].name);
68 		if (!any_fail)
69 			any_fail = retval;
70 	}
71 
72 	return any_fail;
73 }
74 
75 static int do_ut(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
76 {
77 	cmd_tbl_t *cp;
78 
79 	if (argc < 2)
80 		return CMD_RET_USAGE;
81 
82 	/* drop initial "ut" arg */
83 	argc--;
84 	argv++;
85 
86 	cp = find_cmd_tbl(argv[0], cmd_ut_sub, ARRAY_SIZE(cmd_ut_sub));
87 
88 	if (cp)
89 		return cp->cmd(cmdtp, flag, argc, argv);
90 
91 	return CMD_RET_USAGE;
92 }
93 
94 #ifdef CONFIG_SYS_LONGHELP
95 static char ut_help_text[] =
96 	"all - execute all enabled tests\n"
97 #ifdef CONFIG_UT_DM
98 	"ut dm [test-name]\n"
99 #endif
100 #ifdef CONFIG_UT_ENV
101 	"ut env [test-name]\n"
102 #endif
103 #ifdef CONFIG_UT_OVERLAY
104 	"ut overlay [test-name]\n"
105 #endif
106 #ifdef CONFIG_UT_TIME
107 	"ut time - Very basic test of time functions\n"
108 #endif
109 #ifdef CONFIG_SANDBOX
110 	"ut compression - Test compressors and bootm decompression\n"
111 #endif
112 	;
113 #endif
114 
115 U_BOOT_CMD(
116 	ut, CONFIG_SYS_MAXARGS, 1, do_ut,
117 	"unit tests", ut_help_text
118 );
119