// File: /workspace/github_c_makefile_star2_O3/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
leaq -0xca(%rip), %rsi       # 0x1329
movl $0x9, %edi
xorl %edx, %edx
callq 0x117c
movq %rax, %rbx
movq %rax, %rdi
callq *(%rax)
movq %rax, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
testq %r13, %r13
je 0x1460
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x134a
movq %rax, (%r15,%r12,8)
incq %r12
cmpq %r12, %r13
jne 0x1427
xorl %r14d, %r14d
movq (%r15,%r14,8), %rsi
testq %rsi, %rsi
je 0x1458
movq $0x0, 0x8(%rsi)
movq %rbx, %rdi
callq *0x8(%rbx)
incq %r14
cmpq %r14, %r13
jne 0x1441
leaq (,%r13,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1050
movq %rbx, %rdi
callq *(%rbx)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
testq %rax, %rax
je 0x1529
cmpq $0x1, %r13
movq %r13, %r14
adcq $0x0, %r14
xorl %r12d, %r12d
movq %rbx, -0x30(%rbp)
movq %r12, %rax
xorl %edx, %edx
movq %rbx, %rdi
movq %r13, %rbx
divq %r13
movq %rdx, %r13
movq %rdx, %rsi
callq 0x134a
testq %rax, %rax
je 0x1585
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rdx
cmpq %rdx, %rax
je 0x14dc
testq %rdx, %rdx
je 0x14d8
incq %rcx
cmpq %rcx, %r14
jne 0x14bd
jmp 0x1566
movq %rax, (%r15,%rcx,8)
cmpq (%rax), %r13
ja 0x15a4
cmpq %rbx, %r12
setae %dl
cmpq %rcx, %r12
sete %cl
orb %dl, %cl
je 0x15c3
movq %rbx, %r13
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq *0x8(%rbx)
incq %r12
movq %rbx, %rdi
callq *(%rbx)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cmpq %rax, %r12
jb 0x1499
movq %rbx, %rdi
callq *0x18(%rbx)
testq %rax, %rax
jne 0x15e2
movq 0x3aa1(%rip), %rax      # 0x4fe0
movq (%rax), %rcx
leaq 0x1b51(%rip), %rdi      # 0x309a
movl $0xb, %esi
movl $0x1, %edx
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1070
leaq 0x1ad4(%rip), %rdi      # 0x3041
leaq 0x1a95(%rip), %rsi      # 0x3009
leaq 0x1aa6(%rip), %rcx      # 0x3021
movl $0x5f, %edx
callq 0x1040
leaq 0x1a78(%rip), %rdi      # 0x3004
leaq 0x1a76(%rip), %rsi      # 0x3009
leaq 0x1a87(%rip), %rcx      # 0x3021
movl $0x48, %edx
callq 0x1040
leaq 0x1aa1(%rip), %rdi      # 0x304c
leaq 0x1a57(%rip), %rsi      # 0x3009
leaq 0x1a68(%rip), %rcx      # 0x3021
movl $0x60, %edx
callq 0x1040
leaq 0x1aa7(%rip), %rdi      # 0x3071
leaq 0x1a38(%rip), %rsi      # 0x3009
leaq 0x1a49(%rip), %rcx      # 0x3021
movl $0x63, %edx
callq 0x1040
leaq 0x1a99(%rip), %rdi      # 0x3082
leaq 0x1a19(%rip), %rsi      # 0x3009
leaq 0x1a2a(%rip), %rcx      # 0x3021
movl $0x6d, %edx
callq 0x1040