Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8277,14 +8277,28 @@ FS_API fs_bool32 fs_path_is_last(const fs_path_iterator* pIterator)

FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const fs_path_iterator* pIteratorB)
{
int cmp;

FS_ASSERT(pIteratorA != NULL);
FS_ASSERT(pIteratorB != NULL);

if (pIteratorA->pFullPath == pIteratorB->pFullPath && pIteratorA->segmentOffset == pIteratorB->segmentOffset && pIteratorA->segmentLength == pIteratorB->segmentLength) {
return 0;
}

return fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength));
cmp = fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength));
if (cmp != 0) {
return cmp;
}

if (pIteratorA->segmentLength < pIteratorB->segmentLength) {
return -1;
}
if (pIteratorA->segmentLength > pIteratorB->segmentLength) {
return 1;
}

return 0;
}

FS_API int fs_path_compare(const char* pPathA, size_t pathALen, const char* pPathB, size_t pathBLen)
Expand Down
47 changes: 47 additions & 0 deletions tests/fs_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,51 @@ int fs_test_path_normalize(fs_test* pTest)
}
/* END path_normalize */

/* BEG path_compare */
static int fs_test_path_compare_internal(fs_test* pTest, const char* pPathA, const char* pPathB, int expected)
{
int result;

result = fs_path_compare(pPathA, FS_NULL_TERMINATED, pPathB, FS_NULL_TERMINATED);
if (expected == 0) {
if (result != 0) {
printf("%s: Expected \"%s\" and \"%s\" to be equal, but got %d\n", pTest->name, pPathA, pPathB, result);
return 1;
}
} else if (expected < 0) {
if (result >= 0) {
printf("%s: Expected \"%s\" to be less than \"%s\", but got %d\n", pTest->name, pPathA, pPathB, result);
return 1;
}
} else {
if (result <= 0) {
printf("%s: Expected \"%s\" to be greater than \"%s\", but got %d\n", pTest->name, pPathA, pPathB, result);
return 1;
}
}

return 0;
}

int fs_test_path_compare(fs_test* pTest)
{
int errorCount = 0;

errorCount += fs_test_path_compare_internal(pTest, "test.png", "test.png", 0);
errorCount += fs_test_path_compare_internal(pTest, "test.png", "test.png.remap", -1);
errorCount += fs_test_path_compare_internal(pTest, "test.png.remap", "test.png", 1);
errorCount += fs_test_path_compare_internal(pTest, "abc/def", "abc/def", 0);
errorCount += fs_test_path_compare_internal(pTest, "abc/def", "abc/de", 1);
errorCount += fs_test_path_compare_internal(pTest, "abc/de", "abc/def", -1);

if (errorCount == 0) {
return FS_SUCCESS;
} else {
return FS_ERROR;
}
}
/* END path_compare */

/* BEG path_trim_base */
int fs_test_path_trim_base_internal(fs_test* pTest, const char* pPath, size_t pathLen, const char* pBasePath, size_t basePathLen, const char* pExpected)
{
Expand Down Expand Up @@ -4174,6 +4219,7 @@ int main(int argc, char** argv)
fs_test test_path;
fs_test test_path_iteration; /* Tests path breakup logic. This is critical for some internal logic in the library. */
fs_test test_path_normalize; /* Tests path normalization, like resolving ".." and "." segments. Again, this is used extensively for path validation and therefore needs proper testing. */
fs_test test_path_compare;
fs_test test_path_trim_base;
fs_test test_system;
fs_test test_system_sysdir; /* Standard directory tests need to come first because we'll be writing out our test files to a temp folder. */
Expand Down Expand Up @@ -4270,6 +4316,7 @@ int main(int argc, char** argv)
fs_test_init(&test_path, "Path", NULL, NULL, &test_root);
fs_test_init(&test_path_iteration, "Path Iteration", fs_test_path_iteration, NULL, &test_path);
fs_test_init(&test_path_normalize, "Path Normalize", fs_test_path_normalize, NULL, &test_path);
fs_test_init(&test_path_compare, "Path Compare", fs_test_path_compare, NULL, &test_path);
fs_test_init(&test_path_trim_base, "Path Trim Base", fs_test_path_trim_base, NULL, &test_path);

/*
Expand Down
Loading