aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/test/crc32c.c
blob: 8c0c691e2e85f1c016c7d9f9507a0613be4b938a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#include <vppinfra/format.h>
#include <vppinfra/test/test.h>
#include <vppinfra/crc32.h>

#ifndef CLIB_MARCH_VARIANT
char *crc32c_test_string =
  "The quick brown fox jumped over the lazy dog and stumbled.";
u32 crc32c_test_values_data[] = {
  0x00000000, 0x96bf4dcc, 0x65479df4, 0x60a63889, 0xda99c852, 0x3337e4e2,
  0x4651af18, 0x83b586a1, 0x2235e3b5, 0x7f896b6f, 0x1f17a8f3, 0x60dc68bc,
  0x6f95458b, 0x24c5aa40, 0xe40de8f0, 0x3e344ed8, 0x798903f4, 0x73ea05e3,
  0xcfc61ead, 0xe6ed33a9, 0xfaa20d87, 0x5ce246c4, 0x4022138c, 0x111b090a,
  0x1a6b673c, 0x298d6a78, 0x5d3485d5, 0xc6c24fec, 0x91600ac3, 0x877506df,
  0xd9702ff7, 0xb7de5f4b, 0xf8f8e606, 0x905bdc1c, 0xb69298ce, 0x3b748c05,
  0x1577ee4e, 0xc19389c7, 0x842bc1c7, 0x0db915db, 0x437d7c44, 0xa61f7901,
  0x54919807, 0xeb4b5a35, 0xb0f5e17e, 0xfded9015, 0xb6ff2e82, 0xaec598e4,
  0x8258fee0, 0xc30f7e3a, 0x390ac90e, 0x1a4376fc, 0xfa5ea3c2, 0xfca2d721,
  0x52d74c9f, 0xe06c4bcd, 0x28728122, 0x67f288d5, 0
};
u32 *crc32c_test_values = crc32c_test_values_data;

#else
extern char *crc32c_test_string;
extern u32 *crc32c_test_values;
#endif

static clib_error_t *
test_clib_crc32c (clib_error_t *err)
{
  int max_len = strlen (crc32c_test_string);
  int i;
  for (i = 0; i < max_len; i++)
    {
      u32 expected_crc32c = crc32c_test_values[i];
      u32 calculated_crc32 = clib_crc32c ((u8 *) crc32c_test_string, i);
      if (expected_crc32c != calculated_crc32)
	{
	  return clib_error_return (
	    err,
	    "Bad CRC32C for test case %d: expected 0x%08x, calculated: 0x%08x",
	    i, expected_crc32c, calculated_crc32);
	}
    }
  return err;
}

REGISTER_TEST (clib_crc32c) = {
  .name = "clib_crc32c",
  .fn = test_clib_crc32c,
};