xref: /aosp_15_r20/external/webrtc/third_party/crc32c/src/src/crc32c_capi_unittest.c (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 // Copyright 2017 The CRC32C Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 
5 #include "crc32c/crc32c.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
main()13 int main() {
14   /* From rfc3720 section B.4. */
15   uint8_t buf[32];
16 
17   memset(buf, 0, sizeof(buf));
18   if ((uint32_t)0x8a9136aa != crc32c_value(buf, sizeof(buf))) {
19     printf("crc32c_value(zeros) test failed\n");
20     return 1;
21   }
22 
23   memset(buf, 0xff, sizeof(buf));
24   if ((uint32_t)0x62a8ab43 != crc32c_value(buf, sizeof(buf))) {
25     printf("crc32c_value(0xff) test failed\n");
26     return 1;
27   }
28 
29   for (size_t i = 0; i < 32; ++i)
30     buf[i] = (uint8_t)i;
31   if ((uint32_t)0x46dd794e != crc32c_value(buf, sizeof(buf))) {
32     printf("crc32c_value(0..31) test failed\n");
33     return 1;
34   }
35 
36   for (size_t i = 0; i < 32; ++i)
37     buf[i] = (uint8_t)(31 - i);
38   if ((uint32_t)0x113fdb5c != crc32c_value(buf, sizeof(buf))) {
39     printf("crc32c_value(31..0) test failed\n");
40     return 1;
41   }
42 
43   uint8_t data[48] = {
44       0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45       0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
46       0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, 0x28, 0x00, 0x00, 0x00,
47       0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48   };
49   if ((uint32_t)0xd9963a56 != crc32c_value(data, sizeof(data))) {
50     printf("crc32c_value(31..0) test failed\n");
51     return 1;
52   }
53 
54   const uint8_t* hello_space_world = (const uint8_t*)"hello world";
55   const uint8_t* hello_space = (const uint8_t*)"hello ";
56   const uint8_t* world = (const uint8_t*)"world";
57 
58   if (crc32c_value(hello_space_world, 11) !=
59       crc32c_extend(crc32c_value(hello_space, 6), world, 5)) {
60     printf("crc32c_extend test failed\n");
61     return 1;
62   }
63 
64   printf("All tests passed\n");
65   return 0;
66 }
67