1 /*
2 * Copyright (c) 2017 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Runtime Inc
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "fcb_test.h"
9
ZTEST(fcb_test_with_4sectors_set,test_fcb_last_of_n)10 ZTEST(fcb_test_with_4sectors_set, test_fcb_last_of_n)
11 {
12 const uint8_t ENTRIES = 5U;
13 struct fcb *fcb;
14 int rc;
15 struct fcb_entry loc;
16 struct fcb_entry areas[ENTRIES];
17 uint8_t test_data[128] = {0};
18 uint8_t i;
19
20 fcb = &test_fcb;
21 fcb->f_scratch_cnt = 1U;
22
23 /* No fcbs available */
24 rc = fcb_offset_last_n(fcb, 1, &loc);
25 zassert_true(rc != 0, "No fcbs available");
26
27 /*
28 * Add some fcbs.
29 */
30 for (i = 0U; i < ENTRIES; i++) {
31 rc = fcb_append(fcb, sizeof(test_data), &loc);
32 if (rc == -ENOSPC) {
33 break;
34 }
35
36 rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc),
37 test_data, sizeof(test_data));
38 zassert_true(rc == 0, "flash_area_write call failure");
39
40 rc = fcb_append_finish(fcb, &loc);
41 zassert_true(rc == 0, "fcb_append_finish call failure");
42
43 areas[i] = loc;
44 }
45
46 /* last entry */
47 rc = fcb_offset_last_n(fcb, 1, &loc);
48 zassert_true(rc == 0, "fcb_offset_last_n call failure");
49 zassert_true(areas[4].fe_sector == loc.fe_sector &&
50 areas[4].fe_data_off == loc.fe_data_off &&
51 areas[4].fe_data_len == loc.fe_data_len,
52 "fcb_offset_last_n: fetched wrong n-th location");
53
54 /* somewhere in the middle */
55 rc = fcb_offset_last_n(fcb, 3, &loc);
56 zassert_true(rc == 0, "fcb_offset_last_n call failure");
57 zassert_true(areas[2].fe_sector == loc.fe_sector &&
58 areas[2].fe_data_off == loc.fe_data_off &&
59 areas[2].fe_data_len == loc.fe_data_len,
60 "fcb_offset_last_n: fetched wrong n-th location");
61
62 /* first entry */
63 rc = fcb_offset_last_n(fcb, 5, &loc);
64 zassert_true(rc == 0, "fcb_offset_last_n call failure");
65 zassert_true(areas[0].fe_sector == loc.fe_sector &&
66 areas[0].fe_data_off == loc.fe_data_off &&
67 areas[0].fe_data_len == loc.fe_data_len,
68 "fcb_offset_last_n: fetched wrong n-th location");
69
70 /* after last valid entry, returns the first one like for 5 */
71 rc = fcb_offset_last_n(fcb, 6, &loc);
72 zassert_true(rc == 0, "fcb_offset_last_n call failure");
73 zassert_true(areas[0].fe_sector == loc.fe_sector &&
74 areas[0].fe_data_off == loc.fe_data_off &&
75 areas[0].fe_data_len == loc.fe_data_len,
76 "fcb_offset_last_n: fetched wrong n-th location");
77 }
78