// File: /workspace/github_cpp_makefile_star2_O3/s0sasaki[P]ExecutableBlockchain/test.cpp
// Function: main
int main()
{
    typedef std::pair<char*, int32_t> TestCase;
    std::vector<TestCase> testcases;

    testcases.emplace_back(TestCase((char*) "\
    ( simple calculation )                        \n\
    : main                                        \n\
        1 2 3 4 * + + .                           \n\
    ;                                             \n\
    ", 1+2+3*4));

    testcases.emplace_back(TestCase((char*) "\
    ( simple function definition/call/return )    \n\
    : triple                                      \n\
        3 *                                       \n\
    ;                                             \n\
    : main                                        \n\
        5 triple .                                \n\
    ;                                             \n\
    ", 5*3));

    testcases.emplace_back(TestCase((char*) "\
    ( simple conditional statement )              \n\
    : main                                        \n\
        2 1 > if 100 then .                       \n\
    ;                                             \n\
    ", 100));

    testcases.emplace_back(TestCase((char*) "\
    ( nested conditional statements )             \n\
    : main                                        \n\
        1 2 + 2 > if                              \n\
            7 2 4 3 - < if                        \n\
                0 else                            \n\
                7 then                            \n\
            7 then                                \n\
        * * .                                     \n\
    ;                                             \n\
    ",7*7*7));

    testcases.emplace_back(TestCase((char*) "\
    ( recursive call )                            \n\
    : factorial                                   \n\
        dup 1 > if                                \n\
            dup 1 - factorial * then              \n\
    ;                                             \n\
    : main                                        \n\
        5 factorial .                             \n\
    ;                                             \n\
    ", 5*4*3*2*1));

    testcases.emplace_back(TestCase((char*) "\
    ( tail-call )                                     \n\
    : tail_factorial                                  \n\
        dup 1 > if                                    \n\
            swap over * swap 1 - tail_factorial then  \n\
    ;                                                 \n\
    : main                                            \n\
        1 5 tail_factorial drop .                     \n\
    ;                                                 \n\
    ", 5*4*3*2*1));                                    
                                                       
    testcases.emplace_back(TestCase((char*) "\
    ( nested function call )                      \n\
    : sq                                          \n\
        dup *                                     \n\
    ;                                             \n\
    : fourth_power                                \n\
        sq sq                                     \n\
    ;                                             \n\
    : main                                        \n\
        3 fourth_power .                          \n\
    ;                                             \n\
    ", 3*3*3*3));                                   
                                                    
    testcases.emplace_back(TestCase((char*) "\
    ( calling functions at old blocks )           \n\
    : main                                        \n\
        3 factorial fourth_power .                \n\
    ;                                             \n\
    ", (3*2*1)*(3*2*1)*(3*2*1)*(3*2*1)));

    testcases.emplace_back(TestCase((char*) "\
    ( volatile variables )                        \n\
    : main                                        \n\
        variable XXX 10                           \n\
        variable YYY 20                           \n\
        XXX YYY * .                               \n\
    ;                                             \n\
    ", 10*20));

    testcases.emplace_back(TestCase((char*) "\
    ( storage variables )                         \n\
    : main                                        \n\
        3 XXX !                                   \n\
        XXX @ .                                   \n\
    ;                                             \n\
    ", 3));

    Blockchain blockchain = Blockchain();
    VM vm = VM();
    Compiler compiler = Compiler();
    int32_t bin_data[Block::DATA_SIZE];
    for (auto testcase: testcases){
        std::cout << testcase.first << std::flush;
        compiler.compile(bin_data, testcase.first, blockchain.chain.size());
        vm.exec(bin_data, blockchain);
        assert(vm.peep() == testcase.second);

        std::cout << "Mining block... " << std::flush;
        blockchain.addBlock(Block(bin_data));
        std::cout << blockchain.chain.back().hash << std::endl;
    }

    std::cout << "Checking validity..." << std::endl;
    assert(blockchain.isValid());
    
    std::cout << "Checking unmodifiability..." << std::endl;
    blockchain.chain[1].data[3] = 999;
    assert(!blockchain.isValid());

    std::cout << "All tests passed!" << std::endl;
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7298, %rsp           # imm = 0x7298
xorps %xmm0, %xmm0
leaq -0x40(%rbp), %rdi
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x4a6b(%rip), %rax      # 0x7004
leaq -0x5270(%rbp), %rdx
movq %rax, (%rdx)
movl $0xf, 0x8(%rdx)
xorl %esi, %esi
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x4b19(%rip), %rax      # 0x70d5
movq %rax, -0x5270(%rbp)
movl $0xf, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x25eb
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x25ff
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x4c39(%rip), %rax      # 0x723f
movq %rax, -0x5270(%rbp)
movl $0x64, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x2635
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x2649
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x4cc0(%rip), %rax      # 0x7310
movq %rax, -0x5270(%rbp)
movl $0x157, -0x5268(%rbp)   # imm = 0x157
cmpq -0x30(%rbp), %rsi
je 0x267f
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x2693
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x4e46(%rip), %rax      # 0x74e0
movq %rax, -0x5270(%rbp)
movl $0x78, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x26c9
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x26dd
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x4f99(%rip), %rax      # 0x767d
movq %rax, -0x5270(%rbp)
movl $0x78, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x2713
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x2727
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x510c(%rip), %rax      # 0x783a
movq %rax, -0x5270(%rbp)
movl $0x51, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x275d
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x2771
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x52c5(%rip), %rax      # 0x7a3d
movq %rax, -0x5270(%rbp)
movl $0x510, -0x5268(%rbp)   # imm = 0x510
cmpq -0x30(%rbp), %rsi
je 0x27a7
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x27bb
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x534c(%rip), %rax      # 0x7b0e
movq %rax, -0x5270(%rbp)
movl $0xc8, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x27f1
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
movq -0x38(%rbp), %rsi
addq $0x10, %rsi
movq %rsi, -0x38(%rbp)
jmp 0x2805
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
movq -0x38(%rbp), %rsi
leaq 0x5439(%rip), %rax      # 0x7c45
movq %rax, -0x5270(%rbp)
movl $0x3, -0x5268(%rbp)
cmpq -0x30(%rbp), %rsi
je 0x2834
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x2844
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2da8
leaq -0x60(%rbp), %rdi
callq 0x4da4
leaq -0x5270(%rbp), %rdi
callq 0x5ac8
leaq -0xc8(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x8(%rax)
movaps %xmm0, 0x18(%rax)
movaps %xmm0, 0x28(%rax)
movaps %xmm0, -0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
leaq -0x98(%rbp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq %rax, %r13
je 0x2a0a
movq 0x8722(%rip), %rbx      # 0xafd8
leaq -0x72c0(%rbp), %r15
movq (%r13), %r12
movl 0x8(%r13), %r14d
testq %r12, %r12
je 0x28e2
movq %r12, %rdi
callq 0x20d0
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
jmp 0x28f9
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x22e0
movq %rbx, %rdi
callq 0x2150
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
shrq $0x4, %rax
imull $0x2545a3cd, %eax, %ecx # imm = 0x2545A3CD
leaq -0xd0(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x300a
leaq -0x5270(%rbp), %rdi
movq %r15, %rsi
leaq -0x60(%rbp), %rdx
callq 0x60a8
leaq -0x5270(%rbp), %rdi
callq 0x61b8
cmpl %r14d, %eax
jne 0x2b4e
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x541e(%rip), %rsi      # 0x7d7a
callq 0x2250
movq %rbx, %rdi
callq 0x2150
leaq -0x62c0(%rbp), %rdi
movq %r15, %rsi
callq 0x4a04
leaq -0x60(%rbp), %rdi
leaq -0x62c0(%rbp), %rsi
callq 0x5066
movq -0x62a0(%rbp), %rdi
leaq -0x6290(%rbp), %rax
cmpq %rax, %rdi
je 0x29a0
callq 0x21e0
movq -0x62c0(%rbp), %rdi
leaq -0x62b0(%rbp), %rax
cmpq %rax, %rdi
je 0x29b8
callq 0x21e0
movq -0x58(%rbp), %rax
movq -0x1050(%rax), %rsi
movq -0x1048(%rax), %rdx
movq %rbx, %rdi
callq 0x2250
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
addq $0x10, %r13
cmpq -0x68(%rbp), %r13
jne 0x28bd
movq 0x85c7(%rip), %rbx      # 0xafd8
leaq 0x5373(%rip), %rsi      # 0x7d8b
movl $0x14, %edx
movq %rbx, %rdi
callq 0x2250
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq 0x8595(%rip), %rdi      # 0xafd8
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0x60(%rbp), %rdi
callq 0x4e56
testb %al, %al
je 0x2b6d
movq 0x8570(%rip), %rbx      # 0xafd8
leaq 0x5331(%rip), %rsi      # 0x7da0
movl $0x1b, %edx
movq %rbx, %rdi
callq 0x2250
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq 0x853e(%rip), %rdi      # 0xafd8
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0x60(%rbp), %rdi
movq (%rdi), %rax
movl $0x3e7, 0x109c(%rax)    # imm = 0x3E7
callq 0x4e56
testb %al, %al
jne 0x2b8c
movq 0x850c(%rip), %rbx      # 0xafd8
leaq 0x52ff(%rip), %rsi      # 0x7dd2
movl $0x11, %edx
movq %rbx, %rdi
callq 0x2250
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq 0x84da(%rip), %rdi      # 0xafd8
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0xa0(%rbp), %rdi
callq 0x2c5a
leaq -0xd0(%rbp), %rdi
callq 0x2cc4
leaq -0x60(%rbp), %rdi
callq 0x2d2e
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2b3a
callq 0x21e0
xorl %eax, %eax
addq $0x7298, %rsp           # imm = 0x7298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x51f4(%rip), %rdi      # 0x7d49
leaq 0x520a(%rip), %rsi      # 0x7d66
leaq 0x520c(%rip), %rcx      # 0x7d6f
movl $0x71, %edx
callq 0x2130
leaq 0x5249(%rip), %rdi      # 0x7dbd
leaq 0x51eb(%rip), %rsi      # 0x7d66
leaq 0x51ed(%rip), %rcx      # 0x7d6f
movl $0x79, %edx
callq 0x2130
leaq 0x5229(%rip), %rdi      # 0x7dbc
leaq 0x51cc(%rip), %rsi      # 0x7d66
leaq 0x51ce(%rip), %rcx      # 0x7d6f
movl $0x7d, %edx
callq 0x2130
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
jmp 0x2bc4
movq %rax, %rbx
jmp 0x2bf7
jmp 0x2bc4
movq %rax, %rbx
jmp 0x2c00
jmp 0x2bdc
movq %rax, %rbx
leaq -0x62c0(%rbp), %rdi
callq 0x2c16
jmp 0x2bdf
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2c5a
leaq -0xd0(%rbp), %rdi
callq 0x2cc4
leaq -0x60(%rbp), %rdi
callq 0x2d2e
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2c0e
callq 0x21e0
movq %rbx, %rdi
callq 0x2300