06-11-2014 05:46 AM
Hello,
The index returned by ListFindItem is WRONG when:
The code example below shows the bug in the TEST_D:
int main (int argc, char *argv[]) { ssize_t startingPosition = 0; size_t index = 0; int val = 0; ListType myList = NULL; myList = ListCreate (sizeof(int)); val = 1; ListInsertItem (myList, &val, END_OF_LIST); val = 2; ListInsertItem (myList, &val, END_OF_LIST); val = 3; ListInsertItem (myList, &val, END_OF_LIST); // TEST_A: OK startingPosition = 1; val = 1; index = ListFindItem (myList, &val, startingPosition, IntCompare); // index = 1 --> OK // TEST_B: OK startingPosition = 10; // WRONG value: startingPosition is over the number of items. val = 1; index = ListFindItem (myList, &val, startingPosition, IntCompare); // index = 0 --> OK because startingPosition is out of range (>3) // TEST_C: OK startingPosition = 0; // WRONG value: startingPosition must be >= 1 val = 1; index = ListFindItem (myList, &val, startingPosition, IntCompare); // index = 0 --> OK because startingPosition is out of range (<1) // TEST_D: NOT OK startingPosition = 0; // WRONG value: startingPosition must be >= 1 val = 3; // value of the LAST ITEM inserted in the List index = ListFindItem (myList, &val, startingPosition, IntCompare); // index = 3 --> NOT OK: the behaviour is different compare to TEST_C. // In this case, ListFindItem must return 0 (NOT 3) because startingPosition is out of range (<1). // If you look for another item (not the last: val=1 ou val=2) ListFinfItem returns 0 --> OK return 0; }
Solved! Go to Solution.
06-11-2014 03:03 PM
Hello jm73,
The behavior you are seeing is expected. Please note, you can use the following two macros to specify starting position: END_OF_LIST
and FRONT_OF_LIST
. The macros are defined as follows in toolbox.h
:
#define FRONT_OF_LIST -1 /* Handy Constants that substitute for item positions */
#define END_OF_LIST 0 /* END_OF_LIST means one past current length of list when
inserting. Otherwise it refers the last item in the list. */
By specifying 0, you are telling the ListFindItem
function to start looking at the end of the list, which is why it finds the last item.
06-11-2014 03:13 PM
The reason this is not a bug is because passing 0 to ListFindItem is actually not invalid. This happens to be the special value END_OF_LIST:
Given this, your results in TEST_C and TEST_D make sense.
Luis
06-11-2014 03:13 PM
Darn, took too long...