// File: /workspace/github_cpp_makefile_star2_O2/nmmmnu[P]HM3/test/test_blobref.cc
// Function: test_blobref()
int test_blobref(){
	constexpr size_t SIZE = 256;
	char mem[SIZE];

	for(size_t i = 0; i < SIZE; ++i)
		mem[i] = (char) i;

	BlobRef br{ mem };

	assert(*br.as<uint16_t>(0x00) == htobe16(0x0001)	);
	assert(*br.as<uint16_t>(0x0E) == htobe16(0x0E0F)	);
	assert(*br.as<uint32_t>(0x10) == htobe32(0x10111213)	);

	{
		const char *s = br.as<char>('a', 5);
		assert(strncmp(s, "abcde", 5) == 0);

		// relative
		assert(*br.as<char>(s + 5) == 'f');
	}

	{
		struct TestStruct{
			uint16_t	i;
			char		c;
			char		s[4];
		}__attribute__((__packed__));

		const TestStruct *st = br.as<TestStruct>(0x50);
		assert(st->i    == htobe16(0x5051)	);
		assert(st->c    == 0x52			);
		assert(st->s[0] == 0x53			);
		assert(st->s[1] == 0x54			);
		assert(st->s[2] == 0x55			);
		assert(st->s[3] == 0x56			);

		// relative
		const char *stc = (const char *) st;
		assert(*br.as<char>(stc + sizeof(TestStruct)) == 0x57);
	}

	{
		size_t const max = SIZE / sizeof(uint64_t);

		const uint64_t *u64 = br.as<uint64_t>(0, max);
		assert(u64 != nullptr	);
		assert(u64[      0] == htobe64(0x0001020304050607)	);
		assert(u64[max - 1] == htobe64(0xf8f9fafbFCFDFEFF)	);

		// relative
		const uint64_t *p = & u64[max - 2];
		assert(*br.as<uint64_t>(p + 1) == u64[max - 1]	);
	}

	{
		size_t const max = SIZE / sizeof(uint64_t) ;

		// after last
		assert(br.as<uint64_t>(0, max + 1 ) == nullptr		);
	}

	{
		size_t const max = SIZE / sizeof(uint64_t) ;

		const uint64_t *u64 = br.as<uint64_t>(0, max);

		// relative
		// after last
		const uint64_t *p = & u64[max - 1];
		assert(br.as<uint64_t>(p + 1) == nullptr	);
	}

	// zero size
	assert(br.as<char>(0x00, 0) == nullptr	);

	// nullptr
	assert(br.as<char>(nullptr) == nullptr	);

	return 0;
}

// Assembly:
pushq %rbx
subq $0x110, %rsp            # imm = 0x110
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x11b7
movb %al, 0x10(%rsp,%rax)
incq %rax
jmp 0x11a6
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x100, 0x8(%rsp)       # imm = 0x100
cmpw $0x100, (%rax)          # imm = 0x100
jne 0x1355
cmpw $0xf0e, 0x1e(%rsp)      # imm = 0xF0E
jne 0x1371
cmpl $0x13121110, 0x20(%rsp) # imm = 0x13121110
jne 0x138d
leaq 0x71(%rsp), %rdi
leaq 0xeb3(%rip), %rsi       # 0x20ae
pushq $0x5
popq %rdx
callq 0x1050
testl %eax, %eax
jne 0x13a9
leaq 0x76(%rsp), %rsi
movq %rsp, %rdi
pushq $0x1
popq %rdx
callq 0x1578
cmpb $0x66, (%rax)
jne 0x13c5
movq (%rsp), %rax
xorl %esi, %esi
cmpq $0x57, 0x8(%rsp)
leaq 0x50(%rax), %rcx
cmovaeq %rcx, %rsi
testq %rax, %rax
cmoveq %rax, %rsi
cmpw $0x5150, (%rsi)         # imm = 0x5150
jne 0x13e1
cmpb $0x52, 0x2(%rsi)
jne 0x13fd
cmpb $0x53, 0x3(%rsi)
jne 0x1419
cmpb $0x54, 0x4(%rsi)
jne 0x1435
cmpb $0x55, 0x5(%rsi)
jne 0x1451
cmpb $0x56, 0x6(%rsi)
jne 0x146d
addq $0x7, %rsi
movq %rsp, %rdi
pushq $0x1
popq %rdx
callq 0x1578
cmpb $0x57, (%rax)
jne 0x1489
movq (%rsp), %rbx
testq %rbx, %rbx
je 0x14a5
cmpq $0xff, 0x8(%rsp)
jbe 0x14a5
movabsq $0x706050403020100, %rax # imm = 0x706050403020100
cmpq %rax, (%rbx)
jne 0x14be
movabsq $-0x1020304050608, %rax # imm = 0xFFFEFDFCFBFAF9F8
cmpq %rax, 0xf8(%rbx)
jne 0x14d7
addq $0xf8, %rbx
movq %rsp, %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x1578
movq (%rax), %rax
cmpq (%rbx), %rax
jne 0x14f0
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
testq %rax, %rax
sete %dl
cmpq $0x108, %rcx            # imm = 0x108
setb %sil
orb %dl, %sil
je 0x1509
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
cmpq %rdx, %rcx
cmovaeq %rax, %rsi
testq %rax, %rax
cmoveq %rax, %rsi
addq %rdx, %rsi
movq %rsp, %rdi
pushq $0x8
popq %rdx
callq 0x1578
testq %rax, %rax
jne 0x1522
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbx
retq
leaq 0xca8(%rip), %rdi       # 0x2004
leaq 0xccb(%rip), %rsi       # 0x202e
leaq 0xcd9(%rip), %rcx       # 0x2043
pushq $0x15
jmp 0x1539
leaq 0xcde(%rip), %rdi       # 0x2056
leaq 0xcaf(%rip), %rsi       # 0x202e
leaq 0xcbd(%rip), %rcx       # 0x2043
pushq $0x16
jmp 0x1539
leaq 0xcec(%rip), %rdi       # 0x2080
leaq 0xc93(%rip), %rsi       # 0x202e
leaq 0xca1(%rip), %rcx       # 0x2043
pushq $0x17
jmp 0x1539
leaq 0xd04(%rip), %rdi       # 0x20b4
leaq 0xc77(%rip), %rsi       # 0x202e
leaq 0xc85(%rip), %rcx       # 0x2043
pushq $0x1b
jmp 0x1539
leaq 0xd04(%rip), %rdi       # 0x20d0
leaq 0xc5b(%rip), %rsi       # 0x202e
leaq 0xc69(%rip), %rcx       # 0x2043
pushq $0x1e
jmp 0x1539
leaq 0xd03(%rip), %rdi       # 0x20eb
leaq 0xc3f(%rip), %rsi       # 0x202e
leaq 0xc4d(%rip), %rcx       # 0x2043
pushq $0x29
jmp 0x1539
leaq 0xd00(%rip), %rdi       # 0x2104
leaq 0xc23(%rip), %rsi       # 0x202e
leaq 0xc31(%rip), %rcx       # 0x2043
pushq $0x2a
jmp 0x1539
leaq 0xcf2(%rip), %rdi       # 0x2112
leaq 0xc07(%rip), %rsi       # 0x202e
leaq 0xc15(%rip), %rcx       # 0x2043
pushq $0x2b
jmp 0x1539
leaq 0xce7(%rip), %rdi       # 0x2123
leaq 0xbeb(%rip), %rsi       # 0x202e
leaq 0xbf9(%rip), %rcx       # 0x2043
pushq $0x2c
jmp 0x1539
leaq 0xcdc(%rip), %rdi       # 0x2134
leaq 0xbcf(%rip), %rsi       # 0x202e
leaq 0xbdd(%rip), %rcx       # 0x2043
pushq $0x2d
jmp 0x1539
leaq 0xcd1(%rip), %rdi       # 0x2145
leaq 0xbb3(%rip), %rsi       # 0x202e
leaq 0xbc1(%rip), %rcx       # 0x2043
pushq $0x2e
jmp 0x1539
leaq 0xcc6(%rip), %rdi       # 0x2156
leaq 0xb97(%rip), %rsi       # 0x202e
leaq 0xba5(%rip), %rcx       # 0x2043
pushq $0x32
jmp 0x1539
leaq 0xcd9(%rip), %rdi       # 0x2185
leaq 0xb7b(%rip), %rsi       # 0x202e
leaq 0xb89(%rip), %rcx       # 0x2043
pushq $0x39
jmp 0x1539
leaq 0xccf(%rip), %rdi       # 0x2194
leaq 0xb62(%rip), %rsi       # 0x202e
leaq 0xb70(%rip), %rcx       # 0x2043
pushq $0x3a
jmp 0x1539
leaq 0xcdd(%rip), %rdi       # 0x21bb
leaq 0xb49(%rip), %rsi       # 0x202e
leaq 0xb57(%rip), %rcx       # 0x2043
pushq $0x3b
jmp 0x1539
leaq 0xcf0(%rip), %rdi       # 0x21e7
leaq 0xb30(%rip), %rsi       # 0x202e
leaq 0xb3e(%rip), %rcx       # 0x2043
pushq $0x3f
jmp 0x1539
leaq 0xcff(%rip), %rdi       # 0x220f
leaq 0xb17(%rip), %rsi       # 0x202e
leaq 0xb25(%rip), %rcx       # 0x2043
pushq $0x46
jmp 0x1539
leaq 0xd0e(%rip), %rdi       # 0x2237
leaq 0xafe(%rip), %rsi       # 0x202e
leaq 0xb0c(%rip), %rcx       # 0x2043
pushq $0x51
popq %rdx
callq 0x1030