]>
git.saurik.com Git - apt.git/blob - test/versiontest.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: versiontest.cc,v 1.2 2001/02/20 07:03:18 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 ##################################################################### */
17 #define APT_COMPATIBILITY 1
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/version.h>
24 static int verrevcmp(const char *val
, const char *ref
)
37 while (*vp
&& !isdigit(*vp
))
40 while (*rp
&& !isdigit(*rp
))
44 vc
= val
== vp
? 0 : *val
++;
45 rc
= ref
== rp
? 0 : *ref
++;
48 if (vc
&& !isalpha(vc
))
49 vc
+= 256; /* assumes ASCII character set */
50 if (rc
&& !isalpha(rc
))
59 vl
= strtol(val
,(char**)&val
,10);
62 rl
= strtol(ref
,(char**)&ref
,10);
75 static int verrevcmp(const char *val
, const char *ref
)
85 vp
= val
; while (*vp
&& !isdigit(*vp
) && *vp
!= '~') vp
++;
86 rp
= ref
; while (*rp
&& !isdigit(*rp
) && *rp
!= '~') rp
++;
89 vc
= val
== vp
? 0 : *val
++;
90 rc
= ref
== rp
? 0 : *ref
++;
91 if (!rc
&& !vc
) break;
92 if (vc
&& !isalpha(vc
)) vc
+= 256; /* assumes ASCII character set */
93 if (rc
&& !isalpha(rc
)) rc
+= 256;
94 if (vc
!= rc
) return vc
- rc
;
99 if (*vp
== '~') val
++;
100 if (*rp
== '~') ref
++;
101 vl
=0; if (isdigit(*val
)) vl
= strtol(val
,(char**)&val
,10);
102 rl
=0; if (isdigit(*ref
)) rl
= strtol(ref
,(char**)&ref
,10);
103 if (vl
== 0 && rl
== 0)
105 if (*vp
== '~' && *rp
!= '~') return -1;
106 if (*vp
!= '~' && *rp
== '~') return +1;
112 if (vl
!= rl
) return vl
- rl
;
113 if (!*val
&& !*ref
) return 0;
133 bool RunTest(const char *File
)
135 ifstream
F(File
,ios::in
| ios::nocreate
);
144 F
.getline(Buffer
,sizeof(Buffer
));
149 return _error
->Error("Line %u in %s is too long",CurLine
,File
);
152 if (Buffer
[0] == '#' || Buffer
[0] == 0)
157 char *Start
= Buffer
;
158 for (I
= Buffer
; *I
!= 0 && *I
!= ' '; I
++);
159 string
A(Start
, I
- Start
);
162 return _error
->Error("Invalid line %u",CurLine
);
167 for (I
= Start
; *I
!= 0 && *I
!= ' '; I
++);
168 string
B(Start
,I
- Start
);
170 if (*I
== 0 || I
[1] == 0)
171 return _error
->Error("Invalid line %u",CurLine
);
175 int Expected
= atoi(I
);
176 int Res
= pkgVersionCompare(A
.c_str(),B
.c_str());
177 int Res2
= verrevcmp(A
.c_str(),B
.c_str());
178 cout
<< "'" << A
<< "' ? '" << B
<< "' = " << Res
<< " (= " << Expected
<< ") " << Res2
<< endl
;
180 _error
->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine
,A
.c_str(),B
.c_str(),Res
,Expected
);
182 // Check the reverse as well
183 Expected
= -1*Expected
;
184 Res
= pkgVersionCompare(B
.c_str(),A
.c_str());
185 Res2
= verrevcmp(B
.c_str(),A
.c_str());
186 cout
<< "'" << B
<< "' ? '" << A
<< "' = " << 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
);
192 int main(int argc
, char *argv
[])
196 cerr
<< "You must specify a test file" << endl
;
202 // Print any errors or warnings found
203 if (_error
->empty() == false)
206 while (_error
->empty() == false)
209 bool Type
= _error
->PopMessage(Err
);
211 cout
<< "E: " << Err
<< endl
;
213 cout
<< "W: " << Err
<< endl
;