test_qrinput.c

来自「二维码QR的编码实现。C语言实现。希望各位一起学习。一起做条码开发」· C语言 代码 · 共 747 行 · 第 1/2 页

C
747
字号
	QRinput_setVersion(input, version);	QRinput_append(input, mode, size, (unsigned char *)data);	b = QRinput_mergeBitStream(input);	bits = BitStream_size(b);	bytes = QRinput_lengthOfCode(mode, version, bits);	QRinput_free(input);	BitStream_free(b);	return bytes;}void test_lengthOfCode_num(void){	int i, bytes;	char *data;	data = (char *)malloc(8000);	for(i=0; i<8000; i++) {		data[i] = '0' + i % 10;	}	testStart("Checking length of code (numeric)");	for(i=1; i<=9; i++) {		bytes = check_lengthOfCode(QR_MODE_NUM, data, i, 1);		assert_equal(i, bytes, "lengthOfCode failed. (QR_MODE_NUM, version:1, size:%d)\n", i);	}	for(i=1023; i<=1025; i++) {		bytes = check_lengthOfCode(QR_MODE_NUM, data, i, 1);		assert_equal(1023, bytes, "lengthOfCode failed. (QR_MODE_NUM, version:1, size:%d)\n", i);	}	testFinish();	free(data);}void test_lengthOfCode_kanji(void){	int i, bytes;	char str[4]= {0x93, 0x5f,0xe4, 0xaa};	testStart("Checking length of code (kanji)");	for(i=2; i<=4; i+=2) {		bytes = check_lengthOfCode(QR_MODE_KANJI, str, i, 1);		assert_equal(i, bytes, "lengthOfCode failed. (QR_MODE_KANJI, version:1, size:%d)\n", i);	}	testFinish();}void test_struct_split_example(void){	QRinput *input;	QRinput_Struct *s;	QRinput_InputList *e;	QRinput_List *l;	const char *str[4] = { "an example ", "of four Str", "uctured Appe", "nd symbols,"};	int i;	BitStream *bstream;	testStart("Testing the example of structured-append symbols");	s = QRinput_Struct_new();	for(i=0; i<4; i++) {		input = QRinput_new2(1, QR_ECLEVEL_M);		QRinput_append(input, QR_MODE_8, strlen(str[i]), (unsigned char *)str[i]);		QRinput_Struct_appendInput(s, input);	}	QRinput_Struct_insertStructuredAppendHeaders(s);	e = s->head;	i = 0;	while(e != NULL) {		bstream = QRinput_mergeBitStream(e->input);		BitStream_free(bstream);		l = e->input->head->next;		assert_equal(l->mode, QR_MODE_8, "#%d: wrong mode (%d).\n", i, l->mode);		assert_equal(e->input->level, QR_ECLEVEL_M, "#%d: wrong level (%d).\n", i, e->input->level);		e = e->next;		i++;	}	testFinish();	QRinput_Struct_free(s);}void test_struct_split_tooLarge(void){	QRinput *input;	QRinput_Struct *s;	char *str;	int errsv;	testStart("Testing structured-append symbols. (too large data)");	str = (char *)malloc(128);	memset(str, 'a', 128);	input = QRinput_new2(1, QR_ECLEVEL_H);	QRinput_append(input, QR_MODE_8, 128, (unsigned char *)str);	s = QRinput_splitQRinputToStruct(input);	errsv = errno;	assert_null(s, "returns non-null.");	assert_equal(errsv, ERANGE, "did not return ERANGE.");	testFinish();	if(s != NULL) QRinput_Struct_free(s);	QRinput_free(input);	free(str);}void test_struct_split_invalidVersion(void){	QRinput *input;	QRinput_Struct *s;	char *str;	int errsv;	testStart("Testing structured-append symbols. (invalid version 0)");	str = (char *)malloc(128);	memset(str, 'a', 128);	input = QRinput_new2(0, QR_ECLEVEL_H);	QRinput_append(input, QR_MODE_8, 128, (unsigned char *)str);	s = QRinput_splitQRinputToStruct(input);	errsv = errno;	assert_null(s, "returns non-null.");	assert_equal(errsv, ERANGE, "did not return ERANGE.");	testFinish();	if(s != NULL) QRinput_Struct_free(s);	QRinput_free(input);	free(str);}void test_splitentry(void){	QRinput *i1, *i2;	QRinput_List *e;	const char *str = "abcdefghij";	int size1, size2, i;	unsigned char *d1, *d2;	testStart("Testing QRinput_splitEntry. (next == NULL)");	i1 = QRinput_new();	QRinput_append(i1, QR_MODE_8, strlen(str), (unsigned char *)str);	i2 = QRinput_dup(i1);	e = i2->head;	e = i2->head;	QRinput_splitEntry(e, 4);	size1 = size2 = 0;	e = i1->head;	while(e != NULL) {		size1 += e->size;		e = e->next;	}	e = i2->head;	while(e != NULL) {		size2 += e->size;		e = e->next;	}	d1 = (unsigned char *)malloc(size1);	e = i1->head;	i = 0;	while(e != NULL) {		memcpy(&d1[i], e->data, e->size);		i += e->size;		e = e->next;	}	d2 = (unsigned char *)malloc(size2);	e = i2->head;	i = 0;	while(e != NULL) {		memcpy(&d2[i], e->data, e->size);		i += e->size;		e = e->next;	}	assert_equal(size1, size2, "sizes are different. (%d:%d)\n", size1, size2);	assert_equal(i2->head->size, 4, "split failed (first half)");	assert_equal(i2->head->next->size, 6, "split failed(second half)");	assert_zero(memcmp(d1, d2, size1), "strings are different.");	QRinput_free(i1);	QRinput_free(i2);	free(d1);	free(d2);	testFinish();}void test_splitentry2(void){	QRinput *i1, *i2;	QRinput_List *e;	const char *str = "abcdefghij";	int size1, size2, i;	unsigned char *d1, *d2;	testStart("Testing QRinput_splitEntry. (next != NULL)");	i1 = QRinput_new();	QRinput_append(i1, QR_MODE_8, strlen(str), (unsigned char *)str);	QRinput_append(i1, QR_MODE_8, strlen(str), (unsigned char *)str);	i2 = QRinput_dup(i1);	e = i2->head;	e = i2->head;	QRinput_splitEntry(e, 4);	size1 = size2 = 0;	e = i1->head;	while(e != NULL) {		size1 += e->size;		e = e->next;	}	e = i2->head;	while(e != NULL) {		size2 += e->size;		e = e->next;	}	d1 = (unsigned char *)malloc(size1);	e = i1->head;	i = 0;	while(e != NULL) {		memcpy(&d1[i], e->data, e->size);		i += e->size;		e = e->next;	}	d2 = (unsigned char *)malloc(size2);	e = i2->head;	i = 0;	while(e != NULL) {		memcpy(&d2[i], e->data, e->size);		i += e->size;		e = e->next;	}	assert_equal(size1, size2, "sizes are different. (%d:%d)\n", size1, size2);	assert_equal(i2->head->size, 4, "split failed (first half)");	assert_equal(i2->head->next->size, 6, "split failed(second half)");	assert_zero(memcmp(d1, d2, size1), "strings are different.");	QRinput_free(i1);	QRinput_free(i2);	free(d1);	free(d2);	testFinish();}void test_splitentry3(void){	QRinput *input;	QRinput_Struct *s;	QRinput_List *e00, *e01, *e10, *e11;	QRinput_InputList *list;	const char *str = "abcdefghijklmno";	testStart("Testing QRinput_splitEntry. (does not split an entry)");	/* version 1 symbol contains 152 bit (19 byte) data.	 * 20 bits for a structured-append header, so 132 bits can be used.	 * 15 bytes of 8-bit data is suitable for the symbol.	 * (mode(4) + length(8) + data(120) == 132.)	 */	input = QRinput_new2(1, QR_ECLEVEL_L);	QRinput_append(input, QR_MODE_8, strlen(str), (unsigned char *)str);	QRinput_append(input, QR_MODE_8, strlen(str), (unsigned char *)str);	s = QRinput_splitQRinputToStruct(input);	list = s->head;	e00 = list->input->head;	e01 = e00->next;	list = list->next;	e10 = list->input->head;	e11 = e00->next;		assert_equal(e00->mode, QR_MODE_STRUCTURE, "Structure header is missing?");	assert_equal(e01->mode, QR_MODE_8, "no data?!");	assert_null(e01->next, "Input list is not terminated!\n");	assert_equal(e10->mode, QR_MODE_STRUCTURE, "Structure header is missing?");	assert_equal(e11->mode, QR_MODE_8, "no data?!");	assert_null(e11->next, "Input list is not terminated!\n");	QRinput_free(input);	QRinput_Struct_free(s);	testFinish();}void test_parity(void){	QRinput *input;	QRinput_Struct *s;	const char *text = "an example of four Structured Append symbols,";	const char *str[4] = {		"an example ",		"of four Str",		"uctured Appe",		"nd symbols,"};	unsigned char p1, p2;	int i, len;	testStart("Testing parity calc.");	s = QRinput_Struct_new();	for(i=0; i<4; i++) {		input = QRinput_new2(1, QR_ECLEVEL_M);		QRinput_append(input, QR_MODE_8, strlen(str[i]), (unsigned char *)str[i]);		QRinput_Struct_appendInput(s, input);	}	QRinput_Struct_insertStructuredAppendHeaders(s);	p1 = s->parity;	p2 = 0;	len = strlen(text);	for(i=0; i<len; i++) {		p2 ^= text[i];	}	assert_equal(p1, p2, "Parity numbers didn't match. (%02x should be %02x).\n", p1, p2);	testFinish();	QRinput_Struct_free(s);}void test_parity2(void){	QRinput *input;	QRinput_Struct *s;	const char *text = "an example of four Structured Append symbols,";	unsigned char p1, p2;	int i, len;	testStart("Testing parity calc.(split)");	input = QRinput_new2(1, QR_ECLEVEL_L);	QRinput_append(input, QR_MODE_8, strlen(text), (unsigned char *)text);	s = QRinput_splitQRinputToStruct(input);	p1 = s->parity;	p2 = 0;	len = strlen(text);	for(i=0; i<len; i++) {		p2 ^= text[i];	}	assert_equal(p1, p2, "Parity numbers didn't match. (%02x should be %02x).\n", p1, p2);	testFinish();	QRinput_free(input);	QRinput_Struct_free(s);}int main(int argc, char **argv){	test_encodeNumeric();	test_encodeNumeric2();	test_encodeNumeric3();	test_encodeNumeric_versionup();	test_encode8();	test_encode8_versionup();	test_encodeTooLong();	test_encodeAn();	test_encodeAn2();	test_encodeKanji();	test_encodeNumericPadded();	test_encodeNumericPadded2();	test_encodeAnNum();	test_struct_listop();	test_insertStructuredAppendHeader();	test_insertStructuredAppendHeader_error();	test_struct_insertStructuredAppendHeaders();	test_lengthOfCode_num();	test_splitentry();	test_splitentry2();	test_splitentry3();	test_struct_split_example();	test_struct_split_tooLarge();	test_struct_split_invalidVersion();	test_parity();	test_parity2();	report();	return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?