1 /**
2  * @file lv_test_assert.c
3  *
4  * Copyright 2002-2010 Guillaume Cottenceau.
5  *
6  * This software may be freely redistributed under the terms
7  * of the X11 license.
8  *
9  */
10 
11 /*********************
12  *      INCLUDES
13  *********************/
14 #include "lv_test_assert.h"
15 #include "../lvgl.h"
16 #if LV_BUILD_TEST
17 
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdarg.h>
23 
24 #define PNG_DEBUG 3
25 #include <png.h>
26 
27 
28 /*********************
29  *      DEFINES
30  *********************/
31 //#define REF_IMGS_PATH "lvgl/tests/lv_test_ref_imgs/"
32 #define REF_IMGS_PATH "lv_test_ref_imgs/"
33 
34 /**********************
35  *      TYPEDEFS
36  **********************/
37 typedef struct {
38     int width, height;
39     png_byte color_type;
40     png_byte bit_depth;
41 
42     png_structp png_ptr;
43     png_infop info_ptr;
44     int number_of_passes;
45     png_bytep * row_pointers;
46 }png_img_t;
47 
48 /**********************
49  *  STATIC PROTOTYPES
50  **********************/
51 static void read_png_file(png_img_t * p, const char* file_name);
52 //static void write_png_file(png_img_t * p, const char* file_name);
53 static void png_release(png_img_t * p);
54 //static void process_file(png_img_t * p);
55 
56 /**********************
57  *  STATIC VARIABLES
58  **********************/
59 
60 /**********************
61  *      MACROS
62  **********************/
63 
64 /**********************
65  *   GLOBAL FUNCTIONS
66  **********************/
67 
68 
lv_test_print(const char * s,...)69 void lv_test_print(const char * s, ...)
70 {
71     va_list args;
72     va_start(args, s);
73     vfprintf(stdout, s, args);
74     fprintf(stdout, "\n");
75     va_end(args);
76 }
77 
78 
lv_test_exit(const char * s,...)79 void lv_test_exit(const char * s, ...)
80 {
81     va_list args;
82     va_start(args, s);
83     vfprintf(stderr, s, args);
84     fprintf(stderr, "\n");
85     va_end(args);
86 
87     exit(1);
88 }
89 
90 
lv_test_error(const char * s,...)91 void lv_test_error(const char * s, ...)
92 {
93     va_list args;
94     va_start(args, s);
95     vfprintf(stderr, s, args);
96     fprintf(stderr, "\n");
97     va_end(args);
98     exit(1);
99 }
100 
lv_test_assert_true(int32_t expression,const char * s)101 void lv_test_assert_true(int32_t expression, const char * s)
102 {
103     if(!expression) {
104         lv_test_error("   FAIL: %s. (Expected: not zero)", s, expression);
105     } else {
106         lv_test_print("   PASS: %s. (Expected: not zero)", s, expression);
107     }
108 }
109 
lv_test_assert_int_eq(int32_t n_ref,int32_t n_act,const char * s)110 void lv_test_assert_int_eq(int32_t n_ref, int32_t n_act, const char * s)
111 {
112     if(n_ref != n_act) {
113         lv_test_error("   FAIL: %s. (Expected:  %d, Actual: %d)", s, n_ref, n_act);
114     } else {
115         lv_test_print("   PASS: %s. (Expected: %d)", s, n_ref);
116     }
117 }
118 
lv_test_assert_int_gt(int32_t n_ref,int32_t n_act,const char * s)119 void lv_test_assert_int_gt(int32_t n_ref, int32_t n_act, const char * s)
120 {
121     if(n_act <= n_ref) {
122         lv_test_error("   FAIL: %s. (Expected:  > %d, Actual: %d)", s, n_ref, n_act);
123     } else {
124         lv_test_print("   PASS: %s. (Expected: > %d, , Actual: %d)", s, n_ref, n_act);
125     }
126 }
127 
lv_test_assert_int_lt(int32_t n_ref,int32_t n_act,const char * s)128 void lv_test_assert_int_lt(int32_t n_ref, int32_t n_act, const char * s)
129 {
130     if(n_act >= n_ref) {
131         lv_test_error("   FAIL: %s. (Expected:  < %d, Actual: %d)", s, n_ref, n_act);
132     } else {
133         lv_test_print("   PASS: %s. (Expected: < %d, , Actual: %d)", s, n_ref, n_act);
134     }
135 }
136 
137 
lv_test_assert_str_eq(const char * s_ref,const char * s_act,const char * s)138 void lv_test_assert_str_eq(const char * s_ref, const char * s_act, const char * s)
139 {
140     if(strcmp(s_ref, s_act) != 0) {
141         lv_test_error("   FAIL: %s. (Expected:  %s, Actual: %s)", s, s_ref, s_act);
142     } else {
143         lv_test_print("   PASS: %s. (Expected: %s)", s, s_ref);
144     }
145 }
146 
147 
lv_test_assert_array_eq(const uint8_t * p_ref,const uint8_t * p_act,int32_t size,const char * s)148 void lv_test_assert_array_eq(const uint8_t *p_ref, const uint8_t *p_act, int32_t size, const char * s)
149 {
150     if(memcmp(p_ref, p_act, size) != 0) {
151         lv_test_error("   FAIL: %s. (Expected: all %d bytes should be equal)", s, size);
152     } else {
153         lv_test_print("   PASS: %s. (Expected: all %d bytes should be equal)", s, size);
154     }
155 }
156 
157 
lv_test_assert_ptr_eq(const void * p_ref,const void * p_act,const char * s)158 void lv_test_assert_ptr_eq(const void * p_ref, const void * p_act, const char * s)
159 {
160     if(p_ref != p_act) {
161         lv_test_error("   FAIL: %s. (Expected:  0x%lx, Actual: 0x%lx)", s, p_ref, p_act);
162     } else {
163         lv_test_print("   PASS: %s. (Expected: 0x%lx)", s, p_ref);
164     }
165 }
166 
lv_test_assert_color_eq(lv_color_t c_ref,lv_color_t c_act,const char * s)167 void lv_test_assert_color_eq(lv_color_t c_ref, lv_color_t c_act, const char * s)
168 {
169     if(c_ref.full != c_act.full) {
170         lv_test_error("   FAIL: %s. (Expected:  R:%02x, G:%02x, B:%02x, Actual: R:%02x, G:%02x, B:%02x)",  s,
171                 c_ref.ch.red, c_ref.ch.green, c_ref.ch.blue,
172                 c_act.ch.red, c_act.ch.green, c_act.ch.blue);
173     } else {
174         lv_test_print("   PASS: %s. (Expected: R:%02x, G:%02x, B:%02x)", s,
175                 c_ref.ch.red, c_ref.ch.green, c_ref.ch.blue);
176     }
177 }
178 
lv_test_assert_img_eq(const char * fn_ref,const char * s)179 void lv_test_assert_img_eq(const char * fn_ref, const char * s)
180 {
181 #if LV_COLOR_DEPTH != 32
182     lv_test_print("   SKIP: Can't compare '%s' because LV_COLOR_DEPTH != 32", fn_ref);
183     return;
184 #endif
185 
186 #if LV_HOR_RES_MAX != 800 || LV_VER_RES_MAX != 480
187     lv_test_print("   SKIP: Can't compare '%s' because the resolution needs to be 800x480 (LV_HOR_RES_MAX, LV_VER_RES_MAX)", fn_ref);
188     return;
189 #endif
190 
191     char fn_ref_full[512];
192     sprintf(fn_ref_full, "%s%s", REF_IMGS_PATH, fn_ref);
193 
194     png_img_t p;
195     read_png_file(&p, fn_ref_full);
196     uint8_t * screen_buf;
197 
198     lv_disp_t * disp = lv_disp_get_default();
199     lv_obj_invalidate(lv_disp_get_scr_act(disp));
200     lv_refr_now(disp);
201 
202     extern lv_color_t test_fb[];
203 
204     screen_buf = (uint8_t *)test_fb;
205 
206     uint8_t * ptr_act = NULL;
207     const png_byte* ptr_ref = NULL;
208 
209     bool err = false;
210     int x, y, i_buf = 0;
211     for (y=0; y<p.height; y++) {
212         png_byte* row = p.row_pointers[y];
213         for (x=0; x<p.width; x++) {
214             ptr_ref = &(row[x*3]);
215             ptr_act = &(screen_buf[i_buf*4]);
216             uint8_t tmp = ptr_act[0];
217             ptr_act[0] = ptr_act[2];
218             ptr_act[2] = tmp;
219 
220             if(memcmp(ptr_act, ptr_ref, 3) != 0) {
221                 err = true;
222                 break;
223             }
224             i_buf++;
225         }
226         if(err) break;
227     }
228 
229     png_release(&p);
230 
231     if(err) {
232         uint32_t ref_px = 0;
233         uint32_t act_px = 0;
234         memcpy(&ref_px, ptr_ref, 3);
235         memcpy(&act_px, ptr_act, 3);
236         lv_test_error("   FAIL: %s. (Expected:  %s, diff. at (%d;%d), %08x instead of %08x)", s, fn_ref, x, y, act_px, ref_px);
237     } else {
238         lv_test_print("   PASS: %s. (Expected: %s)", s, fn_ref);
239     }
240 }
241 
242 /**********************
243  *   STATIC FUNCTIONS
244  **********************/
245 
read_png_file(png_img_t * p,const char * file_name)246 static void read_png_file(png_img_t * p, const char* file_name)
247 {
248     char header[8];    // 8 is the maximum size that can be checked
249 
250     /* open file and test for it being a png */
251     FILE *fp = fopen(file_name, "rb");
252     if (!fp)
253         lv_test_exit("[read_png_file] File %s could not be opened for reading", file_name);
254     size_t rcnt = fread(header, 1, 8, fp);
255     if (rcnt != 8 || png_sig_cmp((png_const_bytep)header, 0, 8))
256         lv_test_exit("[read_png_file] File %s is not recognized as a PNG file", file_name);
257 
258     /* initialize stuff */
259     p->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
260 
261     if (!p->png_ptr)
262         lv_test_exit("[read_png_file] png_create_read_struct failed");
263 
264     p->info_ptr = png_create_info_struct(p->png_ptr);
265     if (!p->info_ptr)
266         lv_test_exit("[read_png_file] png_create_info_struct failed");
267 
268     if (setjmp(png_jmpbuf(p->png_ptr)))
269         lv_test_exit("[read_png_file] Error during init_io");
270 
271     png_init_io(p->png_ptr, fp);
272     png_set_sig_bytes(p->png_ptr, 8);
273 
274     png_read_info(p->png_ptr, p->info_ptr);
275 
276     p->width = png_get_image_width(p->png_ptr, p->info_ptr);
277     p->height = png_get_image_height(p->png_ptr, p->info_ptr);
278     p->color_type = png_get_color_type(p->png_ptr, p->info_ptr);
279     p->bit_depth = png_get_bit_depth(p->png_ptr, p->info_ptr);
280 
281     p->number_of_passes = png_set_interlace_handling(p->png_ptr);
282     png_read_update_info(p->png_ptr, p->info_ptr);
283 
284 
285     /* read file */
286     if (setjmp(png_jmpbuf(p->png_ptr)))
287         lv_test_exit("[read_png_file] Error during read_image");
288 
289     p->row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * p->height);
290 
291     int y;
292     for (y=0; y<p->height; y++)
293         p->row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(p->png_ptr,p->info_ptr));
294 
295     png_read_image(p->png_ptr, p->row_pointers);
296 
297     fclose(fp);
298 }
299 //
300 //
301 //static void write_png_file(png_img_t * p, const char* file_name)
302 //{
303 //    /* create file */
304 //    FILE *fp = fopen(file_name, "wb");
305 //    if (!fp)
306 //        lv_test_exit("[write_png_file] File %s could not be opened for writing", file_name);
307 //
308 //
309 //    /* initialize stuff */
310 //    p->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
311 //
312 //    if (!p->png_ptr)
313 //        lv_test_exit("[write_png_file] png_create_write_struct failed");
314 //
315 //    p->info_ptr = png_create_info_struct(p->png_ptr);
316 //    if (!p->info_ptr)
317 //        lv_test_exit("[write_png_file] png_create_info_struct failed");
318 //
319 //    if (setjmp(png_jmpbuf(p->png_ptr)))
320 //        lv_test_exit("[write_png_file] Error during init_io");
321 //
322 //    png_init_io(p->png_ptr, fp);
323 //
324 //
325 //    /* write header */
326 //    if (setjmp(png_jmpbuf(p->png_ptr)))
327 //        lv_test_exit("[write_png_file] Error during writing header");
328 //
329 //    png_set_IHDR(p->png_ptr, p->info_ptr, p->width, p->height,
330 //            p->bit_depth, p->color_type, PNG_INTERLACE_NONE,
331 //            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
332 //
333 //    png_write_info(p->png_ptr, p->info_ptr);
334 //
335 //
336 //    /* write bytes */
337 //    if (setjmp(png_jmpbuf(p->png_ptr)))
338 //        lv_test_exit("[write_png_file] Error during writing bytes");
339 //
340 //    png_write_image(p->png_ptr, p->row_pointers);
341 //
342 //
343 //    /* end write */
344 //    if (setjmp(png_jmpbuf(p->png_ptr)))
345 //        lv_test_exit("[write_png_file] Error during end of write");
346 //
347 //    png_write_end(p->png_ptr, NULL);
348 //
349 //    fclose(fp);
350 //}
351 //
png_release(png_img_t * p)352 static void png_release(png_img_t * p)
353 {
354     int y;
355       for (y=0; y<p->height; y++)
356           free(p->row_pointers[y]);
357       free(p->row_pointers);
358 }
359 
360 //static void process_file(png_img_t * p)
361 //{
362 //    if (png_get_color_type(p->png_ptr, p->info_ptr) == PNG_COLOR_TYPE_RGB)
363 //        lv_test_exit("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGBA "
364 //                "(lacks the alpha channel)");
365 //
366 //    if (png_get_color_type(p->png_ptr, p->info_ptr) != PNG_COLOR_TYPE_RGBA)
367 //        lv_test_exit("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (%d) (is %d)",
368 //                PNG_COLOR_TYPE_RGBA, png_get_color_type(p->png_ptr, p->info_ptr));
369 //
370 //    int x, y;
371 //    for (y=0; y<p->height; y++) {
372 //        png_byte* row = p->row_pointers[y];
373 //        for (x=0; x<p->width; x++) {
374 //            png_byte* ptr = &(row[x*4]);
375 //            printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n",
376 //                    x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
377 //
378 //            /* set red value to 0 and green value to the blue one */
379 //            ptr[0] = 0;
380 //            ptr[1] = ptr[2];
381 //        }
382 //    }
383 //}
384 #endif
385 
386