Преглед изворни кода

[test] Generalise pnm_ok() to pixbuf_ok()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown пре 11 година
родитељ
комит
f4d2342e5a
3 измењених фајлова са 157 додато и 88 уклоњено
  1. 76
    0
      src/tests/pixbuf_test.c
  2. 66
    0
      src/tests/pixbuf_test.h
  3. 15
    88
      src/tests/pnm_test.c

+ 76
- 0
src/tests/pixbuf_test.c Прегледај датотеку

@@ -0,0 +1,76 @@
1
+/*
2
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ */
19
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+/** @file
23
+ *
24
+ * Pixel buffer self-tests
25
+ *
26
+ */
27
+
28
+/* Forcibly enable assertions */
29
+#undef NDEBUG
30
+
31
+#include <assert.h>
32
+#include <ipxe/image.h>
33
+#include <ipxe/pixbuf.h>
34
+#include <ipxe/test.h>
35
+#include "pixbuf_test.h"
36
+
37
+/**
38
+ * Report pixel buffer test result
39
+ *
40
+ * @v test		Pixel buffer test
41
+ * @v file		Test code file
42
+ * @v line		Test code line
43
+ */
44
+void pixbuf_okx ( struct pixel_buffer_test *test, const char *file,
45
+		  unsigned int line ) {
46
+	struct pixel_buffer *pixbuf;
47
+	int rc;
48
+
49
+	/* Sanity check */
50
+	assert ( ( test->width * test->height * sizeof ( test->data[0] ) )
51
+		 == test->len );
52
+
53
+	/* Correct image data pointer */
54
+	test->image->data = virt_to_user ( ( void * ) test->image->data );
55
+
56
+	/* Check that image is detected as PNM */
57
+	okx ( image_probe ( test->image ) == 0, file, line );
58
+	okx ( test->image->type == test->type, file, line );
59
+
60
+	/* Check that a pixel buffer can be created from the image */
61
+	okx ( ( rc = image_pixbuf ( test->image, &pixbuf ) ) == 0, file, line );
62
+	if ( rc == 0 ) {
63
+
64
+		/* Check pixel buffer dimensions */
65
+		okx ( pixbuf->width == test->width, file, line );
66
+		okx ( pixbuf->height == test->height, file, line );
67
+
68
+		/* Check pixel buffer data */
69
+		okx ( pixbuf->len == test->len, file, line );
70
+		okx ( memcmp_user ( pixbuf->data, 0,
71
+				    virt_to_user ( test->data ), 0,
72
+				    test->len ) == 0, file, line );
73
+
74
+		pixbuf_put ( pixbuf );
75
+	}
76
+}

+ 66
- 0
src/tests/pixbuf_test.h Прегледај датотеку

@@ -0,0 +1,66 @@
1
+#ifndef _PIXBUF_TEST_H
2
+#define _PIXBUF_TEST_H
3
+
4
+FILE_LICENCE ( GPL2_OR_LATER );
5
+
6
+#include <stdint.h>
7
+#include <ipxe/refcnt.h>
8
+#include <ipxe/image.h>
9
+#include <ipxe/test.h>
10
+
11
+/** A pixel buffer test */
12
+struct pixel_buffer_test {
13
+	/** Image type */
14
+	struct image_type *type;
15
+	/** Source image */
16
+	struct image *image;
17
+	/** Pixel data */
18
+	const uint32_t *data;
19
+	/** Length of pixel data */
20
+	size_t len;
21
+	/** Width */
22
+	unsigned int width;
23
+	/** Height */
24
+	unsigned int height;
25
+};
26
+
27
+/**
28
+ * Define a pixel buffer test
29
+ *
30
+ * @v _name		Test name
31
+ * @v _type		Test image file type
32
+ * @v _file		Test image file data
33
+ * @v _width		Expected pixel buffer width
34
+ * @v _height		Expected pixel buffer height
35
+ * @v _data		Expected pixel buffer data
36
+ * @ret test		Pixel buffer test
37
+ */
38
+#define PIX( _name, _type, _file, _width, _height, _data )		\
39
+	static const char _name ## __file[] = _file;			\
40
+	static const uint32_t _name ## __data[] = _data;		\
41
+	static struct image _name ## __image = {			\
42
+		.refcnt = REF_INIT ( ref_no_free ),			\
43
+		.name = #_name,						\
44
+		.data = ( userptr_t ) ( _name ## __file ),		\
45
+		.len = sizeof ( _name ## __file ),			\
46
+	};								\
47
+	static struct pixel_buffer_test _name = {			\
48
+		.type = _type,						\
49
+		.image = & _name ## __image,				\
50
+		.data = _name ## __data,				\
51
+		.len = sizeof ( _name ## __data ),			\
52
+		.width = _width,					\
53
+		.height = _height,					\
54
+	};
55
+
56
+extern void pixbuf_okx ( struct pixel_buffer_test *test, const char *file,
57
+			 unsigned int line );
58
+
59
+/**
60
+ * Report pixel buffer test result
61
+ *
62
+ * @v test		Pixel buffer test
63
+ */
64
+#define pixbuf_ok( test ) pixbuf_okx ( test, __FILE__, __LINE__ )
65
+
66
+#endif /* _PIXBUF_TEST_H */

+ 15
- 88
src/tests/pnm_test.c Прегледај датотеку

@@ -33,44 +33,13 @@ FILE_LICENCE ( GPL2_OR_LATER );
33 33
 #include <ipxe/pixbuf.h>
34 34
 #include <ipxe/pnm.h>
35 35
 #include <ipxe/test.h>
36
+#include "pixbuf_test.h"
36 37
 
37 38
 /** Define inline pixel data */
38 39
 #define DATA(...) { __VA_ARGS__ }
39 40
 
40
-/** A PNM test */
41
-struct pnm_test {
42
-	/** Source image */
43
-	struct image *image;
44
-	/** Pixel data */
45
-	const uint32_t *data;
46
-	/** Length of pixel data */
47
-	size_t len;
48
-	/** Width */
49
-	unsigned int width;
50
-	/** Height */
51
-	unsigned int height;
52
-};
53
-
54
-/** Define a PNM test */
55
-#define PNM( NAME, FILE, WIDTH, HEIGHT, DATA )				\
56
-	static const char NAME ## _file[] = FILE;			\
57
-	static const uint32_t NAME ## _data[] = DATA;			\
58
-	static struct image NAME ## _image = {				\
59
-		.refcnt = REF_INIT ( ref_no_free ),			\
60
-		.name = #NAME,						\
61
-		.data = ( userptr_t ) ( NAME ## _file ),		\
62
-		.len = sizeof ( NAME ## _file ),			\
63
-	};								\
64
-	static struct pnm_test NAME = {					\
65
-		.image = & NAME ## _image,				\
66
-		.data = NAME ## _data,					\
67
-		.len = sizeof ( NAME ## _data ),			\
68
-		.width = WIDTH,						\
69
-		.height = HEIGHT,					\
70
-	};
71
-
72 41
 /** PBM ASCII example (from Wikipedia) */
73
-PNM ( pbm_ascii,
42
+PIX ( pbm_ascii, &pnm_image_type,
74 43
       "P1\n"
75 44
       "# This is an example bitmap of the letter \"J\"\n"
76 45
       "6 10\n"
@@ -97,7 +66,7 @@ PNM ( pbm_ascii,
97 66
 	     0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ) );
98 67
 
99 68
 /** PGM ASCII example (from Wikipedia) */
100
-PNM ( pgm_ascii,
69
+PIX ( pgm_ascii, &pnm_image_type,
101 70
       "P2\n"
102 71
       "# Shows the word \"FEEP\" (example from Netpbm man page on PGM)\n"
103 72
       "24 7\n"
@@ -140,7 +109,7 @@ PNM ( pgm_ascii,
140 109
 	     0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ) );
141 110
 
142 111
 /** PPM ASCII example (from Wikipedia) */
143
-PNM ( ppm_ascii,
112
+PIX ( ppm_ascii, &pnm_image_type,
144 113
       "P3\n"
145 114
       "# The P3 means colors are in ASCII, then 3 columns and 2 rows,\n"
146 115
       "# then 255 for max color, then RGB triplets\n"
@@ -152,7 +121,7 @@ PNM ( ppm_ascii,
152 121
       DATA ( 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff, 0x000000 ) );
153 122
 
154 123
 /** PBM ASCII with no space between pixel values */
155
-PNM ( pbm_ascii_no_space,
124
+PIX ( pbm_ascii_no_space, &pnm_image_type,
156 125
       "P1\n"
157 126
       "3 3\n"
158 127
       "001\n"
@@ -163,7 +132,7 @@ PNM ( pbm_ascii_no_space,
163 132
 	     0x000000, 0x000000, 0x000000 ) );
164 133
 
165 134
 /** PBM binary example (converted from Wikipedia) */
166
-PNM ( pbm_binary,
135
+PIX ( pbm_binary, &pnm_image_type,
167 136
       DATA ( 0x50, 0x34, 0x0a, 0x23, 0x20, 0x43, 0x52, 0x45, 0x41, 0x54, 0x4f,
168 137
 	     0x52, 0x3a, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x20, 0x50, 0x4e, 0x4d,
169 138
 	     0x20, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x20, 0x56, 0x65, 0x72,
@@ -183,7 +152,7 @@ PNM ( pbm_binary,
183 152
 	     0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff ) );
184 153
 
185 154
 /** PGM binary example (converted from Wikipedia) */
186
-PNM ( pgm_binary,
155
+PIX ( pgm_binary, &pnm_image_type,
187 156
       DATA ( 0x50, 0x35, 0x0a, 0x32, 0x34, 0x20, 0x37, 0x0a, 0x31, 0x35, 0x0a,
188 157
 	     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 158
 	     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -232,68 +201,26 @@ PNM ( pgm_binary,
232 201
 	     0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000 ) );
233 202
 
234 203
 /** PPM binary example (converted from Wikipedia) */
235
-PNM ( ppm_binary,
204
+PIX ( ppm_binary, &pnm_image_type,
236 205
       DATA ( 0x50, 0x36, 0x0a, 0x33, 0x20, 0x32, 0x0a, 0x32, 0x35, 0x35, 0x0a,
237 206
 	     0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
238 207
 	     0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00 ),
239 208
       3, 2,
240 209
       DATA ( 0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xffffff, 0x000000 ) );
241 210
 
242
-/**
243
- * Report PNM test result
244
- *
245
- * @v test		PNM test
246
- * @v file		Test code file
247
- * @v line		Test code line
248
- */
249
-static void pnm_okx ( struct pnm_test *test, const char *file,
250
-		      unsigned int line ) {
251
-	struct pixel_buffer *pixbuf;
252
-	int rc;
253
-
254
-	/* Sanity check */
255
-	assert ( ( test->width * test->height * sizeof ( test->data[0] ) )
256
-		 == test->len );
257
-
258
-	/* Correct image data pointer */
259
-	test->image->data = virt_to_user ( ( void * ) test->image->data );
260
-
261
-	/* Check that image is detected as PNM */
262
-	okx ( image_probe ( test->image ) == 0, file, line );
263
-	okx ( test->image->type == &pnm_image_type, file, line );
264
-
265
-	/* Check that a pixel buffer can be created from the image */
266
-	okx ( ( rc = image_pixbuf ( test->image, &pixbuf ) ) == 0, file, line );
267
-	if ( rc == 0 ) {
268
-
269
-		/* Check pixel buffer dimensions */
270
-		okx ( pixbuf->width == test->width, file, line );
271
-		okx ( pixbuf->height == test->height, file, line );
272
-
273
-		/* Check pixel buffer data */
274
-		okx ( pixbuf->len == test->len, file, line );
275
-		okx ( memcmp_user ( pixbuf->data, 0,
276
-				    virt_to_user ( test->data ), 0,
277
-				    test->len ) == 0, file, line );
278
-
279
-		pixbuf_put ( pixbuf );
280
-	}
281
-}
282
-#define pnm_ok( test ) pnm_okx ( test, __FILE__, __LINE__ )
283
-
284 211
 /**
285 212
  * Perform PNM self-test
286 213
  *
287 214
  */
288 215
 static void pnm_test_exec ( void ) {
289 216
 
290
-	pnm_ok ( &pbm_ascii );
291
-	pnm_ok ( &pgm_ascii );
292
-	pnm_ok ( &ppm_ascii );
293
-	pnm_ok ( &pbm_ascii_no_space );
294
-	pnm_ok ( &pbm_binary );
295
-	pnm_ok ( &pgm_binary );
296
-	pnm_ok ( &ppm_binary );
217
+	pixbuf_ok ( &pbm_ascii );
218
+	pixbuf_ok ( &pgm_ascii );
219
+	pixbuf_ok ( &ppm_ascii );
220
+	pixbuf_ok ( &pbm_ascii_no_space );
221
+	pixbuf_ok ( &pbm_binary );
222
+	pixbuf_ok ( &pgm_binary );
223
+	pixbuf_ok ( &ppm_binary );
297 224
 }
298 225
 
299 226
 /** PNM self-test */

Loading…
Откажи
Сачувај