Skip to content

Commit 29585ee

Browse files
fabiobaltiericfriedt
authored andcommitted
tests: iterable_sections: add a tests for iterable_sections
Add a test for iterable sections macro, covering read write and read only sections. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 171739d commit 29585ee

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(iterable_sections)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
zephyr_linker_sources(DATA_SECTIONS sections-ram.ld)
11+
zephyr_linker_sources(SECTIONS sections-rom.ld)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ITERABLE_SECTION_RAM(test_ram, 4)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ITERABLE_SECTION_ROM(test_rom, 4)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2021 Google LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ztest.h>
8+
9+
struct test_ram {
10+
int i;
11+
};
12+
13+
#define CHECK_BIT 0x80
14+
15+
/* declare in random order to check that the linker is sorting by name */
16+
STRUCT_SECTION_ITERABLE(test_ram, ram3) = {0x03};
17+
STRUCT_SECTION_ITERABLE(test_ram, ram2) = {0x02};
18+
STRUCT_SECTION_ITERABLE(test_ram, ram4) = {0x04};
19+
STRUCT_SECTION_ITERABLE(test_ram, ram1) = {0x01};
20+
21+
#define RAM_EXPECT 0x01020304
22+
23+
/**
24+
*
25+
* @brief Test iterable in read write section.
26+
*
27+
*/
28+
void test_ram(void)
29+
{
30+
int out = 0;
31+
32+
STRUCT_SECTION_FOREACH(test_ram, t) {
33+
out = (out << 8) | t->i;
34+
t->i |= CHECK_BIT;
35+
}
36+
37+
zassert_equal(out, RAM_EXPECT, "Check value incorrect (got: 0x%08x)", out);
38+
39+
zassert_equal(ram1.i & CHECK_BIT, CHECK_BIT,
40+
"ram1.i check bit incorrect (got: 0x%x)", ram1.i);
41+
zassert_equal(ram2.i & CHECK_BIT, CHECK_BIT,
42+
"ram2.i check bit incorrect (got: 0x%x)", ram2.i);
43+
zassert_equal(ram3.i & CHECK_BIT, CHECK_BIT,
44+
"ram3.i check bit incorrect (got: 0x%x)", ram3.i);
45+
zassert_equal(ram4.i & CHECK_BIT, CHECK_BIT,
46+
"ram4.i check bit incorrect (got: 0x%x)", ram4.i);
47+
}
48+
49+
struct test_rom {
50+
int i;
51+
};
52+
53+
/* declare in random order to check that the linker is sorting by name */
54+
STRUCT_SECTION_ITERABLE(test_rom, rom1) = {0x10};
55+
STRUCT_SECTION_ITERABLE(test_rom, rom3) = {0x30};
56+
STRUCT_SECTION_ITERABLE(test_rom, rom4) = {0x40};
57+
STRUCT_SECTION_ITERABLE(test_rom, rom2) = {0x20};
58+
59+
#define ROM_EXPECT 0x10203040
60+
61+
/**
62+
*
63+
* @brief Test iterable in read only section.
64+
*
65+
*/
66+
void test_rom(void)
67+
{
68+
int out = 0;
69+
70+
STRUCT_SECTION_FOREACH(test_rom, t) {
71+
out = (out << 8) | t->i;
72+
}
73+
74+
zassert_equal(out, ROM_EXPECT, "Check value incorrect (got: 0x%x)", out);
75+
}
76+
77+
/**
78+
*
79+
* @brief Test entry point
80+
*
81+
*/
82+
void test_main(void)
83+
{
84+
ztest_test_suite(iterable_sections,
85+
ztest_unit_test(test_ram),
86+
ztest_unit_test(test_rom));
87+
ztest_run_test_suite(iterable_sections);
88+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
misc.iterable_sections:
3+
tags: iterable_sections
4+
arch_exclude: posix xtensa

0 commit comments

Comments
 (0)