testCMFilesystemPath.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include <cm/filesystem>
  9. namespace {
  10. namespace fs = cm::filesystem;
  11. void checkResult(bool success)
  12. {
  13. if (!success) {
  14. std::cout << " => failed";
  15. }
  16. std::cout << std::endl;
  17. }
  18. bool testConstructors()
  19. {
  20. std::cout << "testConstructors()";
  21. bool result = true;
  22. {
  23. fs::path p;
  24. if (p != fs::path()) {
  25. result = false;
  26. }
  27. }
  28. {
  29. fs::path p1("/a/b/c");
  30. fs::path p2("/a/b/c");
  31. if (p1 != p2) {
  32. result = false;
  33. }
  34. if (p1.string() != p2.string()) {
  35. result = false;
  36. }
  37. if (p1.string() != "/a/b/c") {
  38. result = false;
  39. }
  40. }
  41. {
  42. std::string s("/a/b/c");
  43. fs::path p1(s);
  44. fs::path p2(s.begin(), s.end());
  45. if (p1 != p2) {
  46. result = false;
  47. }
  48. if (p1.string() != s || p2.string() != s) {
  49. result = false;
  50. }
  51. #if CM_FILESYSTEM_SOURCE_TRAITS_ITERATOR
  52. std::string s2(s);
  53. s2 += '\0';
  54. fs::path p3(s2.begin());
  55. if (p1 != p3 || p3.string() != s) {
  56. result = false;
  57. }
  58. #endif
  59. }
  60. {
  61. std::wstring s(L"/a/b/c");
  62. fs::path p1(s);
  63. fs::path p2(s.begin(), s.end());
  64. if (p1 != p2) {
  65. result = false;
  66. }
  67. if (p1.wstring() != s || p2.wstring() != s) {
  68. result = false;
  69. }
  70. #if CM_FILESYSTEM_SOURCE_TRAITS_ITERATOR
  71. std::wstring s2(s);
  72. s2 += L'\0';
  73. fs::path p3(s2.begin());
  74. if (p1 != p3 || p3.wstring() != s) {
  75. result = false;
  76. }
  77. #endif
  78. }
  79. {
  80. std::string s("/a/b/c");
  81. fs::path::string_type ws;
  82. for (auto c : s) {
  83. ws += fs::path::value_type(c);
  84. }
  85. fs::path p1(ws);
  86. fs::path p2(ws.begin(), ws.end());
  87. if (p1 != p2) {
  88. result = false;
  89. }
  90. if (p1.native() != ws || p2.native() != ws) {
  91. result = false;
  92. }
  93. #if CM_FILESYSTEM_SOURCE_TRAITS_ITERATOR
  94. fs::path::string_type ws2(ws);
  95. ws2 += fs::path::value_type('\0');
  96. fs::path p3(ws2.begin());
  97. if (p1 != p3 || p3.native() != ws) {
  98. result = false;
  99. }
  100. #endif
  101. }
  102. checkResult(result);
  103. return result;
  104. }
  105. bool testConcatenation()
  106. {
  107. std::cout << "testConcatenation()";
  108. bool result = true;
  109. {
  110. fs::path p("/a/b");
  111. p /= "c";
  112. if (!(p.string() == "/a/b/c" || p.string() == "/a/b\\c")) {
  113. result = false;
  114. }
  115. p += "d";
  116. if (!(p.string() == "/a/b/cd" || p.string() == "/a/b\\cd")) {
  117. result = false;
  118. }
  119. fs::path p2("x/y");
  120. p /= p2;
  121. if (!(p.string() == "/a/b/cd/x/y" || p.string() == "/a/b\\cd\\x/y")) {
  122. result = false;
  123. }
  124. p = p / p2;
  125. if (!(p.string() == "/a/b/cd/x/y/x/y" ||
  126. p.string() == "/a/b\\cd\\x/y\\x/y")) {
  127. result = false;
  128. }
  129. }
  130. {
  131. fs::path p("a");
  132. p /= "";
  133. if (!(p.string() == "a/" || p.string() == "a\\")) {
  134. result = false;
  135. }
  136. p /= "/b";
  137. if (p.string() != "/b") {
  138. result = false;
  139. }
  140. }
  141. #if defined(_WIN32)
  142. {
  143. fs::path p("a");
  144. p /= "c:/b";
  145. if (p.string() != "c:/b") {
  146. result = false;
  147. }
  148. p = fs::path("a") / "c:";
  149. if (p.string() != "c:") {
  150. result = false;
  151. }
  152. p = fs::path("c:") / "";
  153. if (p.string() != "c:") {
  154. result = false;
  155. }
  156. p = fs::path("c:a") / "/b";
  157. if (p.string() != "c:/b") {
  158. result = false;
  159. }
  160. p = fs::path("c:a") / "c:b";
  161. if (p.string() != "c:a\\b") {
  162. result = false;
  163. }
  164. p = fs::path("//host") / "b";
  165. if (p.string() != "//host\\b") {
  166. result = false;
  167. }
  168. p = fs::path("//host/") / "b";
  169. if (p.string() != "//host/b") {
  170. result = false;
  171. }
  172. }
  173. #endif
  174. checkResult(result);
  175. return result;
  176. }
  177. bool testModifiers()
  178. {
  179. std::cout << "testModifiers()";
  180. bool result = true;
  181. {
  182. std::string s("a///b/");
  183. fs::path p(s);
  184. std::replace(
  185. s.begin(), s.end(), '/',
  186. static_cast<std::string::value_type>(fs::path::preferred_separator));
  187. p.make_preferred();
  188. if (p.string() != s) {
  189. result = false;
  190. }
  191. }
  192. {
  193. fs::path p("a/b/c.e.f");
  194. p.remove_filename();
  195. if (p.string() != "a/b/") {
  196. result = false;
  197. }
  198. p.remove_filename();
  199. if (p.string() != "a/b/") {
  200. result = false;
  201. }
  202. }
  203. {
  204. fs::path p("a/b/c.e.f");
  205. p.replace_filename("x.y");
  206. if (p.string() != "a/b/x.y") {
  207. result = false;
  208. }
  209. }
  210. {
  211. fs::path p("a/b/c.e.f");
  212. p.replace_extension(".x");
  213. if (p.string() != "a/b/c.e.x") {
  214. result = false;
  215. }
  216. p.replace_extension(".y");
  217. if (p.string() != "a/b/c.e.y") {
  218. result = false;
  219. }
  220. p.replace_extension();
  221. if (p.string() != "a/b/c.e") {
  222. result = false;
  223. }
  224. p = "/a/b";
  225. p.replace_extension(".x");
  226. if (p.string() != "/a/b.x") {
  227. result = false;
  228. }
  229. p = "/a/b/";
  230. p.replace_extension(".x");
  231. if (p.string() != "/a/b/.x") {
  232. result = false;
  233. }
  234. }
  235. checkResult(result);
  236. return result;
  237. }
  238. bool testObservers()
  239. {
  240. std::cout << "testObservers()";
  241. bool result = true;
  242. {
  243. std::string s("a/b/c");
  244. fs::path p(s);
  245. fs::path::string_type st;
  246. for (auto c : s) {
  247. st += static_cast<fs::path::value_type>(c);
  248. }
  249. if (p.native() != st || static_cast<fs::path::string_type>(p) != st ||
  250. p.c_str() != st) {
  251. result = false;
  252. }
  253. }
  254. {
  255. std::string s("a//b//c");
  256. std::wstring ws(L"a//b//c");
  257. fs::path p(s);
  258. if (p.string() != s || p.wstring() != ws) {
  259. result = false;
  260. }
  261. }
  262. {
  263. std::string s("a/b/c");
  264. std::wstring ws;
  265. for (auto c : s) {
  266. ws += static_cast<std::wstring::value_type>(c);
  267. }
  268. std::string ns(s);
  269. std::replace(
  270. ns.begin(), ns.end(), '/',
  271. static_cast<std::string::value_type>(fs::path::preferred_separator));
  272. fs::path p(ns);
  273. if (p.generic_string() != s || p.generic_wstring() != ws) {
  274. result = false;
  275. }
  276. }
  277. checkResult(result);
  278. return result;
  279. }
  280. bool testCompare()
  281. {
  282. std::cout << "testCompare()";
  283. bool result = true;
  284. {
  285. std::string s("a/b/c");
  286. fs::path p1(s);
  287. fs::path p2(s);
  288. if (p1.compare(p2) != 0) {
  289. result = false;
  290. }
  291. p2 = "a/b";
  292. if (p1.compare(p2) <= 0) {
  293. result = false;
  294. }
  295. p2 = "a/d";
  296. if (p1.compare(p2) >= 0) {
  297. result = false;
  298. }
  299. p2 = "a/b/d";
  300. if (p1.compare(p2) >= 0) {
  301. result = false;
  302. }
  303. p2 = "a/b/a";
  304. if (p1.compare(p2) <= 0) {
  305. result = false;
  306. }
  307. p2 = "a/b/c/d";
  308. if (p1.compare(p2) >= 0) {
  309. result = false;
  310. }
  311. p1 = "a";
  312. p2 = "b";
  313. if (p1.compare(p2) == 0) {
  314. result = false;
  315. }
  316. }
  317. {
  318. // LWG 3096 (https://cplusplus.github.io/LWG/issue3096)
  319. // fs::path p1("/a/");
  320. // fs::path p2("/a/.");
  321. // if (p1.compare(p2) != 0) {
  322. // result = false;
  323. // }
  324. }
  325. checkResult(result);
  326. return result;
  327. }
  328. bool testGeneration()
  329. {
  330. std::cout << "testGeneration()";
  331. bool result = true;
  332. {
  333. fs::path p("a/./b/..");
  334. if (p.lexically_normal().generic_string() != "a/") {
  335. result = false;
  336. }
  337. p = "a/.///b/../";
  338. if (p.lexically_normal().generic_string() != "a/") {
  339. result = false;
  340. }
  341. }
  342. #if defined(_WIN32)
  343. {
  344. fs::path p("//host/./b/..");
  345. if (p.lexically_normal().string() != "\\\\host\\") {
  346. result = false;
  347. }
  348. p = "//host/.///b/../";
  349. if (p.lexically_normal().string() != "\\\\host\\") {
  350. result = false;
  351. }
  352. p = "c://a/.///b/../";
  353. if (p.lexically_normal().string() != "c:\\a\\") {
  354. result = false;
  355. }
  356. }
  357. #endif
  358. {
  359. if (fs::path("/a//d").lexically_relative("/a/b/c") != "../../d") {
  360. result = false;
  361. }
  362. if (fs::path("/a//b///c").lexically_relative("/a/d") != "../b/c") {
  363. result = false;
  364. }
  365. if (fs::path("a/b/c").lexically_relative("a") != "b/c") {
  366. result = false;
  367. }
  368. if (fs::path("a/b/c").lexically_relative("a/b/c/x/y") != "../..") {
  369. result = false;
  370. }
  371. if (fs::path("a/b/c").lexically_relative("a/b/c") != ".") {
  372. result = false;
  373. }
  374. if (fs::path("a/b").lexically_relative("c/d") != "../../a/b") {
  375. result = false;
  376. }
  377. }
  378. {
  379. #if defined(_WIN32)
  380. if (fs::path("/a/d").lexically_relative("e/d/c") != "/a/d") {
  381. result = false;
  382. }
  383. if (!fs::path("c:/a/d").lexically_relative("e/d/c").empty()) {
  384. result = false;
  385. }
  386. #else
  387. if (!fs::path("/a/d").lexically_relative("e/d/c").empty()) {
  388. result = false;
  389. }
  390. #endif
  391. }
  392. {
  393. #if defined(_WIN32)
  394. if (fs::path("c:/a/d").lexically_proximate("e/d/c") != "c:/a/d") {
  395. result = false;
  396. }
  397. #else
  398. if (fs::path("/a/d").lexically_proximate("e/d/c") != "/a/d") {
  399. result = false;
  400. }
  401. #endif
  402. if (fs::path("/a/d").lexically_proximate("/a/b/c") != "../../d") {
  403. result = false;
  404. }
  405. }
  406. // LWG 3070
  407. {
  408. #if defined(_WIN32)
  409. if (!fs::path("/a:/b:").lexically_relative("/a:/c:").empty()) {
  410. result = false;
  411. }
  412. if (fs::path("c:/a/b").lexically_relative("c:/a/d") != "../b") {
  413. result = false;
  414. }
  415. if (!fs::path("c:/a/b:").lexically_relative("c:/a/d").empty()) {
  416. result = false;
  417. }
  418. if (!fs::path("c:/a/b").lexically_relative("c:/a/d:").empty()) {
  419. result = false;
  420. }
  421. #else
  422. if (fs::path("/a:/b:").lexically_relative("/a:/c:") != "../b:") {
  423. result = false;
  424. }
  425. #endif
  426. }
  427. // LWG 3096
  428. {
  429. if (fs::path("/a").lexically_relative("/a/.") != ".") {
  430. result = false;
  431. }
  432. if (fs::path("/a").lexically_relative("/a/") != ".") {
  433. result = false;
  434. }
  435. if (fs::path("a/b/c").lexically_relative("a/b/c") != ".") {
  436. result = false;
  437. }
  438. if (fs::path("a/b/c").lexically_relative("a/b/c/") != ".") {
  439. result = false;
  440. }
  441. if (fs::path("a/b/c").lexically_relative("a/b/c/.") != ".") {
  442. result = false;
  443. }
  444. if (fs::path("a/b/c/").lexically_relative("a/b/c") != ".") {
  445. result = false;
  446. }
  447. if (fs::path("a/b/c/.").lexically_relative("a/b/c") != ".") {
  448. result = false;
  449. }
  450. if (fs::path("a/b/c/.").lexically_relative("a/b/c/") != ".") {
  451. result = false;
  452. }
  453. }
  454. checkResult(result);
  455. return result;
  456. }
  457. bool testDecomposition()
  458. {
  459. std::cout << "testDecomposition()";
  460. bool result = true;
  461. {
  462. if (!fs::path("/a/b").root_name().empty()) {
  463. result = false;
  464. }
  465. #if defined(_WIN32)
  466. if (fs::path("c:/a/b").root_name() != "c:") {
  467. result = false;
  468. }
  469. if (fs::path("c:a/b").root_name() != "c:") {
  470. result = false;
  471. }
  472. if (fs::path("c:").root_name() != "c:") {
  473. result = false;
  474. }
  475. if (fs::path("//host/b").root_name() != "//host") {
  476. result = false;
  477. }
  478. if (fs::path("//host").root_name() != "//host") {
  479. result = false;
  480. }
  481. #endif
  482. }
  483. {
  484. if (!fs::path("a/b").root_directory().empty()) {
  485. result = false;
  486. }
  487. if (fs::path("/a/b").root_directory() != "/") {
  488. result = false;
  489. }
  490. #if defined(_WIN32)
  491. if (!fs::path("c:a/b").root_directory().empty()) {
  492. result = false;
  493. }
  494. if (fs::path("/a/b").root_directory() != "/") {
  495. result = false;
  496. }
  497. if (fs::path("c:/a/b").root_directory() != "/") {
  498. result = false;
  499. }
  500. if (fs::path("//host/b").root_directory() != "/") {
  501. result = false;
  502. }
  503. #endif
  504. }
  505. {
  506. if (!fs::path("a/b").root_path().empty()) {
  507. result = false;
  508. }
  509. if (fs::path("/a/b").root_path() != "/") {
  510. result = false;
  511. }
  512. #if defined(_WIN32)
  513. if (fs::path("c:a/b").root_path() != "c:") {
  514. result = false;
  515. }
  516. if (fs::path("/a/b").root_path() != "/") {
  517. result = false;
  518. }
  519. if (fs::path("c:/a/b").root_path() != "c:/") {
  520. result = false;
  521. }
  522. if (fs::path("//host/b").root_path() != "//host/") {
  523. result = false;
  524. }
  525. #endif
  526. }
  527. {
  528. if (!fs::path("/").relative_path().empty()) {
  529. result = false;
  530. }
  531. if (fs::path("a/b").relative_path() != "a/b") {
  532. result = false;
  533. }
  534. if (fs::path("/a/b").relative_path() != "a/b") {
  535. result = false;
  536. }
  537. #if defined(_WIN32)
  538. if (fs::path("c:a/b").relative_path() != "a/b") {
  539. result = false;
  540. }
  541. if (fs::path("/a/b").relative_path() != "a/b") {
  542. result = false;
  543. }
  544. if (fs::path("c:/a/b").relative_path() != "a/b") {
  545. result = false;
  546. }
  547. if (fs::path("//host/b").relative_path() != "b") {
  548. result = false;
  549. }
  550. #endif
  551. }
  552. {
  553. if (fs::path("/a/b").parent_path() != "/a") {
  554. result = false;
  555. }
  556. if (fs::path("/a/b/").parent_path() != "/a/b") {
  557. result = false;
  558. }
  559. if (fs::path("/a/b/.").parent_path() != "/a/b") {
  560. result = false;
  561. }
  562. if (fs::path("/").parent_path() != "/") {
  563. result = false;
  564. }
  565. #if defined(_WIN32)
  566. if (fs::path("c:/a/b").parent_path() != "c:/a") {
  567. result = false;
  568. }
  569. if (fs::path("c:a").parent_path() != "c:") {
  570. result = false;
  571. }
  572. if (fs::path("c:/").parent_path() != "c:/") {
  573. result = false;
  574. }
  575. if (fs::path("c:").parent_path() != "c:") {
  576. result = false;
  577. }
  578. if (fs::path("//host/").parent_path() != "//host/") {
  579. result = false;
  580. }
  581. if (fs::path("//host").parent_path() != "//host") {
  582. result = false;
  583. }
  584. #endif
  585. }
  586. {
  587. if (fs::path("/a/b.txt").filename() != "b.txt") {
  588. result = false;
  589. }
  590. if (fs::path("/a/.b").filename() != ".b") {
  591. result = false;
  592. }
  593. if (fs::path("/foo/bar/").filename() != "") {
  594. result = false;
  595. }
  596. if (fs::path("/foo/.").filename() != ".") {
  597. result = false;
  598. }
  599. if (fs::path("/foo/..").filename() != "..") {
  600. result = false;
  601. }
  602. if (fs::path(".").filename() != ".") {
  603. result = false;
  604. }
  605. if (fs::path("..").filename() != "..") {
  606. result = false;
  607. }
  608. if (!fs::path("/").filename().empty()) {
  609. result = false;
  610. }
  611. #if defined(_WIN32)
  612. if (fs::path("c:a").filename() != "a") {
  613. result = false;
  614. }
  615. if (fs::path("c:/a").filename() != "a") {
  616. result = false;
  617. }
  618. if (!fs::path("c:").filename().empty()) {
  619. result = false;
  620. }
  621. if (!fs::path("c:/").filename().empty()) {
  622. result = false;
  623. }
  624. if (!fs::path("//host").filename().empty()) {
  625. result = false;
  626. }
  627. #endif
  628. }
  629. {
  630. if (fs::path("/a/b.txt").stem() != "b") {
  631. result = false;
  632. }
  633. if (fs::path("/a/b.c.txt").stem() != "b.c") {
  634. result = false;
  635. }
  636. if (fs::path("/a/.b").stem() != ".b") {
  637. result = false;
  638. }
  639. if (fs::path("/a/b").stem() != "b") {
  640. result = false;
  641. }
  642. if (fs::path("/a/b/.").stem() != ".") {
  643. result = false;
  644. }
  645. if (fs::path("/a/b/..").stem() != "..") {
  646. result = false;
  647. }
  648. if (!fs::path("/a/b/").stem().empty()) {
  649. result = false;
  650. }
  651. #if defined(_WIN32)
  652. if (!fs::path("c:/a/b/").stem().empty()) {
  653. result = false;
  654. }
  655. if (!fs::path("c:/").stem().empty()) {
  656. result = false;
  657. }
  658. if (!fs::path("c:").stem().empty()) {
  659. result = false;
  660. }
  661. if (!fs::path("//host/").stem().empty()) {
  662. result = false;
  663. }
  664. if (!fs::path("//host").stem().empty()) {
  665. result = false;
  666. }
  667. #endif
  668. }
  669. {
  670. if (fs::path("/a/b.txt").extension() != ".txt") {
  671. result = false;
  672. }
  673. if (fs::path("/a/b.").extension() != ".") {
  674. result = false;
  675. }
  676. if (!fs::path("/a/b").extension().empty()) {
  677. result = false;
  678. }
  679. if (fs::path("/a/b.txt/b.cc").extension() != ".cc") {
  680. result = false;
  681. }
  682. if (fs::path("/a/b.txt/b.").extension() != ".") {
  683. result = false;
  684. }
  685. if (!fs::path("/a/b.txt/b").extension().empty()) {
  686. result = false;
  687. }
  688. if (!fs::path("/a/.").extension().empty()) {
  689. result = false;
  690. }
  691. if (!fs::path("/a/..").extension().empty()) {
  692. result = false;
  693. }
  694. if (!fs::path("/a/.hidden").extension().empty()) {
  695. result = false;
  696. }
  697. if (fs::path("/a/..b").extension() != ".b") {
  698. result = false;
  699. }
  700. }
  701. checkResult(result);
  702. return result;
  703. }
  704. bool testQueries()
  705. {
  706. std::cout << "testQueries()";
  707. bool result = true;
  708. {
  709. if (fs::path("/a/b").has_root_name()) {
  710. result = false;
  711. }
  712. fs::path p("/a/b");
  713. if (!p.has_root_directory() || !p.has_root_path()) {
  714. result = false;
  715. }
  716. if (!fs::path("/a/b").has_root_path() || fs::path("a/b").has_root_path()) {
  717. result = false;
  718. }
  719. if (!fs::path("/a/b").has_relative_path() ||
  720. fs::path("/").has_relative_path()) {
  721. result = false;
  722. }
  723. if (!fs::path("/a/b").has_parent_path() ||
  724. !fs::path("/").has_parent_path() || fs::path("a").has_parent_path()) {
  725. result = false;
  726. }
  727. if (!fs::path("/a/b").has_filename() || !fs::path("a.b").has_filename() ||
  728. fs::path("/a/").has_filename() || fs::path("/").has_filename()) {
  729. result = false;
  730. }
  731. if (!fs::path("/a/b").has_stem() || !fs::path("a.b").has_stem() ||
  732. !fs::path("/.a").has_stem() || fs::path("/a/").has_stem() ||
  733. fs::path("/").has_stem()) {
  734. result = false;
  735. }
  736. if (!fs::path("/a/b.c").has_extension() ||
  737. !fs::path("a.b").has_extension() || fs::path("/.a").has_extension() ||
  738. fs::path("/a/").has_extension() || fs::path("/").has_extension()) {
  739. result = false;
  740. }
  741. #if defined(_WIN32)
  742. p = "c:/a/b";
  743. if (!fs::path("c:/a/b").has_root_name() || !p.has_root_directory() ||
  744. !p.has_root_path()) {
  745. result = false;
  746. }
  747. p = "c:a/b";
  748. if (!p.has_root_name() || p.has_root_directory() || !p.has_root_path()) {
  749. result = false;
  750. }
  751. p = "//host/b";
  752. if (!p.has_root_name() || !p.has_root_directory() || !p.has_root_path()) {
  753. result = false;
  754. }
  755. p = "//host";
  756. if (!p.has_root_name() || p.has_root_directory() || !p.has_root_path()) {
  757. result = false;
  758. }
  759. if (!fs::path("c:/a/b").has_relative_path() ||
  760. !fs::path("c:a/b").has_relative_path() ||
  761. !fs::path("//host/b").has_relative_path()) {
  762. result = false;
  763. }
  764. if (!fs::path("c:/a/b").has_parent_path() ||
  765. !fs::path("c:/").has_parent_path() ||
  766. !fs::path("c:").has_parent_path() ||
  767. !fs::path("//host/").has_parent_path() ||
  768. !fs::path("//host").has_parent_path()) {
  769. result = false;
  770. }
  771. #endif
  772. }
  773. {
  774. #if defined(_WIN32)
  775. fs::path p("c:/a");
  776. #else
  777. fs::path p("/a");
  778. #endif
  779. if (!p.is_absolute() || p.is_relative()) {
  780. result = false;
  781. }
  782. p = "a/b";
  783. if (p.is_absolute() || !p.is_relative()) {
  784. result = false;
  785. }
  786. #if defined(_WIN32)
  787. p = "c:/a/b";
  788. if (!p.is_absolute() || p.is_relative()) {
  789. result = false;
  790. }
  791. p = "//host/b";
  792. if (!p.is_absolute() || p.is_relative()) {
  793. result = false;
  794. }
  795. p = "/a";
  796. if (p.is_absolute() || !p.is_relative()) {
  797. result = false;
  798. }
  799. p = "c:a";
  800. if (p.is_absolute() || !p.is_relative()) {
  801. result = false;
  802. }
  803. #endif
  804. }
  805. checkResult(result);
  806. return result;
  807. }
  808. bool testIterators()
  809. {
  810. std::cout << "testIterators()";
  811. bool result = true;
  812. {
  813. fs::path p("/a/b/");
  814. #if defined(_WIN32)
  815. std::vector<fs::path::string_type> ref{ L"/", L"a", L"b", L"" };
  816. #else
  817. std::vector<fs::path::string_type> ref{ "/", "a", "b", "" };
  818. #endif
  819. std::vector<fs::path::string_type> res;
  820. for (auto i = p.begin(), e = p.end(); i != e; ++i) {
  821. res.push_back(*i);
  822. }
  823. if (res != ref) {
  824. result = false;
  825. }
  826. res.clear();
  827. for (const auto& e : p) {
  828. res.push_back(e);
  829. }
  830. if (res != ref) {
  831. result = false;
  832. }
  833. }
  834. {
  835. fs::path p("/a/b/");
  836. #if defined(_WIN32)
  837. std::vector<fs::path::string_type> ref{ L"", L"b", L"a", L"/" };
  838. #else
  839. std::vector<fs::path::string_type> ref{ "", "b", "a", "/" };
  840. #endif
  841. std::vector<fs::path::string_type> res;
  842. auto i = p.end(), b = p.begin();
  843. do {
  844. res.push_back(*--i);
  845. } while (i != b);
  846. if (res != ref) {
  847. result = false;
  848. }
  849. }
  850. checkResult(result);
  851. return result;
  852. }
  853. bool testNonMemberFunctions()
  854. {
  855. std::cout << "testNonMemberFunctions()";
  856. bool result = true;
  857. {
  858. fs::path p1("/a/b/");
  859. fs::path p2("/c/d");
  860. fs::swap(p1, p2);
  861. if (p1.string() != "/c/d" || p2.string() != "/a/b/")
  862. result = false;
  863. }
  864. {
  865. auto h1 = fs::hash_value(fs::path("/a//b//"));
  866. auto h2 = fs::hash_value(fs::path("/a/b/"));
  867. if (h1 != h2)
  868. result = false;
  869. }
  870. {
  871. fs::path p1("/a/b/");
  872. fs::path p2("/c/d");
  873. if (p1 == p2)
  874. result = false;
  875. p1 = "/a//b//";
  876. p2 = "/a/b/";
  877. if (p1 != p2)
  878. result = false;
  879. }
  880. {
  881. fs::path p = "/a";
  882. p = p / "b" / "c";
  883. if (p.generic_string() != "/a/b/c") {
  884. result = false;
  885. }
  886. fs::path::string_type ref;
  887. ref += fs::path::value_type('/');
  888. ref += fs::path::value_type('a');
  889. ref += fs::path::preferred_separator;
  890. ref += fs::path::value_type('b');
  891. ref += fs::path::preferred_separator;
  892. ref += fs::path::value_type('c');
  893. if (p.native() != ref) {
  894. result = false;
  895. }
  896. }
  897. {
  898. fs::path p("/a b\\c/");
  899. std::ostringstream oss;
  900. oss << p;
  901. if (oss.str() != "\"/a b\\\\c/\"") {
  902. result = false;
  903. }
  904. std::istringstream iss(oss.str());
  905. fs::path p2;
  906. iss >> p2;
  907. if (p2 != p) {
  908. result = false;
  909. }
  910. }
  911. checkResult(result);
  912. return result;
  913. }
  914. }
  915. int testCMFilesystemPath(int /*unused*/, char* /*unused*/[])
  916. {
  917. int result = 0;
  918. if (!testConstructors()) {
  919. result = 1;
  920. }
  921. if (!testConcatenation()) {
  922. result = 1;
  923. }
  924. if (!testModifiers()) {
  925. result = 1;
  926. }
  927. if (!testObservers()) {
  928. result = 1;
  929. }
  930. if (!testCompare()) {
  931. result = 1;
  932. }
  933. if (!testGeneration()) {
  934. result = 1;
  935. }
  936. if (!testDecomposition()) {
  937. result = 1;
  938. }
  939. if (!testQueries()) {
  940. result = 1;
  941. }
  942. if (!testIterators()) {
  943. result = 1;
  944. }
  945. if (!testNonMemberFunctions()) {
  946. result = 1;
  947. }
  948. return result;
  949. }