diff options
Diffstat (limited to 'tests/gtests/blenlib')
-rw-r--r-- | tests/gtests/blenlib/BLI_array_store_test.cc | 28 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_path_util_test.cc | 237 |
2 files changed, 248 insertions, 17 deletions
diff --git a/tests/gtests/blenlib/BLI_array_store_test.cc b/tests/gtests/blenlib/BLI_array_store_test.cc index 5af6e639e64..370a4111bae 100644 --- a/tests/gtests/blenlib/BLI_array_store_test.cc +++ b/tests/gtests/blenlib/BLI_array_store_test.cc @@ -36,15 +36,15 @@ static void print_mem_saved(const char *id, const BArrayStore *bs) /* -------------------------------------------------------------------- */ /* Test Chunks (building data from list of chunks) */ -typedef struct TestChunnk { - struct TestChunnk *next, *prev; +typedef struct TestChunk { + struct TestChunk *next, *prev; const void *data; size_t data_len; -} TestChunnk; +} TestChunk; -static TestChunnk *testchunk_list_add(ListBase *lb, const void *data, size_t data_len) +static TestChunk *testchunk_list_add(ListBase *lb, const void *data, size_t data_len) { - TestChunnk *tc = (TestChunnk *)MEM_mallocN(sizeof(*tc), __func__); + TestChunk *tc = (TestChunk *)MEM_mallocN(sizeof(*tc), __func__); tc->data = data; tc->data_len = data_len; BLI_addtail(lb, tc); @@ -53,7 +53,7 @@ static TestChunnk *testchunk_list_add(ListBase *lb, const void *data, size_t dat } #if 0 -static TestChunnk *testchunk_list_add_copydata(ListBase *lb, const void *data, size_t data_len) +static TestChunk *testchunk_list_add_copydata(ListBase *lb, const void *data, size_t data_len) { void *data_copy = MEM_mallocN(data_len, __func__); memcpy(data_copy, data, data_len); @@ -63,7 +63,7 @@ static TestChunnk *testchunk_list_add_copydata(ListBase *lb, const void *data, s static void testchunk_list_free(ListBase *lb) { - for (TestChunnk *tc = (TestChunnk *)lb->first, *tb_next; tc; tc = tb_next) { + for (TestChunk *tc = (TestChunk *)lb->first, *tb_next; tc; tc = tb_next) { tb_next = tc->next; MEM_freeN((void *)tc->data); MEM_freeN(tc); @@ -77,12 +77,12 @@ static char *testchunk_as_data( size_t *r_data_len) { size_t data_len = 0; - for (TestChunnk *tc = (TestChunnk *)lb->first; tc; tc = tc->next) { + for (TestChunk *tc = (TestChunk *)lb->first; tc; tc = tc->next) { data_len += tc->data_len; } char *data = (char *)MEM_mallocN(data_len, __func__); size_t i = 0; - for (TestChunnk *tc = (TestChunnk *)lb->first; tc; tc = tc->next) { + for (TestChunk *tc = (TestChunk *)lb->first; tc; tc = tc->next) { memcpy(&data[i], tc->data, tc->data_len); data_len += tc->data_len; i += tc->data_len; @@ -95,7 +95,7 @@ static char *testchunk_as_data( #endif static char *testchunk_as_data_array( - TestChunnk **tc_array, int tc_array_len, + TestChunk **tc_array, int tc_array_len, size_t *r_data_len) { size_t data_len = 0; @@ -105,7 +105,7 @@ static char *testchunk_as_data_array( char *data = (char *)MEM_mallocN(data_len, __func__); size_t i = 0; for (int tc_index = 0; tc_index < tc_array_len; tc_index++) { - TestChunnk *tc = tc_array[tc_index]; + TestChunk *tc = tc_array[tc_index]; memcpy(&data[i], tc->data, tc->data_len); i += tc->data_len; } @@ -677,9 +677,9 @@ static void random_chunk_mutate_helper( ListBase random_chunks; BLI_listbase_clear(&random_chunks); random_chunk_generate(&random_chunks, chunks_per_buffer, stride, chunk_count, random_seed); - TestChunnk **chunks_array = (TestChunnk **)MEM_mallocN(chunks_per_buffer * sizeof(TestChunnk *), __func__); + TestChunk **chunks_array = (TestChunk **)MEM_mallocN(chunks_per_buffer * sizeof(TestChunk *), __func__); { - TestChunnk *tc = (TestChunnk *)random_chunks.first; + TestChunk *tc = (TestChunk *)random_chunks.first; for (int i = 0; i < chunks_per_buffer; i++, tc = tc->next) { chunks_array[i] = tc; } @@ -692,7 +692,7 @@ static void random_chunk_mutate_helper( { RNG *rng = BLI_rng_new(random_seed); for (int i = 0; i < items_total; i++) { - BLI_rng_shuffle_array(rng, chunks_array, sizeof(TestChunnk *), chunks_per_buffer); + BLI_rng_shuffle_array(rng, chunks_array, sizeof(TestChunk *), chunks_per_buffer); size_t data_len; char *data = testchunk_as_data_array(chunks_array, chunks_per_buffer, &data_len); BLI_assert(data_len == chunks_per_buffer * chunk_count * stride); diff --git a/tests/gtests/blenlib/BLI_path_util_test.cc b/tests/gtests/blenlib/BLI_path_util_test.cc index d017ab18b4d..ef469da50b2 100644 --- a/tests/gtests/blenlib/BLI_path_util_test.cc +++ b/tests/gtests/blenlib/BLI_path_util_test.cc @@ -5,6 +5,7 @@ extern "C" { #include "BLI_fileops.h" #include "BLI_path_util.h" +#include "BLI_string.h" #include "../../../source/blender/imbuf/IMB_imbuf.h" #ifdef _WIN32 @@ -57,7 +58,7 @@ char *zLhm65070058860608_br_find_exe(const char *default_exe) /* BLI_cleanup_path */ #ifndef _WIN32 -TEST(path_util, PathUtilClean) +TEST(path_util, Clean) { /* "/./" -> "/" */ { @@ -113,8 +114,238 @@ TEST(path_util, PathUtilClean) } #endif + +#define AT_INDEX(str_input, index_input, str_expect) \ + { \ + char path[] = str_input; \ + const char *expect = str_expect; \ + int index_output, len_output; \ + const bool ret = BLI_path_name_at_index(path, index_input, &index_output, &len_output); \ + if (expect == NULL) { \ + EXPECT_EQ(ret, false); \ + } \ + else { \ + EXPECT_EQ(ret, true); \ + EXPECT_EQ(strlen(expect), len_output); \ + path[index_output + len_output] = '\0'; \ + EXPECT_STREQ(&path[index_output], expect); \ + } \ + }((void)0) + +/* BLI_path_name_at_index */ +TEST(path_util, NameAtIndex_Single) +{ + AT_INDEX("/a", 0, "a"); + AT_INDEX("/a/", 0, "a"); + AT_INDEX("a/", 0, "a"); + AT_INDEX("//a//", 0, "a"); + AT_INDEX("a/b", 0, "a"); + + AT_INDEX("/a", 1, NULL); + AT_INDEX("/a/", 1, NULL); + AT_INDEX("a/", 1, NULL); + AT_INDEX("//a//", 1, NULL); +} +TEST(path_util, NameAtIndex_SingleNeg) +{ + AT_INDEX("/a", -1, "a"); + AT_INDEX("/a/", -1, "a"); + AT_INDEX("a/", -1, "a"); + AT_INDEX("//a//", -1, "a"); + AT_INDEX("a/b", -1, "b"); + + AT_INDEX("/a", -2, NULL); + AT_INDEX("/a/", -2, NULL); + AT_INDEX("a/", -2, NULL); + AT_INDEX("//a//", -2, NULL); +} + +TEST(path_util, NameAtIndex_Double) +{ + AT_INDEX("/ab", 0, "ab"); + AT_INDEX("/ab/", 0, "ab"); + AT_INDEX("ab/", 0, "ab"); + AT_INDEX("//ab//", 0, "ab"); + AT_INDEX("ab/c", 0, "ab"); + + AT_INDEX("/ab", 1, NULL); + AT_INDEX("/ab/", 1, NULL); + AT_INDEX("ab/", 1, NULL); + AT_INDEX("//ab//", 1, NULL); +} + +TEST(path_util, NameAtIndex_DoublNeg) +{ + AT_INDEX("/ab", -1, "ab"); + AT_INDEX("/ab/", -1, "ab"); + AT_INDEX("ab/", -1, "ab"); + AT_INDEX("//ab//", -1, "ab"); + AT_INDEX("ab/c", -1, "c"); + + AT_INDEX("/ab", -2, NULL); + AT_INDEX("/ab/", -2, NULL); + AT_INDEX("ab/", -2, NULL); + AT_INDEX("//ab//", -2, NULL); +} + +TEST(path_util, NameAtIndex_Misc) +{ + AT_INDEX("/how/now/brown/cow", 0, "how"); + AT_INDEX("/how/now/brown/cow", 1, "now"); + AT_INDEX("/how/now/brown/cow", 2, "brown"); + AT_INDEX("/how/now/brown/cow", 3, "cow"); + AT_INDEX("/how/now/brown/cow", 4, NULL); + AT_INDEX("/how/now/brown/cow/", 4, NULL); +} + +TEST(path_util, NameAtIndex_MiscNeg) +{ + AT_INDEX("/how/now/brown/cow", 0, "how"); + AT_INDEX("/how/now/brown/cow", 1, "now"); + AT_INDEX("/how/now/brown/cow", 2, "brown"); + AT_INDEX("/how/now/brown/cow", 3, "cow"); + AT_INDEX("/how/now/brown/cow", 4, NULL); + AT_INDEX("/how/now/brown/cow/", 4, NULL); +} + +TEST(path_util, NameAtIndex_MiscComplex) +{ + AT_INDEX("how//now/brown/cow", 0, "how"); + AT_INDEX("//how///now\\/brown/cow", 1, "now"); + AT_INDEX("/how/now\\//brown\\/cow", 2, "brown"); + AT_INDEX("/how/now/brown/cow//\\", 3, "cow"); + AT_INDEX("/how/now/brown/\\cow", 4, NULL); + AT_INDEX("how/now/brown/\\cow\\", 4, NULL); +} + +TEST(path_util, NameAtIndex_MiscComplexNeg) +{ + AT_INDEX("how//now/brown/cow", -4, "how"); + AT_INDEX("//how///now\\/brown/cow", -3, "now"); + AT_INDEX("/how/now\\//brown\\/cow", -2, "brown"); + AT_INDEX("/how/now/brown/cow//\\", -1, "cow"); + AT_INDEX("/how/now/brown/\\cow", -5, NULL); + AT_INDEX("how/now/brown/\\cow\\", -5, NULL); +} + +TEST(path_util, NameAtIndex_NoneComplex) +{ + AT_INDEX("", 0, NULL); + AT_INDEX("/", 0, NULL); + AT_INDEX("//", 0, NULL); + AT_INDEX("///", 0, NULL); +} + +TEST(path_util, NameAtIndex_NoneComplexNeg) +{ + AT_INDEX("", -1, NULL); + AT_INDEX("/", -1, NULL); + AT_INDEX("//", -1, NULL); + AT_INDEX("///", -1, NULL); +} + +#undef AT_INDEX + +#define JOIN(str_expect, out_size, ...) \ + { \ + const char *expect = str_expect; \ + char result[(out_size) + 1024]; \ + /* check we don't write past the last byte */ \ + result[out_size] = '\0'; \ + BLI_path_join(result, out_size, __VA_ARGS__, NULL); \ + /* simplify expected string */ \ + BLI_str_replace_char(result, '\\', '/'); \ + EXPECT_STREQ(result, expect); \ + EXPECT_EQ(result[out_size], '\0'); \ + } ((void)0) + +/* BLI_path_join */ +TEST(path_util, JoinNop) +{ + JOIN("", 100, ""); + JOIN("", 100, "", ""); + JOIN("", 100, "", "", ""); + JOIN("/", 100, "/", "", ""); + JOIN("/", 100, "/", "/"); + JOIN("/", 100, "/", "", "/"); + JOIN("/", 100, "/", "", "/", ""); +} + +TEST(path_util, JoinSingle) +{ + JOIN("test", 100, "test"); + JOIN("", 100, ""); + JOIN("a", 100, "a"); + JOIN("/a", 100, "/a"); + JOIN("a/", 100, "a/"); + JOIN("/a/", 100, "/a/"); + JOIN("/a/", 100, "/a//"); + JOIN("//a/", 100, "//a//"); +} + +TEST(path_util, JoinTriple) +{ + JOIN("/a/b/c", 100, "/a", "b", "c"); + JOIN("/a/b/c", 100, "/a/", "/b/", "/c"); + JOIN("/a/b/c", 100, "/a/b/", "/c"); + JOIN("/a/b/c", 100, "/a/b/c"); + JOIN("/a/b/c", 100, "/", "a/b/c"); + + JOIN("/a/b/c/", 100, "/a/", "/b/", "/c/"); + JOIN("/a/b/c/", 100, "/a/b/c/"); + JOIN("/a/b/c/", 100, "/a/b/", "/c/"); + JOIN("/a/b/c/", 100, "/a/b/c", "/"); + JOIN("/a/b/c/", 100, "/", "a/b/c", "/"); +} + +TEST(path_util, JoinTruncateShort) +{ + JOIN("", 1, "/"); + JOIN("/", 2, "/"); + JOIN("a", 2, "", "aa"); + JOIN("a", 2, "", "a/"); + JOIN("a/b", 4, "a", "bc"); + JOIN("ab/", 4, "ab", "c"); + JOIN("/a/", 4, "/a", "b"); + JOIN("/a/", 4, "/a/", "b/"); + JOIN("/a/", 4, "/a", "/b/"); + JOIN("/a/", 4, "/", "a/b/"); + JOIN("//a", 4, "//", "a/b/"); + + JOIN("/a/b", 5, "/a", "b", "c"); +} + +TEST(path_util, JoinTruncateLong) +{ + JOIN("", 1, "//", "//longer", "path"); + JOIN("/", 2, "//", "//longer", "path"); + JOIN("//", 3, "//", "//longer", "path"); + JOIN("//l", 4, "//", "//longer", "path"); + /* snip */ + JOIN("//longe", 8, "//", "//longer", "path"); + JOIN("//longer", 9, "//", "//longer", "path"); + JOIN("//longer/", 10, "//", "//longer", "path"); + JOIN("//longer/p", 11, "//", "//longer", "path"); + JOIN("//longer/pa", 12, "//", "//longer", "path"); + JOIN("//longer/pat", 13, "//", "//longer", "path"); + JOIN("//longer/path", 14, "//", "//longer", "path"); // not truncated + JOIN("//longer/path", 14, "//", "//longer", "path/"); + JOIN("//longer/path/", 15, "//", "//longer", "path/"); // not truncated + JOIN("//longer/path/", 15, "//", "//longer", "path/", "trunc"); + JOIN("//longer/path/t", 16, "//", "//longer", "path/", "trunc"); +} + +TEST(path_util, JoinComplex) +{ + JOIN("/a/b/c/d/e/f/g/", 100, "/", "\\a/b", "//////c/d", "", "e\\\\", "f", "g//"); + JOIN("/aa/bb/cc/dd/ee/ff/gg/", 100, "/", "\\aa/bb", "//////cc/dd", "", "ee\\\\", "ff", "gg//"); + JOIN("1/2/3/", 100, "1", "////////", "", "2", "3\\"); +} + +#undef JOIN + /* BLI_path_frame */ -TEST(path_util, PathUtilFrame) +TEST(path_util, Frame) { bool ret; @@ -177,7 +408,7 @@ TEST(path_util, PathUtilFrame) } /* BLI_split_dirfile */ -TEST(path_util, PathUtilSplitDirfile) +TEST(path_util, SplitDirfile) { { const char *path = ""; |