// File: /workspace/github_c_makefile_star2_O1/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 -0xd8(%rip), %rsi       # 0x1333
movl $0x9, %edi
xorl %edx, %edx
callq 0x117c
movq %rax, %r13
movq %rax, %rdi
callq *(%rax)
movq %rax, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
testq %rbx, %rbx
je 0x1479
xorl %r12d, %r12d
movq %r13, %rdi
movq %r12, %rsi
callq 0x135c
movq %rax, (%r15,%r12,8)
incq %r12
cmpq %r12, %rbx
jne 0x143f
xorl %r14d, %r14d
movq (%r15,%r14,8), %rsi
testq %rsi, %rsi
je 0x1471
movq $0x0, 0x8(%rsi)
movq %r13, %rdi
callq *0x8(%r13)
incq %r14
cmpq %r14, %rbx
jne 0x1459
leaq (,%rbx,8), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1050
movq %r13, %rdi
callq *(%r13)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
testq %rax, %rax
je 0x1567
cmpq $0x1, %rbx
movq %rbx, %r14
adcq $0x0, %r14
xorl %r12d, %r12d
movq %r13, -0x30(%rbp)
movq %r12, %rax
xorl %edx, %edx
divq %rbx
movq %r13, %rdi
movq %rdx, %r13
movq %rdx, %rsi
callq 0x135c
testq %rax, %rax
je 0x15a5
movb $0x1, %dl
xorl %ecx, %ecx
movq (%r15,%rcx,8), %rsi
cmpq %rsi, %rax
je 0x14fb
testq %rsi, %rsi
je 0x14f7
incq %rcx
cmpq %rbx, %rcx
setb %dl
cmpq %rcx, %r14
jne 0x14d7
xorl %ecx, %ecx
jmp 0x14fb
movq %rax, (%r15,%rcx,8)
testb $0x1, %dl
je 0x1621
cmpq (%rax), %r13
ja 0x15c4
cmpq %rbx, %r12
setae %dl
cmpq %rcx, %r12
sete %sil
orb %dl, %sil
movq -0x30(%rbp), %r13
je 0x1602
cmpq %rbx, %r12
setbe %dl
cmpq %rcx, %r12
seta %cl
orb %dl, %cl
je 0x15e3
movq $0x0, 0x8(%rax)
movq %r13, %rdi
movq %rax, %rsi
callq *0x8(%r13)
incq %r12
movq %r13, %rdi
callq *(%r13)
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
cmpq %rax, %r12
jb 0x14b4
movq %r13, %rdi
callq *0x18(%r13)
testq %rax, %rax
jne 0x1640
movq 0x3a62(%rip), %rax      # 0x4fe0
movq (%rax), %rcx
leaq 0x1b22(%rip), %rdi      # 0x30aa
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 0x1a58(%rip), %rdi      # 0x3004
leaq 0x1a56(%rip), %rsi      # 0x3009
leaq 0x1a67(%rip), %rcx      # 0x3021
movl $0x48, %edx
callq 0x1040
leaq 0x1a81(%rip), %rdi      # 0x304c
leaq 0x1a37(%rip), %rsi      # 0x3009
leaq 0x1a48(%rip), %rcx      # 0x3021
movl $0x60, %edx
callq 0x1040
leaq 0x1a98(%rip), %rdi      # 0x3082
leaq 0x1a18(%rip), %rsi      # 0x3009
leaq 0x1a29(%rip), %rcx      # 0x3021
movl $0x66, %edx
callq 0x1040
leaq 0x1a68(%rip), %rdi      # 0x3071
leaq 0x19f9(%rip), %rsi      # 0x3009
leaq 0x1a0a(%rip), %rcx      # 0x3021
movl $0x63, %edx
callq 0x1040
leaq 0x1a19(%rip), %rdi      # 0x3041
leaq 0x19da(%rip), %rsi      # 0x3009
leaq 0x19eb(%rip), %rcx      # 0x3021
movl $0x5f, %edx
callq 0x1040
leaq 0x1a4b(%rip), %rdi      # 0x3092
leaq 0x19bb(%rip), %rsi      # 0x3009
leaq 0x19cc(%rip), %rcx      # 0x3021
movl $0x6d, %edx
callq 0x1040