' Big Integer Comparison — compare arbitrarily large integers as strings
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL a AS STRING
    LOCAL b AS STRING
    LOCAL result AS LONG
    LOCAL i AS LONG
    LOCAL testCount AS LONG
    LOCAL passCount AS LONG
    LOCAL failCount AS LONG
    LOCAL lenA AS LONG
    LOCAL lenB AS LONG
    LOCAL cmp AS LONG
    LOCAL digitA AS LONG
    LOCAL digitB AS LONG
    LOCAL labelStr AS STRING
    LOCAL sgnA AS LONG
    LOCAL sgnB AS LONG
    LOCAL startA AS LONG
    LOCAL startB AS LONG
    LOCAL padLen AS LONG
    LOCAL paddedA AS STRING
    LOCAL paddedB AS STRING
    LOCAL padChar AS STRING
    LOCAL chA AS STRING
    LOCAL chB AS STRING
    LOCAL valA AS LONG
    LOCAL valB AS LONG
    LOCAL expected AS LONG
    LOCAL got AS STRING
    LOCAL lineOut AS STRING
    PRINT "============================================"
    PRINT "     BIG INTEGER COMPARISON TEST"
    PRINT "============================================"
    PRINT ""
    testCount = 0
    passCount = 0
    failCount = 0
    result = 0
    a = "123"
    b = "456"
    testCount = testCount + 1
    lenA = LEN(a)
    lenB = LEN(b)
    sgnA = 1
    sgnB = 1
    startA = 1
    startB = 1
    IF ASC(a, 1) = 45 THEN
        sgnA = 0 - 1
        startA = 2
    END IF
    IF ASC(b, 1) = 45 THEN
        sgnB = 0 - 1
        startB = 2
    END IF
    IF sgnA > sgnB THEN
        cmp = 1
    ELSE
        IF sgnA < sgnB THEN
            cmp = 0 - 1
        ELSE
            lenA = LEN(a) - startA + 1
            lenB = LEN(b) - startB + 1
            IF lenA > lenB THEN
                IF sgnA = 1 THEN
                    cmp = 1
                ELSE
                    cmp = 0 - 1
                END IF
            ELSE
                IF lenA < lenB THEN
                    IF sgnA = 1 THEN
                        cmp = 0 - 1
                    ELSE
                        cmp = 1
                    END IF
                ELSE
                    cmp = 0
                    FOR i = startA TO LEN(a)
                        digitA = ASC(a, i) - 48
                        digitB = ASC(b, i - startA + startB) - 48
                        IF digitA > digitB THEN
                            cmp = sgnA
                            EXIT FOR
                        END IF
                        IF digitA < digitB THEN
                            cmp = 0 - sgnA
                            EXIT FOR
                        END IF
                    NEXT i
                END IF
            END IF
        END IF
    END IF
    expected = 0 - 1
    IF cmp = expected THEN
        passCount = passCount + 1
        lineOut = "PASS"
    ELSE
        failCount = failCount + 1
        lineOut = "FAIL"
    END IF
    got = STR$(cmp)
    PRINT "Test " + STR$(testCount) + ": " + a + " vs " + b + " -> " + got + " (" + lineOut + ")"
    a = "999"
    b = "123"
    testCount = testCount + 1
    lenA = LEN(a)
    lenB = LEN(b)
    sgnA = 1
    sgnB = 1
    startA = 1
    startB = 1
    IF ASC(a, 1) = 45 THEN
        sgnA = 0 - 1
        startA = 2
    END IF
    IF ASC(b, 1) = 45 THEN
        sgnB = 0 - 1
        startB = 2
    END IF
    IF sgnA > sgnB THEN
        cmp = 1
    ELSE
        IF sgnA < sgnB THEN
            cmp = 0 - 1
        ELSE
            lenA = LEN(a) - startA + 1
            lenB = LEN(b) - startB + 1
            IF lenA > lenB THEN
                IF sgnA = 1 THEN
                    cmp = 1
                ELSE
                    cmp = 0 - 1
                END IF
            ELSE
                IF lenA < lenB THEN
                    IF sgnA = 1 THEN
                        cmp = 0 - 1
                    ELSE
                        cmp = 1
                    END IF
                ELSE
                    cmp = 0
                    FOR i = startA TO LEN(a)
                        digitA = ASC(a, i) - 48
                        digitB = ASC(b, i - startA + startB) - 48
                        IF digitA > digitB THEN
                            cmp = sgnA
                            EXIT FOR
                        END IF
                        IF digitA < digitB THEN
                            cmp = 0 - sgnA
                            EXIT FOR
                        END IF
                    NEXT i
                END IF
            END IF
        END IF
    END IF
    expected = 1
    IF cmp = expected THEN
        passCount = passCount + 1
        lineOut = "PASS"
    ELSE
        failCount = failCount + 1
        lineOut = "FAIL"
    END IF
    got = STR$(cmp)
    PRINT "Test " + STR$(testCount) + ": " + a + " vs " + b + " -> " + got + " (" + lineOut + ")"
    a = "555"
    b = "555"
    testCount = testCount + 1
    lenA = LEN(a)
    lenB = LEN(b)
    sgnA = 1
    sgnB = 1
    startA = 1
    startB = 1
    IF ASC(a, 1) = 45 THEN
        sgnA = 0 - 1
        startA = 2
    END IF
    IF ASC(b, 1) = 45 THEN
        sgnB = 0 - 1
        startB = 2
    END IF
    IF sgnA > sgnB THEN
        cmp = 1
    ELSE
        IF sgnA < sgnB THEN
            cmp = 0 - 1
        ELSE
            lenA = LEN(a) - startA + 1
            lenB = LEN(b) - startB + 1
            IF lenA > lenB THEN
                IF sgnA = 1 THEN
                    cmp = 1
                ELSE
                    cmp = 0 - 1
                END IF
            ELSE
                IF lenA < lenB THEN
                    IF sgnA = 1 THEN
                        cmp = 0 - 1
                    ELSE
                        cmp = 1
                    END IF
                ELSE
                    cmp = 0
                    FOR i = startA TO LEN(a)
                        digitA = ASC(a, i) - 48
                        digitB = ASC(b, i - startA + startB) - 48
                        IF digitA > digitB THEN
                            cmp = sgnA
                            EXIT FOR
                        END IF
                        IF digitA < digitB THEN
                            cmp = 0 - sgnA
                            EXIT FOR
                        END IF
                    NEXT i
                END IF
            END IF
        END IF
    END IF
    expected = 0
    IF cmp = expected THEN
        passCount = passCount + 1
        lineOut = "PASS"
    ELSE
        failCount = failCount + 1
        lineOut = "FAIL"
    END IF
    got = STR$(cmp)
    PRINT "Test " + STR$(testCount) + ": " + a + " vs " + b + " -> " + got + " (" + lineOut + ")"
    a = "-999"
    b = "1000"
    testCount = testCount + 1
    lenA = LEN(a)
    lenB = LEN(b)
    sgnA = 1
    sgnB = 1
    startA = 1
    startB = 1
    IF ASC(a, 1) = 45 THEN
        sgnA = 0 - 1
        startA = 2
    END IF
    IF ASC(b, 1) = 45 THEN
        sgnB = 0 - 1
        startB = 2
    END IF
    IF sgnA > sgnB THEN
        cmp = 1
    ELSE
        IF sgnA < sgnB THEN
            cmp = 0 - 1
        ELSE
            lenA = LEN(a) - startA + 1
            lenB = LEN(b) - startB + 1
            IF lenA > lenB THEN
                IF sgnA = 1 THEN
                    cmp = 1
                ELSE
                    cmp = 0 - 1
                END IF
            ELSE
                IF lenA < lenB THEN
                    IF sgnA = 1 THEN
                        cmp = 0 - 1
                    ELSE
                        cmp = 1
                    END IF
                ELSE
                    cmp = 0
                    FOR i = startA TO LEN(a)
                        digitA = ASC(a, i) - 48
                        digitB = ASC(b, i - startA + startB) - 48
                        IF digitA > digitB THEN
                            cmp = sgnA
                            EXIT FOR
                        END IF
                        IF digitA < digitB THEN
                            cmp = 0 - sgnA
                            EXIT FOR
                        END IF
                    NEXT i
                END IF
            END IF
        END IF
    END IF
    expected = 0 - 1
    IF cmp = expected THEN
        passCount = passCount + 1
        lineOut = "PASS"
    ELSE
        failCount = failCount + 1
        lineOut = "FAIL"
    END IF
    got = STR$(cmp)
    PRINT "Test " + STR$(testCount) + ": " + a + " vs " + b + " -> " + got + " (" + lineOut + ")"
    a = "1000"
    b = "999"
    testCount = testCount + 1
    lenA = LEN(a)
    lenB = LEN(b)
    sgnA = 1
    sgnB = 1
    startA = 1
    startB = 1
    IF ASC(a, 1) = 45 THEN
        sgnA = 0 - 1
        startA = 2
    END IF
    IF ASC(b, 1) = 45 THEN
        sgnB = 0 - 1
        startB = 2
    END IF
    IF sgnA > sgnB THEN
        cmp = 1
    ELSE
        IF sgnA < sgnB THEN
            cmp = 0 - 1
        ELSE
            lenA = LEN(a) - startA + 1
            lenB = LEN(b) - startB + 1
            IF lenA > lenB THEN
                IF sgnA = 1 THEN
                    cmp = 1
                ELSE
                    cmp = 0 - 1
                END IF
            ELSE
                IF lenA < lenB THEN
                    IF sgnA = 1 THEN
                        cmp = 0 - 1
                    ELSE
                        cmp = 1
                    END IF
                ELSE
                    cmp = 0
                    FOR i = startA TO LEN(a)
                        digitA = ASC(a, i) - 48
                        digitB = ASC(b, i - startA + startB) - 48
                        IF digitA > digitB THEN
                            cmp = sgnA
                            EXIT FOR
                        END IF
                        IF digitA < digitB THEN
                            cmp = 0 - sgnA
                            EXIT FOR
                        END IF
                    NEXT i
                END IF
            END IF
        END IF
    END IF
    expected = 1
    IF cmp = expected THEN
        passCount = passCount + 1
        lineOut = "PASS"
    ELSE
        failCount = failCount + 1
        lineOut = "FAIL"
    END IF
    got = STR$(cmp)
    PRINT "Test " + STR$(testCount) + ": " + a + " vs " + b + " -> " + got + " (" + lineOut + ")"
    a = "12345678901234567890"
    b = "12345678901234567891"
    testCount = testCount + 1
    lenA = LEN(a)
    lenB = LEN(b)
    sgnA = 1
    sgnB = 1
    startA = 1
    startB = 1
    IF ASC(a, 1) = 45 THEN
        sgnA = 0 - 1
        startA = 2
    END IF
    IF ASC(b, 1) = 45 THEN
        sgnB = 0 - 1
        startB = 2
    END IF
    IF sgnA > sgnB THEN
        cmp = 1
    ELSE
        IF sgnA < sgnB THEN
            cmp = 0 - 1
        ELSE
            lenA = LEN(a) - startA + 1
            lenB = LEN(b) - startB + 1
            IF lenA > lenB THEN
                IF sgnA = 1 THEN
                    cmp = 1
                ELSE
                    cmp = 0 - 1
                END IF
            ELSE
                IF lenA < lenB THEN
                    IF sgnA = 1 THEN
                        cmp = 0 - 1
                    ELSE
                        cmp = 1
                    END IF
                ELSE
                    cmp = 0
                    FOR i = startA TO LEN(a)
                        digitA = ASC(a, i) - 48
                        digitB = ASC(b, i - startA + startB) - 48
                        IF digitA > digitB THEN
                            cmp = sgnA
                            EXIT FOR
                        END IF
                        IF digitA < digitB THEN
                            cmp = 0 - sgnA
                            EXIT FOR
                        END IF
                    NEXT i
                END IF
            END IF
        END IF
    END IF
    expected = 0 - 1
    IF cmp = expected THEN
        passCount = passCount + 1
        lineOut = "PASS"
    ELSE
        failCount = failCount + 1
        lineOut = "FAIL"
    END IF
    got = STR$(cmp)
    PRINT "Test " + STR$(testCount) + ": " + a + " vs " + b + " -> " + got + " (" + lineOut + ")"
    PRINT ""
    PRINT "Results: " + STR$(passCount) + " passed, " + STR$(failCount) + " failed of " + STR$(testCount)
    FUNCTION = 0
END FUNCTION
