]>
git.saurik.com Git - apt.git/blob - test/libapt/compareversion_test.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Version Test - Simple program to run through a file and comare versions.
7 Each version is compared and the result is checked against an expected
8 result in the file. The format of the file is
10 Where Res is -1, 1, 0. dpkg -D=1 --compare-versions a "<" b can be
11 used to determine what Res should be. # at the start of the line
12 is a comment and blank lines are skipped
14 The runner will also call dpkg --compare-versions to check if APT and
15 dpkg have (still) the same idea.
17 ##################################################################### */
19 #include <apt-pkg/macros.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/version.h>
22 #include <apt-pkg/debversion.h>
23 #include <apt-pkg/fileutl.h>
29 #include <sys/types.h>
34 bool callDPkg(const char *val
, const char *ref
, const char &op
) {
35 pid_t Process
= ExecFork();
39 args
[0] = "/usr/bin/dpkg";
40 args
[1] = "--compare-versions";
42 args
[3] = (op
== 1) ? ">>" : ( (op
== 0) ? "=" : "<<");
45 execv(args
[0], (char**) args
);
49 waitpid(Process
, &Ret
, 0);
50 return WIFEXITED(Ret
) == true && WEXITSTATUS(Ret
) == 0;
53 void assertVersion(int const &CurLine
, string
const &A
, string
const &B
, int const &Expected
) {
54 int Res
= debVS
.CmpVersion(A
.c_str(), B
.c_str());
55 bool const dpkg
= callDPkg(A
.c_str(),B
.c_str(), Expected
);
56 Res
= (Res
< 0) ? -1 : ( (Res
> 0) ? 1 : Res
);
59 _error
->Error("Comparison failed on line %u. '%s' '%s' '%s' %i != %i",CurLine
,A
.c_str(),((Expected
== 1) ? "<<" : ( (Expected
== 0) ? "=" : ">>")) ,B
.c_str(),Res
,Expected
);
61 _error
->Error("DPkg differ with line: %u. '%s' '%s' '%s' == false",CurLine
,A
.c_str(),((Expected
== 1) ? "<<" : ( (Expected
== 0) ? "=" : ">>")),B
.c_str());
64 bool RunTest(const char *File
)
66 ifstream
F(File
,ios::in
);
75 F
.getline(Buffer
,sizeof(Buffer
));
80 return _error
->Error("Line %u in %s is too long",CurLine
,File
);
83 if (Buffer
[0] == '#' || Buffer
[0] == 0)
89 for (I
= Buffer
; *I
!= 0 && *I
!= ' '; I
++);
90 string
A(Start
, I
- Start
);
93 return _error
->Error("Invalid line %u",CurLine
);
98 for (I
= Start
; *I
!= 0 && *I
!= ' '; I
++);
99 string
B(Start
,I
- Start
);
101 if (*I
== 0 || I
[1] == 0)
102 return _error
->Error("Invalid line %u",CurLine
);
106 int const Expected
= atoi(I
);
107 assertVersion(CurLine
, A
, B
, Expected
);
108 // Check the reverse as well
109 assertVersion(CurLine
, B
, A
, Expected
*-1);
113 int main(int argc
, char *argv
[])
116 RunTest("../versions.lst");
120 // Print any errors or warnings found
121 _error
->DumpErrors();