BitVecLongestRun
Description
Find the longest consecutive sequence of a specific bit value.
Parameters
| Name | Direction | Description |
|---|---|---|
bv |
in | Bitvector to analyze |
value |
in | Bit value to find runs of (true or false) |
Usage example (from documentation)
u64 longest = BitVecLongestRun(&flags, true);Success
Returns the length of the longest consecutive run of bits equal to value. The bitvector is not modified.
Failure
Returns 0 when no bit matches value (including an empty bitvector). The bitvector is not modified.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
BitVec.c:1225:
}
u64 BitVecLongestRun(const BitVec *bv, bool value) {
ValidateBitVec(bv);
bool test_bitvec_longest_run_deadend_tests(void) {
WriteFmt("Testing BitVecLongestRun deadend scenarios\n");
// This should cause LOG_FATAL and terminate the program
// This should cause LOG_FATAL and terminate the program
BitVecLongestRun(NULL, true);
return true; // Should never reach here
DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecLongestRun\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Longest run of true should be 3, longest run of false should be 4
result = result && (BitVecLongestRun(&bv, true) == 3);
result = result && (BitVecLongestRun(&bv, false) == 4); // Longest run of true should be 3, longest run of false should be 4
result = result && (BitVecLongestRun(&bv, true) == 3);
result = result && (BitVecLongestRun(&bv, false) == 4);
// Test with all same values
BitVecPush(&bv, true);
}
result = result && (BitVecLongestRun(&bv, true) == 10);
result = result && (BitVecLongestRun(&bv, false) == 0); }
result = result && (BitVecLongestRun(&bv, true) == 10);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test alternating pattern
BitVecPush(&bv, i % 2 == 0);
}
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 1); }
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 1);
BitVecDeinit(&bv); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecLongestRun edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Test empty bitvector
result = result && (BitVecLongestRun(&bv, true) == 0);
result = result && (BitVecLongestRun(&bv, false) == 0); // Test empty bitvector
result = result && (BitVecLongestRun(&bv, true) == 0);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test single element
// Test single element
BitVecPush(&bv, true);
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 0); BitVecPush(&bv, true);
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test large runs
BitVecPush(&bv, true);
}
result = result && (BitVecLongestRun(&bv, true) == 10000);
result = result && (BitVecLongestRun(&bv, false) == 0); }
result = result && (BitVecLongestRun(&bv, true) == 10000);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test with one interruption in the middle
// Test with one interruption in the middle
BitVecSet(&bv, 5000, false);
result = result && (BitVecLongestRun(&bv, true) == 5000);
result = result && (BitVecLongestRun(&bv, false) == 1); BitVecSet(&bv, 5000, false);
result = result && (BitVecLongestRun(&bv, true) == 5000);
result = result && (BitVecLongestRun(&bv, false) == 1);
BitVecDeinit(&bv); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecLongestRun function\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Longest run of trues should be 5
result = result && (BitVecLongestRun(&bv, true) == 5);
// Longest run of falses should be 2
// Longest run of falses should be 2
result = result && (BitVecLongestRun(&bv, false) == 2);
// Test with all same values
BitVecPush(&bv, true);
}
result = result && (BitVecLongestRun(&bv, true) == 10);
result = result && (BitVecLongestRun(&bv, false) == 0); }
result = result && (BitVecLongestRun(&bv, true) == 10);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test alternating pattern
BitVecPush(&bv, i % 2 == 0);
}
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 1); }
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 1);
BitVecDeinit(&bv); DefaultAllocator alloc = DefaultAllocatorInit();
WriteFmt("Testing BitVecLongestRun edge cases\n");
BitVec bv = BitVecInit(ALLOCATOR_OF(&alloc));
// Test empty bitvector
result = result && (BitVecLongestRun(&bv, true) == 0);
result = result && (BitVecLongestRun(&bv, false) == 0); // Test empty bitvector
result = result && (BitVecLongestRun(&bv, true) == 0);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test single element
// Test single element
BitVecPush(&bv, true);
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 0); BitVecPush(&bv, true);
result = result && (BitVecLongestRun(&bv, true) == 1);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test large runs
BitVecPush(&bv, true);
}
result = result && (BitVecLongestRun(&bv, true) == 10000);
result = result && (BitVecLongestRun(&bv, false) == 0); }
result = result && (BitVecLongestRun(&bv, true) == 10000);
result = result && (BitVecLongestRun(&bv, false) == 0);
// Test with one interruption in the middle
// Test with one interruption in the middle
BitVecSet(&bv, 5000, false);
result = result && (BitVecLongestRun(&bv, true) == 5000);
result = result && (BitVecLongestRun(&bv, false) == 1); BitVecSet(&bv, 5000, false);
result = result && (BitVecLongestRun(&bv, true) == 5000);
result = result && (BitVecLongestRun(&bv, false) == 1);
BitVecDeinit(&bv);
bool test_bitvec_longest_run_deadend_tests(void) {
WriteFmt("Testing BitVecLongestRun deadend scenarios\n");
// This should cause LOG_FATAL and terminate the program
// This should cause LOG_FATAL and terminate the program
BitVecLongestRun(NULL, true);
return true; // Should never reach here
Last updated on