|
| 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 | +} |
0 commit comments