2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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 (at your option) any later version.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
27 * Test vectors generated using Perl's Digest::CRC:
29 * use Digest::CRC qw ( crc );
31 * printf "%#08x", crc ( $data, 32, $seed, 0, 1, 0x04c11db7, 1 );
35 /* Forcibly enable assertions */
39 #include <ipxe/crc32.h>
40 #include <ipxe/test.h>
42 /** Define inline data */
43 #define DATA(...) { __VA_ARGS__ }
49 /** Length of test data */
63 * @v CRC32 Expected CRC32
64 * @ret test CRC32 test
66 #define CRC32_TEST( name, DATA, SEED, CRC32 ) \
67 static const uint8_t name ## _data[] = DATA; \
68 static struct crc32_test name = { \
69 .data = name ## _data, \
70 .len = sizeof ( name ## _data ), \
76 * Report a CRC32 test result
80 #define crc32_ok( test ) do { \
82 crc32 = crc32_le ( (test)->seed, (test)->data, (test)->len ); \
83 ok ( crc32 == (test)->crc32 ); \
87 CRC32_TEST ( empty_test,
89 0x12345678UL, 0x12345678UL );
91 DATA ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' ),
92 0xffffffffUL, 0xf2b5ee7aUL );
93 CRC32_TEST ( hw_split_part1_test,
94 DATA ( 'h', 'e', 'l', 'l', 'o' ),
95 0xffffffffUL, 0xc9ef5979UL );
96 CRC32_TEST ( hw_split_part2_test,
97 DATA ( ' ', 'w', 'o', 'r', 'l', 'd' ),
98 0xc9ef5979UL, 0xf2b5ee7aUL );
101 * Perform CRC32 self-tests
104 static void crc32_test_exec ( void ) {
106 crc32_ok ( &empty_test );
107 crc32_ok ( &hw_test );
108 crc32_ok ( &hw_split_part1_test );
109 crc32_ok ( &hw_split_part2_test );
112 /** CRC32 self-test */
113 struct self_test crc32_test __self_test = {
115 .exec = crc32_test_exec,