// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movl $0x9, %edi
callq 0x1560
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq (%rdi), %rax
callq *%rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movq %rsp, %rcx
movq %rcx, -0x18(%rbp)
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x17ab
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15f0
movq %rax, %rdx
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1777
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x17e0
movq -0x60(%rbp), %rax
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x16c0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x17b3
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rdx
shlq $0x3, %rdx
xorl %esi, %esi
callq 0x1050
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rdi
callq *%rax
movq %rax, %rcx
movq -0x70(%rbp), %rax
imulq $0x3e8, %rcx, %rcx      # imm = 0x3E8
cmpq %rcx, %rax
jae 0x19d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x10(%rbp)
movq %rdx, %rsi
callq 0x15f0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x184c
jmp 0x186b
leaq 0x17b1(%rip), %rdi      # 0x3004
leaq 0x17af(%rip), %rsi      # 0x3009
movl $0x48, %edx
leaq 0x17bb(%rip), %rcx      # 0x3021
callq 0x1040
movb $0x0, -0x41(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x18e7
movq -0x60(%rbp), %rcx
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rdx
cmpq (%rcx,%rdx,8), %rax
jne 0x18a9
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
movb $0x1, -0x41(%rbp)
jmp 0x18e7
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
xorl %eax, %eax
cmpq (%rcx,%rdx,8), %rax
jne 0x18d7
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, -0x50(%rbp)
movq -0x40(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movb $0x1, -0x41(%rbp)
jmp 0x18e7
jmp 0x18d9
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x187f
testb $0x1, -0x41(%rbp)
je 0x18ef
jmp 0x190e
leaq 0x174b(%rip), %rdi      # 0x3041
leaq 0x170c(%rip), %rsi      # 0x3009
movl $0x5f, %edx
leaq 0x1718(%rip), %rcx      # 0x3021
callq 0x1040
movq -0x38(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x10(%rbp)
movq -0x40(%rbp), %rax
cmpq (%rax), %rdx
ja 0x1925
jmp 0x1944
leaq 0x1720(%rip), %rdi      # 0x304c
leaq 0x16d6(%rip), %rsi      # 0x3009
movl $0x60, %edx
leaq 0x16e2(%rip), %rcx      # 0x3021
callq 0x1040
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jae 0x197b
movq -0x38(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x195a
jmp 0x1979
leaq 0x1710(%rip), %rdi      # 0x3071
leaq 0x16a1(%rip), %rsi      # 0x3009
movl $0x63, %edx
leaq 0x16ad(%rip), %rcx      # 0x3021
callq 0x1040
jmp 0x197b
movq -0x38(%rbp), %rax
cmpq -0x10(%rbp), %rax
jbe 0x19b2
movq -0x38(%rbp), %rax
cmpq -0x50(%rbp), %rax
jbe 0x1991
jmp 0x19b0
leaq 0x16ea(%rip), %rdi      # 0x3082
leaq 0x166a(%rip), %rsi      # 0x3009
movl $0x66, %edx
leaq 0x1676(%rip), %rcx      # 0x3021
callq 0x1040
jmp 0x19b2
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x16c0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x17fb
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x8(%rbp), %rdi
callq *%rax
movq %rax, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x19ea
jmp 0x1a09
leaq 0x16a1(%rip), %rdi      # 0x3092
leaq 0x1611(%rip), %rsi      # 0x3009
movl $0x6d, %edx
leaq 0x161d(%rip), %rcx      # 0x3021
callq 0x1040
movq 0x35d0(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0x1690(%rip), %rsi      # 0x30aa
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)