// File: 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
subq $0x7470, %rsp           # imm = 0x7470
movl $0x0, -0x4(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x2c90
leaq 0xaa02(%rip), %rax      # 0xd004
movq %rax, -0x38(%rbp)
movl $0xf, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq 0x2d20
jmp 0x2620
leaq -0x20(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x2cb0
jmp 0x262f
leaq 0xaa9f(%rip), %rax      # 0xd0d5
movq %rax, -0x68(%rbp)
movl $0xf, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x6c(%rbp), %rdx
callq 0x2d20
jmp 0x2654
leaq -0x20(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x2cb0
jmp 0x2663
leaq 0xabd5(%rip), %rax      # 0xd23f
movq %rax, -0x88(%rbp)
movl $0x64, -0x8c(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x8c(%rbp), %rdx
callq 0x2d20
jmp 0x2694
leaq -0x20(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x2cb0
jmp 0x26a3
leaq 0xac66(%rip), %rax      # 0xd310
movq %rax, -0xa8(%rbp)
movl $0x157, -0xac(%rbp)     # imm = 0x157
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0xac(%rbp), %rdx
callq 0x2d20
jmp 0x26d7
leaq -0x20(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2cb0
jmp 0x26e9
leaq 0xadf0(%rip), %rax      # 0xd4e0
movq %rax, -0xc8(%rbp)
movl $0x78, -0xcc(%rbp)
leaq -0xc0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
leaq -0xcc(%rbp), %rdx
callq 0x2d20
jmp 0x271d
leaq -0x20(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x2cb0
jmp 0x272f
leaq 0xaf47(%rip), %rax      # 0xd67d
movq %rax, -0xe8(%rbp)
movl $0x78, -0xec(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0xec(%rbp), %rdx
callq 0x2d20
jmp 0x2763
leaq -0x20(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x2cb0
jmp 0x2775
leaq 0xb0be(%rip), %rax      # 0xd83a
movq %rax, -0x108(%rbp)
movl $0x51, -0x10c(%rbp)
leaq -0x100(%rbp), %rdi
leaq -0x108(%rbp), %rsi
leaq -0x10c(%rbp), %rdx
callq 0x2d20
jmp 0x27a9
leaq -0x20(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x2cb0
jmp 0x27bb
leaq 0xb27b(%rip), %rax      # 0xda3d
movq %rax, -0x128(%rbp)
movl $0x510, -0x12c(%rbp)    # imm = 0x510
leaq -0x120(%rbp), %rdi
leaq -0x128(%rbp), %rsi
leaq -0x12c(%rbp), %rdx
callq 0x2d20
jmp 0x27ef
leaq -0x20(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x2cb0
jmp 0x2801
leaq 0xb306(%rip), %rax      # 0xdb0e
movq %rax, -0x148(%rbp)
movl $0xc8, -0x14c(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x148(%rbp), %rsi
leaq -0x14c(%rbp), %rdx
callq 0x2d20
jmp 0x2835
leaq -0x20(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x2cb0
jmp 0x2847
leaq 0xb3f7(%rip), %rax      # 0xdc45
movq %rax, -0x168(%rbp)
movl $0x3, -0x16c(%rbp)
leaq -0x160(%rbp), %rdi
leaq -0x168(%rbp), %rsi
leaq -0x16c(%rbp), %rdx
callq 0x2d20
jmp 0x287b
leaq -0x20(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x2cb0
jmp 0x288d
leaq -0x190(%rbp), %rdi
callq 0x9760
jmp 0x289b
leaq -0x5330(%rbp), %rdi
callq 0xb290
jmp 0x28a9
leaq -0x5390(%rbp), %rdi
xorl %esi, %esi
movl $0x60, %edx
callq 0x2110
leaq -0x5390(%rbp), %rdi
callq 0x2d50
leaq -0x20(%rbp), %rax
movq %rax, -0x6398(%rbp)
movq -0x6398(%rbp), %rdi
callq 0x2d80
movq %rax, -0x63a0(%rbp)
movq -0x6398(%rbp), %rdi
callq 0x2db0
movq %rax, -0x63a8(%rbp)
leaq -0x63a0(%rbp), %rdi
leaq -0x63a8(%rbp), %rsi
callq 0x2de0
testb $0x1, %al
jne 0x2915
jmp 0x2af6
leaq -0x63a0(%rbp), %rdi
callq 0x2e20
movups (%rax), %xmm0
movaps %xmm0, -0x63c0(%rbp)
movq -0x63c0(%rbp), %rsi
movq 0x1168f(%rip), %rdi     # 0x13fc8
callq 0x2220
movq %rax, -0x7418(%rbp)
jmp 0x2947
movq -0x7418(%rbp), %rdi
movq 0x1167b(%rip), %rsi     # 0x13fd0
callq 0x2260
jmp 0x295c
movq -0x63c0(%rbp), %rax
movq %rax, -0x7420(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x2e40
movq -0x7420(%rbp), %rdx
movl %eax, %ecx
leaq -0x5390(%rbp), %rdi
leaq -0x6390(%rbp), %rsi
callq 0x4770
jmp 0x2994
leaq -0x5330(%rbp), %rdi
leaq -0x6390(%rbp), %rsi
leaq -0x190(%rbp), %rdx
callq 0xbb10
jmp 0x29b0
leaq -0x5330(%rbp), %rdi
callq 0xbc70
movl %eax, -0x7424(%rbp)
jmp 0x29c4
movl -0x7424(%rbp), %eax
cmpl -0x63b8(%rbp), %eax
jne 0x29d4
jmp 0x2a26
leaq 0xb36e(%rip), %rdi      # 0xdd49
leaq 0xb384(%rip), %rsi      # 0xdd66
movl $0x71, %edx
leaq 0xb381(%rip), %rcx      # 0xdd6f
callq 0x2150
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2c78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2c6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x2c60
movq 0x1159b(%rip), %rdi     # 0x13fc8
leaq 0xb346(%rip), %rsi      # 0xdd7a
callq 0x2220
movq %rax, -0x7430(%rbp)
jmp 0x2a42
movq -0x7430(%rbp), %rdi
movq 0x11580(%rip), %rsi     # 0x13fd0
callq 0x2260
jmp 0x2a57
leaq -0x7410(%rbp), %rdi
leaq -0x6390(%rbp), %rsi
callq 0x92b0
jmp 0x2a6c
leaq -0x190(%rbp), %rdi
leaq -0x7410(%rbp), %rsi
callq 0x9a60
jmp 0x2a81
leaq -0x7410(%rbp), %rdi
callq 0x2e70
leaq -0x190(%rbp), %rdi
callq 0x2ea0
movq %rax, %rsi
movq 0x11525(%rip), %rdi     # 0x13fc8
callq 0x21d0
movq %rax, -0x7438(%rbp)
jmp 0x2ab1
movq -0x7438(%rbp), %rdi
movq 0x114f9(%rip), %rsi     # 0x13fb8
callq 0x2260
jmp 0x2ac6
jmp 0x2ac8
leaq -0x63a0(%rbp), %rdi
callq 0x2ee0
jmp 0x28f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x7410(%rbp), %rdi
callq 0x2e70
jmp 0x2c60
movq 0x114cb(%rip), %rdi     # 0x13fc8
leaq 0xb287(%rip), %rsi      # 0xdd8b
callq 0x2220
movq %rax, -0x7440(%rbp)
jmp 0x2b12
movq -0x7440(%rbp), %rdi
movq 0x11498(%rip), %rsi     # 0x13fb8
callq 0x2260
jmp 0x2b27
leaq -0x190(%rbp), %rdi
callq 0x9840
movb %al, -0x7441(%rbp)
jmp 0x2b3b
movb -0x7441(%rbp), %al
testb $0x1, %al
jne 0x2b47
jmp 0x2b49
jmp 0x2b68
leaq 0xb26d(%rip), %rdi      # 0xddbd
leaq 0xb20f(%rip), %rsi      # 0xdd66
movl $0x79, %edx
leaq 0xb20c(%rip), %rcx      # 0xdd6f
callq 0x2150
movq 0x11459(%rip), %rdi     # 0x13fc8
leaq 0xb22a(%rip), %rsi      # 0xdda0
callq 0x2220
movq %rax, -0x7450(%rbp)
jmp 0x2b84
movq -0x7450(%rbp), %rdi
movq 0x11426(%rip), %rsi     # 0x13fb8
callq 0x2260
jmp 0x2b99
leaq -0x190(%rbp), %rdi
movq %rdi, -0x7460(%rbp)
movl $0x1, %esi
callq 0x2f00
movq -0x7460(%rbp), %rdi
movl $0x3e7, 0x4c(%rax)      # imm = 0x3E7
callq 0x9840
movb %al, -0x7451(%rbp)
jmp 0x2bcc
movb -0x7451(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2bda
jmp 0x2bdc
jmp 0x2bfb
leaq 0xb1d9(%rip), %rdi      # 0xddbc
leaq 0xb17c(%rip), %rsi      # 0xdd66
movl $0x7d, %edx
leaq 0xb179(%rip), %rcx      # 0xdd6f
callq 0x2150
movq 0x113c6(%rip), %rdi     # 0x13fc8
leaq 0xb1c9(%rip), %rsi      # 0xddd2
callq 0x2220
movq %rax, -0x7468(%rbp)
jmp 0x2c17
movq -0x7468(%rbp), %rdi
movq 0x11393(%rip), %rsi     # 0x13fb8
callq 0x2260
jmp 0x2c2c
movl $0x0, -0x4(%rbp)
leaq -0x5390(%rbp), %rdi
callq 0x2f20
leaq -0x190(%rbp), %rdi
callq 0x2f50
leaq -0x20(%rbp), %rdi
callq 0x2f70
movl -0x4(%rbp), %eax
addq $0x7470, %rsp           # imm = 0x7470
popq %rbp
retq
leaq -0x5390(%rbp), %rdi
callq 0x2f20
leaq -0x190(%rbp), %rdi
callq 0x2f50
leaq -0x20(%rbp), %rdi
callq 0x2f70
movq -0x48(%rbp), %rdi
callq 0x2360
nopw (%rax,%rax)