Stable Tag: Removing old tag
[synfig.git] / ETL / trunk / test / stringf.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** stringf Procedure Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #include <iostream>
25 #include <ETL/stringf>
26 #include <stdio.h>
27
28 /* === M A C R O S ========================================================= */
29
30 using namespace etl;
31 using namespace std;
32
33 /* === C L A S S E S ======================================================= */
34
35
36 /* === P R O C E D U R E S ================================================= */
37
38 int basic_test(void)
39 {
40         int ret=0;
41         char mystring[80]="My formatted string!";
42         string myotherstring="my other string!";
43
44         cout<<strprintf("This is a test of >>%s<<.",mystring)<<endl;
45
46         myotherstring="5 6.75 George 7";
47         int i,i2;
48         float f;
49
50 #ifndef ETL_NO_STRSCANF
51         strscanf(myotherstring,"%d %f %s %d",&i, &f, mystring, &i2);
52 #else
53         cout<<"warning: strscanf() disabled at compile time..."<<endl;
54         i=5;f=6.75;i2=7;
55 #endif
56
57         cout<<myotherstring+"=="+strprintf("%d %f %s %d",i, f, mystring, i2)<<endl;
58
59         cout<<stratof(strprintf("32.5849"))<<"==32.5849"<<endl;
60         return ret;
61 }
62
63 int base_and_dir_name_test(void)
64 {
65         int ret=0;
66
67         string str(unix_to_local_path("/usr/bin/bleh.exe"));
68         cout<<"Test Case 1 -> "<<str<<endl;
69         cout<<"basename -> "<<basename(str)<<endl;
70         if(basename(str)!="bleh.exe")
71                 cerr<<"error:Bad basename"<<endl,ret++;
72         cout<<"dirname -> "<<dirname(str)<<endl;
73         if(dirname(str)!=unix_to_local_path("/usr/bin"))
74                 cerr<<"error:Bad dirname"<<endl,ret++;
75         cout<<endl;
76
77         str=unix_to_local_path("/usr/bin/");
78         cout<<"Test Case 2 -> "<<str<<endl;
79         cout<<"basename -> "<<basename(str)<<endl;
80         if(basename(str)!="bin")
81                 cerr<<"error:Bad basename"<<endl,ret++;
82         cout<<"dirname -> "<<dirname(str)<<endl;
83         if(dirname(str)!=unix_to_local_path("/usr"))
84                 cerr<<"error:Bad dirname"<<endl,ret++;
85         cout<<endl;
86
87         str="bleh.exe";
88         cout<<"Test Case 3 -> "<<str<<endl;
89         cout<<"basename -> "<<basename(str)<<endl;
90         if(basename(str)!="bleh.exe")
91                 cerr<<"error:Bad basename"<<endl,ret++;
92         cout<<"dirname -> "<<dirname(str)<<endl;
93         if(dirname(str)!=unix_to_local_path("."))
94                 cerr<<"error:Bad dirname"<<endl,ret++;
95         cout<<endl;
96
97         return ret;
98 }
99
100 int relative_path_test()
101 {
102         int ret=0;
103
104         string curr_path=unix_to_local_path("/usr/local/bin/.");
105         string dest_path=unix_to_local_path("/usr/share");
106
107         cout<<"curr_path="<<curr_path<<" dest_path="<<dest_path<<endl;
108         cout<<"relative_path="<<relative_path(curr_path,dest_path)<<endl;
109         if(relative_path(curr_path,dest_path)!=unix_to_local_path("../../share"))
110                 cerr<<"Bad relative path"<<endl,ret++;
111
112         cout<<endl;
113
114         curr_path=unix_to_local_path("/home/darco/projects/voria");
115         dest_path=unix_to_local_path("/home/darco/projects/voria/myfile.txt");
116         cout<<"curr_path="<<curr_path<<" dest_path="<<dest_path<<endl;
117         cout<<"relative_path="<<relative_path(curr_path,dest_path)<<endl;
118         if(relative_path(curr_path,dest_path)!=unix_to_local_path("myfile.txt"))
119                 cerr<<"Bad relative path"<<endl,ret++;
120
121         cout<<endl;
122
123         curr_path=unix_to_local_path("/home/darco/projects/voria");
124         dest_path=unix_to_local_path("/home/darco/projects/voria/files/myfile.txt");
125         cout<<"curr_path="<<curr_path<<" dest_path="<<dest_path<<endl;
126         cout<<"relative_path="<<relative_path(curr_path,dest_path)<<endl;
127         if(relative_path(curr_path,dest_path)!=unix_to_local_path("files/myfile.txt"))
128                 cerr<<"Bad relative path"<<endl,ret++;
129
130         cout<<endl;
131
132         curr_path=unix_to_local_path("/usr/local/../include/sys/../linux/linux.h");
133         cout<<"dirty_path="<<curr_path<<endl;
134         cout<<"clean_path="<<cleanup_path(curr_path)<<endl;
135
136         cout<<"current_working_directory="<<current_working_directory()<<endl;
137         return ret;
138 }
139
140
141 /* === E N T R Y P O I N T ================================================= */
142
143 int main()
144 {
145         int error=0;
146
147         error+=basic_test();
148         error+=base_and_dir_name_test();
149         error+=relative_path_test();
150
151         return error;
152 }