]>
git.saurik.com Git - apt.git/blob - test/versiontest.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: versiontest.cc,v 1.1 1998/11/26 23:29:20 jgg Exp $
4 /* ######################################################################
6 Version Test - Simple program to run through a file and comare versions.
8 Each version is compared and the result is checked against an expected
9 result in the file. The format of the file is
11 Where Res is -1, 1, 0. dpkg -D=1 --compare-versions a "<" b can be
12 used to determine what Res should be. # at the start of the line
13 is a comment and blank lines are skipped
15 ##################################################################### */
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/version.h>
23 static int verrevcmp(const char *val
, const char *ref
)
36 while (*vp
&& !isdigit(*vp
))
39 while (*rp
&& !isdigit(*rp
))
43 vc
= val
== vp
? 0 : *val
++;
44 rc
= ref
== rp
? 0 : *ref
++;
47 if (vc
&& !isalpha(vc
))
48 vc
+= 256; /* assumes ASCII character set */
49 if (rc
&& !isalpha(rc
))
58 vl
= strtol(val
,(char**)&val
,10);
61 rl
= strtol(ref
,(char**)&ref
,10);
74 static int verrevcmp(const char *val
, const char *ref
)
84 vp
= val
; while (*vp
&& !isdigit(*vp
) && *vp
!= '~') vp
++;
85 rp
= ref
; while (*rp
&& !isdigit(*rp
) && *rp
!= '~') rp
++;
88 vc
= val
== vp
? 0 : *val
++;
89 rc
= ref
== rp
? 0 : *ref
++;
90 if (!rc
&& !vc
) break;
91 if (vc
&& !isalpha(vc
)) vc
+= 256; /* assumes ASCII character set */
92 if (rc
&& !isalpha(rc
)) rc
+= 256;
93 if (vc
!= rc
) return vc
- rc
;
98 if (*vp
== '~') val
++;
99 if (*rp
== '~') ref
++;
100 vl
=0; if (isdigit(*val
)) vl
= strtol(val
,(char**)&val
,10);
101 rl
=0; if (isdigit(*ref
)) rl
= strtol(ref
,(char**)&ref
,10);
102 if (vl
== 0 && rl
== 0)
104 if (*vp
== '~' && *rp
!= '~') return -1;
105 if (*vp
!= '~' && *rp
== '~') return +1;
111 if (vl
!= rl
) return vl
- rl
;
112 if (!*val
&& !*ref
) return 0;
132 bool RunTest(const char *File
)
134 ifstream
F(File
,ios::in
| ios::nocreate
);
143 F
.getline(Buffer
,sizeof(Buffer
));
148 return _error
->Error("Line %u in %s is too long",CurLine
,File
);
151 if (Buffer
[0] == '#' || Buffer
[0] == 0)
156 char *Start
= Buffer
;
157 for (I
= Buffer
; *I
!= 0 && *I
!= ' '; I
++);
158 string
A(Start
, I
- Start
);
161 return _error
->Error("Invalid line %u",CurLine
);
166 for (I
= Start
; *I
!= 0 && *I
!= ' '; I
++);
167 string
B(Start
,I
- Start
);
169 if (*I
== 0 || I
[1] == 0)
170 return _error
->Error("Invalid line %u",CurLine
);
174 int Expected
= atoi(I
);
175 int Res
= pkgVersionCompare(A
.c_str(),B
.c_str());
176 int Res2
= verrevcmp(A
.c_str(),B
.c_str());
177 cout
<< "'" << A
<< "' ? '" << B
<< "' = " << Res
<< " (= " << Expected
<< ") " << Res2
<< endl
;
179 _error
->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine
,A
.c_str(),B
.c_str(),Res
,Expected
);
181 // Check the reverse as well
182 Expected
= -1*Expected
;
183 Res
= pkgVersionCompare(B
.c_str(),A
.c_str());
184 Res2
= verrevcmp(B
.c_str(),A
.c_str());
185 cout
<< "'" << B
<< "' ? '" << A
<< "' = " << Res
<< " (= " << Expected
<< ") " << Res2
<< endl
;
187 _error
->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine
,A
.c_str(),B
.c_str(),Res
,Expected
);
191 int main(int argc
, char *argv
[])
195 cerr
<< "You must specify a test file" << endl
;
201 // Print any errors or warnings found
202 if (_error
->empty() == false)
205 while (_error
->empty() == false)
208 bool Type
= _error
->PopMessage(Err
);
210 cout
<< "E: " << Err
<< endl
;
212 cout
<< "W: " << Err
<< endl
;