]>
git.saurik.com Git - apt-legacy.git/blob - test/versiontest.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: versiontest.cc,v 1.5 2003/08/18 15:55:19 mdz 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>
20 #include <apt-pkg/debversion.h>
26 static int verrevcmp(const char *val
, const char *ref
)
39 while (*vp
&& !isdigit(*vp
))
42 while (*rp
&& !isdigit(*rp
))
46 vc
= val
== vp
? 0 : *val
++;
47 rc
= ref
== rp
? 0 : *ref
++;
50 if (vc
&& !isalpha(vc
))
51 vc
+= 256; /* assumes ASCII character set */
52 if (rc
&& !isalpha(rc
))
61 vl
= strtol(val
,(char**)&val
,10);
64 rl
= strtol(ref
,(char**)&ref
,10);
77 static int verrevcmp(const char *val
, const char *ref
)
87 vp
= val
; while (*vp
&& !isdigit(*vp
) && *vp
!= '~') vp
++;
88 rp
= ref
; while (*rp
&& !isdigit(*rp
) && *rp
!= '~') rp
++;
91 vc
= val
== vp
? 0 : *val
++;
92 rc
= ref
== rp
? 0 : *ref
++;
93 if (!rc
&& !vc
) break;
94 if (vc
&& !isalpha(vc
)) vc
+= 256; /* assumes ASCII character set */
95 if (rc
&& !isalpha(rc
)) rc
+= 256;
96 if (vc
!= rc
) return vc
- rc
;
101 if (*vp
== '~') val
++;
102 if (*rp
== '~') ref
++;
103 vl
=0; if (isdigit(*val
)) vl
= strtol(val
,(char**)&val
,10);
104 rl
=0; if (isdigit(*ref
)) rl
= strtol(ref
,(char**)&ref
,10);
105 if (vl
== 0 && rl
== 0)
107 if (*vp
== '~' && *rp
!= '~') return -1;
108 if (*vp
!= '~' && *rp
== '~') return +1;
114 if (vl
!= rl
) return vl
- rl
;
115 if (!*val
&& !*ref
) return 0;
135 bool RunTest(const char *File
)
137 ifstream
F(File
,ios::in
);
146 F
.getline(Buffer
,sizeof(Buffer
));
151 return _error
->Error("Line %u in %s is too long",CurLine
,File
);
154 if (Buffer
[0] == '#' || Buffer
[0] == 0)
159 char *Start
= Buffer
;
160 for (I
= Buffer
; *I
!= 0 && *I
!= ' '; I
++);
161 string
A(Start
, I
- Start
);
164 return _error
->Error("Invalid line %u",CurLine
);
169 for (I
= Start
; *I
!= 0 && *I
!= ' '; I
++);
170 string
B(Start
,I
- Start
);
172 if (*I
== 0 || I
[1] == 0)
173 return _error
->Error("Invalid line %u",CurLine
);
177 int Expected
= atoi(I
);
178 int Res
= debVS
.CmpVersion(A
.c_str(), B
.c_str());
179 int Res2
= verrevcmp(A
.c_str(),B
.c_str());
180 cout
<< "'" << A
<< "' ? '" << B
<< "' = " << Res
<< " (= " << Expected
<< ") " << Res2
<< endl
;
188 _error
->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine
,A
.c_str(),B
.c_str(),Res
,Expected
);
190 // Check the reverse as well
191 Expected
= -1*Expected
;
192 Res
= debVS
.CmpVersion(B
.c_str(), A
.c_str());
193 Res2
= verrevcmp(B
.c_str(),A
.c_str());
195 cout
<< "'" << B
<< "' ? '" << A
<< "' = " << Res
<< " (= " << Expected
<< ") " << Res2
<< endl
;
203 _error
->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine
,B
.c_str(),A
.c_str(),Res
,Expected
);
207 int main(int argc
, char *argv
[])
211 cerr
<< "You must specify a test file" << endl
;
217 // Print any errors or warnings found
218 if (_error
->empty() == false)
221 while (_error
->empty() == false)
224 bool Type
= _error
->PopMessage(Err
);
226 cout
<< "E: " << Err
<< endl
;
228 cout
<< "W: " << Err
<< endl
;