Skip to content

Commit 7faafda

Browse files
joerchankartben
authored andcommitted
tests: kernel: Test endian-specific buffer convert and copy functions
Test endian-specific buffer convert and copy functions. Signed-off-by: Joakim Andersson <[email protected]>
1 parent 6a3f885 commit 7faafda

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/kernel/common/src/byteorder.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,122 @@ ZTEST(byteorder, test_sys_uint64_to_array)
588588
#undef VAL
589589
}
590590

591+
ZTEST(byteorder, test_sys_le_to_cpu)
592+
{
593+
uint8_t val[9] = { 0x87, 0x95, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab };
594+
uint8_t exp[9] = {
595+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
596+
(0x87, 0x95, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab),
597+
(0xab, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x95, 0x87))
598+
};
599+
600+
sys_le_to_cpu(val, sizeof(val));
601+
602+
zassert_mem_equal(val, exp, sizeof(exp), "sys_le_to_cpu() failed");
603+
}
604+
605+
ZTEST(byteorder, test_sys_cpu_to_le)
606+
{
607+
uint8_t val[9] = { 0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab };
608+
uint8_t exp[9] = {
609+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
610+
(0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab),
611+
(0xab, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87))
612+
};
613+
614+
sys_cpu_to_le(val, sizeof(val));
615+
616+
zassert_mem_equal(val, exp, sizeof(exp), "sys_cpu_to_le() failed");
617+
}
618+
619+
ZTEST(byteorder, test_sys_be_to_cpu)
620+
{
621+
uint8_t val[9] = { 0x87, 0x97, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab };
622+
uint8_t exp[9] = {
623+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
624+
(0xab, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x97, 0x87),
625+
(0x87, 0x97, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab))
626+
};
627+
628+
sys_be_to_cpu(val, sizeof(val));
629+
630+
zassert_mem_equal(val, exp, sizeof(exp), "sys_be_to_cpu() failed");
631+
}
632+
633+
ZTEST(byteorder, test_sys_cpu_to_be)
634+
{
635+
uint8_t val[9] = { 0x87, 0x98, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab };
636+
uint8_t exp[9] = {
637+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
638+
(0xab, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x98, 0x87),
639+
(0x87, 0x98, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xab))
640+
};
641+
642+
sys_cpu_to_be(val, sizeof(val));
643+
644+
zassert_mem_equal(val, exp, sizeof(exp), "sys_cpu_to_be() failed");
645+
}
646+
647+
ZTEST(byteorder, test_sys_put_le)
648+
{
649+
uint8_t host[9] = { 0x87, 0x12, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba };
650+
uint8_t prot[9] = { 0 };
651+
uint8_t exp[9] = {
652+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
653+
(0x87, 0x12, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba),
654+
(0xba, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x12, 0x87))
655+
};
656+
657+
sys_put_le(prot, host, sizeof(prot));
658+
659+
zassert_mem_equal(prot, exp, sizeof(exp), "sys_put_le() failed");
660+
}
661+
662+
ZTEST(byteorder, test_sys_put_be)
663+
{
664+
uint8_t host[9] = { 0x87, 0x13, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba };
665+
uint8_t prot[9] = { 0 };
666+
uint8_t exp[9] = {
667+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
668+
(0xba, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x13, 0x87),
669+
(0x87, 0x13, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba))
670+
};
671+
672+
sys_put_be(prot, host, sizeof(prot));
673+
674+
zassert_mem_equal(prot, exp, sizeof(exp), "sys_put_be() failed");
675+
}
676+
677+
ZTEST(byteorder, test_sys_get_le)
678+
{
679+
uint8_t prot[9] = { 0x87, 0x14, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba };
680+
uint8_t host[9] = { 0 };
681+
uint8_t exp[9] = {
682+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
683+
(0x87, 0x14, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba),
684+
(0xba, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x14, 0x87))
685+
};
686+
687+
sys_get_le(host, prot, sizeof(host));
688+
689+
zassert_mem_equal(host, exp, sizeof(exp), "sys_get_le() failed");
690+
}
691+
692+
ZTEST(byteorder, test_sys_get_be)
693+
{
694+
uint8_t prot[9] = { 0x87, 0x15, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba };
695+
uint8_t host[9] = { 0 };
696+
uint8_t exp[9] = {
697+
COND_CODE_1(CONFIG_LITTLE_ENDIAN,
698+
(0xba, 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x15, 0x87),
699+
(0x87, 0x15, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0, 0xba))
700+
};
701+
702+
sys_get_be(host, prot, sizeof(host));
703+
704+
zassert_mem_equal(host, exp, sizeof(exp), "sys_get_be() failed");
705+
}
706+
591707
extern void *common_setup(void);
592708
ZTEST_SUITE(byteorder, NULL, common_setup, NULL, NULL, NULL);
593709

0 commit comments

Comments
 (0)