OSDN Git Service

VER0.1.4
[lib1stclass/main.git] / t / test.cpp
1 /*
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright: 2010- 1stclass.co.jp.  All rights reserved.
5 *
6 * Created by Hajime Kurita
7 */
8 #include <string>
9 #include <string.h>
10 #include <iostream>
11 #include <sys/stat.h>
12 #include "../1stclass.hpp"
13 #include "../1stclass.h"
14 using namespace std;
15
16 class tester{
17 public:
18   tester(){ok=0;bad=0;};
19   void test();
20   void report();
21 private:
22   int ok;
23   int bad;
24 };
25
26 void tester::test(){
27   firstclass fst;
28   struct stat st;
29 // is_num:
30 // Judge whether this is number or not
31 // 数字かどうかの判別
32   if(fst.is_num("2") && fst.is_num("test")==0){
33     cout << "is_num/C++: ok "<< endl;
34     ok++;
35   }
36   else{
37     cout << "is_num/C++: bad" << endl;
38     bad++;
39   }
40
41   if(is_num("2") && is_num("test")==0){
42     cout << "is_num/C: ok "<< endl;
43     ok++;
44   }
45   else{
46     cout << "is_num/C: bad" << endl;
47     bad++;
48   }
49
50 // host2ip
51 // Get IP from host name
52 // ホスト名からIPを得る
53   string server="www.yahoo.co.jp";
54   string ip=fst.host2ip(server);
55   if( (ip.at(0) - '1'>=0) && (ip.at(0) - '1'<=9) ){
56     cout << "host2ip/C++: ok" << endl;
57     ok++;
58   }
59   else{
60     cout << "host2ip/C++: bad" << endl;
61     bad++;
62   }
63
64   char c_server[]="www.yahoo.co.jp";
65   char c_ip[32]="";
66   host2ip(c_server, c_ip);
67   if( (c_ip[0] - '1' >=0) && (c_ip[0] - '1' <=9) ){
68     cout << "host2ip/C: ok" << endl;
69     ok++;
70   }
71   else{
72     cout << "host2ip/C: bad" << endl;
73     bad++;
74   }
75
76 // rel2abs:
77 // Get absolute path from relative path
78 // 相対パスから絶対パスを得る
79   string rel_path="../lib1stclass/1/2/3";
80   string abs_path=fst.rel2abs(rel_path);
81   if( abs_path.find("/")==0 &&  abs_path.find("lib1stclass/1/2/3")!=string::npos){
82     cout << "rel2abs/C++: ok" << endl;
83     ok++;
84   }
85   else{
86     cout << "rel2abs/C++: bad" << endl;
87     bad++;
88   }
89   
90   char rel_path_char[]="../lib1stclass/1/2/3";
91   char abs_path_char[1024]="";
92   rel2abs(rel_path_char, abs_path_char);
93   if( abs_path_char[0]=='/' && strstr(abs_path_char, "lib1stclass/1/2/3")!=NULL){
94     cout << "rel2abs/C: ok" << endl;
95     ok++;
96   }
97   else{
98     cout << "rel2abs/C: bad" << "/" << abs_path_char << endl;
99     bad++;
100   }
101
102   string rel_path2="lib1stclass/1/2/3";
103   string abs_path2=fst.rel2abs(rel_path2);
104   if( abs_path2.find("/")==0 &&  abs_path2.find("/lib1stclass/1/2/3")!=string::npos){
105     cout << "rel2abs/C++: ok" << endl;
106     ok++;
107   }
108   else{
109     cout << "rel2abs/C++: bad" << endl;
110     bad++;
111   }
112   
113   char rel_path_char2[]="lib1stclass/1/2/3";
114   char abs_path_char2[1024]="";
115   rel2abs(rel_path_char2, abs_path_char2);
116   if( abs_path_char2[0]=='/' && strstr(abs_path_char2, "/lib1stclass/1/2/3")!=NULL){
117     cout << "rel2abs 2/C: ok" << endl;
118     ok++;
119   }
120   else{
121     cout << "rel2abs 2/C: bad" << endl;
122     bad++;
123   }
124   
125   string rel_path3="/tmp/1/2/3";
126   string abs_path3=fst.rel2abs(rel_path3);
127   if( abs_path3=="/tmp/1/2/3"){
128     cout << "rel2abs 3/C++: ok" << endl;
129     ok++;
130   }
131   else{
132     cout << "rel2abs 3/C++: bad" << endl;
133     bad++;
134   }
135   
136   char rel_path_char3[]="/tmp/1/2/3";
137   char abs_path_char3[1024]="";
138   rel2abs(rel_path_char3, abs_path_char3);
139   if( strcmp(abs_path_char3, "/tmp/1/2/3")==0){
140     cout << "rel2abs 3/C: ok" << endl;
141     ok++;
142   }
143   else{
144     cout << "rel2abs 3/C: bad" << endl;
145     bad++;
146   }
147
148 // rmkdir:
149 // mkdir recursively
150 // ディレクトリを再帰的に作る
151   fst.rmkdir("test_dir/depth2");
152   if(stat("test_dir/depth2", &st)==0){
153     cout << "rmkdir/C++: ok" << endl;
154     ok++;
155
156 // remove_path:
157 // Remove path completely
158 // パス全体を消去(ディレクトリを再帰的に消去)
159     fst.remove_path("test_dir");
160     if(stat("test_dir", &st)!=0){
161       cout << "remove_path/C++: ok" << endl;
162       ok++;
163     }
164     else{
165       cout << "remove_path/C++: bad" << endl;
166       bad++;
167     }
168   }
169   else{
170     cout << "rmkdir/C++: bad" << endl;
171     bad++;
172   }
173
174   rmkdir("test_dir/depth_2");
175   if(stat("test_dir/depth_2", &st)==0){
176     cout << "rmkdir/C: ok" << endl;
177     ok++;
178     remove_path("test_dir");
179     if(stat("test_dir", &st)!=0){
180       cout << "remove_path/C: ok" << endl;
181       ok++;
182     }
183     else{
184       cout << "remove_path/C: bad" << endl;
185       bad++;
186     }
187   }
188   else{
189     cout << "rmkdir/C: bad" << endl;
190     bad++;
191   }
192
193 // itoa
194 // Convert int to char
195 // int型をchar型に変換
196   int integer=12345;
197   char itoa_str[6]="";
198
199   fst.itoa(integer, itoa_str);
200   if (strcmp(itoa_str,"12345") == 0){
201     cout << "itoa/C++: ok" << endl;
202     ok++;
203   }
204   else{
205     cout << "itoa/C++: bad" << endl;
206     bad++;
207   }
208
209   itoa_str[0]='\0';
210   itoa(integer, itoa_str);
211   if (strcmp(itoa_str,"12345") == 0){
212     cout << "itoa/C: ok" << endl;
213     ok++;
214   }
215   else{
216     cout << "itoa/C: bad" << endl;
217     bad++;
218   }
219
220 // reverse_char:
221 // Reverse char
222 // charを逆並びにする
223   char example1[]="1234";
224   fst.reverse_char(example1);
225   if (strcmp(example1,"4321") == 0){
226     cout << "reverse_char/C++: ok" << endl;
227     ok++;
228   }
229   else{
230     cout << "reverse_char/C++: bad" << endl;
231     bad++;
232   }
233   
234   char example2[]="1234";
235   reverse_char(example2);
236   if (strcmp(example2,"4321") == 0){
237     cout << "reverse_char/C: ok" << endl;
238     ok++;
239   }
240   else{
241     cout << "reverse_char/C: bad" << endl;
242     bad++;
243   }
244
245 // remove_not_file_char:
246 // Convert ascii characters which cannot be used for file name into "_"
247 // ファイルシステムに使えないアスキー文字を「_」に変換
248   string not_file_str="bk\\sl/sm:as*qt?dq\"lt<rt>vt|";
249   string file_str=fst.remove_not_file_char(&not_file_str);
250   if (file_str=="bk_sl_sm_as_qt_dq_lt_rt_vt_"){
251     cout << "remove_not_file_char/C++: ok" << endl;
252     ok++;
253   }
254   else{
255     cout << "remove_not_file_char/C++: bad" << endl;
256     bad++;
257   }
258
259   char not_file_char[]="bk\\sl/sm:as*qt?dq\"lt<rt>vt|";
260   char file_char[1024]="";
261   remove_not_file_char(not_file_char, file_char);
262   if (strcmp(file_char,"bk_sl_sm_as_qt_dq_lt_rt_vt_")==0){
263     cout << "remove_not_file_char/C: ok" << endl;
264     ok++;
265   }
266   else{
267     cout << "remove_not_file_char/C: bad" << endl;
268     bad++;
269   }
270
271 // shmf:
272 // Create temporary file on /dev/shm/
273 // /dev/shm/上への一時ファイル生成
274   string file="/tmp/test.txt";
275   if(fst.shmf(file)=="/dev/shm/_tmp_test.txt"){
276     cout << "shmf/C++: ok" << endl;
277     ok++;
278   }
279   else{
280     cout << "shmf/C++: bad" << endl;
281     bad++;
282   }
283
284   char tfile[]="/tmp/test.txt";
285   char sfile[1024]="";
286   shmf(tfile, sfile);
287   if(strcmp(sfile, "/dev/shm/_tmp_test.txt")==0){
288     cout << "shmf/C: ok" << endl;
289     ok++;
290   }
291   else{
292     cout << "shmf/C: bad" << endl;
293     bad++;
294   }
295
296 // safe_strcat:
297 // Safe strcat
298 // 安全なstrcat
299   char a1[20]="";
300   char b1[]="12345";
301   fst.safe_strcat(a1, b1, sizeof(a1));
302   if(strcmp(a1,b1)==0){
303     cout << "safe_strcat 1/C++: ok" << endl;
304     ok++;
305   }
306   else{
307     cout << "safe_strcat 1/C++: bad" << endl;
308     bad++;
309   }
310   
311   char a2[20]="";
312   char b2[]="12345";
313   safe_strcat(a2, b2, sizeof(a2));
314   if(strcmp(a2,b2)==0){
315     cout << "safe_strcat 2/C: ok" << endl;
316     ok++;
317   }
318   else{
319     cout << "safe_strcat 2/C: bad" << endl;
320     bad++;
321   }
322   
323   char a3[2]="";
324   char b3[]="12345";
325   fst.safe_strcat(a3, b3, sizeof(a3));
326   if(strcmp(a3,"1")==0){
327     cout << "safe_strcat 3/C++: ok" << endl;
328     ok++;
329   }
330   else{
331     cout << "safe_strcat 3/C++: bad" << endl;
332     bad++;
333   }
334   
335   char a4[2]="";
336   char b4[]="12345";
337   safe_strcat(a4, b4, sizeof(a4));
338   if(strcmp(a4,"1")==0){
339     cout << "safe_strcat 4/C: ok" << endl;
340     ok++;
341   }
342   else{
343     cout << "safe_strcat 4/C: bad" << endl;
344     bad++;
345   }
346 }
347
348 void tester::report(){
349   cout << "ok=" << ok << "\t" << "bad=" << bad << endl;
350 }
351
352 int main(){
353   tester tester;
354   tester.test();
355   tester.report();
356 }