// File: /workspace/github_cpp_makefile_star2_O2/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)
andq $0x0, 0x10(%rdi)
leaq 0x3ade(%rip), %rax      # 0x6004
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0xf, 0x8(%rsi)
callq 0x2908
leaq 0x3b92(%rip), %rax      # 0x60d5
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0xf, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x3cdb(%rip), %rax      # 0x623f
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x64, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x3d8b(%rip), %rax      # 0x6310
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x157, 0x8(%rsi)       # imm = 0x157
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x3f3a(%rip), %rax      # 0x64e0
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x78, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x40b6(%rip), %rax      # 0x667d
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x78, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x4252(%rip), %rax      # 0x683a
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x51, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x4434(%rip), %rax      # 0x6a3d
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x510, 0x8(%rsi)       # imm = 0x510
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x44e4(%rip), %rax      # 0x6b0e
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0xc8, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq 0x45fa(%rip), %rax      # 0x6c45
leaq -0x5270(%rbp), %rsi
movq %rax, (%rsi)
movl $0x3, 0x8(%rsi)
leaq -0x40(%rbp), %rdi
callq 0x2908
leaq -0x60(%rbp), %rdi
callq 0x47cc
leaq -0x5270(%rbp), %rdi
callq 0x5264
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
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq -0x40(%rbp), %r13
movq -0x38(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x72c0(%rbp), %r15
leaq -0x62c0(%rbp), %r14
cmpq -0x68(%rbp), %r13
je 0x27ad
movq (%r13), %r12
movl 0x8(%r13), %ebx
movq 0x88e5(%rip), %rdi      # 0xafd0
movq %r12, %rsi
callq 0x2200
movq %rax, %rdi
callq 0x2150
movq -0x58(%rbp), %rax
subq -0x60(%rbp), %rax
cqto
movl $0x1050, %ecx           # imm = 0x1050
idivq %rcx
leaq -0xd0(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movl %eax, %ecx
callq 0x2d14
leaq -0x5270(%rbp), %rdi
movq %r15, %rsi
leaq -0x60(%rbp), %rdx
callq 0x57a4
leaq -0x5270(%rbp), %rdi
callq 0x5866
cmpl %ebx, %eax
jne 0x285f
movq 0x8881(%rip), %rdi      # 0xafd0
leaq 0x4624(%rip), %rsi      # 0x6d7a
callq 0x2200
movq %rax, %rdi
callq 0x2150
movq %r14, %rdi
movq %r15, %rsi
callq 0x4508
leaq -0x60(%rbp), %rdi
movq %r14, %rsi
callq 0x4970
movq %r14, %rdi
callq 0x292c
movq -0x58(%rbp), %rsi
movq $-0x1050, %rax          # imm = 0xEFB0
addq %rax, %rsi
movq 0x8839(%rip), %rdi      # 0xafd0
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
addq $0x10, %r13
jmp 0x26d2
movq 0x881c(%rip), %rdi      # 0xafd0
leaq 0x45d0(%rip), %rsi      # 0x6d8b
callq 0x2200
movq %rax, %rdi
callq 0x20e0
leaq -0x60(%rbp), %rdi
callq 0x485e
testb %al, %al
je 0x287c
movq 0x87f0(%rip), %rdi      # 0xafd0
leaq 0x45b9(%rip), %rsi      # 0x6da0
callq 0x2200
movq %rax, %rdi
callq 0x20e0
leaq -0x60(%rbp), %rdi
movq (%rdi), %rax
movl $0x3e7, 0x109c(%rax)    # imm = 0x3E7
callq 0x485e
testb %al, %al
jne 0x2895
movq 0x87b7(%rip), %rdi      # 0xafd0
leaq 0x45b2(%rip), %rsi      # 0x6dd2
callq 0x2200
movq %rax, %rdi
callq 0x20e0
leaq -0xd0(%rbp), %rdi
callq 0x294c
leaq -0x60(%rbp), %rdi
callq 0x2a6a
leaq -0x40(%rbp), %rdi
callq 0x2ad2
xorl %eax, %eax
addq $0x7298, %rsp           # imm = 0x7298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x44e3(%rip), %rdi      # 0x6d49
leaq 0x44f9(%rip), %rsi      # 0x6d66
leaq 0x44fb(%rip), %rcx      # 0x6d6f
pushq $0x71
popq %rdx
callq 0x2130
leaq 0x453a(%rip), %rdi      # 0x6dbd
leaq 0x44dc(%rip), %rsi      # 0x6d66
leaq 0x44de(%rip), %rcx      # 0x6d6f
pushq $0x79
jmp 0x2876
leaq 0x4520(%rip), %rdi      # 0x6dbc
leaq 0x44c3(%rip), %rsi      # 0x6d66
leaq 0x44c5(%rip), %rcx      # 0x6d6f
pushq $0x7d
jmp 0x2876
movq %rax, %rbx
jmp 0x28ee
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
jmp 0x28c7
movq %rax, %rbx
jmp 0x28f7
jmp 0x28df
movq %rax, %rbx
leaq -0x62c0(%rbp), %rdi
callq 0x292c
jmp 0x28e2
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x294c
leaq -0x60(%rbp), %rdi
callq 0x2a6a
leaq -0x40(%rbp), %rdi
callq 0x2ad2
movq %rbx, %rdi
callq 0x22d0