xref: /haiku/src/tools/cppunit/TestUtils.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
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_PARTIAL_READ:
95 			str = "B_PARTIAL_READ";
96 			break;
97 
98 		case B_PARTIAL_WRITE:
99 			str = "B_PARTIAL_WRITE";
100 			break;
101 
102 		case B_BAD_MIME_SNIFFER_RULE:
103 			str = "B_BAD_MIME_SNIFFER_RULE";
104 			break;
105 
106 		// General Errors
107 		case B_NO_MEMORY:
108 			str = "B_NO_MEMORY";
109 			break;
110 
111 		case B_IO_ERROR:
112 			str = "B_IO_ERROR";
113 			break;
114 
115 		case B_PERMISSION_DENIED:
116 			str = "B_PERMISSION_DENIED";
117 			break;
118 
119 		case B_BAD_INDEX:
120 			str = "B_BAD_INDEX";
121 			break;
122 
123 		case B_BAD_TYPE:
124 			str = "B_BAD_TYPE";
125 			break;
126 
127 		case B_BAD_VALUE:
128 			str = "B_BAD_VALUE";
129 			break;
130 
131 		case B_MISMATCHED_VALUES:
132 			str = "B_MISMATCHED_VALUES";
133 			break;
134 
135 		case B_NAME_NOT_FOUND:
136 			str = "B_NAME_NOT_FOUND";
137 			break;
138 
139 		case B_NAME_IN_USE:
140 			str = "B_NAME_IN_USE";
141 			break;
142 
143 		case B_TIMED_OUT:
144 			str = "B_TIMED_OUT";
145 			break;
146 
147 		case B_INTERRUPTED:
148 			str = "B_INTERRUPTED";
149 			break;
150 
151 		case B_WOULD_BLOCK:
152 			str = "B_WOULD_BLOCK";
153 			break;
154 
155 		case B_CANCELED:
156 			str = "B_CANCELED";
157 			break;
158 
159 		case B_NO_INIT:
160 			str = "B_NO_INIT";
161 			break;
162 
163 		case B_BUSY:
164 			str = "B_BUSY";
165 			break;
166 
167 		case B_NOT_ALLOWED:
168 			str = "B_NOT_ALLOWED";
169 			break;
170 
171 
172 		// Kernel Errors
173 		case B_BAD_ADDRESS:
174 			str = "B_BAD_ADDRESS";
175 			break;
176 
177 		case B_BAD_TEAM_ID:
178 			str = "B_BAD_TEAM_ID";
179 			break;
180 
181 		// OS Errors
182 		case B_BAD_PORT_ID:
183 			str = "B_BAD_PORT_ID";
184 			break;
185 
186 		// Anything Else
187 		default:
188 			str = "??????????";
189 			break;
190 
191 	}
192 
193 	cout << endl << "DecodeResult() -- " "0x" << hex << result << " (" << dec << result << ") == " << str << endl;
194 
195 	return result;
196 }
197 
198 _EXPORT
199 string IntToStr(int i) {
200 	char num[32];
201 	sprintf(num, "%d", i);
202 	return string(num);
203 }
204 
205 _EXPORT
206 void ExecCommand(const char *command) {
207 	if (command)
208 		system(command);
209 }
210 
211 _EXPORT
212 void ExecCommand(const char *command, const char *parameter) {
213 	if (command && parameter) {
214 		char *cmdLine = new char[strlen(command) + strlen(parameter) + 1];
215 		strcpy(cmdLine, command);
216 		strcat(cmdLine, parameter);
217 		system(cmdLine);
218 		delete[] cmdLine;
219 	}
220 }
221 
222 _EXPORT
223 void ExecCommand(const char *command, const char *parameter1,
224 							const char *parameter2) {
225 	if (command && parameter1 && parameter2) {
226 		char *cmdLine = new char[strlen(command) + strlen(parameter1)
227 								 + 1 + strlen(parameter2) + 1];
228 		strcpy(cmdLine, command);
229 		strcat(cmdLine, parameter1);
230 		strcat(cmdLine, " ");
231 		strcat(cmdLine, parameter2);
232 		system(cmdLine);
233 		delete[] cmdLine;
234 	}
235 }
236