// File: /workspace/github_c_makefile_star2_O2/vidarr[P]ringbuffer/test/buffercache_test.c
// Function: test_buffercache_caching
void test_buffercache_caching() {

    Ringbuffer* cache = buffercache_create(9);

    const size_t capacity = cache->capacity(cache);
    void* pointers[capacity];

    /* First step - fill buffer */
    for(size_t i = 0; i < capacity; ++i) {
        pointers[i] = buffercache_get_buffer(cache, i);
    }

    for(size_t i = 0; i < capacity; ++i) {
        buffercache_release_buffer(cache, pointers[i]);
    }

    /* No more buffers should be allocated - thus there should be 50 different
     * pointers appearing and no more -
     * we keep track in this array */

    memset(pointers, 0, sizeof(pointers));

    /* Shift some elements out of cache into ringbuffer */
    for(size_t i = 0; i < 1000 * cache->capacity(cache); ++i) {
        Buffer* item = buffercache_get_buffer(cache, i % capacity);
        assert(item);

        bool pointer_ok = false;

        size_t index_found = 0;

        for(size_t index = 0; index < capacity; ++index) {

            if(item == pointers[index]) {
                index_found = index;
                pointer_ok = true;
                break;
            }

            if(0 == pointers[index]) {
                index_found = index;
                pointers[index] = item;
                pointer_ok = true;
                break;
            }

        }

        assert(pointer_ok);
        assert(i % capacity <= item->capacity_bytes);

        if(i < capacity) {
            assert(i == index_found);
        }
        if(i > capacity) {
            assert(i > index_found);
        }


        buffercache_release_buffer(cache, item);
    }

    assert(0 == cache->free(cache));

    fprintf(stdout, "Caching ok\n");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $0x9
popq %rdi
callq 0x1320
movq %rax, %rbx
movq %rax, %rdi
callq *(%rax)
movq %rax, %r14
movq %rsp, %r15
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x141d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x134a
movq %rax, (%r15,%r12,8)
incq %r12
jmp 0x1404
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x143e
movq (%r15,%r12,8), %rsi
testq %rsi, %rsi
je 0x1439
andq $0x0, 0x8(%rsi)
movq %rbx, %rdi
callq *0x8(%rbx)
incq %r12
jmp 0x1420
leaq (,%r14,8), %rdx
xorl %r12d, %r12d
movq %r15, %rdi
xorl %esi, %esi
callq 0x1050
movq %rbx, %rdi
callq *(%rbx)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cmpq %rax, %r12
jae 0x14d8
movq %r12, %rax
xorl %edx, %edx
divq %r14
movq %rdx, %r13
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x134a
testq %rax, %rax
je 0x152e
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x1511
movq (%r15,%rcx,8), %rdx
cmpq %rdx, %rax
je 0x14a5
testq %rdx, %rdx
je 0x14a1
incq %rcx
jmp 0x1485
movq %rax, (%r15,%rcx,8)
cmpq (%rax), %r13
ja 0x1560
cmpq %r14, %r12
setae %dl
cmpq %rcx, %r12
sete %cl
orb %dl, %cl
je 0x1547
andq $0x0, 0x8(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq *0x8(%rbx)
incq %r12
jmp 0x1453
movq %rbx, %rdi
callq *0x18(%rbx)
testq %rax, %rax
jne 0x1579
movq 0x3af2(%rip), %rax      # 0x4fe0
movq (%rax), %rcx
leaq 0x1ba2(%rip), %rdi      # 0x309a
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1070
leaq 0x1b29(%rip), %rdi      # 0x3041
leaq 0x1aea(%rip), %rsi      # 0x3009
leaq 0x1afb(%rip), %rcx      # 0x3021
pushq $0x5f
popq %rdx
callq 0x1040
leaq 0x1acf(%rip), %rdi      # 0x3004
leaq 0x1acd(%rip), %rsi      # 0x3009
leaq 0x1ade(%rip), %rcx      # 0x3021
pushq $0x48
jmp 0x1528
leaq 0x1b23(%rip), %rdi      # 0x3071
leaq 0x1ab4(%rip), %rsi      # 0x3009
leaq 0x1ac5(%rip), %rcx      # 0x3021
pushq $0x63
jmp 0x1528
leaq 0x1ae5(%rip), %rdi      # 0x304c
leaq 0x1a9b(%rip), %rsi      # 0x3009
leaq 0x1aac(%rip), %rcx      # 0x3021
pushq $0x60
jmp 0x1528
leaq 0x1b02(%rip), %rdi      # 0x3082
leaq 0x1a82(%rip), %rsi      # 0x3009
leaq 0x1a93(%rip), %rcx      # 0x3021
pushq $0x6d
jmp 0x1528