Sigrid Solveig Haflínudóttirinitial import8808c535 months agoHistory
132 lines · 2.7 KB · C
RawDownload
#include <stdio.h>#include <stddef.h>#include <stdint.h>#include <setjmp.h>#include <string.h>#include <stdlib.h>#include <stdarg.h>#include <cmocka.h> struct bytes { //# decoder encoder	uint8_t b[4];     //#1	uint8_t b2[5];    //#2 size=4	int r_num;	const uint8_t *r; //#3 required num_at=^}; #include "test_bytes.pb.h" enum {	Rsize = 65536*4,}; voidtest_bytes_default_pretty(void **state){	struct bytes b;	char buf[128]; 	(void)state;	memset(&b, 0, sizeof(b));	assert_non_null(pb_seprint_bytes(&b, buf, buf+sizeof(buf), 0, 0));	assert_string_equal(		buf,		"{}"	);} voidtest_bytes_pretty(void **state){	struct bytes b;	char buf[128]; 	(void)state;	b.b[0] = 0x01;	b.b[1] = 0x80;	b.b[2] = 0xff;	b.b[3] = 0xff;	b.b2[0] = 0x01;	b.b2[1] = 0x02;	b.b2[2] = 0x03;	b.b2[3] = 0x04;	b.b2[4] = 0x05;	b.r = (const uint8_t*)"ABCDEF";	b.r_num = 7;	assert_non_null(pb_seprint_bytes(&b, buf, buf+sizeof(buf), 0, 1));	assert_string_equal(		buf,		"{\n"		" b = 0x0180ffff (4 bytes), // #1\n"		" b2 = 0x01020304 (4 bytes), // #2\n"		" r = 0x41424344454600 (7 bytes), // #3\n"		"}"	);} voidtest_bytes_encode(void **state){	uint8_t *r, *buf, *raw;	int i, sz;	FILE *f;	struct bytes b = {		.b = {0, 1, 2, 0},		.b2 = {5, 4, 3, 2, 1},	}; 	(void)state;	sz = Rsize;	r = malloc(sz);	for(i = 0; i < sz; i++)		r[i] = i;	b.r = r;	b.r_num = sz, 	// awk 'BEGIN {printf "b:\"\\x00\\x01\\x02\\x00\", b2:\"\\x05\\x04\\x03\\x02\", r:\""; for(i = 0; i < 65536*4; i++) printf "\\x%02x", (i%256); print "\""}'	assert_non_null(f = fopen("bytes_encode.bin", "rb"));	assert_true(fseek(f, 0, SEEK_END) == 0);	assert_false((sz = ftell(f)) < 1);	assert_true(fseek(f, 0, SEEK_SET) == 0);	assert_non_null(raw = malloc(sz));	assert_true(fread(raw, 1, sz, f) == (size_t)sz);	fclose(f); 	assert_non_null(buf = malloc(sz));	assert_ptr_equal(pb_encode_bytes(&b, buf, buf+sz), buf+sz);	assert_memory_equal(raw, buf, sz); 	free(buf);	free(raw);} voidtest_bytes_decode(void **state){	uint8_t *raw;	int i, sz;	FILE *f;	struct bytes b; 	(void)state;	assert_non_null(f = fopen("bytes_encode.bin", "rb"));	assert_true(fseek(f, 0, SEEK_END) == 0);	assert_false((sz = ftell(f)) < 1);	assert_true(fseek(f, 0, SEEK_SET) == 0);	assert_non_null(raw = malloc(sz));	assert_true(fread(raw, 1, sz, f) == (size_t)sz);	fclose(f); 	assert_ptr_equal(pb_decode_bytes(&b, raw, raw+sz), raw+sz);	assert_int_equal(b.b[0], 0);	assert_int_equal(b.b[1], 1);	assert_int_equal(b.b[2], 2);	assert_int_equal(b.b2[0], 5);	assert_int_equal(b.b2[1], 4);	assert_int_equal(b.b2[2], 3);	assert_int_equal(b.b2[3], 2);	assert_int_equal(b.r_num, Rsize);	for(i = 0; i < Rsize; i++)		assert_int_equal(b.r[i], i&0xff);	free(raw);}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t