Skip to content

Commit 0c100b8

Browse files
Duy Vothenguyenyf
Duy Vo
authored andcommitted
tests: subsys: crc: add ztests for CRC subsystem
Add ztests for CRC subsystem Signed-off-by: Duy Vo <[email protected]>
1 parent 575a5b2 commit 0c100b8

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed

tests/subsys/crc/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
3+
4+
project(subsys_crc_test)
5+
6+
target_sources(app PRIVATE src/main.c)

tests/subsys/crc/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_CRC=y
3+
CONFIG_LOG=y

tests/subsys/crc/src/main.c

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright (c) 2025 Renesas Electronics Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr/drivers/crc.h>
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/logging/log.h>
9+
#include <zephyr/sys/crc.h>
10+
11+
/* Define result of CRC computation */
12+
#define RESULT_CRC8 0xB2
13+
14+
/**
15+
* @brief Test crc8 works
16+
*/
17+
ZTEST(crc_subsys, test_crc_8)
18+
{
19+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
20+
21+
zassert_equal(crc8(data, sizeof(data), CRC8_REFLECT_POLY, 0x00, true), RESULT_CRC8);
22+
}
23+
24+
/* Define result of CRC computation */
25+
#define RESULT_CRC8_CCITT 0x4D
26+
27+
/**
28+
* @brief Test crc8_ccitt works
29+
*/
30+
ZTEST(crc_subsys, test_crc_8_ccitt)
31+
{
32+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
33+
34+
zassert_equal(crc8_ccitt(0x00, data, sizeof(data)), RESULT_CRC8_CCITT);
35+
}
36+
37+
/* Define result of CRC computation */
38+
#define RESULT_CRC8_ROHC 0xB2
39+
40+
/**
41+
* @brief Test that crc_8_rohc works
42+
*/
43+
ZTEST(crc_subsys, test_crc_8_rohc)
44+
{
45+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
46+
47+
zassert_equal(crc8_rohc(0x00, data, sizeof(data)), RESULT_CRC8_ROHC);
48+
}
49+
50+
/* Define result of CRC computation */
51+
#define RESULT_CRC16 0xE58F
52+
53+
/**
54+
* @brief Test that crc_16 works
55+
*/
56+
ZTEST(crc_subsys, test_crc_16)
57+
{
58+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
59+
60+
zassert_equal(crc16(CRC16_POLY, CRC16_INIT_VAL, data, sizeof(data)), RESULT_CRC16);
61+
}
62+
/* Define result of CRC computation */
63+
#define RESULT_CRC16_REFLECT 0xD543
64+
65+
/**
66+
* @brief Test that crc_16_reflect works
67+
*/
68+
ZTEST(crc_subsys, test_crc_16_reflect)
69+
{
70+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
71+
72+
zassert_equal(crc16_reflect(CRC16_REFLECT_POLY, CRC16_INIT_VAL, data, sizeof(data)),
73+
RESULT_CRC16_REFLECT);
74+
}
75+
76+
/* Define result of CRC computation */
77+
#define RESULT_CRC16_ANSI 0xDE03
78+
79+
/**
80+
* @brief Test that crc_16_ansi works
81+
*/
82+
ZTEST(crc_subsys, test_crc_16_ansi)
83+
{
84+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
85+
86+
zassert_equal(crc16_ansi(data, sizeof(data)), RESULT_CRC16_ANSI);
87+
}
88+
89+
/* Define result of CRC computation */
90+
#define RESULT_CRC_CCITT 0x445C
91+
92+
/**
93+
* @brief Test that crc_16_ccitt works
94+
*/
95+
ZTEST(crc_subsys, test_crc_16_ccitt)
96+
{
97+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
98+
99+
zassert_equal(crc16_ccitt(0x0000, data, sizeof(data)), RESULT_CRC_CCITT);
100+
}
101+
102+
/* Define result of CRC computation */
103+
#define RESULT_CRC16_ITU_T 0x8866
104+
105+
/**
106+
* @brief Test that crc_16_itu_t works
107+
*/
108+
ZTEST(crc_subsys, test_crc_16_itu_t)
109+
{
110+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
111+
112+
zassert_equal(crc16_itu_t(0x0000, data, sizeof(data)), RESULT_CRC16_ITU_T);
113+
}
114+
115+
/* Define result of CRC computation */
116+
#define RESULT_CRC32_C 0xBB19ECB2
117+
118+
/**
119+
* @brief Test that crc32_c works
120+
*/
121+
ZTEST(crc_subsys, test_crc_32_c)
122+
{
123+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
124+
125+
zassert_equal(crc32_c(0x000, data, sizeof(data), true, false), RESULT_CRC32_C);
126+
}
127+
/* Define result of CRC computation */
128+
#define RESULT_CRC32_IEEE 0xCEA4A6C2
129+
130+
/**
131+
* @brief Test that crc_32_ieee works
132+
*/
133+
ZTEST(crc_subsys, test_crc_32_ieee)
134+
{
135+
uint8_t data[8] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4};
136+
137+
zassert_equal(crc32_ieee(data, sizeof(data)), RESULT_CRC32_IEEE);
138+
}
139+
140+
/* Define result of CRC computation */
141+
#define RESULT_CRC8_CCITT_REMAIN_1 0x57
142+
143+
/**
144+
* @brief Test crc8_ccitt_remain_1 work
145+
*/
146+
ZTEST(crc_subsys, test_crc_8_ccitt_remain_1)
147+
{
148+
uint8_t data[9] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4, 0x3D};
149+
150+
zassert_equal(crc8_ccitt(0x00, data, sizeof(data)), RESULT_CRC8_CCITT_REMAIN_1);
151+
}
152+
153+
/* Define result of CRC computation */
154+
#define RESULT_CRC8_ROHC_REMAIN_2 0x4F
155+
156+
/**
157+
* @brief Test that crc_8_rohc_remain_2 works
158+
*/
159+
ZTEST(crc_subsys, test_crc_8_rohc_remain_2)
160+
{
161+
uint8_t data[10] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4, 0x3D, 0xFF};
162+
163+
zassert_equal(crc8_rohc(0x00, data, sizeof(data)), RESULT_CRC8_ROHC_REMAIN_2);
164+
}
165+
166+
/* Define result of CRC computation */
167+
#define RESULT_CRC_CCITT_REMAIN_3 0x454B
168+
169+
/**
170+
* @brief Test that crc_16_ccitt_remain_3 works
171+
*/
172+
ZTEST(crc_subsys, test_crc_16_ccitt_remain_3)
173+
{
174+
uint8_t data[11] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4, 0x3D, 0xFF, 0xE2};
175+
176+
zassert_equal(crc16_ccitt(0x0000, data, sizeof(data)), RESULT_CRC_CCITT_REMAIN_3);
177+
}
178+
179+
/* Define result of CRC computation */
180+
#define RESULT_CRC16_ITU_T_REMAIN_1 0x917E
181+
182+
/**
183+
* @brief Test that crc_16_itu_t_remain_1 works
184+
*/
185+
ZTEST(crc_subsys, test_crc_16_itu_t_remain_1)
186+
{
187+
uint8_t data[9] = {0x0A, 0x2B, 0x4C, 0x6D, 0x8E, 0x49, 0x00, 0xC4, 0x3D};
188+
189+
zassert_equal(crc16_itu_t(0x0000, data, sizeof(data)), RESULT_CRC16_ITU_T_REMAIN_1);
190+
}
191+
192+
ZTEST_SUITE(crc_subsys, NULL, NULL, NULL, NULL, NULL);

tests/subsys/crc/testcase.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
tests:
2+
subsys.crc:
3+
depends_on: crc
4+
tags:
5+
- subsys
6+
- crc
7+
harness: ztest

0 commit comments

Comments
 (0)