BitVecLongestRun

Table of Contents

BitVecLongestRun

Description

Find the longest consecutive sequence of a specific bit value.

Parameters

NameDirectionDescription
bvinBitvector to analyze
valueinBit value to find runs of (true or false)

Usage example (from documentation)

  u64 longest = BitVecLongestRun(&flags, true);

Usage example (Cross-references)

    }
    
    u64 BitVecLongestRun(BitVec *bv, bool value) {
    ValidateBitVec(bv);
    // Test BitVecLongestRun function
    bool test_bitvec_longest_run(void) {
    printf("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) {
    printf("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) {
    printf("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) {
    printf("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) {
    printf("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) {
    printf("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

Share :

Related Posts

BitVecAny

BitVecAny Description Check if any bit in bitvector matches the given value.

Read More

BitVecEmpty

BitVecEmpty Description Check if bitvector is empty.

Read More

BitVecFindLast

BitVecFindLast Description Find index of last occurrence of a specific bit value.

Read More