File: test_estimatebit.c

package info (click to toggle)
qrencode 3.3.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,556 kB
  • sloc: sh: 11,606; ansic: 10,317; makefile: 98
file content (158 lines) | stat: -rw-r--r-- 3,447 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "../qrinput.h"

QRinput *gstream;

void test_numbit(void)
{
	QRinput *stream;
	char num[9]="01234567";
	int bits;

	testStart("Estimation of Numeric stream (8 digits)");
	stream = QRinput_new();
	QRinput_append(stream, QR_MODE_NUM, 8, (unsigned char *)num);
	bits = QRinput_estimateBitStreamSize(stream, 0);
	testEndExp(bits == 41);

	QRinput_append(gstream, QR_MODE_NUM, 8, (unsigned char *)num);
	QRinput_free(stream);
}

void test_numbit2(void)
{
	QRinput *stream;
	char num[17]="0123456789012345";
	int bits;

	testStart("Estimation of Numeric stream (16 digits)");
	stream = QRinput_new();
	QRinput_append(stream, QR_MODE_NUM, 16, (unsigned char *)num);
	bits = QRinput_estimateBitStreamSize(stream, 0);
	testEndExp(bits == 68);

	QRinput_append(gstream, QR_MODE_NUM, 16, (unsigned char *)num);
	QRinput_free(stream);
}

void test_numbit3(void)
{
	QRinput *stream;
	char *num;
	int bits;

	testStart("Estimation of Numeric stream (400 digits)");
	stream = QRinput_new();
	num = (char *)malloc(401);
	memset(num, '1', 400);
	num[400] = '\0';
	QRinput_append(stream, QR_MODE_NUM, 400, (unsigned char *)num);
	bits = QRinput_estimateBitStreamSize(stream, 0);
	/* 4 + 10 + 133*10 + 4 = 1348 */
	testEndExp(bits == 1348);

	QRinput_append(gstream, QR_MODE_NUM, 400, (unsigned char *)num);
	QRinput_free(stream);
	free(num);
}

void test_an(void)
{
	QRinput *stream;
	char str[6]="AC-42";
	int bits;

	testStart("Estimation of Alphabet-Numeric stream (5 chars)");
	stream = QRinput_new();
	QRinput_append(stream, QR_MODE_AN, 5, (unsigned char *)str);
	bits = QRinput_estimateBitStreamSize(stream, 0);
	testEndExp(bits == 41);

	QRinput_append(gstream, QR_MODE_AN, 5, (unsigned char *)str);
	QRinput_free(stream);
}

void test_8(void)
{
	QRinput *stream;
	char str[9]="12345678";
	int bits;

	testStart("Estimation of 8 bit data stream (8 bytes)");
	stream = QRinput_new();
	QRinput_append(stream, QR_MODE_8, 8, (unsigned char *)str);
	bits = QRinput_estimateBitStreamSize(stream, 0);
	testEndExp(bits == 76);

	QRinput_append(gstream, QR_MODE_8, 8, (unsigned char *)str);
	QRinput_free(stream);
}

void test_structure(void)
{
	QRinput *stream;
	int bits;

	testStart("Estimation of a structure-append header");
	stream = QRinput_new();
	QRinput_insertStructuredAppendHeader(stream, 10, 1, 0);
	bits = QRinput_estimateBitStreamSize(stream, 1);
	testEndExp(bits == 20);

	QRinput_insertStructuredAppendHeader(gstream, 10, 1, 0);
	QRinput_free(stream);
}

void test_kanji(void)
{
	int res;

	QRinput *stream;
	unsigned char str[4]= {0x93, 0x5f,0xe4, 0xaa};
	int bits;

	testStart("Estimation of Kanji stream (2 chars)");
	stream = QRinput_new();
	res = QRinput_append(stream, QR_MODE_KANJI, 4, (unsigned char *)str);
	if(res < 0) {
		printf("Failed to add.\n");
		testEnd(1);
	} else {
		bits = QRinput_estimateBitStreamSize(stream, 0);
		testEndExp(bits == 38);
		QRinput_append(gstream, QR_MODE_KANJI, 4, (unsigned char *)str);
	}

	QRinput_free(stream);
}

void test_mix(void)
{
	int bits;

	testStart("Estimation of Mixed stream");
	bits = QRinput_estimateBitStreamSize(gstream, 0);
	testEndExp(bits == (41 + 68 + 1348 + 41 + 76 + 38 + 20));
	QRinput_free(gstream);
}

int main(void)
{
	gstream = QRinput_new();

	test_numbit();
	test_numbit2();
	test_numbit3();
	test_an();
	test_8();
	test_kanji();
	test_structure();
	test_mix();

	report();

	return 0;
}