-
Notifications
You must be signed in to change notification settings - Fork 43
CFBitVector: fix multiple correctness bugs #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #include "CoreFoundation/CFBitVector.h" | ||
| #include "../CFTesting.h" | ||
|
|
||
| int main (void) | ||
| { | ||
| CFMutableBitVectorRef bv; | ||
| UInt8 out[4]; | ||
|
|
||
| bv = CFBitVectorCreateMutable (NULL, 0); | ||
| CFBitVectorSetCount (bv, 16); | ||
| PASS_CF(CFBitVectorGetCount(bv) == 16, "Count set to 16."); | ||
|
|
||
| /* Newly grown bits start cleared. */ | ||
| PASS_CF(CFBitVectorGetBitAtIndex(bv, 0) == 0 | ||
| && CFBitVectorGetBitAtIndex(bv, 15) == 0, "New bits are 0."); | ||
|
|
||
| /* SetAllBits must set every bit. */ | ||
| CFBitVectorSetAllBits (bv, 1); | ||
| PASS_CF(CFBitVectorGetBitAtIndex(bv, 0) == 1 | ||
| && CFBitVectorGetBitAtIndex(bv, 15) == 1, "SetAllBits sets all bits."); | ||
| CFBitVectorSetAllBits (bv, 0); | ||
|
|
||
| /* Set a couple of bits and search within sub-ranges (location > 0). */ | ||
| CFBitVectorSetBitAtIndex (bv, 5, 1); | ||
| CFBitVectorSetBitAtIndex (bv, 11, 1); | ||
| PASS_CF(CFBitVectorGetFirstIndexOfBit(bv, CFRangeMake(3, 10), 1) == 5, | ||
| "GetFirstIndexOfBit honours the range location."); | ||
| PASS_CF(CFBitVectorGetLastIndexOfBit(bv, CFRangeMake(0, 16), 1) == 11, | ||
| "GetLastIndexOfBit returns the last set bit."); | ||
| PASS_CF(CFBitVectorGetFirstIndexOfBit(bv, CFRangeMake(0, 5), 1) == kCFNotFound, | ||
| "GetFirstIndexOfBit reports no match when the range has none."); | ||
|
|
||
| /* GetBits packs the requested range, most-significant bit first. | ||
| Range (4,8) covers bits 4..11; the set bits 5 and 11 map to range | ||
| offsets 1 and 7 -> 0b01000001 = 0x41. */ | ||
| CFBitVectorGetBits (bv, CFRangeMake(4, 8), out); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should happen when the range is out of bounds or if any index is out of bounds in the other functions? Is this UB or should we expect an exception? |
||
| PASS_CF(out[0] == 0x41, "GetBits packs the requested range."); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also test a range larger than one byte, for example range size 11. |
||
|
|
||
| CFRelease (bv); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also check some other bit vector sizes that are not a multiple of 8, for example size 13, size 0, size 1.