xref: /haiku/src/tools/cppunit/TestUtils.cpp (revision a1163de83ea633463a79de234b8742ee106531b2)
1 // TestUtils.cpp
2 
3 #include <TestUtils.h>
4 #include <TestShell.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <iostream>
9 
10 using std::cout;
11 using std::dec;
12 using std::endl;
13 using std::hex;
14 using std::string;
15 
16 _EXPORT
17 status_t DecodeResult(status_t result) {
18 	if (!BTestShell::GlobalBeVerbose())
19 		return result;
20 
21 	string str;
22 	switch (result) {
23 
24 		case B_OK:
25 			str = "B_OK";
26 			break;
27 
28 		case B_ERROR:
29 			str = "B_ERROR";
30 			break;
31 
32 
33 		// Storage Kit Errors
34 		case B_FILE_ERROR:
35 			str = "B_FILE_ERROR";
36 			break;
37 
38 		case B_FILE_NOT_FOUND:
39 			str = "B_FILE_NOT_FOUND";
40 			break;
41 
42 		case B_FILE_EXISTS:
43 			str = "B_FILE_EXISTS";
44 			break;
45 
46 		case B_ENTRY_NOT_FOUND:
47 			str = "B_ENTRY_NOT_FOUND";
48 			break;
49 
50 		case B_NAME_TOO_LONG:
51 			str = "B_NAME_TOO_LONG";
52 			break;
53 
54 		case B_DIRECTORY_NOT_EMPTY:
55 			str = "B_DIRECTORY_NOT_EMPTY";
56 			break;
57 
58 		case B_DEVICE_FULL:
59 			str = "B_DEVICE_FULL";
60 			break;
61 
62 		case B_READ_ONLY_DEVICE:
63 			str = "B_READ_ONLY_DEVICE";
64 			break;
65 
66 		case B_IS_A_DIRECTORY:
67 			str = "B_IS_A_DIRECTORY";
68 			break;
69 
70 		case B_NO_MORE_FDS:
71 			str = "B_NO_MORE_FDS";
72 			break;
73 
74 		case B_CROSS_DEVICE_LINK:
75 			str = "B_CROSS_DEVICE_LINK";
76 			break;
77 
78 		case B_LINK_LIMIT:
79 			str = "B_LINK_LIMIT";
80 			break;
81 
82 		case B_BUSTED_PIPE:
83 			str = "B_BUSTED_PIPE";
84 			break;
85 
86 		case B_UNSUPPORTED:
87 			str = "B_UNSUPPORTED";
88 			break;
89 
90 		case B_PARTITION_TOO_SMALL:
91 			str = "B_PARTITION_TOO_SMALL";
92 			break;
93 
94 		case B_BAD_MIME_SNIFFER_RULE:
95 			str = "B_BAD_MIME_SNIFFER_RULE";
96 			break;
97 
98 		// General Errors
99 		case B_NO_MEMORY:
100 			str = "B_NO_MEMORY";
101 			break;
102 
103 		case B_IO_ERROR:
104 			str = "B_IO_ERROR";
105 			break;
106 
107 		case B_PERMISSION_DENIED:
108 			str = "B_PERMISSION_DENIED";
109 			break;
110 
111 		case B_BAD_INDEX:
112 			str = "B_BAD_INDEX";
113 			break;
114 
115 		case B_BAD_TYPE:
116 			str = "B_BAD_TYPE";
117 			break;
118 
119 		case B_BAD_VALUE:
120 			str = "B_BAD_VALUE";
121 			break;
122 
123 		case B_MISMATCHED_VALUES:
124 			str = "B_MISMATCHED_VALUES";
125 			break;
126 
127 		case B_NAME_NOT_FOUND:
128 			str = "B_NAME_NOT_FOUND";
129 			break;
130 
131 		case B_NAME_IN_USE:
132 			str = "B_NAME_IN_USE";
133 			break;
134 
135 		case B_TIMED_OUT:
136 			str = "B_TIMED_OUT";
137 			break;
138 
139 		case B_INTERRUPTED:
140 			str = "B_INTERRUPTED";
141 			break;
142 
143 		case B_WOULD_BLOCK:
144 			str = "B_WOULD_BLOCK";
145 			break;
146 
147 		case B_CANCELED:
148 			str = "B_CANCELED";
149 			break;
150 
151 		case B_NO_INIT:
152 			str = "B_NO_INIT";
153 			break;
154 
155 		case B_BUSY:
156 			str = "B_BUSY";
157 			break;
158 
159 		case B_NOT_ALLOWED:
160 			str = "B_NOT_ALLOWED";
161 			break;
162 
163 
164 		// Kernel Errors
165 		case B_BAD_ADDRESS:
166 			str = "B_BAD_ADDRESS";
167 			break;
168 
169 		case B_BAD_TEAM_ID:
170 			str = "B_BAD_TEAM_ID";
171 			break;
172 
173 		// OS Errors
174 		case B_BAD_PORT_ID:
175 			str = "B_BAD_PORT_ID";
176 			break;
177 
178 		// Anything Else
179 		default:
180 			str = "??????????";
181 			break;
182 
183 	}
184 
185 	cout << endl << "DecodeResult() -- " "0x" << hex << result << " (" << dec << result << ") == " << str << endl;
186 
187 	return result;
188 }
189 
190 _EXPORT
191 string IntToStr(int i) {
192 	char num[32];
193 	sprintf(num, "%d", i);
194 	return string(num);
195 }
196 
197 _EXPORT
198 void ExecCommand(const char *command) {
199 	if (command)
200 		system(command);
201 }
202 
203 _EXPORT
204 void ExecCommand(const char *command, const char *parameter) {
205 	if (command && parameter) {
206 		char *cmdLine = new char[strlen(command) + strlen(parameter) + 1];
207 		strcpy(cmdLine, command);
208 		strcat(cmdLine, parameter);
209 		system(cmdLine);
210 		delete[] cmdLine;
211 	}
212 }
213 
214 _EXPORT
215 void ExecCommand(const char *command, const char *parameter1,
216 							const char *parameter2) {
217 	if (command && parameter1 && parameter2) {
218 		char *cmdLine = new char[strlen(command) + strlen(parameter1)
219 								 + 1 + strlen(parameter2) + 1];
220 		strcpy(cmdLine, command);
221 		strcat(cmdLine, parameter1);
222 		strcat(cmdLine, " ");
223 		strcat(cmdLine, parameter2);
224 		system(cmdLine);
225 		delete[] cmdLine;
226 	}
227 }
228