Skip to content

Commit 5ae6fc5

Browse files
authored
Fix path comparison bug
2 parents db96d6a + c631c21 commit 5ae6fc5

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

fs.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8277,14 +8277,28 @@ FS_API fs_bool32 fs_path_is_last(const fs_path_iterator* pIterator)
82778277

82788278
FS_API int fs_path_iterators_compare(const fs_path_iterator* pIteratorA, const fs_path_iterator* pIteratorB)
82798279
{
8280+
int cmp;
8281+
82808282
FS_ASSERT(pIteratorA != NULL);
82818283
FS_ASSERT(pIteratorB != NULL);
82828284

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

8287-
return fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength));
8289+
cmp = fs_strncmp(pIteratorA->pFullPath + pIteratorA->segmentOffset, pIteratorB->pFullPath + pIteratorB->segmentOffset, FS_MIN(pIteratorA->segmentLength, pIteratorB->segmentLength));
8290+
if (cmp != 0) {
8291+
return cmp;
8292+
}
8293+
8294+
if (pIteratorA->segmentLength < pIteratorB->segmentLength) {
8295+
return -1;
8296+
}
8297+
if (pIteratorA->segmentLength > pIteratorB->segmentLength) {
8298+
return 1;
8299+
}
8300+
8301+
return 0;
82888302
}
82898303

82908304
FS_API int fs_path_compare(const char* pPathA, size_t pathALen, const char* pPathB, size_t pathBLen)

tests/fs_test.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,51 @@ int fs_test_path_normalize(fs_test* pTest)
498498
}
499499
/* END path_normalize */
500500

501+
/* BEG path_compare */
502+
static int fs_test_path_compare_internal(fs_test* pTest, const char* pPathA, const char* pPathB, int expected)
503+
{
504+
int result;
505+
506+
result = fs_path_compare(pPathA, FS_NULL_TERMINATED, pPathB, FS_NULL_TERMINATED);
507+
if (expected == 0) {
508+
if (result != 0) {
509+
printf("%s: Expected \"%s\" and \"%s\" to be equal, but got %d\n", pTest->name, pPathA, pPathB, result);
510+
return 1;
511+
}
512+
} else if (expected < 0) {
513+
if (result >= 0) {
514+
printf("%s: Expected \"%s\" to be less than \"%s\", but got %d\n", pTest->name, pPathA, pPathB, result);
515+
return 1;
516+
}
517+
} else {
518+
if (result <= 0) {
519+
printf("%s: Expected \"%s\" to be greater than \"%s\", but got %d\n", pTest->name, pPathA, pPathB, result);
520+
return 1;
521+
}
522+
}
523+
524+
return 0;
525+
}
526+
527+
int fs_test_path_compare(fs_test* pTest)
528+
{
529+
int errorCount = 0;
530+
531+
errorCount += fs_test_path_compare_internal(pTest, "test.png", "test.png", 0);
532+
errorCount += fs_test_path_compare_internal(pTest, "test.png", "test.png.remap", -1);
533+
errorCount += fs_test_path_compare_internal(pTest, "test.png.remap", "test.png", 1);
534+
errorCount += fs_test_path_compare_internal(pTest, "abc/def", "abc/def", 0);
535+
errorCount += fs_test_path_compare_internal(pTest, "abc/def", "abc/de", 1);
536+
errorCount += fs_test_path_compare_internal(pTest, "abc/de", "abc/def", -1);
537+
538+
if (errorCount == 0) {
539+
return FS_SUCCESS;
540+
} else {
541+
return FS_ERROR;
542+
}
543+
}
544+
/* END path_compare */
545+
501546
/* BEG path_trim_base */
502547
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)
503548
{
@@ -4174,6 +4219,7 @@ int main(int argc, char** argv)
41744219
fs_test test_path;
41754220
fs_test test_path_iteration; /* Tests path breakup logic. This is critical for some internal logic in the library. */
41764221
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. */
4222+
fs_test test_path_compare;
41774223
fs_test test_path_trim_base;
41784224
fs_test test_system;
41794225
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. */
@@ -4270,6 +4316,7 @@ int main(int argc, char** argv)
42704316
fs_test_init(&test_path, "Path", NULL, NULL, &test_root);
42714317
fs_test_init(&test_path_iteration, "Path Iteration", fs_test_path_iteration, NULL, &test_path);
42724318
fs_test_init(&test_path_normalize, "Path Normalize", fs_test_path_normalize, NULL, &test_path);
4319+
fs_test_init(&test_path_compare, "Path Compare", fs_test_path_compare, NULL, &test_path);
42734320
fs_test_init(&test_path_trim_base, "Path Trim Base", fs_test_path_trim_base, NULL, &test_path);
42744321

42754322
/*

0 commit comments

Comments
 (0)