// File: /workspace/github_cpp_makefile_star2_O1/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
movaps %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
leaq 0x4a6e(%rip), %rax      # 0x7004
movq %rax, -0x5270(%rbp)
movl $0xf, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
testq %rsi, %rsi
je 0x25c1
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x25d1
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x4afd(%rip), %rax      # 0x70d5
movq %rax, -0x5270(%rbp)
movl $0xf, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x2604
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x2614
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x4c24(%rip), %rax      # 0x723f
movq %rax, -0x5270(%rbp)
movl $0x64, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x2647
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x2657
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x4cb2(%rip), %rax      # 0x7310
movq %rax, -0x5270(%rbp)
movl $0x157, -0x5268(%rbp)   # imm = 0x157
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x268a
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x269a
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x4e3f(%rip), %rax      # 0x74e0
movq %rax, -0x5270(%rbp)
movl $0x78, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x26cd
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x26dd
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x4f99(%rip), %rax      # 0x767d
movq %rax, -0x5270(%rbp)
movl $0x78, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x2710
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x2720
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x5113(%rip), %rax      # 0x783a
movq %rax, -0x5270(%rbp)
movl $0x51, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x2753
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x2763
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x52d3(%rip), %rax      # 0x7a3d
movq %rax, -0x5270(%rbp)
movl $0x510, -0x5268(%rbp)   # imm = 0x510
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x2796
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x27a6
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x5361(%rip), %rax      # 0x7b0e
movq %rax, -0x5270(%rbp)
movl $0xc8, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x27d9
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x27e9
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq 0x5455(%rip), %rax      # 0x7c45
movq %rax, -0x5270(%rbp)
movl $0x3, -0x5268(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0x281c
movups -0x5270(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, -0x38(%rbp)
jmp 0x282c
leaq -0x40(%rbp), %rdi
leaq -0x5270(%rbp), %rdx
callq 0x2d9c
leaq -0x60(%rbp), %rdi
callq 0x4dd4
leaq -0x5270(%rbp), %rdi
callq 0x5b30
leaq -0xc8(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x8(%rax)
movaps %xmm0, 0x18(%rax)
movaps %xmm0, 0x28(%rax)
movaps %xmm0, 0x38(%rax)
movaps %xmm0, 0x48(%rax)
movaps %xmm0, -0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
xorl %ecx, %ecx
movq %rcx, 0x20(%rax)
leaq -0x98(%rbp), %rax
movl $0x0, (%rax)
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 0x29fe
movq 0x872e(%rip), %rbx      # 0xafd8
leaq -0x72c0(%rbp), %r15
movq (%r13), %r12
movl 0x8(%r13), %r14d
testq %r12, %r12
je 0x28d6
movq %r12, %rdi
callq 0x20d0
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2250
jmp 0x28ed
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 0x6110
leaq -0x5270(%rbp), %rdi
callq 0x6226
cmpl %r14d, %eax
jne 0x2b42
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x542a(%rip), %rsi      # 0x7d7a
callq 0x2250
movq %rbx, %rdi
callq 0x2150
leaq -0x62c0(%rbp), %rdi
movq %r15, %rsi
callq 0x4a2c
leaq -0x60(%rbp), %rdi
leaq -0x62c0(%rbp), %rsi
callq 0x50dc
movq -0x62a0(%rbp), %rdi
leaq -0x6290(%rbp), %rax
cmpq %rax, %rdi
je 0x2994
callq 0x21e0
movq -0x62c0(%rbp), %rdi
leaq -0x62b0(%rbp), %rax
cmpq %rax, %rdi
je 0x29ac
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 0x28b1
movq 0x85d3(%rip), %rbx      # 0xafd8
leaq 0x537f(%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 0x85a1(%rip), %rdi      # 0xafd8
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0x60(%rbp), %rdi
callq 0x4e86
testb %al, %al
je 0x2b61
movq 0x857c(%rip), %rbx      # 0xafd8
leaq 0x533d(%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 0x854a(%rip), %rdi      # 0xafd8
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0x60(%rbp), %rdi
movq (%rdi), %rax
movl $0x3e7, 0x109c(%rax)    # imm = 0x3E7
callq 0x4e86
testb %al, %al
jne 0x2b80
movq 0x8518(%rip), %rbx      # 0xafd8
leaq 0x530b(%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 0x84e6(%rip), %rdi      # 0xafd8
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0xa0(%rbp), %rdi
callq 0x2c4e
leaq -0xd0(%rbp), %rdi
callq 0x2cb8
leaq -0x60(%rbp), %rdi
callq 0x2d22
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2b2e
callq 0x21e0
xorl %eax, %eax
addq $0x7298, %rsp           # imm = 0x7298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5200(%rip), %rdi      # 0x7d49
leaq 0x5216(%rip), %rsi      # 0x7d66
leaq 0x5218(%rip), %rcx      # 0x7d6f
movl $0x71, %edx
callq 0x2130
leaq 0x5255(%rip), %rdi      # 0x7dbd
leaq 0x51f7(%rip), %rsi      # 0x7d66
leaq 0x51f9(%rip), %rcx      # 0x7d6f
movl $0x79, %edx
callq 0x2130
leaq 0x5235(%rip), %rdi      # 0x7dbc
leaq 0x51d8(%rip), %rsi      # 0x7d66
leaq 0x51da(%rip), %rcx      # 0x7d6f
movl $0x7d, %edx
callq 0x2130
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
jmp 0x2bb8
movq %rax, %rbx
jmp 0x2beb
movq %rax, %rbx
jmp 0x2bf4
jmp 0x2bd0
movq %rax, %rbx
leaq -0x62c0(%rbp), %rdi
callq 0x2c0a
jmp 0x2bd3
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2c4e
leaq -0xd0(%rbp), %rdi
callq 0x2cb8
leaq -0x60(%rbp), %rdi
callq 0x2d22
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2c02
callq 0x21e0
movq %rbx, %rdi
callq 0x2300