versiontest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: versiontest.cc,v 1.1 1998/11/26 23:29:20 jgg Exp $
  4. /* ######################################################################
  5. Version Test - Simple program to run through a file and comare versions.
  6. Each version is compared and the result is checked against an expected
  7. result in the file. The format of the file is
  8. a b Res
  9. Where Res is -1, 1, 0. dpkg -D=1 --compare-versions a "<" b can be
  10. used to determine what Res should be. # at the start of the line
  11. is a comment and blank lines are skipped
  12. ##################################################################### */
  13. /*}}}*/
  14. #include <system.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/version.h>
  17. #include <iostream.h>
  18. #include <fstream.h>
  19. static int verrevcmp(const char *val, const char *ref)
  20. {
  21. int vc, rc;
  22. long vl, rl;
  23. const char *vp, *rp;
  24. if (!val)
  25. val = "";
  26. if (!ref)
  27. ref = "";
  28. for (;;)
  29. {
  30. vp = val;
  31. while (*vp && !isdigit(*vp))
  32. vp++;
  33. rp = ref;
  34. while (*rp && !isdigit(*rp))
  35. rp++;
  36. for (;;)
  37. {
  38. vc= val == vp ? 0 : *val++;
  39. rc= ref == rp ? 0 : *ref++;
  40. if (!rc && !vc)
  41. break;
  42. if (vc && !isalpha(vc))
  43. vc += 256; /* assumes ASCII character set */
  44. if (rc && !isalpha(rc))
  45. rc += 256;
  46. if (vc != rc)
  47. return vc - rc;
  48. }
  49. val = vp;
  50. ref = rp;
  51. vl = 0;
  52. if (isdigit(*vp))
  53. vl = strtol(val,(char**)&val,10);
  54. rl = 0;
  55. if (isdigit(*rp))
  56. rl = strtol(ref,(char**)&ref,10);
  57. if (vl != rl)
  58. return vl - rl;
  59. if (!*val && !*ref)
  60. return 0;
  61. if (!*val)
  62. return -1;
  63. if (!*ref)
  64. return +1;
  65. }
  66. }
  67. #if 0
  68. static int verrevcmp(const char *val, const char *ref)
  69. {
  70. int vc, rc;
  71. long vl, rl;
  72. const char *vp, *rp;
  73. if (!val) val= "";
  74. if (!ref) ref= "";
  75. for (;;)
  76. {
  77. vp= val; while (*vp && !isdigit(*vp) && *vp != '~') vp++;
  78. rp= ref; while (*rp && !isdigit(*rp) && *rp != '~') rp++;
  79. for (;;)
  80. {
  81. vc= val == vp ? 0 : *val++;
  82. rc= ref == rp ? 0 : *ref++;
  83. if (!rc && !vc) break;
  84. if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
  85. if (rc && !isalpha(rc)) rc += 256;
  86. if (vc != rc) return vc - rc;
  87. }
  88. val= vp;
  89. ref= rp;
  90. if (*vp == '~') val++;
  91. if (*rp == '~') ref++;
  92. vl=0; if (isdigit(*val)) vl= strtol(val,(char**)&val,10);
  93. rl=0; if (isdigit(*ref)) rl= strtol(ref,(char**)&ref,10);
  94. if (vl == 0 && rl == 0)
  95. {
  96. if (*vp == '~' && *rp != '~') return -1;
  97. if (*vp != '~' && *rp == '~') return +1;
  98. }
  99. if (*vp == '~')
  100. vl *= -1;
  101. if (*rp == '~')
  102. rl *= -1;
  103. if (vl != rl) return vl - rl;
  104. if (!*val && !*ref) return 0;
  105. if (!*val)
  106. {
  107. if (*ref == '~')
  108. return +1;
  109. else
  110. return -1;
  111. }
  112. if (!*ref)
  113. {
  114. if (*val == '~')
  115. return -1;
  116. else
  117. return +1;
  118. }
  119. }
  120. }
  121. #endif
  122. bool RunTest(const char *File)
  123. {
  124. ifstream F(File,ios::in | ios::nocreate);
  125. if (!F != 0)
  126. return false;
  127. char Buffer[300];
  128. int CurLine = 0;
  129. while (1)
  130. {
  131. F.getline(Buffer,sizeof(Buffer));
  132. CurLine++;
  133. if (F.eof() != 0)
  134. return true;
  135. if (!F != 0)
  136. return _error->Error("Line %u in %s is too long",CurLine,File);
  137. // Comment
  138. if (Buffer[0] == '#' || Buffer[0] == 0)
  139. continue;
  140. // First version
  141. char *I;
  142. char *Start = Buffer;
  143. for (I = Buffer; *I != 0 && *I != ' '; I++);
  144. string A(Start, I - Start);
  145. if (*I == 0)
  146. return _error->Error("Invalid line %u",CurLine);
  147. // Second version
  148. I++;
  149. Start = I;
  150. for (I = Start; *I != 0 && *I != ' '; I++);
  151. string B(Start,I - Start);
  152. if (*I == 0 || I[1] == 0)
  153. return _error->Error("Invalid line %u",CurLine);
  154. // Result
  155. I++;
  156. int Expected = atoi(I);
  157. int Res = pkgVersionCompare(A.c_str(),B.c_str());
  158. int Res2 = verrevcmp(A.c_str(),B.c_str());
  159. cout << "'" << A << "' ? '" << B << "' = " << Res << " (= " << Expected << ") " << Res2 << endl;
  160. if (Res != Expected)
  161. _error->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine,A.c_str(),B.c_str(),Res,Expected);
  162. // Check the reverse as well
  163. Expected = -1*Expected;
  164. Res = pkgVersionCompare(B.c_str(),A.c_str());
  165. Res2 = verrevcmp(B.c_str(),A.c_str());
  166. cout << "'" << B << "' ? '" << A << "' = " << Res << " (= " << Expected << ") " << Res2 << endl;
  167. if (Res != Expected)
  168. _error->Error("Comparison failed on line %u. '%s' ? '%s' %i != %i",CurLine,A.c_str(),B.c_str(),Res,Expected);
  169. }
  170. }
  171. int main(int argc, char *argv[])
  172. {
  173. if (argc <= 1)
  174. {
  175. cerr << "You must specify a test file" << endl;
  176. return 0;
  177. }
  178. RunTest(argv[1]);
  179. // Print any errors or warnings found
  180. if (_error->empty() == false)
  181. {
  182. string Err;
  183. while (_error->empty() == false)
  184. {
  185. bool Type = _error->PopMessage(Err);
  186. if (Type == true)
  187. cout << "E: " << Err << endl;
  188. else
  189. cout << "W: " << Err << endl;
  190. }
  191. return 0;
  192. }
  193. }