texpfncase.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. program texpfncase;
  2. {$IFDEF FPC}
  3. {$MODE DELPHI}
  4. {$ENDIF FPC}
  5. {$H+}
  6. {$APPTYPE CONSOLE}
  7. {$IFDEF FPC}
  8. {$DEFINE FPCTEST}
  9. {$ENDIF FPC}
  10. {$I+}
  11. uses
  12. SysUtils
  13. {$IFNDEF FPC}
  14. , StrUtils
  15. {$ENDIF FPC}
  16. ;
  17. const
  18. TestFilesNumber = 3;
  19. {$IFDEF UNIX}
  20. MinPathLength = 1;
  21. {$ELSE UNIX}
  22. MinPathLength = 3;
  23. {$ENDIF UNIX}
  24. {$ifndef FPC}
  25. DirectorySeparator = PathDelim;
  26. AllowDirectorySeparators: set of char = [PathDelim];
  27. {$endif}
  28. type
  29. TTestFiles = array [1..TestFilesNumber] of shortstring;
  30. const
  31. TestFiles: TTestFiles = ('testFile1.tst', 'testFile2.tst', 'TestFile2.tst');
  32. {$IFNDEF FPC}
  33. const
  34. FilenameCaseMatchStr: array [mkNone..mkAmbiguous] of shortstring =
  35. ('mkNone', 'mkExactMatch', 'mkSingleMatch', 'mkAmbiguous');
  36. {$ENDIF FPC}
  37. var
  38. Failed: byte;
  39. procedure TestExpFNC (const FN1, ExpReturn: string; ExpMatch: TFilenameCaseMatch);
  40. var
  41. FN2: string;
  42. Match: TFilenameCaseMatch;
  43. begin
  44. FN2 := ExpandFileNameCase (FN1, Match);
  45. if (Match <> ExpMatch) or ((ExpReturn <> '') and (FN2 <> ExpReturn) and
  46. ((Match <> mkAmbiguous) or not (FileNameCaseSensitive) or
  47. (UpperCase (FN2) <> UpperCase (ExpReturn)))) then
  48. begin
  49. Inc (Failed);
  50. WriteLn ('Error: Input = ', FN1, ', Output = ', FN2, ' (expected ', ExpReturn, '), MatchFound = ',
  51. {$IFNDEF FPC}
  52. FileNameCaseMatchStr [
  53. {$ENDIF FPC}
  54. Match
  55. {$IFNDEF FPC}
  56. ]
  57. {$ENDIF FPC}
  58. , ' (expected ',
  59. {$IFNDEF FPC}
  60. FileNameCaseMatchStr [
  61. {$ENDIF FPC}
  62. ExpMatch
  63. {$IFNDEF FPC}
  64. ]
  65. {$ENDIF FPC}
  66. , ')');
  67. end
  68. {$IFDEF DEBUG}
  69. else
  70. WriteLn ('Input = ', FN1, ', Output = ', FN2, ', MatchFound = ',
  71. {$IFNDEF FPC}
  72. FileNameCaseMatchStr [
  73. {$ENDIF FPC}
  74. Match
  75. {$IFNDEF FPC}
  76. ]
  77. {$ENDIF FPC}
  78. )
  79. {$ENDIF DEBUG}
  80. ;
  81. end;
  82. var
  83. I: byte;
  84. TempDir, TestDir: string;
  85. CurDir: string;
  86. begin
  87. {$IFNDEF FPC}
  88. TempDir := ExpandFilename (GetEnvironmentVariable ('TEMP'));
  89. {$ELSE FPC}
  90. TempDir := ExpandFilename (GetTempDir);
  91. {$ENDIF FPC}
  92. if (Length (TempDir) > MinPathLength) and
  93. (TempDir [Length (TempDir)] in AllowDirectorySeparators) then
  94. TempDir := LeftStr (TempDir, Length (TempDir) - 1);
  95. CurDir := GetCurrentDir;
  96. {$IFDEF DEBUG}
  97. {$IFDEF FPC}
  98. WriteLn ('FileNameCaseSensitive = ', FileNameCaseSensitive);
  99. {$ENDIF FPC}
  100. WriteLn ('TempDir = ', TempDir);
  101. WriteLn ('SetCurrentDir result = ', SetCurrentDir (TempDir));
  102. WriteLn ('Current directory = ', GetCurrentDir);
  103. {$ELSE DEBUG}
  104. SetCurrentDir (TempDir);
  105. {$ENDIF DEBUG}
  106. for I := 1 to TestFilesNumber do
  107. FileClose (FileCreate (TestFiles [I]));
  108. TestExpFNC ('*File1.tst', ExpandFileName ('testFile1.tst'), mkExactMatch);
  109. if FileNameCaseSensitive then
  110. TestExpFNC ('TestFile1.tst', ExpandFileName ('testFile1.tst'), mkSingleMatch)
  111. else
  112. TestExpFNC ('TestFile1.tst', ExpandFileName ('testFile1.tst'), mkExactMatch);
  113. TestExpFNC ('testFile1.tst', ExpandFileName ('testFile1.tst'), mkExactMatch);
  114. TestExpFNC ('testFile2.tst', ExpandFileName ('testFile2.tst'), mkExactMatch);
  115. if FileNameCaseSensitive then
  116. TestExpFNC ('TestFile2.tst', ExpandFileName ('TestFile2.tst'), mkExactMatch)
  117. else
  118. TestExpFNC ('TestFile2.tst', ExpandFileName ('testFile2.tst'), mkExactMatch);
  119. if FileNameCaseSensitive then
  120. TestExpFNC ('testfile2.tst', ExpandFileName ('testFile2.tst'), mkAmbiguous)
  121. else
  122. TestExpFNC ('testfile2.tst', ExpandFileName ('testFile2.tst'), mkExactMatch);
  123. (* Return value depends on ordering of files in the particular filesystem used thus not checked *)
  124. TestExpFNC ('*File2.tst', '', mkExactMatch);
  125. if FileNameCaseSensitive then
  126. TestExpFNC ('*File*.tst', '', mkExactMatch)
  127. else
  128. TestExpFNC ('*File*.tst', '', mkExactMatch);
  129. TestExpFNC ('..' + DirectorySeparator + '*' + DirectorySeparator + '*File*.tst',
  130. ExpandFileName ('..' + DirectorySeparator + '*' + DirectorySeparator + '*File*.tst'),
  131. mkNone);
  132. I := Length (TempDir);
  133. TestDir := TempDir;
  134. while (I > 1) and not (TempDir [I] in ['a'..'z','A'..'Z']) do
  135. Dec (I);
  136. if I > 0 then
  137. begin
  138. if TestDir [I] in ['a'..'z'] then
  139. TestDir [I] := char (Ord (TestDir [I]) and not $20)
  140. else
  141. TestDir [I] := char (Ord (TestDir [I]) or $20);
  142. end
  143. else
  144. WriteLn ('Warning: Cannot perform all required tests; please set TEMP!');
  145. if FileNameCaseSensitive then
  146. TestExpFNC (TestDir + DirectorySeparator + 'testFile1.tst',
  147. ExpandFileName (TempDir + DirectorySeparator + 'testFile1.tst'), mkSingleMatch)
  148. else
  149. TestExpFNC (TestDir + DirectorySeparator + 'testFile1.tst',
  150. ExpandFileName (TestDir + DirectorySeparator + 'testFile1.tst'), mkExactMatch);
  151. if FileNameCaseSensitive then
  152. TestExpFNC (TestDir + DirectorySeparator + 'testfile1.tst',
  153. ExpandFileName (TempDir + DirectorySeparator + 'testFile1.tst'), mkSingleMatch)
  154. else
  155. TestExpFNC (TestDir + DirectorySeparator + 'testfile1.tst',
  156. ExpandFileName (TestDir + DirectorySeparator + 'testFile1.tst'), mkExactMatch);
  157. for I := 1 to TestFilesNumber do
  158. if not (DeleteFile (TestFiles [I])) then
  159. begin
  160. if FileNameCaseSensitive or (I <> 3) then
  161. WriteLn ('Warning: Deletion of ', TestFiles [I], ' (file #', I, ') failed - possibly due to case insensitive file system!');
  162. end;
  163. SetCurrentDir (CurDir);
  164. if Failed > 0 then
  165. begin
  166. WriteLn (Failed, ' failures!!');
  167. Halt (Failed);
  168. end;
  169. end.