openat: don’t close (-1)
[gnulib.git] / tests / test-crc.c
blob975db5d619884493c99162ab9e89195f71f06de9
1 /*
2 * Copyright (C) 2005-2007, 2010-2024 Free Software Foundation, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU 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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Simon Josefsson. */
19 #include <config.h>
21 #include "crc.h"
23 #include <stdio.h>
24 #include <string.h>
26 #include "macros.h"
28 #define MAX_ALIGNMENT 32
30 uint32_t trailing_unaligned_byte_hashes[32] = {
31 0xDFDD19BE, 0xD2D258E4, 0x3F67D485, 0xFF300DA4, 0xFC46B871,
32 0x6B97174C, 0x3C6873C6, 0x3DE4FDA4, 0x783A2D5F, 0x2ECC37AC,
33 0xFEFAD741, 0x5FFB72DF, 0xF6E6DD29, 0xCD21E04E, 0x78CAE842,
34 0xCFC25F71, 0x7DC85CEE, 0xC971E8DA, 0x541726C1, 0xB15FFE02,
35 0xFABE629D, 0x4F254C30, 0x444B083F, 0x049F6D1D, 0x2D0670E0,
36 0x719C83F9, 0xEB7FAABC, 0xF0E4ECEE, 0x80958327, 0xD3566178,
37 0xD1683190, 0x7ABB3299
40 uint32_t leading_unaligned_byte_hashes[32] = {
41 0xA6C6EC55, 0x10BA5C53, 0xE991A1AB, 0xD9279D86, 0xEF21C396,
42 0x78A4387C, 0x2F865A77, 0xAEA7587C, 0x9779C9D0, 0x914DD47A,
43 0xB42B53CB, 0x47D39201, 0xE53E891F, 0xB4501BA1, 0xE946965F,
44 0xF092BC6F, 0xA4D33D75, 0xD602ECA6, 0x107142FB, 0xD83B4AFD,
45 0x9B9CE2B9, 0xCF2C3E17, 0xCDE401A1, 0xE686DFE7, 0x5137E14F,
46 0xB309A32D, 0x2E1BB47F, 0xECA84001, 0x1F5B5472, 0x5BA21753,
47 0x203ACDCA, 0x48B56C22
50 uint32_t variable_alignment_variable_length_hashes[] = {
51 0x00000000, 0xB40BBE37, 0x98662B02, 0x062ED334, 0xFD64D7A9, 0xCA47A65D,
52 0x70A0B329, 0x191C96C2, 0xFECD07E0, 0x5120CD3D, 0xC886714D, 0x5416D158,
53 0xA9E8B882, 0x7B18C316, 0x0A7BB6E4, 0x19664DC7, 0x71A8E3C4, 0xDD7CD42A,
54 0x95623656, 0x2C4CFCBD, 0x7046B873, 0x7777762E, 0xAD10FF30, 0x15773DB6,
55 0x0515202E, 0xE0D0FC3E, 0x2F8FAC7B, 0x3A9ADE62, 0x13560288, 0x8BADEEE8,
56 0x4B8F79A9, 0xBDF67D65
59 int
60 main (void)
62 uint32_t p;
63 size_t i, j;
64 char data[MAX_ALIGNMENT * 3];
66 p = crc32_update_no_xor (42, "foo", 3);
67 if (p != 0x46e87f05)
69 printf ("cunx got %lx\n", (unsigned long) p);
70 return 1;
73 p = crc32_no_xor ("foo", 3);
74 if (p != 0x7332bc33)
76 printf ("cnx got %lx\n", (unsigned long) p);
77 return 1;
80 p = crc32_update (42, "foo", 3);
81 if (p != 0xb9a9a617)
83 printf ("cu got %lx\n", (unsigned long) p);
84 return 1;
87 p = crc32 ("foo", 3);
88 if (p != 0x8c736521)
90 printf ("c got %lx\n", (unsigned long) p);
91 return 1;
95 * Tests for new CRC32 implementation
96 * The original implementation works on a byte-by-byte basis
97 * but new implementations may work on longer alignments.
98 * This test will confirm correct operation with non-aligned
99 * data.
102 /* Test for trailing unaligned bytes */
104 for (i = 0; i < MAX_ALIGNMENT; i++)
106 p = crc32_update_no_xor (0, randomb, MAX_ALIGNMENT + i);
107 if (p != trailing_unaligned_byte_hashes[i])
109 printf ("aligned c at %lu length %lu got %lx\n", (size_t) 0,
110 MAX_ALIGNMENT + i, (unsigned long) p);
111 return 1;
115 /* Test for leading unaligned bytes */
117 for (i = 0; i < MAX_ALIGNMENT; i++)
119 p = crc32_update_no_xor (0, randomb + i, (MAX_ALIGNMENT * 2) - i);
120 if (p != leading_unaligned_byte_hashes[i])
122 printf ("aligned c at %lu length %lu got %lx\n", i,
123 (MAX_ALIGNMENT * 2) - i, (unsigned long) p);
124 return 1;
128 /* Combined test of multiple lengths at multiple alignments */
130 for (i = 0; i < MAX_ALIGNMENT; i++)
131 for (j = 0; j < MAX_ALIGNMENT; j++)
133 memcpy (data + i, randomb, MAX_ALIGNMENT * 2);
134 p = crc32_update_no_xor (0, data + i, j);
135 if (p != variable_alignment_variable_length_hashes[j])
137 printf ("aligned c at %lu length %lu got %lx\n", i, j,
138 (unsigned long) p);
139 return 1;
144 return 0;