Skip to content

IntCompare

Description

Compare an integer against another integer-like value. Dispatches on the type of rhs to the matching internal handler.

Parameters

Name Direction Description
lhs in Left-hand integer
rhs in Right-hand operand (Int, pointer, u64, or i64 compatible type)

Usage example (from documentation)

  int cmp = IntCompare(&value, 42);

Success

Returns -1 if lhs < rhs, 0 if equal, 1 if lhs > rhs.

Failure

Pure comparison cannot fail. LOG_FATAL if either operand pointer is NULL (propagated from the underlying handler’s ValidateInt).

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_compare_ignores_leading_zeros(void) {
        WriteFmt("Testing IntCompare leading-zero normalization\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int rhs = IntFrom(11, ALLOCATOR_OF(&alloc));
    
        bool result = IntCompare(&lhs, &rhs) == 0;
        result      = result && IntEQ(&lhs, &rhs);
    
    bool test_int_compare(void) {
        WriteFmt("Testing IntCompare\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int c = IntFromBinary("000101010", &alloc.base);
    
        bool result = IntCompare(&a, &b) < 0;
        result      = result && (IntCompare(&b, &a) > 0);
        result      = result && (IntCompare(&b, &c) == 0);
    
        bool result = IntCompare(&a, &b) < 0;
        result      = result && (IntCompare(&b, &a) > 0);
        result      = result && (IntCompare(&b, &c) == 0);
        bool result = IntCompare(&a, &b) < 0;
        result      = result && (IntCompare(&b, &a) > 0);
        result      = result && (IntCompare(&b, &c) == 0);
    
        IntDeinit(&a);
    
    bool test_int_compare_generic(void) {
        WriteFmt("Testing IntCompare generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntShiftLeft(&big, 80);
    
        bool result = (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
    
        bool result = (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        bool result = (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&big, UINT64_MAX) > 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&big, UINT64_MAX) > 0);
        result      = result && IntEQ(&value, 42);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&big, UINT64_MAX) > 0);
        result      = result && IntEQ(&value, 42);
        result      = result && IntLE(&value, 42);
    
        IntModMul(&check, &result_value, &b, &m);
        result = result && (IntCompare(&check, 10) == 0);
    
        IntDeinit(&a);
        bool result = IntModSqrt(&root, &value, &mod);
        IntSquareMod(&check, &root, &mod);
        result = result && (IntCompare(&check, 10) == 0);
    
        IntDeinit(&value);
    
        bool result = !IntModSqrt(&root, &value, &mod);
        result      = result && (IntCompare(&root, 99) == 0);
    
        IntDeinit(&value);
    
        bool result = !IntModDiv(&result_value, &a, &b, &m);
        result      = result && (IntCompare(&result_value, 99) == 0);
    
        IntDeinit(&a);
        bool result = !IntDivMod(&quotient, &remainder, &dividend, &divisor);
    
        result = result && (IntCompare(&quotient, 99) == 0);
        result = result && (IntCompare(&remainder, 77) == 0);
    
        result = result && (IntCompare(&quotient, 99) == 0);
        result = result && (IntCompare(&remainder, 77) == 0);
    
        IntDeinit(&dividend);
        bool result    = !IntRootRem(&root, &remainder, &value, 0);
    
        result = result && (IntCompare(&root, 99) == 0);
        result = result && (IntCompare(&remainder, 77) == 0);
    
        result = result && (IntCompare(&root, 99) == 0);
        result = result && (IntCompare(&remainder, 77) == 0);
    
        IntDeinit(&value);
    
        IntDiv(&quotient, &dividend, 0u);
        bool result = IntCompare(&quotient, 99) == 0;
    
        IntDeinit(&dividend);
    
        IntMod(&result_value, &value, 0u);
        bool result = IntCompare(&result_value, 99) == 0;
    
        IntDeinit(&value);
    
        bool result = !IntModDiv(&result_value, &a, &b, &m);
        result      = result && (IntCompare(&result_value, 99) == 0);
    
        IntDeinit(&a);
    
        bool result = !IntPowMod(&result_value, &base, &exp, &mod);
        result      = result && (IntCompare(&result_value, 99) == 0);
    
        IntDeinit(&base);
    /// TAGS: Int, Compare, Equal, Generic
    ///
    #    define IntEQ(lhs, rhs) (IntCompare((lhs), (rhs)) == 0)
    ///
    /// Test whether `lhs` is strictly less than `rhs`.
    /// TAGS: Int, Compare, LessThan, Generic
    ///
    #    define IntLT(lhs, rhs) (IntCompare((lhs), (rhs)) < 0)
    ///
    /// Test whether `lhs` is less than or equal to `rhs`.
    /// TAGS: Int, Compare, LessEqual, Generic
    ///
    #    define IntLE(lhs, rhs) (IntCompare((lhs), (rhs)) <= 0)
    ///
    /// Test whether `lhs` is strictly greater than `rhs`.
    /// TAGS: Int, Compare, GreaterThan, Generic
    ///
    #    define IntGT(lhs, rhs) (IntCompare((lhs), (rhs)) > 0)
    ///
    /// Test whether `lhs` is greater than or equal to `rhs`.
    /// TAGS: Int, Compare, GreaterEqual, Generic
    ///
    #    define IntGE(lhs, rhs) (IntCompare((lhs), (rhs)) >= 0)
    ///
    /// Test whether two numeric values differ.
    /// TAGS: Int, Compare, NotEqual, Generic
    ///
    #    define IntNE(lhs, rhs) (IntCompare((lhs), (rhs)) != 0)
    #endif
Last updated on