Hash :
c83400cf
Author :
Date :
2014-08-05T15:04:27
Added show_QRcode() for testing purposes. (Thanks to Sunil Maganally)
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/*
* common part of test units.
*/
#ifndef __COMMON_H__
#define __COMMON_H__
#include <stdlib.h>
#include "../config.h"
#include "../qrencode.h"
#include "../qrinput.h"
#include "../bitstream.h"
#include "../qrencode_inner.h"
#define testStart(__arg__) (testStartReal(__func__, __arg__))
#define testEndExp(__arg__) (testEnd(!(__arg__)))
static int tests = 0;
static int failed = 0;
static int assertionFailed = 0;
static int assertionNum = 0;
static const char *testName = NULL;
static const char *testFunc = NULL;
char levelChar[4] = {'L', 'M', 'Q', 'H'};
const char *modeStr[5] = {"nm", "an", "8", "kj", "st"};
void printQRinput(QRinput *input)
{
QRinput_List *list;
int i;
list = input->head;
while(list != NULL) {
for(i=0; i<list->size; i++) {
printf("0x%02x,", list->data[i]);
}
list = list->next;
}
printf("\n");
}
void printQRinputInfo(QRinput *input)
{
QRinput_List *list;
BitStream *b;
int i, ret;
printf("QRinput info:\n");
printf(" version: %d\n", input->version);
printf(" level : %c\n", levelChar[input->level]);
list = input->head;
i = 0;
while(list != NULL) {
i++;
list = list->next;
}
printf(" chunks: %d\n", i);
b = BitStream_new();
ret = QRinput_mergeBitStream(input, b);
if(ret == 0) {
printf(" bitstream-size: %d\n", BitStream_size(b));
BitStream_free(b);
}
list = input->head;
i = 0;
while(list != NULL) {
printf("\t#%d: mode = %s, size = %d\n", i, modeStr[list->mode], list->size);
i++;
list = list->next;
}
}
void printQRinputStruct(QRinput_Struct *s)
{
QRinput_InputList *list;
int i = 1;
printf("Struct size: %d\n", s->size);
printf("Struct parity: %08x\n", s->parity);
for(list = s->head; list != NULL; list = list->next) {
printf("Symbol %d - ", i);
printQRinputInfo(list->input);
i++;
}
}
void printFrame(int width, unsigned char *frame)
{
int x, y;
for(y=0; y<width; y++) {
for(x=0; x<width; x++) {
printf("%02x ", *frame++);
}
printf("\n");
}
}
void printQRcode(QRcode *code)
{
printFrame(code->width, code->data);
}
void testStartReal(const char *func, const char *name)
{
tests++;
testName = name;
testFunc = func;
assertionFailed = 0;
assertionNum = 0;
printf("_____%d: %s: %s...\n", tests, func, name);
}
void testEnd(int result)
{
printf(".....%d: %s: %s, ", tests, testFunc, testName);
if(result) {
puts("FAILED.");
failed++;
} else {
puts("PASSED.");
}
}
#define assert_exp(__exp__, ...) \
{assertionNum++;if(!(__exp__)) {assertionFailed++; printf(__VA_ARGS__);}}
#define assert_zero(__exp__, ...) assert_exp((__exp__) == 0, __VA_ARGS__)
#define assert_nonzero(__exp__, ...) assert_exp((__exp__) != 0, __VA_ARGS__)
#define assert_null(__ptr__, ...) assert_exp((__ptr__) == NULL, __VA_ARGS__)
#define assert_nonnull(__ptr__, ...) assert_exp((__ptr__) != NULL, __VA_ARGS__)
#define assert_equal(__e1__, __e2__, ...) assert_exp((__e1__) == (__e2__), __VA_ARGS__)
#define assert_notequal(__e1__, __e2__, ...) assert_exp((__e1__) != (__e2__), __VA_ARGS__)
#define assert_nothing(__exp__, ...) {printf(__VA_ARGS__); __exp__;}
void testFinish(void)
{
printf(".....%d: %s: %s, ", tests, testFunc, testName);
if(assertionFailed) {
printf("FAILED. (%d assertions failed.)\n", assertionFailed);
failed++;
} else {
printf("PASSED. (%d assertions passed.)\n", assertionNum);
}
}
void report()
{
printf("Total %d tests, %d fails.\n", tests, failed);
if(failed) exit(-1);
}
int ncmpBin(char *correct, BitStream *bstream, int len)
{
int i, bit;
char *p;
if(len != BitStream_size(bstream)) {
printf("Length is not match: %d, %d expected.\n", BitStream_size(bstream), len);
return -1;
}
p = correct;
i = 0;
while(*p != '\0') {
while(*p == ' ') {
p++;
}
bit = (*p == '1')?1:0;
if(bstream->data[i] != bit) return -1;
i++;
p++;
if(i == len) break;
}
return 0;
}
int cmpBin(char *correct, BitStream *bstream)
{
int len = 0;
char *p;
for(p = correct; *p != '\0'; p++) {
if(*p != ' ') len++;
}
return ncmpBin(correct, bstream, len);
}
void printBstream(BitStream *bstream)
{
int i, size;
size = BitStream_size(bstream);
for(i=0; i<size; i++) {
printf(bstream->data[i]?"1":"0");
}
printf("\n");
}
#if HAVE_SDL
/* Experimental debug function */
/* You can call show_QRcode(QRcode *code) to display the QR Code from anywhere
* in test code using SDL. */
#include <SDL.h>
static SDL_Surface *screen = NULL;
static void draw_QRcode(QRcode *qrcode, int ox, int oy, int margin, int size)
{
int x, y, width;
unsigned char *p;
SDL_Rect rect;
ox += margin * size;
oy += margin * size;
width = qrcode->width;
p = qrcode->data;
for(y=0; y<width; y++) {
for(x=0; x<width; x++) {
rect.x = ox + x * size;
rect.y = oy + y * size;
rect.w = size;
rect.h = size;
SDL_FillRect(screen, &rect, (*p&1)?0:0xffffff);
p++;
}
}
}
void show_QRcode(QRcode *qrcode)
{
SDL_Event event;
if(!SDL_WasInit(SDL_INIT_VIDEO)) {
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
}
int width = (qrcode->width + 4 * 2) * 4; //maring = 4, size = 4
screen = SDL_SetVideoMode(width, width, 32, 0);
SDL_FillRect(screen, NULL, 0xffffff);
draw_QRcode(qrcode, 0, 0, 4, 4);
SDL_Flip(screen);
fprintf(stderr, "Press any key on the QR Code window to proceed.\n");
int loop = 1;
while(loop) {
SDL_WaitEvent(&event);
if(event.type == SDL_KEYDOWN) {
loop = 0;
}
}
}
#else
void show_QRcode() {
}
#endif
#endif /* __COMMON_H__ */