// File: /workspace/github_c_makefile_star2_O1/OhYee[P]computer_application_programming_experiment/utils/ac_automaton/test/test_ac_automaton.c
// Function: main
int main() {
    boolean pass = T;
    mp_init(1 << 20, mp_exit);

    char *         string = "aababbcabcaacca";
    char *         patterns[] = {"a", "ab", "bab", "bc", "bca", "c", "caa"};
    int            want[] = {7, 3, 1, 2, 2, 4, 1};
    int            length = sizeof(patterns) / sizeof(char *);
    ac_tree_node **nodes = mp_new(length * sizeof(ac_tree_node *));

    ac_automaton *ac = ac_init();
    for (int i = 0; i < length; ++i) {
        nodes[i] = ac_add(ac, patterns[i]);
    }
    ac_build(ac);

    // ac_print(ac);
    pass &= judge(ac->root,  ac->root);

    pass &= judge(ac->root->children['a'], ac->root);
    pass &=
        judge(ac->root->children['a']->children['b'], ac->root->children['b']);

    pass &= judge(ac->root->children['b'], ac->root);
    pass &=
        judge(ac->root->children['b']->children['a'], ac->root->children['a']);
    pass &= judge(ac->root->children['b']->children['a']->children['b'],
                  ac->root->children['a']->children['b']);

    pass &=
        judge(ac->root->children['b']->children['c'], ac->root->children['c']);
    pass &= judge(ac->root->children['b']->children['c']->children['a'],
                  ac->root->children['c']->children['a']);

    pass &= judge(ac->root->children['c'], ac->root);
    pass &=
        judge(ac->root->children['c']->children['a'], ac->root->children['a']);
    pass &= judge(ac->root->children['c']->children['a']->children['a'],
                  ac->root->children['a']);

    char *c = string;
    while (*c != '\0') {
        ac_match_char(ac, *c);
        ++c;
    }

    for (int i = 0; i < length; ++i) {
        if (want[i] != nodes[i]->match_number) {
            print_err("'%s' matchs %d times, but want %d times\n", patterns[i],
                      nodes[i]->match_number, want[i]);
            pass = F;
        }
    }

    if (pass) {
        print_ok("All testcases passed\n");
    } else {
        print_err("Some errors!\n");
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
leaq 0x418(%rip), %r12       # 0x2044
movl (%r12), %ebp
leaq -0x487(%rip), %rsi      # 0x17b0
movl $0x100000, %edi         # imm = 0x100000
callq 0x17bb
movl $0x38, %edi
callq 0x19f0
movq %rax, %rbx
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x1269
movq %rax, %r14
leaq 0x214e(%rip), %r15      # 0x3db0
movq (%r13,%r15), %rsi
movq %r14, %rdi
callq 0x131b
movq %rax, (%rbx,%r13)
addq $0x8, %r13
cmpq $0x38, %r13
jne 0x1c62
movq %r14, %rdi
callq 0x13f4
movq (%r14), %rdx
movq 0x8(%rdx), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1cac
leaq 0x4fa(%rip), %rdi       # 0x2195
movq %rdx, %rsi
xorl %eax, %eax
callq 0x10a0
leaq 0x39c(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rdx
movq (%rdx), %rax
movq 0x308(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1cdc
leaq 0x4c7(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x36c(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rax
movq (%rax), %rax
movq 0x308(%rax), %rcx
movq 0x310(%rax), %rdx
movq (%rcx), %rax
movq 0x310(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1d1d
leaq 0x486(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x32b(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rdx
movq (%rdx), %rax
movq 0x310(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1d4d
leaq 0x456(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x2fb(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rax
movq (%rax), %rax
movq 0x308(%rax), %rdx
movq 0x310(%rax), %rax
movq (%rax), %rax
movq 0x308(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1d8e
leaq 0x415(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x2ba(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rax
movq (%rax), %rax
movq 0x308(%rax), %rcx
movq 0x310(%rax), %rax
movq (%rax), %rax
movq 0x308(%rax), %rax
movq (%rax), %rax
movq 0x310(%rax), %rsi
movq (%rcx), %rax
movq 0x310(%rax), %rdx
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1de3
leaq 0x3c0(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x265(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rax
movq (%rax), %rax
movq 0x310(%rax), %rcx
movq 0x318(%rax), %rdx
movq (%rcx), %rax
movq 0x318(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1e24
leaq 0x37f(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x224(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rax
movq (%rax), %rax
movq 0x310(%rax), %rcx
movq 0x318(%rax), %rax
movq (%rcx), %rcx
movq 0x318(%rcx), %rcx
movq (%rcx), %rcx
movq 0x308(%rcx), %rsi
movq (%rax), %rax
movq 0x308(%rax), %rdx
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1e79
leaq 0x32a(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x1cf(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rdx
movq (%rdx), %rax
movq 0x318(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1ea9
leaq 0x2fa(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x19f(%rip), %rax       # 0x2048
andl (%rax), %ebp
movq (%r14), %rax
movq (%rax), %rax
movq 0x308(%rax), %rdx
movq 0x318(%rax), %rax
movq (%rax), %rax
movq 0x308(%rax), %rsi
movq 0x8(%rsi), %rcx
movq %r12, %rax
cmpq %rdx, %rcx
je 0x1eea
leaq 0x2b9(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x15e(%rip), %rax       # 0x2048
andl (%rax), %ebp
movl %ebp, 0x4(%rsp)
movq (%r14), %rax
movq (%rax), %rax
movq 0x308(%rax), %rdx
movq 0x318(%rax), %rax
movq (%rax), %rax
movq 0x308(%rax), %rax
movq (%rax), %rax
movq 0x308(%rax), %rsi
movq 0x8(%rsi), %rcx
cmpq %rdx, %rcx
je 0x1f36
leaq 0x26d(%rip), %rdi       # 0x2195
xorl %eax, %eax
callq 0x10a0
leaq 0x112(%rip), %r12       # 0x2048
movl (%r12), %ebp
movb $0x61, %al
movl $0x1, %r12d
leaq 0x275(%rip), %r13       # 0x21be
movsbl %al, %esi
movq %r14, %rdi
callq 0x14c9
movb (%r12,%r13), %al
incq %r12
cmpq $0x10, %r12
jne 0x1f49
movl 0x4(%rsp), %esi
andl %ebp, %esi
leaq 0xda(%rip), %rax        # 0x2048
movl (%rax), %r12d
leaq 0x2b8(%rip), %r13       # 0x2230
leaq 0x25e(%rip), %r14       # 0x21dd
xorl %ebp, %ebp
movl (%rbp,%r13), %ecx
movq (%rbx,%rbp,2), %rax
movl 0x1c(%rax), %edx
cmpl %edx, %ecx
je 0x1fa1
movq (%r15), %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x10a0
movl %r12d, %esi
addq $0x8, %r15
addq $0x4, %rbp
cmpq $0x1c, %rbp
jne 0x1f81
testl %esi, %esi
je 0x1fc3
leaq 0x24c(%rip), %rdi       # 0x2206
xorl %eax, %eax
callq 0x167d
jmp 0x1fd1
leaq 0x252(%rip), %rdi       # 0x221c
xorl %eax, %eax
callq 0x10a0
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq