5#include "testing/testing.h"
16#define DO_PERF_TESTS 0
23# define ABS_PREFIX "C:"
32 if (
str[0] ==
'/' &&
str[1] ==
'/') {
47 char *str_dupe = strdup(
str);
58#define NORMALIZE(input, output_expect) \
60 char path[FILE_MAX] = input; \
62 str_replace_char_with_relative_exception(path, '/', '\\'); \
64 const int path_len_test = BLI_path_normalize(path); \
66 BLI_string_replace_char(path, '\\', '/'); \
68 EXPECT_STREQ(path, output_expect); \
69 EXPECT_EQ(path_len_test, strlen(path)); \
74TEST(path_utils, Normalize_Nop)
83TEST(path_utils, Normalize_NopRelative)
93TEST(path_utils, Normalize_Dot)
101TEST(path_utils, Normalize_ComplexAbsolute)
103 NORMALIZE(
"/a/./b/./c/./.././.././",
"/a/");
104 NORMALIZE(
"/a//.//b//.//c//.//..//.//..//.//",
"/a/");
106TEST(path_utils, Normalize_ComplexRelative)
108 NORMALIZE(
"a/b/c/d/e/f/g/../a/../b/../../c/../../../d/../../../..",
".");
109 NORMALIZE(
"a/b/c/d/e/f/g/../a/../../../../b/../../../c/../../d/..",
".");
112TEST(path_utils, Normalize_DoubleSlash)
120TEST(path_utils, Normalize_Parent)
123 NORMALIZE(
"/a/../a/b/../b/c/../c/",
"/a/b/c/");
126TEST(path_utils, Normalize_UnbalancedAbsolute)
130 NORMALIZE(
"/a/b/c/../../../../../d",
"/d");
135 NORMALIZE(
"/home/username/Downloads/../../../../../Users/Example/Desktop/test.jpg",
136 "/Users/Example/Desktop/test.jpg");
140TEST(path_utils, Normalize_UnbalancedRelative)
150 NORMALIZE(
"//./a/../../../",
"//../../");
152 NORMALIZE(
"../a/../../../",
"../../../");
153 NORMALIZE(
"a/b/../c/../../d/../../../e/../../../../f",
"../../../../../f");
154 NORMALIZE(
".../.../a/.../b/../c/../../d/../../../e/../../../.../../f",
"../f");
157TEST(path_utils, Normalize_UnbalancedRelativeTrailing)
182TEST(path_utils, CompareNormalized)
200#define PARENT_DIR(input, output_expect) \
202 char path[FILE_MAX] = input; \
204 BLI_string_replace_char(path, '/', '\\'); \
206 BLI_path_parent_dir(path); \
208 BLI_string_replace_char(path, '\\', '/'); \
210 EXPECT_STREQ(path, output_expect); \
214TEST(path_utils, ParentDir_Simple)
231TEST(path_utils, ParentDir_TrailingPeriod)
244TEST(path_utils, ParentDir_Complex)
252 PARENT_DIR(
"{test_variable}/a",
"{test_variable}/");
257 PARENT_DIR(
"{test_variable}/a/{test_variable}",
"{test_variable}/a/");
268#define PARENT_DIR_END(input, offset_expect) \
270 const char *path = input; \
271 const char *path_end = BLI_path_parent_dir_end(path, strlen(path)); \
272 EXPECT_EQ(path_end ? int(path_end - path) : -1, offset_expect) << "path: " << path; \
276TEST(path_utils, ParentDirEnd_Simple)
284TEST(path_utils, ParentDirEnd_Redundant)
303#define AT_INDEX(str_input, index_input, str_expect) \
305 char path[] = str_input; \
308 BLI_string_replace_char(path, '/', '\\'); \
310 const char *expect = str_expect; \
311 int index_output, len_output; \
312 const bool ret = BLI_path_name_at_index(path, index_input, &index_output, &len_output); \
313 if (expect == nullptr) { \
318 EXPECT_EQ(len_output, strlen(expect)); \
319 path[index_output + len_output] = '\0'; \
320 EXPECT_STREQ(&path[index_output], expect); \
325TEST(path_utils, NameAtIndex_Single)
338TEST(path_utils, NameAtIndex_SingleNeg)
352TEST(path_utils, NameAtIndex_Double)
366TEST(path_utils, NameAtIndex_DoublNeg)
380TEST(path_utils, NameAtIndex_Misc)
382 AT_INDEX(
"/how/now/brown/cow", 0,
"how");
383 AT_INDEX(
"/how/now/brown/cow", 1,
"now");
384 AT_INDEX(
"/how/now/brown/cow", 2,
"brown");
385 AT_INDEX(
"/how/now/brown/cow", 3,
"cow");
386 AT_INDEX(
"/how/now/brown/cow", 4,
nullptr);
387 AT_INDEX(
"/how/now/brown/cow/", 4,
nullptr);
390TEST(path_utils, NameAtIndex_MiscNeg)
392 AT_INDEX(
"/how/now/brown/cow", 0,
"how");
393 AT_INDEX(
"/how/now/brown/cow", 1,
"now");
394 AT_INDEX(
"/how/now/brown/cow", 2,
"brown");
395 AT_INDEX(
"/how/now/brown/cow", 3,
"cow");
396 AT_INDEX(
"/how/now/brown/cow", 4,
nullptr);
397 AT_INDEX(
"/how/now/brown/cow/", 4,
nullptr);
400#define TEST_STR "./a/./b/./c/."
402TEST(path_utils, NameAtIndex_SingleDot)
411TEST(path_utils, NameAtIndex_SingleDotNeg)
422#define TEST_STR ".//a//.//b//.//c//.//"
424TEST(path_utils, NameAtIndex_SingleDotDoubleSlash)
433TEST(path_utils, NameAtIndex_SingleDotDoubleSlashNeg)
444TEST(path_utils, NameAtIndex_SingleDotSeries)
446 AT_INDEX(
"abc/././/././xyz", 0,
"abc");
447 AT_INDEX(
"abc/././/././xyz", 1,
"xyz");
448 AT_INDEX(
"abc/././/././xyz", 2,
nullptr);
451TEST(path_utils, NameAtIndex_SingleDotSeriesNeg)
453 AT_INDEX(
"abc/././/././xyz", -3,
nullptr);
454 AT_INDEX(
"abc/././/././xyz", -2,
"abc");
455 AT_INDEX(
"abc/././/././xyz", -1,
"xyz");
458TEST(path_utils, NameAtIndex_MiscComplex)
460 AT_INDEX(
"how//now/brown/cow", 0,
"how");
461 AT_INDEX(
"//how///now//brown/cow", 1,
"now");
462 AT_INDEX(
"/how/now///brown//cow", 2,
"brown");
463 AT_INDEX(
"/how/now/brown/cow///", 3,
"cow");
464 AT_INDEX(
"/how/now/brown//cow", 4,
nullptr);
465 AT_INDEX(
"how/now/brown//cow/", 4,
nullptr);
468TEST(path_utils, NameAtIndex_MiscComplexNeg)
470 AT_INDEX(
"how//now/brown/cow", -4,
"how");
471 AT_INDEX(
"//how///now//brown/cow", -3,
"now");
472 AT_INDEX(
"/how/now///brown//cow", -2,
"brown");
473 AT_INDEX(
"/how/now/brown/cow///", -1,
"cow");
474 AT_INDEX(
"/how/now/brown//cow", -5,
nullptr);
475 AT_INDEX(
"how/now/brown//cow/", -5,
nullptr);
478TEST(path_utils, NameAtIndex_NoneComplex)
486TEST(path_utils, NameAtIndex_NoneComplexNeg)
524# define PATH_NORMALIZE_UNC(input, output_expect) \
526 char path[FILE_MAX]; \
527 STRNCPY(path, input); \
528 BLI_path_normalize_unc(path, sizeof(path)); \
529 EXPECT_STREQ(path, output_expect); \
533TEST(path_utils, NormalizeUNC)
536 PATH_NORMALIZE_UNC(
"\\\\?\\C:\\dir\\file",
"C:\\dir\\file");
538 PATH_NORMALIZE_UNC(
"\\\\?\\UNC\\server\\share\\dir",
"\\\\server\\share\\dir");
540 PATH_NORMALIZE_UNC(
"\\\\?\\UNC\\server\\share",
"\\\\server\\share\\");
542 PATH_NORMALIZE_UNC(
"C:\\dir\\file",
"C:\\dir\\file");
543 PATH_NORMALIZE_UNC(
"\\\\server\\share\\dir",
"\\\\server\\share\\dir");
546# undef PATH_NORMALIZE_UNC
581TEST(path_utils, IsWin32DriveOnly)
607TEST(path_utils, IsWin32DriveWithSlash)
632#define JOIN_FORWARD_SLASH(str_expect, out_size, ...) \
634 const char *expect = str_expect; \
635 char result[(out_size) + 1024]; \
637 result[out_size] = '\0'; \
638 BLI_path_join(result, out_size, __VA_ARGS__); \
639 EXPECT_STREQ(result, expect); \
640 EXPECT_EQ(result[out_size], '\0'); \
647#define JOIN_BACK_SLASH(str_expect, out_size, ...) \
649 const char *expect = str_expect; \
650 char result[(out_size) + 1024]; \
651 const char *input_forward_slash[] = {__VA_ARGS__}; \
652 char *input_back_slash[ARRAY_SIZE(input_forward_slash)] = {nullptr}; \
653 for (int i = 0; i < ARRAY_SIZE(input_forward_slash); i++) { \
654 input_back_slash[i] = strdup(input_forward_slash[i]); \
655 BLI_string_replace_char(input_back_slash[i], '/', '\\'); \
658 result[out_size] = '\0'; \
659 BLI_path_join_array(result, \
661 const_cast<const char **>(input_back_slash), \
662 ARRAY_SIZE(input_back_slash)); \
663 BLI_string_replace_char(result, '\\', '/'); \
664 EXPECT_STREQ(result, expect); \
665 EXPECT_EQ(result[out_size], '\0'); \
666 for (int i = 0; i < ARRAY_SIZE(input_forward_slash); i++) { \
667 free(input_back_slash[i]); \
673# define JOIN JOIN_BACK_SLASH
675# define JOIN JOIN_FORWARD_SLASH
681 JOIN(
"", 100,
"",
"");
682 JOIN(
"", 100,
"",
"",
"");
683 JOIN(
"/", 100,
"/",
"",
"");
684 JOIN(
"/", 100,
"/",
"/");
685 JOIN(
"/", 100,
"/",
"",
"/");
686 JOIN(
"/", 100,
"/",
"",
"/",
"");
691 JOIN(
"test", 100,
"test");
694 JOIN(
"/a", 100,
"/a");
695 JOIN(
"a/", 100,
"a/");
696 JOIN(
"/a/", 100,
"/a/");
697 JOIN(
"/a/", 100,
"/a//");
698 JOIN(
"//a/", 100,
"//a//");
703 JOIN(
"/a/b/c", 100,
"/a",
"b",
"c");
704 JOIN(
"/a/b/c", 100,
"/a/",
"/b/",
"/c");
705 JOIN(
"/a/b/c", 100,
"/a/b/",
"/c");
706 JOIN(
"/a/b/c", 100,
"/a/b/c");
707 JOIN(
"/a/b/c", 100,
"/",
"a/b/c");
709 JOIN(
"/a/b/c/", 100,
"/a/",
"/b/",
"/c/");
710 JOIN(
"/a/b/c/", 100,
"/a/b/c/");
711 JOIN(
"/a/b/c/", 100,
"/a/b/",
"/c/");
712 JOIN(
"/a/b/c/", 100,
"/a/b/c",
"/");
713 JOIN(
"/a/b/c/", 100,
"/",
"a/b/c",
"/");
716TEST(path_utils, JoinTruncateShort)
720 JOIN(
"a", 2,
"",
"aa");
721 JOIN(
"a", 2,
"",
"a/");
722 JOIN(
"a/b", 4,
"a",
"bc");
723 JOIN(
"ab/", 4,
"ab",
"c");
724 JOIN(
"/a/", 4,
"/a",
"b");
725 JOIN(
"/a/", 4,
"/a/",
"b/");
726 JOIN(
"/a/", 4,
"/a",
"/b/");
727 JOIN(
"/a/", 4,
"/",
"a/b/");
728 JOIN(
"//a", 4,
"//",
"a/b/");
730 JOIN(
"/a/b", 5,
"/a",
"b",
"c");
733TEST(path_utils, JoinTruncateLong)
735 JOIN(
"", 1,
"//",
"//longer",
"path");
736 JOIN(
"/", 2,
"//",
"//longer",
"path");
737 JOIN(
"//", 3,
"//",
"//longer",
"path");
738 JOIN(
"//l", 4,
"//",
"//longer",
"path");
740 JOIN(
"//longe", 8,
"//",
"//longer",
"path");
741 JOIN(
"//longer", 9,
"//",
"//longer",
"path");
742 JOIN(
"//longer/", 10,
"//",
"//longer",
"path");
743 JOIN(
"//longer/p", 11,
"//",
"//longer",
"path");
744 JOIN(
"//longer/pa", 12,
"//",
"//longer",
"path");
745 JOIN(
"//longer/pat", 13,
"//",
"//longer",
"path");
746 JOIN(
"//longer/path", 14,
"//",
"//longer",
"path");
747 JOIN(
"//longer/path", 14,
"//",
"//longer",
"path/");
748 JOIN(
"//longer/path/", 15,
"//",
"//longer",
"path/");
749 JOIN(
"//longer/path/", 15,
"//",
"//longer",
"path/",
"trunc");
750 JOIN(
"//longer/path/t", 16,
"//",
"//longer",
"path/",
"trunc");
755 JOIN(
"/a/b/c/d/e/f/g/", 100,
"/",
"a/b",
"//////c/d",
"",
"e",
"f",
"g//");
756 JOIN(
"/aa/bb/cc/dd/ee/ff/gg/", 100,
"/",
"aa/bb",
"//////cc/dd",
"",
"ee",
"ff",
"gg//");
757 JOIN(
"1/2/3/", 100,
"1",
"////////",
"",
"2",
"3///");
760TEST(path_utils, JoinRelativePrefix)
762 JOIN(
"//a/b/c", 100,
"//a",
"b",
"c");
763 JOIN(
"//a/b/c", 100,
"//",
"//a//",
"//b//",
"//c");
764 JOIN(
"//a/b/c", 100,
"//",
"//",
"a",
"//",
"b",
"//",
"c");
768#undef JOIN_BACK_SLASH
769#undef JOIN_FORWARD_SLASH
778#define APPEND(str_expect, size, path, filename) \
780 const char *expect = str_expect; \
781 char result[(size) + 1024] = path; \
782 char filename_native[] = filename; \
785 BLI_string_replace_char(filename_native, '/', '\\'); \
786 BLI_string_replace_char(result, '/', '\\'); \
788 BLI_path_append(result, size, filename_native); \
790 BLI_string_replace_char(result, '\\', '/'); \
792 EXPECT_STREQ(result, expect); \
798 APPEND(
"a/b", 100,
"a",
"b");
799 APPEND(
"a/b", 100,
"a/",
"b");
802TEST(path_utils, AppendFile_Truncate)
804 APPEND(
"/A", 3,
"/",
"ABC");
805 APPEND(
"/", 2,
"/",
"test");
806 APPEND(
"X", 2,
"X",
"ABC");
807 APPEND(
"X/", 3,
"X/",
"ABC");
826 EXPECT_STREQ(path,
"123");
833 EXPECT_STREQ(path,
"000000000123");
840 EXPECT_STREQ(path,
"test_123");
847 EXPECT_STREQ(path,
"test_000000000001");
851 char path[
FILE_MAX] =
"test_############";
854 EXPECT_STREQ(path,
"test_000000000001");
858 char path[
FILE_MAX] =
"test_#_#_middle";
861 EXPECT_STREQ(path,
"test_#_123_middle");
869 EXPECT_STREQ(path,
"");
873 char path[
FILE_MAX] =
"test_middle";
876 EXPECT_STREQ(path,
"test_middle");
884 EXPECT_STREQ(path,
"test_-0001");
890 EXPECT_STREQ(path,
"test_-0100");
896 memset(path,
'#',
sizeof(path));
897 path[
sizeof(path) - 1] =
'\0';
905 memset(path, 0xff,
sizeof(path));
906 path[
sizeof(path) - 1] =
'\0';
910 EXPECT_STREQ(path,
"test_ab");
911 EXPECT_STREQ(path + 8,
"\xff\xff\xff\xff");
924 const char *path =
"";
927 EXPECT_STREQ(dir,
"");
928 EXPECT_STREQ(file,
"");
932 const char *path =
"/";
935 EXPECT_STREQ(dir,
"/");
936 EXPECT_STREQ(file,
"");
940 const char *path =
"fileonly";
943 EXPECT_STREQ(dir,
"");
944 EXPECT_STREQ(file,
"fileonly");
948 const char *path =
"dironly/";
951 EXPECT_STREQ(dir,
"dironly/");
952 EXPECT_STREQ(file,
"");
956 const char *path =
"/a/b";
959 EXPECT_STREQ(dir,
"/a/");
960 EXPECT_STREQ(file,
"b");
964 const char *path =
"/dirtoobig/filetoobig";
965 char dir[5], file[5];
967 EXPECT_STREQ(dir,
"/dir");
968 EXPECT_STREQ(file,
"file");
971 EXPECT_STREQ(dir,
"");
972 EXPECT_STREQ(file,
"");
982#define PATH_FRAME_STRIP(input_path, expect_path, expect_ext) \
984 char path[FILE_MAX]; \
985 char ext[FILE_MAX]; \
986 STRNCPY(path, (input_path)); \
987 BLI_path_frame_strip(path, ext, sizeof(ext)); \
988 EXPECT_STREQ(path, expect_path); \
989 EXPECT_STREQ(ext, expect_ext); \
998 PATH_FRAME_STRIP(
"/abspath/to/somefile.001.abc",
"/abspath/to/somefile.###",
".abc");
999 PATH_FRAME_STRIP(
"/ext/longer/somefile.001.alembic",
"/ext/longer/somefile.###",
".alembic");
1000 PATH_FRAME_STRIP(
"/ext/shorter/somefile.123001.abc",
"/ext/shorter/somefile.######",
".abc");
1006#undef PATH_FRAME_STRIP
1045#define PATH_EXTENSION_CHECK(input_path, input_ext, expect_ext) \
1047 const bool ret = BLI_path_extension_check(input_path, input_ext); \
1048 if (STREQ(input_ext, expect_ext)) { \
1052 EXPECT_FALSE(ret); \
1065 ".veryveryverylonglonglongextension",
1066 ".veryveryverylonglonglongextension");
1080#undef PATH_EXTENSION_CHECK
1088#define PATH_EXTENSION_REPLACE_WITH_MAXLEN( \
1089 input_path, input_ext, expect_result, expect_path, maxlen) \
1091 BLI_assert(maxlen <= FILE_MAX); \
1092 char path[FILE_MAX]; \
1093 STRNCPY(path, input_path); \
1094 const bool ret = BLI_path_extension_replace(path, maxlen, input_ext); \
1095 if (expect_result) { \
1099 EXPECT_FALSE(ret); \
1101 EXPECT_STREQ(path, expect_path); \
1105#define PATH_EXTENSION_REPLACE(input_path, input_ext, expect_result, expect_path) \
1106 PATH_EXTENSION_REPLACE_WITH_MAXLEN(input_path, input_ext, expect_result, expect_path, FILE_MAX)
1137TEST(path_utils, ExtensionReplace_Overflow)
1150#undef PATH_EXTENSION_REPLACE
1151#undef PATH_EXTENSION_REPLACE_WITH_MAXLEN
1159#define PATH_EXTENSION_ENSURE_WITH_MAXLEN( \
1160 input_path, input_ext, expect_result, expect_path, maxlen) \
1162 BLI_assert(maxlen <= FILE_MAX); \
1163 char path[FILE_MAX]; \
1164 STRNCPY(path, input_path); \
1165 const bool ret = BLI_path_extension_ensure(path, maxlen, input_ext); \
1166 if (expect_result) { \
1170 EXPECT_FALSE(ret); \
1172 EXPECT_STREQ(path, expect_path); \
1176#define PATH_EXTENSION_ENSURE(input_path, input_ext, expect_result, expect_path) \
1177 PATH_EXTENSION_ENSURE_WITH_MAXLEN(input_path, input_ext, expect_result, expect_path, FILE_MAX)
1205TEST(path_utils, ExtensionEnsure_Overflow)
1215#undef PATH_EXTENSION_ENSURE
1216#undef PATH_EXTENSION_ENSURE_WITH_MAXLEN
1224#define PATH_FRAME_CHECK_CHARS(input_path, expect_hasChars) \
1226 const bool ret = BLI_path_frame_check_chars(input_path); \
1227 if (expect_hasChars) { \
1231 EXPECT_FALSE(ret); \
1253#undef PATH_FRAME_CHECK_CHARS
1261#define PATH_FRAME_RANGE(input_path, sta, end, digits, expect_outpath) \
1263 char path[FILE_MAX]; \
1265 STRNCPY(path, input_path); \
1266 ret = BLI_path_frame_range(path, sizeof(path), sta, end, digits); \
1267 if (expect_outpath == nullptr) { \
1268 EXPECT_FALSE(ret); \
1272 EXPECT_STREQ(path, expect_outpath); \
1288 PATH_FRAME_RANGE(
"", 1, 0, 20,
"00000000000000000001-00000000000000000000");
1293 memset(path, 0xff,
sizeof(path));
1294 path[
sizeof(path) - 1] =
'\0';
1297 EXPECT_STREQ(path,
"test_ab");
1298 EXPECT_STREQ(path + 8,
"\xff\xff\xff\xff");
1301#undef PATH_FRAME_RANGE
1309#define PATH_FRAME_GET(input_path, expect_frame, expect_numdigits, expect_pathisvalid) \
1311 char path[FILE_MAX]; \
1312 int out_frame = -1, out_numdigits = -1; \
1313 STRNCPY(path, input_path); \
1314 const bool ret = BLI_path_frame_get(path, &out_frame, &out_numdigits); \
1315 if (expect_pathisvalid) { \
1319 EXPECT_FALSE(ret); \
1321 EXPECT_EQ(out_frame, expect_frame); \
1322 EXPECT_EQ(out_numdigits, expect_numdigits); \
1330 PATH_FRAME_GET(
"path/to/frame_2810.dummy_quite_long_extension", 2810, 4,
true);
1340#undef PATH_FRAME_GET
1348#define PATH_SEQ_DECODE(path_literal, expect_result, expect_head, expect_tail, expect_numdigits) \
1350 const char *path = path_literal; \
1351 char head[FILE_MAX]; \
1352 char tail[FILE_MAX]; \
1353 ushort numdigits = 0; \
1354 const int result = BLI_path_sequence_decode( \
1355 path, head, sizeof(head), tail, sizeof(tail), &numdigits); \
1356 EXPECT_EQ(result, expect_result); \
1357 EXPECT_STREQ(head, expect_head); \
1358 EXPECT_STREQ(tail, expect_tail); \
1359 EXPECT_EQ(numdigits, expect_numdigits); \
1377#undef PATH_SEQ_DECODE
1385#define PATH_SUFFIX(path_literal, path_literal_max, sep, suffix, expect_result, expect_path) \
1387 char path[FILE_MAX] = path_literal; \
1388 const bool result = BLI_path_suffix(path, path_literal_max, suffix, sep); \
1389 EXPECT_EQ(result, expect_result); \
1390 EXPECT_STREQ(path, expect_path); \
1405 PATH_SUFFIX(
"file.txt", 10,
"A",
"B",
false,
"file.txt");
1407 PATH_SUFFIX(
"file.txt", 11,
"A",
"B",
true,
"fileAB.txt");
1427#define PATH_SLASH_FIND(path, expect_index) \
1429 const char *result = BLI_path_slash_find(path); \
1430 EXPECT_EQ(result, &path[expect_index]); \
1450#undef PATH_SLASH_FIND
1458#define PATH_SLASH_RFIND(path, expect_index) \
1460 const char *result = BLI_path_slash_rfind(path); \
1461 EXPECT_STREQ(result, &path[expect_index]); \
1483#undef PATH_SLASH_RFIND
1491#define PATH_SLASH_ENSURE(input, size, output_expect, length_expect) \
1493 char path[size] = input; \
1494 if (SEP == '\\') { \
1495 BLI_string_replace_char(path, '/', '\\'); \
1497 int result = BLI_path_slash_ensure(path, size); \
1498 if (SEP == '\\') { \
1499 BLI_string_replace_char(path, '\\', '/'); \
1501 EXPECT_STREQ(path, output_expect); \
1502 EXPECT_EQ(result, length_expect); \
1521#undef PATH_SLASH_ENSURE
1529#define PATH_SLASH_RSTRIP(input, expected) \
1531 char path[FILE_MAX] = input; \
1532 if (SEP == '\\') { \
1533 BLI_string_replace_char(path, '/', '\\'); \
1535 BLI_path_slash_rstrip(path); \
1536 if (SEP == '\\') { \
1537 BLI_string_replace_char(path, '\\', '/'); \
1539 EXPECT_STREQ(path, expected); \
1557#undef PATH_SLASH_RSTRIP
1565#define PATH_SLASH_SKIP(input, expected) \
1567 char path[FILE_MAX] = input; \
1568 if (SEP == '\\') { \
1569 BLI_string_replace_char(path, '/', '\\'); \
1571 const char *skipped = BLI_path_slash_skip(path); \
1572 if (SEP == '\\') { \
1573 BLI_string_replace_char(path, '\\', '/'); \
1575 EXPECT_STREQ(skipped, expected); \
1592#undef PATH_SLASH_SKIP
1600#define PATH_SLASH_NATIVE(input, expected) \
1602 char path[FILE_MAX] = input; \
1603 BLI_path_slash_native(path); \
1604 EXPECT_STREQ(path, expected); \
1659#undef PATH_SLASH_NATIVE
1667#define PATH_REL(abs_path, ref_path, rel_path_expect) \
1669 char path[FILE_MAX]; \
1670 const char *ref_path_test = ref_path; \
1671 STRNCPY(path, abs_path); \
1672 if (SEP == '\\') { \
1673 BLI_string_replace_char(path, '/', '\\'); \
1674 ref_path_test = str_replace_char_strdup(ref_path_test, '/', '\\'); \
1676 BLI_path_rel(path, ref_path_test); \
1677 if (SEP == '\\') { \
1678 BLI_string_replace_char(path, '\\', '/'); \
1679 free((void *)ref_path_test); \
1681 EXPECT_STREQ(path, rel_path_expect); \
1690TEST(path_utils, RelPath_SimpleSubdir)
1709TEST(path_utils, RelPath_NoCommonRoot)
1715TEST(path_utils, RelPath_BufferOverflowRoot)
1720 abs_path_in[i] =
'A';
1725 abs_path_out[i] =
'A';
1727 abs_path_out[
FILE_MAX - std::max((strlen(abs_prefix) - 1),
size_t(1))] =
'\0';
1728 PATH_REL(abs_path_in, abs_prefix, abs_path_out);
1731TEST(path_utils, RelPath_BufferOverflowSubdir)
1734 const char *ref_path_in =
ABS_PREFIX "/foo/bar/";
1735 const size_t ref_path_in_len = strlen(ref_path_in);
1737 abs_path_in[i] =
'A';
1741 for (
int i =
STRNCPY_RLEN(abs_path_out,
"//"); i <
FILE_MAX - (int(ref_path_in_len) - 1); i++) {
1742 abs_path_out[i] =
'A';
1744 abs_path_out[
FILE_MAX - std::max((ref_path_in_len - 1),
size_t(1))] =
'\0';
1745 PATH_REL(abs_path_in, ref_path_in, abs_path_out);
1756#define PATH_ABS(rel_path, base_path, abs_path_expect, result_expect) \
1758 char path[FILE_MAX]; \
1759 const char *base_path_test = base_path; \
1760 STRNCPY(path, rel_path); \
1761 if (SEP == '\\') { \
1762 base_path_test = str_replace_char_strdup(base_path_test, '/', '\\'); \
1764 const bool result_test = BLI_path_abs(path, base_path_test); \
1765 if (SEP == '\\') { \
1766 BLI_string_replace_char(path, '\\', '/'); \
1767 free((void *)base_path_test); \
1769 EXPECT_STREQ(path, abs_path_expect); \
1770 EXPECT_EQ(result_test, result_expect); \
1788TEST(path_utils, AbsPath_PrefixOnly)
1806TEST(path_utils, AbsPath_NotRelative)
1814TEST(path_utils, AbsPath_Win32DriveOnPosix)
1816 PATH_ABS(
"C:\\foo.JPG",
"/foo/bar/blender.blend",
"/c/foo.JPG",
false);
1820TEST(path_utils, AbsPath_ExtraSlashes)
1828 PATH_ABS(
"//a.png",
"",
"a.png",
true);
1829 PATH_ABS(
"//a/b.png",
"",
"a/b.png",
true);
1830 PATH_ABS(
"//a.png",
"blender.blend",
"a.png",
true);
1831 PATH_ABS(
"//../a.png",
"",
"../a.png",
true);
1834TEST(path_utils, AbsPath_NoBaseDirExtraSlashes)
1837 PATH_ABS(
"///a.png",
"",
"a.png",
true);
1838 PATH_ABS(
"////a.png",
"",
"a.png",
true);
1839 PATH_ABS(
"///a.png",
"blender.blend",
"a.png",
true);
1843TEST(path_utils, AbsPath_BufferOverflow)
1845 const char *base_dir =
ABS_PREFIX "/foo/bar/";
1848 rel_path_in[i] =
'A';
1853 abs_path_out[i] =
'A';
1928 <<
"A trailing slash on the container makes no difference";
1930 <<
"A trailing slash on the containee makes no difference";
1932 <<
"A path contains its subdirectory";
1934 <<
"Paths should be normalized";
1936 <<
"Windows paths should be supported as well";
1939 <<
"Windows paths should be supported as well";
1941 <<
"Root directory not be contained in a subdirectory";
1943 <<
"Paths should be normalized";
1945 <<
"Just sharing a suffix is not enough, path semantics should be followed";
1947 <<
"Relative paths are not supported";
1952TEST(path_utils, Contains_Windows_case_insensitive)
1955 <<
"On Windows path comparison should ignore case";
1957 <<
"On Windows UNC server and share should ignore case and slash direction";
1959 <<
"A UNC share contains itself";
1961 <<
"Different UNC shares";
1963 <<
"A UNC server is not a parent of its shares";
1978 EXPECT_STREQ(relative,
"") <<
"Path is relative to itself";
1982 EXPECT_STREQ(relative,
"") <<
"Trailing slash makes no difference";
1986 EXPECT_STREQ(relative,
"inside");
1990 <<
"Trailing slash on base makes no difference";
1991 EXPECT_STREQ(relative,
"in" SEP_STR "side");
1994 "/some/path/../path/inside",
"/some/path",
false, relative,
sizeof(relative)));
1995 EXPECT_STREQ(relative,
"inside") <<
"Relative path is normalized";
1997 STRNCPY(relative,
"uninitialized");
2000 EXPECT_STREQ(relative,
"");
2002 STRNCPY(relative,
"uninitialized");
2005 EXPECT_STREQ(relative,
"");
2008 <<
"Blend file relative paths are not supported";
2013 "c:\\SOME\\path\\InSide",
"C:\\some\\path",
false, relative,
sizeof(relative)));
2014 EXPECT_STREQ(relative,
"InSide") <<
"Case insensitive on Windows";
2017 "D:\\some\\path\\inside",
"C:\\some\\path",
true, relative,
sizeof(relative)))
2018 <<
"Another drive has no common root";
2022TEST(path_utils, RelativeTo_walk_up)
2028 EXPECT_STREQ(relative,
"inside");
2031 EXPECT_STREQ(relative,
"") <<
"Equal paths";
2035 EXPECT_STREQ(relative,
".." SEP_STR "outside");
2039 EXPECT_STREQ(relative,
".." SEP_STR "path_library") <<
"Sibling path";
2046 EXPECT_STREQ(relative,
".." SEP_STR ".." SEP_STR) <<
"Parent of the base directory";
2055TEST(path_utils, HasHiddenComponents)
2119TEST(path_utils, HasHiddenComponents_Performance)
2122 const char *test_paths[] = {
2124 "test/a_fairly_long/path/here/shall_we/ok.txt",
2125 "test/a_fairly_long/path/here/.with_a_hidden_component/shall_we/ok.txt",
2126 "test/.another_path_with_hidden_component/yes.txt",
2127 "test/another/path/with/.hidden_filename",
2129 const int RUN_COUNT = 10'000'000;
2131 for (
int i = 0; i < RUN_COUNT; i++) {
2132 for (
int j = 0; j <
ARRAY_SIZE(test_paths); j++) {
File and directory operations.
#define PATH_EXTENSION_REPLACE(input_path, input_ext, expect_result, expect_path)
#define PATH_SLASH_NATIVE(input, expected)
#define PATH_FRAME_RANGE(input_path, sta, end, digits, expect_outpath)
#define PATH_SLASH_RSTRIP(input, expected)
#define PATH_SLASH_ENSURE(input, size, output_expect, length_expect)
#define PATH_ABS(rel_path, base_path, abs_path_expect, result_expect)
#define PATH_EXTENSION_CHECK(input_path, input_ext, expect_ext)
#define PATH_SLASH_FIND(path, expect_index)
#define PATH_FRAME_STRIP(input_path, expect_path, expect_ext)
#define APPEND(str_expect, size, path, filename)
#define PATH_SLASH_RFIND(path, expect_index)
#define PATH_SEQ_DECODE(path_literal, expect_result, expect_head, expect_tail, expect_numdigits)
#define AT_INDEX(str_input, index_input, str_expect)
#define PATH_EXTENSION_REPLACE_WITH_MAXLEN(input_path, input_ext, expect_result, expect_path, maxlen)
#define PATH_SLASH_SKIP(input, expected)
#define NORMALIZE(input, output_expect)
#define PATH_EXTENSION_ENSURE_WITH_MAXLEN(input_path, input_ext, expect_result, expect_path, maxlen)
#define PATH_FRAME_GET(input_path, expect_frame, expect_numdigits, expect_pathisvalid)
#define PATH_FRAME_CHECK_CHARS(input_path, expect_hasChars)
#define PARENT_DIR(input, output_expect)
#define PATH_SUFFIX(path_literal, path_literal_max, sep, suffix, expect_result, expect_path)
#define PATH_REL(abs_path, ref_path, rel_path_expect)
#define PARENT_DIR_END(input, offset_expect)
#define PATH_EXTENSION_ENSURE(input_path, input_ext, expect_result, expect_path)
#define STRNCPY_RLEN(dst, src)
#define SCOPED_TIMER(name)
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
bool BLI_path_is_win32_drive_with_slash(const char *path)
bool BLI_path_is_win32_drive(const char *path)
char * STRNCPY(char(&dst)[N], const char *src)
int bool bool BLI_str_endswith(const char *__restrict str, const char *__restrict end) ATTR_NONNULL(1
void BLI_string_replace_char(char *str, char src, char dst) ATTR_NONNULL(1)
const char * BLI_path_extension(const char *filepath) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
bool BLI_path_relative_to(const char *path, const char *base_dir, bool walk_up, char *r_path_relative, size_t r_path_relative_maxncpy) ATTR_NONNULL(1
bool BLI_path_frame_range(char *path, size_t path_maxncpy, int sta, int end, int digits) ATTR_NONNULL(1)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool BLI_path_is_unc(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
bool BLI_path_is_abs_from_cwd(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool BLI_path_is_win32_drive_only(const char *path)
static char * str_replace_char_strdup(const char *str, char src, char dst)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool BLI_path_has_hidden_component(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
const char * BLI_path_slash_find(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
static void str_replace_char_with_relative_exception(char *str, char src, char dst)
const char * BLI_path_slash_rfind(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT