Skip to content
BitVecLongestRun

BitVecLongestRun

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);

Returns

Length of longest consecutive sequence

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    u64 BitVecLongestRun(BitVec *bv, bool value) {
        ValidateBitVec(bv);
    // Test BitVecLongestRun function
    bool test_bitvec_longest_run(void) {
        WriteFmt("Testing BitVecLongestRun\n");
    
        BitVec bv     = BitVecInit();
    
        // 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);
    // Edge case tests for LongestRun function
    bool test_bitvec_longest_run_edge_cases(void) {
        WriteFmt("Testing BitVecLongestRun edge cases\n");
    
        BitVec bv     = BitVecInit();
    
        // 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
    // Test BitVecLongestRun function
    bool test_bitvec_longest_run(void) {
        WriteFmt("Testing BitVecLongestRun function\n");
    
        BitVec bv     = BitVecInit();
    
        // 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);
    // Edge case tests for LongestRun function
    bool test_bitvec_longest_run_edge_cases(void) {
        WriteFmt("Testing BitVecLongestRun edge cases\n");
    
        BitVec bv     = BitVecInit();
    
        // 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