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