pathsplit_ut.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // File includes itself to make multiple passes of its suites with different platform-dependent definitions
  2. #ifndef PS_INCLUDED
  3. // Outer part
  4. #include "pathsplit.h"
  5. #include <library/cpp/testing/unittest/registar.h>
  6. #define VAR(NAME) Y_CAT(NAME, __LINE__)
  7. #define PS_CHECK(input, ...) \
  8. const char* VAR(model)[] = {"", __VA_ARGS__}; \
  9. UNIT_ASSERT_EQUAL(input.size(), sizeof(VAR(model)) / sizeof(const char*) - 1); \
  10. for (size_t n = 0; n < input.size(); ++n) { \
  11. UNIT_ASSERT_STRINGS_EQUAL(input[n], VAR(model)[n + 1]); \
  12. }
  13. #define PS_INCLUDED
  14. #define PSUF(NAME) NAME
  15. #define PSUF_LOCAL(NAME) NAME##Local
  16. #include <util/folder/pathsplit_ut.cpp>
  17. #undef PSUF
  18. #undef PSUF_LOCAL
  19. #define PSUF(NAME) NAME##Unix
  20. #define PSUF_LOCAL(NAME) PSUF(NAME)
  21. #ifdef _win_
  22. #undef _win_
  23. #define REVERT_WIN
  24. #endif
  25. #include <util/folder/pathsplit_ut.cpp>
  26. #ifdef REVERT_WIN
  27. #define _win_
  28. #undef REVERT_WIN
  29. #endif
  30. #undef PSUF
  31. #undef PSUF_LOCAL
  32. #define PSUF(NAME) NAME##Windows
  33. #define PSUF_LOCAL(NAME) PSUF(NAME)
  34. #ifndef _win_
  35. #define _win_
  36. #define REVERT_WIN
  37. #endif
  38. #include <util/folder/pathsplit_ut.cpp>
  39. #ifdef REVERT_WIN
  40. #undef _win_
  41. #undef REVERT_WIN
  42. #endif
  43. #undef PSUF
  44. #undef PSUF_LOCAL
  45. #undef PS_INCLUDED
  46. #else
  47. // Inner part
  48. #ifdef _win_
  49. #define TRUE_ONLY_WIN true
  50. #else
  51. #define TRUE_ONLY_WIN false
  52. #endif
  53. Y_UNIT_TEST_SUITE(PSUF(PathSplit)) {
  54. Y_UNIT_TEST(Empty) {
  55. PSUF(TPathSplit)
  56. ps;
  57. PS_CHECK(ps);
  58. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  59. }
  60. Y_UNIT_TEST(Relative) {
  61. PSUF(TPathSplit)
  62. ps("some/usual/path");
  63. PS_CHECK(ps, "some", "usual", "path");
  64. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  65. }
  66. Y_UNIT_TEST(Absolute) {
  67. PSUF(TPathSplit)
  68. ps("/some/usual/path");
  69. PS_CHECK(ps, "some", "usual", "path");
  70. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  71. }
  72. Y_UNIT_TEST(Self) {
  73. PSUF(TPathSplit)
  74. ps(".");
  75. PS_CHECK(ps, ".");
  76. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  77. }
  78. Y_UNIT_TEST(Parent) {
  79. PSUF(TPathSplit)
  80. ps("..");
  81. PS_CHECK(ps, "..");
  82. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  83. }
  84. Y_UNIT_TEST(Root) {
  85. PSUF(TPathSplit)
  86. ps("/");
  87. PS_CHECK(ps);
  88. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  89. }
  90. Y_UNIT_TEST(Reconstruct) {
  91. PSUF(TPathSplit)
  92. ps("some/usual/path/../../other/././//path");
  93. #ifdef _win_
  94. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\other\\path");
  95. #else
  96. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some/other/path");
  97. #endif
  98. ps = PSUF(TPathSplit)("/some/usual/path/../../other/././//path");
  99. #ifdef _win_
  100. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\other\\path");
  101. #else
  102. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "/some/other/path");
  103. #endif
  104. }
  105. Y_UNIT_TEST(ParseFirstPart) {
  106. PSUF(TPathSplit)
  107. ps;
  108. ps.ParseFirstPart("some/usual/path");
  109. PS_CHECK(ps, "some", "usual", "path");
  110. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  111. ps = PSUF(TPathSplit)();
  112. ps.ParseFirstPart("/some/usual/path");
  113. PS_CHECK(ps, "some", "usual", "path");
  114. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  115. }
  116. Y_UNIT_TEST(ParsePart) {
  117. PSUF(TPathSplit)
  118. ps("some/usual/path");
  119. ps.ParsePart("sub/path");
  120. PS_CHECK(ps, "some", "usual", "path", "sub", "path");
  121. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  122. ps = PSUF(TPathSplit)("some/usual/path");
  123. ps.ParsePart("/sub/path");
  124. PS_CHECK(ps, "some", "usual", "path", "sub", "path");
  125. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  126. }
  127. Y_UNIT_TEST(ParsePartSelf) {
  128. PSUF(TPathSplit)
  129. ps("some/usual/path");
  130. ps.ParsePart(".");
  131. PS_CHECK(ps, "some", "usual", "path");
  132. ps = PSUF(TPathSplit)("some/usual/path");
  133. ps.ParsePart("././.");
  134. PS_CHECK(ps, "some", "usual", "path");
  135. }
  136. Y_UNIT_TEST(ParsePartParent) {
  137. PSUF(TPathSplit)
  138. ps("some/usual/path");
  139. ps.ParsePart("..");
  140. PS_CHECK(ps, "some", "usual");
  141. ps = PSUF(TPathSplit)("some/usual/path");
  142. ps.ParsePart("../..");
  143. PS_CHECK(ps, "some");
  144. ps = PSUF(TPathSplit)("some/usual/path");
  145. ps.ParsePart("../../..");
  146. PS_CHECK(ps);
  147. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  148. ps = PSUF(TPathSplit)("/some/usual/path");
  149. ps.ParsePart("../../..");
  150. PS_CHECK(ps);
  151. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  152. }
  153. Y_UNIT_TEST(ParsePartOverflow) {
  154. PSUF(TPathSplit)
  155. ps("some/usual/path");
  156. ps.ParsePart("../../../../..");
  157. PS_CHECK(ps, "..", "..");
  158. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  159. ps = PSUF(TPathSplit)("/some/usual/path");
  160. ps.ParsePart("../../../../..");
  161. PS_CHECK(ps, "..", "..");
  162. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  163. }
  164. Y_UNIT_TEST(WinRelative) {
  165. PSUF(TPathSplit)
  166. ps("some\\usual\\path");
  167. #ifdef _win_
  168. PS_CHECK(ps, "some", "usual", "path");
  169. #else
  170. PS_CHECK(ps, "some\\usual\\path");
  171. #endif
  172. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  173. }
  174. Y_UNIT_TEST(WinAbsolute) {
  175. PSUF(TPathSplit)
  176. ps("\\some\\usual\\path");
  177. #ifdef _win_
  178. PS_CHECK(ps, "some", "usual", "path");
  179. #else
  180. PS_CHECK(ps, "\\some\\usual\\path");
  181. #endif
  182. UNIT_ASSERT_EQUAL(ps.IsAbsolute, TRUE_ONLY_WIN);
  183. PSUF(TPathSplit)
  184. psDrive("C:\\some\\usual\\path");
  185. #ifdef _win_
  186. PS_CHECK(psDrive, "some", "usual", "path");
  187. UNIT_ASSERT_EQUAL(psDrive.Drive, "C:");
  188. #else
  189. PS_CHECK(psDrive, "C:\\some\\usual\\path");
  190. #endif
  191. UNIT_ASSERT_EQUAL(psDrive.IsAbsolute, TRUE_ONLY_WIN);
  192. PSUF(TPathSplit)
  193. psDrive2("C:/some/usual/path");
  194. #ifdef _win_
  195. PS_CHECK(psDrive2, "some", "usual", "path");
  196. UNIT_ASSERT_EQUAL(psDrive2.Drive, "C:");
  197. #else
  198. PS_CHECK(psDrive2, "C:", "some", "usual", "path");
  199. #endif
  200. UNIT_ASSERT_EQUAL(psDrive2.IsAbsolute, TRUE_ONLY_WIN);
  201. }
  202. Y_UNIT_TEST(WinRoot) {
  203. PSUF(TPathSplit)
  204. ps("\\");
  205. #ifdef _win_
  206. PS_CHECK(ps);
  207. #else
  208. PS_CHECK(ps, "\\");
  209. #endif
  210. UNIT_ASSERT_EQUAL(ps.IsAbsolute, TRUE_ONLY_WIN);
  211. PSUF(TPathSplit)
  212. psDrive("C:");
  213. #ifdef _win_
  214. PS_CHECK(psDrive);
  215. UNIT_ASSERT_EQUAL(psDrive.Drive, "C:");
  216. #else
  217. PS_CHECK(psDrive, "C:");
  218. #endif
  219. UNIT_ASSERT_EQUAL(psDrive.IsAbsolute, TRUE_ONLY_WIN);
  220. }
  221. Y_UNIT_TEST(WinReconstruct) {
  222. PSUF(TPathSplit)
  223. ps("some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path");
  224. #ifdef _win_
  225. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\other\\path");
  226. #else
  227. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path");
  228. #endif
  229. ps = PSUF(TPathSplit)("\\some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path");
  230. #ifdef _win_
  231. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\other\\path");
  232. #else
  233. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path");
  234. #endif
  235. }
  236. Y_UNIT_TEST(WinParseFirstPart) {
  237. PSUF(TPathSplit)
  238. ps;
  239. ps.ParseFirstPart("some\\usual\\path");
  240. #ifdef _win_
  241. PS_CHECK(ps, "some", "usual", "path");
  242. #else
  243. PS_CHECK(ps, "some\\usual\\path");
  244. #endif
  245. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  246. ps = PSUF(TPathSplit)();
  247. ps.ParseFirstPart("\\some\\usual\\path");
  248. #ifdef _win_
  249. PS_CHECK(ps, "some", "usual", "path");
  250. #else
  251. PS_CHECK(ps, "\\some\\usual\\path");
  252. #endif
  253. UNIT_ASSERT_EQUAL(ps.IsAbsolute, TRUE_ONLY_WIN);
  254. }
  255. Y_UNIT_TEST(WinParsePart) {
  256. PSUF(TPathSplit)
  257. ps("some\\usual\\path");
  258. ps.ParsePart("sub\\path");
  259. #ifdef _win_
  260. PS_CHECK(ps, "some", "usual", "path", "sub", "path");
  261. #else
  262. PS_CHECK(ps, "some\\usual\\path", "sub\\path");
  263. #endif
  264. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  265. ps = PSUF(TPathSplit)("some\\usual\\path");
  266. ps.ParsePart("\\sub\\path");
  267. #ifdef _win_
  268. PS_CHECK(ps, "some", "usual", "path", "sub", "path");
  269. #else
  270. PS_CHECK(ps, "some\\usual\\path", "\\sub\\path");
  271. #endif
  272. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  273. }
  274. #ifdef _win_
  275. Y_UNIT_TEST(WinParsePartSelf) {
  276. PSUF(TPathSplit)
  277. ps("some\\usual\\path");
  278. ps.ParsePart(".");
  279. PS_CHECK(ps, "some", "usual", "path");
  280. ps = PSUF(TPathSplit)("some\\usual\\path");
  281. ps.ParsePart(".\\.\\.");
  282. PS_CHECK(ps, "some", "usual", "path");
  283. }
  284. Y_UNIT_TEST(WinParsePartParent) {
  285. PSUF(TPathSplit)
  286. ps("some\\usual\\path");
  287. ps.ParsePart("..");
  288. PS_CHECK(ps, "some", "usual");
  289. ps = PSUF(TPathSplit)("some\\usual\\path");
  290. ps.ParsePart("..\\..");
  291. PS_CHECK(ps, "some");
  292. ps = PSUF(TPathSplit)("some\\usual\\path");
  293. ps.ParsePart("..\\..\\..");
  294. PS_CHECK(ps);
  295. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  296. ps = PSUF(TPathSplit)("\\some\\usual\\path");
  297. ps.ParsePart("..\\..\\..");
  298. PS_CHECK(ps);
  299. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  300. ps = PSUF(TPathSplit)("C:\\some\\usual\\path");
  301. ps.ParsePart("..\\..\\..");
  302. PS_CHECK(ps);
  303. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  304. UNIT_ASSERT_EQUAL(ps.Drive, "C:");
  305. }
  306. Y_UNIT_TEST(WinParsePartOverflow) {
  307. PSUF(TPathSplit)
  308. ps("some\\usual\\path");
  309. ps.ParsePart("..\\..\\..\\..\\..");
  310. PS_CHECK(ps, "..", "..");
  311. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  312. ps = PSUF(TPathSplit)("\\some\\usual\\path");
  313. ps.ParsePart("..\\..\\..\\..\\..");
  314. PS_CHECK(ps, "..", "..");
  315. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  316. ps = PSUF(TPathSplit)("C:\\some\\usual\\path");
  317. ps.ParsePart("..\\..\\..\\..\\..");
  318. PS_CHECK(ps, "..", "..");
  319. UNIT_ASSERT_EQUAL(ps.IsAbsolute, true);
  320. UNIT_ASSERT_EQUAL(ps.Drive, "C:");
  321. }
  322. #endif
  323. Y_UNIT_TEST(WinMixed) {
  324. PSUF(TPathSplit)
  325. ps("some\\usual/path");
  326. #ifdef _win_
  327. PS_CHECK(ps, "some", "usual", "path");
  328. #else
  329. PS_CHECK(ps, "some\\usual", "path");
  330. #endif
  331. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  332. }
  333. Y_UNIT_TEST(WinParsePartMixed) {
  334. PSUF(TPathSplit)
  335. ps("some\\usual/path");
  336. ps.ParsePart("sub/sub\\path");
  337. #ifdef _win_
  338. PS_CHECK(ps, "some", "usual", "path", "sub", "sub", "path");
  339. #else
  340. PS_CHECK(ps, "some\\usual", "path", "sub", "sub\\path");
  341. #endif
  342. UNIT_ASSERT_EQUAL(ps.IsAbsolute, false);
  343. }
  344. Y_UNIT_TEST(BeginWithSelf) {
  345. PSUF(TPathSplit)
  346. ps("./some/usual/path");
  347. PS_CHECK(ps, "some", "usual", "path");
  348. #ifdef _win_
  349. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\usual\\path");
  350. #else
  351. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some/usual/path");
  352. #endif
  353. }
  354. Y_UNIT_TEST(BeginWithParent) {
  355. PSUF(TPathSplit)
  356. ps("../some/usual/path");
  357. PS_CHECK(ps, "..", "some", "usual", "path");
  358. #ifdef _win_
  359. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "..\\some\\usual\\path");
  360. #else
  361. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "../some/usual/path");
  362. #endif
  363. }
  364. Y_UNIT_TEST(InOut) {
  365. PSUF(TPathSplit)
  366. ps("path/..");
  367. PS_CHECK(ps);
  368. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "");
  369. }
  370. Y_UNIT_TEST(OutIn) {
  371. PSUF(TPathSplit)
  372. ps("../path");
  373. PS_CHECK(ps, "..", "path");
  374. #ifdef _win_
  375. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "..\\path");
  376. #else
  377. UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "../path");
  378. #endif
  379. }
  380. }
  381. Y_UNIT_TEST_SUITE(PSUF(PathSplitTraits)) {
  382. Y_UNIT_TEST(IsPathSep) {
  383. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsPathSep('/'), true);
  384. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsPathSep('\\'), TRUE_ONLY_WIN);
  385. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsPathSep(' '), false);
  386. }
  387. Y_UNIT_TEST(IsAbsolutePath) {
  388. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath(""), false);
  389. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("/"), true);
  390. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("some/usual/path"), false);
  391. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("/some/usual/path"), true);
  392. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("."), false);
  393. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath(".."), false);
  394. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("/."), true);
  395. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("/.."), true);
  396. }
  397. Y_UNIT_TEST(WinIsAbsolutePath) {
  398. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("somepath"), false);
  399. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("\\"), TRUE_ONLY_WIN);
  400. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("\\somepath"), TRUE_ONLY_WIN);
  401. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("\\."), TRUE_ONLY_WIN);
  402. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("\\.."), TRUE_ONLY_WIN);
  403. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C"), false);
  404. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C:"), TRUE_ONLY_WIN);
  405. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C:somepath"), false);
  406. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C:\\"), TRUE_ONLY_WIN);
  407. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C:\\somepath"), TRUE_ONLY_WIN);
  408. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C:/"), TRUE_ONLY_WIN);
  409. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("C:/somepath"), TRUE_ONLY_WIN);
  410. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("#:"), false);
  411. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("#:somepath"), false);
  412. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("#:\\somepath"), false);
  413. UNIT_ASSERT_EQUAL(PSUF_LOCAL(TPathSplitTraits)::IsAbsolutePath("#:/somepath"), false);
  414. }
  415. }
  416. #undef TRUE_ONLY_WIN
  417. #endif