Changeset View
Changeset View
Standalone View
Standalone View
tests/gtests/blenlib/BLI_path_util_test.cc
| /* Apache License, Version 2.0 */ | /* Apache License, Version 2.0 */GG | ||||
| #include "testing/testing.h" | #include "testing/testing.h" | ||||
| Context not available. | |||||
| EXPECT_STREQ("", file); | EXPECT_STREQ("", file); | ||||
| } | } | ||||
| } | } | ||||
| /* BLI_split_name_num */ | |||||
| TEST(path_util, PathUtilSplitNameNum) | |||||
| { | |||||
| { | |||||
| const char *name = "/any/path/file"; | |||||
| char true_name[16], delimiter='.'; | |||||
| int number, len; | |||||
| len = BLI_split_name_num(true_name, &number, name, delimiter); | |||||
| EXPECT_EQ(len, 14); | |||||
| EXPECT_EQ(number, 0); | |||||
| EXPECT_STREQ(true_name, name); | |||||
| } | |||||
| { | |||||
| const char *name = "/any/path/file567"; | |||||
| char true_name[16], delimiter='.'; | |||||
| int number, len; | |||||
| len = BLI_split_name_num(true_name, &number, name, delimiter); | |||||
| EXPECT_EQ(len, 17); | |||||
| EXPECT_EQ(number, 0); | |||||
| EXPECT_STREQ(true_name, "/any/path/file567"); | |||||
| } | |||||
| { | |||||
| const char *name = "/any/path/file567"; | |||||
| char true_name[16], delimiter='e'; | |||||
| int number, len; | |||||
| len = BLI_split_name_num(true_name, &number, name, delimiter); | |||||
| EXPECT_EQ(len, 13); | |||||
| EXPECT_EQ(number, 567); | |||||
| EXPECT_STREQ(true_name, "/any/path/fil"); | |||||
| } | |||||
| { | |||||
| const char *name = "e567"; | |||||
| char true_name[16], delimiter='e'; | |||||
| int number, len; | |||||
| len = BLI_split_name_num(true_name, &number, name, delimiter); | |||||
| EXPECT_EQ(len, 0); | |||||
| EXPECT_EQ(number, 567); | |||||
| EXPECT_STREQ(true_name, ""); | |||||
| } | |||||
| { | |||||
| const char *name = "567"; | |||||
| char true_name[16], delimiter='e'; | |||||
| int number, len; | |||||
| len = BLI_split_name_num(true_name, &number, name, delimiter); | |||||
| EXPECT_EQ(len, 3); | |||||
| EXPECT_EQ(number, 0); | |||||
| EXPECT_STREQ(true_name, "567"); | |||||
| } | |||||
| } | |||||
| /* BLI_make_file_string */ | |||||
| TEST(path_util, PathUtilMakeFlieString) | |||||
| { | |||||
| { | |||||
| const char *name = "filename"; | |||||
| const char *dir = "mydir/subdir"; | |||||
| const char *relabase = "/any/path/file"; | |||||
| char result_string[30]; | |||||
| BLI_make_file_string(relabase, result_string, dir, name); | |||||
| EXPECT_STREQ(result_string, "mydir/subdir/filename"); | |||||
| } | |||||
| { | |||||
| const char *name = "filename"; | |||||
| const char *dir = "mydir/subdir/"; | |||||
| const char *relabase = "/any/path/file"; | |||||
| char result_string[30]; | |||||
| BLI_make_file_string(relabase, result_string, dir, name); | |||||
| EXPECT_STREQ(result_string, "mydir/subdir/filename"); | |||||
| } | |||||
| { | |||||
| const char *name = "filename"; | |||||
| const char *dir = "//mydir/subdir"; | |||||
| const char *relabase = "/any/path/"; | |||||
| char result_string[40]; | |||||
| BLI_make_file_string(relabase, result_string, dir, name); | |||||
| EXPECT_STREQ(result_string, "/any/path/mydir/subdir/filename"); | |||||
| } | |||||
| { | |||||
| const char *name = "filename"; | |||||
| const char *dir = "//mydir/subdir"; | |||||
| const char *relabase = "/any/path/another_file"; | |||||
| char result_string[40]; | |||||
| BLI_make_file_string(relabase, result_string, dir, name); | |||||
| EXPECT_STREQ(result_string, "/any/path/mydir/subdir/filename"); | |||||
| } | |||||
| } | |||||
| /* BLI_path_sufix */ | |||||
| TEST(path_util, PathUtilPathSufix) | |||||
| { | |||||
| { | |||||
| char name[20] = "filename"; | |||||
| const char *separator = "_____"; | |||||
| const char *suffix = "SUFFIX"; | |||||
| const int maxlen = 20; | |||||
| bool res = BLI_path_suffix(name, maxlen, suffix, separator); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(name, "filename_____SUFFIX"); | |||||
| } | |||||
| { | |||||
| char name[20] = "filename.txt"; | |||||
| const char *separator = "_"; | |||||
| const char *suffix = "SUFFIX"; | |||||
| const int maxlen = 20; | |||||
| bool res = BLI_path_suffix(name, maxlen, suffix, separator); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(name, "filename_SUFFIX.txt"); | |||||
| } | |||||
| { | |||||
| char name[20] = "filename"; | |||||
| const char *separator = "_____"; | |||||
| const char *suffix = "SUFFIX1"; | |||||
| const int maxlen = 20; | |||||
| bool res = BLI_path_suffix(name, maxlen, suffix, separator); | |||||
| EXPECT_FALSE(res); | |||||
| } | |||||
| } | |||||
| /* BLI_path_frame_range */ | |||||
| TEST(path_util, PathUtilPathFrameRange) | |||||
| { | |||||
| { | |||||
| char name[20] = "filename__"; | |||||
| const int digits = 3, start = 21, end = 40; | |||||
| bool res = BLI_path_frame_range(name, start, end, digits); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(name, "filename__021-040"); | |||||
| } | |||||
| { | |||||
| char name[16] = "filename__#.txt"; | |||||
| const int digits = 3, start = 21, end = 40; | |||||
| bool res = BLI_path_frame_range(name, start, end, digits); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(name, "filename__21-40.txt"); | |||||
| } | |||||
| { | |||||
| char name[50] = "filename_###_.txt"; | |||||
| const int digits = 3, start = 21, end = 40; | |||||
| bool res = BLI_path_frame_range(name, start, end, digits); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(name, "filename_021-040_.txt"); | |||||
| } | |||||
| } | |||||
| /* BLI_string_dec */ | |||||
| TEST(path_util, PathUtilStringDec) | |||||
| { | |||||
| { | |||||
| const char * name = "/any/path/filename"; | |||||
| char head[20], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(0, res); | |||||
| EXPECT_EQ(0, numlen); | |||||
| EXPECT_STREQ(head, name); | |||||
| EXPECT_STREQ(tail, ""); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path/filename.ext"; | |||||
| char head[30], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(0, res); | |||||
| EXPECT_EQ(0, numlen); | |||||
| EXPECT_STREQ(head, "/any/path/filename"); | |||||
| EXPECT_STREQ(tail, ".ext"); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path/filename72"; | |||||
| char head[30], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(72, res); | |||||
| EXPECT_EQ(2, numlen); | |||||
| EXPECT_STREQ(head, "/any/path/filename"); | |||||
| EXPECT_STREQ(tail, ""); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path/filename72.ext64"; | |||||
| char head[30], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(res,72); | |||||
| EXPECT_EQ(numlen,2); | |||||
| EXPECT_STREQ(head, "/any/path/filename"); | |||||
| EXPECT_STREQ(tail, ".ext64"); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path/file0072name.ext"; | |||||
| char head[30], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(res,72); | |||||
| EXPECT_EQ(numlen,4); | |||||
| EXPECT_STREQ(head, "/any/path/file"); | |||||
| EXPECT_STREQ(tail, "name.ext"); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path/file53name012.ext"; | |||||
| char head[30], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(res,12); | |||||
| EXPECT_EQ(numlen,3); | |||||
| EXPECT_STREQ(head, "/any/path/file53name"); | |||||
| EXPECT_STREQ(tail, ".ext"); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path32/filename.ext64"; | |||||
| char head[30], tail[20]; | |||||
| short unsigned numlen; | |||||
| int res = BLI_stringdec(name, head, tail, &numlen); | |||||
| EXPECT_EQ(res,0); | |||||
| EXPECT_EQ(numlen,0); | |||||
| EXPECT_STREQ(head, "/any/path32/filename"); | |||||
| EXPECT_STREQ(tail, ".ext64"); | |||||
| } | |||||
| } | |||||
| /* BLI_string_enc */ | |||||
| TEST(path_util, PathUtilStringEnc) | |||||
| { | |||||
| { | |||||
| char result[30]; | |||||
| const char * head = "/any/path/file"; | |||||
| const char * tail = ".ext"; | |||||
| short unsigned numlen = 1; | |||||
| int num = 52; | |||||
| BLI_stringenc(result, head, tail, numlen, num); | |||||
| EXPECT_STREQ(result, "/any/path/file52.ext"); | |||||
| } | |||||
| { | |||||
| char result[30]; | |||||
| const char * head = "/any/path/file"; | |||||
| const char * tail = ".ext"; | |||||
| short unsigned numlen = 5; | |||||
| int num = 52; | |||||
| BLI_stringenc(result, head, tail, numlen, num); | |||||
| EXPECT_STREQ(result, "/any/path/file00052.ext"); | |||||
| } | |||||
| } | |||||
| /* BLI_string_dec_AND_enc */ | |||||
| TEST(path_util, PathUtilStringDecANDEnc) | |||||
| { | |||||
| { | |||||
| const char * name = "/any/path/file53name012.ext"; | |||||
| char head[30], tail[20], result[30]; | |||||
| short unsigned numlen; | |||||
| int num = BLI_stringdec(name, head, tail, &numlen); | |||||
| BLI_stringenc(result, head, tail, numlen, num); | |||||
| EXPECT_STREQ("/any/path/file53name012.ext", result); | |||||
| } | |||||
| { | |||||
| const char * name = "/any/path64/filename.ext64"; | |||||
| char head[30], tail[20], result[30]; | |||||
| short unsigned numlen; | |||||
| int num = BLI_stringdec(name, head, tail, &numlen); | |||||
| BLI_stringenc(result, head, tail, numlen, num); | |||||
| EXPECT_STREQ(result, "/any/path64/filename.ext64"); | |||||
| } | |||||
| } | |||||
| /* BLI_string_enc_AND_dec */ | |||||
| TEST(path_util, PathUtilStringEncANDDec) | |||||
| { | |||||
| { | |||||
| char result[30]; | |||||
| char head[15] = "/any/path/file"; | |||||
| char tail[5] = ".ext"; | |||||
| short unsigned numlen = 1; | |||||
| int num = 52; | |||||
| BLI_stringenc(result, head, tail, numlen, num); | |||||
| int res = BLI_stringdec(result, head, tail, &numlen); | |||||
| EXPECT_EQ(52, res); | |||||
| EXPECT_EQ(2, numlen); | |||||
| EXPECT_STREQ(head, "/any/path/file"); | |||||
| EXPECT_STREQ(tail, ".ext"); | |||||
| } | |||||
| { | |||||
| char result[30]; | |||||
| char head[17] = "/any/path32/file"; | |||||
| char tail[7] = ".ext64"; | |||||
| short unsigned numlen = 5; | |||||
| int num = 52; | |||||
| BLI_stringenc(result, head, tail, numlen, num); | |||||
| int res = BLI_stringdec(result, head, tail, &numlen); | |||||
| EXPECT_EQ(52, res); | |||||
| EXPECT_EQ(5, numlen); | |||||
| EXPECT_STREQ(head, "/any/path32/file"); | |||||
| EXPECT_STREQ(tail, ".ext64"); | |||||
| } | |||||
| } | |||||
| /* BLI_path_abs */ | |||||
| TEST(path_util, PathUtilPathAbs) | |||||
| { | |||||
| { | |||||
| char path[30] = "//any/path/file"; | |||||
| const char * base = "/some/base/path/"; | |||||
| int res = BLI_path_abs(path, base); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/some/base/path/any/path/file"); | |||||
| } | |||||
| { | |||||
| char path[30] = "//any/path\\file"; | |||||
| const char * base = "/some/base\\path/file"; | |||||
| int res = BLI_path_abs(path, base); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/some/base/path/any/path/file"); | |||||
| } | |||||
| { | |||||
| char path[30] = "/any/path\\file"; | |||||
| const char * base = "/some/base\\path/file"; | |||||
| int res = BLI_path_abs(path, base); | |||||
| EXPECT_FALSE(res); | |||||
| EXPECT_STREQ(path, "/any/path/file"); | |||||
| } | |||||
| } | |||||
| /* BLI_path_rel */ | |||||
| TEST(path_util, PathUtilPathRel) | |||||
| { | |||||
| { | |||||
| char path[30] = "//any/path/file"; | |||||
| const char * base = "/some/base/path/"; | |||||
| BLI_path_rel(path, base); | |||||
| EXPECT_STREQ(path, "//any/path/file"); | |||||
| } | |||||
| { | |||||
| char path[30] = "/any/path/file"; | |||||
| const char * base = "/some/base/path/"; | |||||
| BLI_path_rel(path, base); | |||||
| EXPECT_STREQ(path, "//../../../any/path/file"); | |||||
| } | |||||
| { | |||||
| char path[30] = "/any/path\\file"; | |||||
| const char * base = "/some\\/base/path/file"; | |||||
| BLI_path_rel(path, base); | |||||
| EXPECT_STREQ(path, "//../../../any/path/file"); | |||||
| } | |||||
| { | |||||
| char path[30] = "/any/path\\file"; | |||||
| const char * base = "/any/some\\/path/file"; | |||||
| BLI_path_rel(path, base); | |||||
| EXPECT_STREQ(path, "//../../path/file"); | |||||
| } | |||||
| } | |||||
| /* BLI_join_dirfileJoinDirFile */ | |||||
| TEST(path_util, PathUtilJoinDirFile) | |||||
| { | |||||
| { | |||||
| const char * file = "filename.ext"; | |||||
| const char * dir = "/any/path/"; | |||||
| char result[30]; | |||||
| const int maxlen = 6; | |||||
| BLI_join_dirfile(result, maxlen, dir, file); | |||||
| EXPECT_STREQ(result, "/any/"); | |||||
| } | |||||
| { | |||||
| const char * file = "filename.ext"; | |||||
| const char * dir = "/any/path/no_slash"; | |||||
| char result[40]; | |||||
| const int maxlen = 40; | |||||
| BLI_join_dirfile(result, maxlen, dir, file); | |||||
| EXPECT_STREQ(result, "/any/path/no_slash/filename.ext"); | |||||
| } | |||||
| } | |||||
| /* BLI_replace_extension */ | |||||
| TEST(path_util, PathUtilReplaceExtension) | |||||
| { | |||||
| { | |||||
| char path[26] = "/any/path/filename.ext"; | |||||
| const char * ext = ".abcdef"; | |||||
| const int maxlen = 26; | |||||
| bool res = BLI_replace_extension(path, maxlen, ext); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.abcdef"); | |||||
| } | |||||
| { | |||||
| char path[26] = "/any/path/filename.ext"; | |||||
| const char * ext = ".abcdef"; | |||||
| const int maxlen = 25; | |||||
| bool res = BLI_replace_extension(path, maxlen, ext); | |||||
| EXPECT_FALSE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.ext"); | |||||
| } | |||||
| { | |||||
| char path[26] = "/any/path/filename.ext"; | |||||
| const char * ext = ".a"; | |||||
| const int maxlen = 26; | |||||
| bool res = BLI_replace_extension(path, maxlen, ext); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.a"); | |||||
| } | |||||
| } | |||||
| /* BLI_ensure_extension */ | |||||
| TEST(path_util, PathUtilEnsureExtension) | |||||
| { | |||||
| { | |||||
| char path[25] = "/any/path/filename.ext"; | |||||
| const char * ext = ".ext"; | |||||
| const int maxlen = 25; | |||||
| bool res = BLI_ensure_extension(path, maxlen, ext); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.ext"); | |||||
| } | |||||
| { | |||||
| char path[25] = "/any/path/filename"; | |||||
| const char * ext = ".ext"; | |||||
| const int maxlen = 25; | |||||
| bool res = BLI_ensure_extension(path, maxlen, ext); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.ext"); | |||||
| } | |||||
| { | |||||
| char path[25] = "/any/path/filename"; | |||||
| const char * ext = ".ext"; | |||||
| const int maxlen = 22; | |||||
| bool res = BLI_ensure_extension(path, maxlen, ext); | |||||
| EXPECT_FALSE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename"); | |||||
| } | |||||
| { | |||||
| char path[25] = "/any/path/filename...."; | |||||
| const char * ext = ".ext"; | |||||
| const int maxlen = 25; | |||||
| bool res = BLI_ensure_extension(path, maxlen, ext); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.ext"); | |||||
| } | |||||
| } | |||||
| /* BLI_ensure_filename */ | |||||
| TEST(path_util, PathUtilEnsureFilename) | |||||
| { | |||||
| { | |||||
| char path[30] = "/any/path/random_name.txt"; | |||||
| const char * filename = "filename.ext"; | |||||
| const int maxlen = 30; | |||||
| bool res = BLI_ensure_filename(path, maxlen, filename); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/filename.ext"); | |||||
| } | |||||
| { | |||||
| char path[27] = "/any/path/short.a"; | |||||
| const char * filename = "quite_longer.ext"; | |||||
| const int maxlen = 27; | |||||
| bool res = BLI_ensure_filename(path, maxlen, filename); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "/any/path/quite_longer.ext"); | |||||
| } | |||||
| { | |||||
| char path[25] = "/any/path/short.a"; | |||||
| const char * filename = "quite_longer.ext"; | |||||
| const int maxlen = 25; | |||||
| bool res = BLI_ensure_filename(path, maxlen, filename); | |||||
| EXPECT_FALSE(res); | |||||
| EXPECT_STREQ(path, "/any/path/short.a"); | |||||
| } | |||||
| { | |||||
| char path[25] = "no_dirs"; | |||||
| const char * filename = "just_replace.txt"; | |||||
| const int maxlen = 25; | |||||
| bool res = BLI_ensure_filename(path, maxlen, filename); | |||||
| EXPECT_TRUE(res); | |||||
| EXPECT_STREQ(path, "just_replace.txt"); | |||||
| } | |||||
| } | |||||
| Context not available. | |||||