#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/QUnit.qm

%exec-class RegexExtractTest

class RegexExtractTest inherits QUnit::Test {
    private {
        int SUBPATTERNS_TO_TEST  = 10000;
        int SUBPATTERNS_AT_LEAST = 1;
        list CAPTURE_LENGTHS = (1, 10, 100, 1000, 10000, 100000);
    }

    constructor() : QUnit::Test("Regex extract test", "1.0") {
        addTestCase("Test", \testRegexExtract());
        set_return_value(main());
    }

    testRegexExtract() {
        foreach int capture_length in (CAPTURE_LENGTHS) {
            string fixedlenstr = (map "a", xrange(1, capture_length + 1)).join('');
            foreach int i in (xrange(1, SUBPATTERNS_TO_TEST)) {
                string str   = (map fixedlenstr, xrange(1, i + 1)).join('');
                string regex = (map "(" + fixedlenstr + ")", xrange(1, i + 1)).join('');

                auto result;
                try {
                    result = regex_extract(str, regex);
                } catch(hash<ExceptionInfo> ex) {
                    assertTrue(i >= SUBPATTERNS_AT_LEAST, i + " subpatterns of length " + capture_length + " available");
                    break;
                }

                if (!exists result) {
                    fail("no subpatterns found but it either should or throw an exception for subpattern length " + capture_length);
                }
            }
        }

        *list<string> l = regex_extract("qaz\n\twsx\n\t edc", "(^[ \t]*)", Qore::RE_MultiLine + Qore::RE_Global);
        assertEq(NOTHING, l);
    }
}

